|
|
|
@ -2,10 +2,7 @@ package org.thoughtcrime.securesms.preferences
|
|
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
|
import android.app.Activity
|
|
|
|
|
import android.content.ClipData
|
|
|
|
|
import android.content.ClipboardManager
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.*
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import android.os.AsyncTask
|
|
|
|
|
import android.os.Bundle
|
|
|
|
@ -19,6 +16,7 @@ import android.view.MenuItem
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.inputmethod.InputMethodManager
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
|
import androidx.core.view.isVisible
|
|
|
|
|
import network.loki.messenger.BuildConfig
|
|
|
|
|
import network.loki.messenger.R
|
|
|
|
@ -35,6 +33,7 @@ import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol
|
|
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
|
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
|
|
|
|
import org.thoughtcrime.securesms.avatar.AvatarSelection
|
|
|
|
|
import org.thoughtcrime.securesms.components.ProfilePictureView
|
|
|
|
|
import org.thoughtcrime.securesms.home.PathActivity
|
|
|
|
|
import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity
|
|
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp
|
|
|
|
@ -57,8 +56,6 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
private var displayNameEditActionMode: ActionMode? = null
|
|
|
|
|
set(value) { field = value; handleDisplayNameEditActionModeChanged() }
|
|
|
|
|
private lateinit var glide: GlideRequests
|
|
|
|
|
private var displayNameToBeUploaded: String? = null
|
|
|
|
|
private var profilePictureToBeUploaded: ByteArray? = null
|
|
|
|
|
private var tempFile: File? = null
|
|
|
|
|
|
|
|
|
|
private val hexEncodedPublicKey: String
|
|
|
|
@ -79,11 +76,7 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
val displayName = TextSecurePreferences.getProfileName(this) ?: hexEncodedPublicKey
|
|
|
|
|
glide = GlideApp.with(this)
|
|
|
|
|
with(binding) {
|
|
|
|
|
profilePictureView.root.glide = glide
|
|
|
|
|
profilePictureView.root.publicKey = hexEncodedPublicKey
|
|
|
|
|
profilePictureView.root.displayName = displayName
|
|
|
|
|
profilePictureView.root.isLarge = true
|
|
|
|
|
profilePictureView.root.update()
|
|
|
|
|
setupProfilePictureView(profilePictureView.root)
|
|
|
|
|
profilePictureView.root.setOnClickListener { showEditProfilePictureUI() }
|
|
|
|
|
ctnGroupNameSection.setOnClickListener { startActionMode(DisplayNameEditActionModeCallback()) }
|
|
|
|
|
btnGroupNameDisplay.text = displayName
|
|
|
|
@ -105,6 +98,14 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setupProfilePictureView(view: ProfilePictureView) {
|
|
|
|
|
view.glide = glide
|
|
|
|
|
view.publicKey = hexEncodedPublicKey
|
|
|
|
|
view.displayName = TextSecurePreferences.getProfileName(this) ?: hexEncodedPublicKey
|
|
|
|
|
view.isLarge = true
|
|
|
|
|
view.update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
|
|
|
super.onSaveInstanceState(outState)
|
|
|
|
|
val scrollBundle = SparseArray<Parcelable>()
|
|
|
|
@ -154,9 +155,9 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
}
|
|
|
|
|
AsyncTask.execute {
|
|
|
|
|
try {
|
|
|
|
|
profilePictureToBeUploaded = BitmapUtil.createScaledBytes(this@SettingsActivity, AvatarSelection.getResultUri(data), ProfileMediaConstraints()).bitmap
|
|
|
|
|
val profilePictureToBeUploaded = BitmapUtil.createScaledBytes(this@SettingsActivity, AvatarSelection.getResultUri(data), ProfileMediaConstraints()).bitmap
|
|
|
|
|
Handler(Looper.getMainLooper()).post {
|
|
|
|
|
updateProfile(true)
|
|
|
|
|
updateProfile(true, profilePictureToBeUploaded)
|
|
|
|
|
}
|
|
|
|
|
} catch (e: BitmapDecodingException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
@ -190,23 +191,30 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun updateProfile(isUpdatingProfilePicture: Boolean) {
|
|
|
|
|
private fun updateProfile(
|
|
|
|
|
isUpdatingProfilePicture: Boolean,
|
|
|
|
|
profilePicture: ByteArray? = null,
|
|
|
|
|
displayName: String? = null
|
|
|
|
|
) {
|
|
|
|
|
binding.loader.isVisible = true
|
|
|
|
|
val promises = mutableListOf<Promise<*, Exception>>()
|
|
|
|
|
val displayName = displayNameToBeUploaded
|
|
|
|
|
if (displayName != null) {
|
|
|
|
|
TextSecurePreferences.setProfileName(this, displayName)
|
|
|
|
|
}
|
|
|
|
|
val profilePicture = profilePictureToBeUploaded
|
|
|
|
|
val encodedProfileKey = ProfileKeyUtil.generateEncodedProfileKey(this)
|
|
|
|
|
if (isUpdatingProfilePicture && profilePicture != null) {
|
|
|
|
|
promises.add(ProfilePictureUtilities.upload(profilePicture, encodedProfileKey, this))
|
|
|
|
|
if (isUpdatingProfilePicture) {
|
|
|
|
|
if (profilePicture != null) {
|
|
|
|
|
promises.add(ProfilePictureUtilities.upload(profilePicture, encodedProfileKey, this))
|
|
|
|
|
} else {
|
|
|
|
|
TextSecurePreferences.setLastProfilePictureUpload(this, System.currentTimeMillis())
|
|
|
|
|
TextSecurePreferences.setProfilePictureURL(this, null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val compoundPromise = all(promises)
|
|
|
|
|
compoundPromise.successUi { // Do this on the UI thread so that it happens before the alwaysUi clause below
|
|
|
|
|
if (isUpdatingProfilePicture && profilePicture != null) {
|
|
|
|
|
if (isUpdatingProfilePicture) {
|
|
|
|
|
AvatarHelper.setAvatar(this, Address.fromSerialized(TextSecurePreferences.getLocalNumber(this)!!), profilePicture)
|
|
|
|
|
TextSecurePreferences.setProfileAvatarId(this, SecureRandom().nextInt())
|
|
|
|
|
TextSecurePreferences.setProfileAvatarId(this, profilePicture?.let { SecureRandom().nextInt() } ?: 0 )
|
|
|
|
|
TextSecurePreferences.setLastProfilePictureUpload(this, Date().time)
|
|
|
|
|
ProfileKeyUtil.setEncodedProfileKey(this, encodedProfileKey)
|
|
|
|
|
}
|
|
|
|
@ -218,12 +226,10 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
if (displayName != null) {
|
|
|
|
|
binding.btnGroupNameDisplay.text = displayName
|
|
|
|
|
}
|
|
|
|
|
if (isUpdatingProfilePicture && profilePicture != null) {
|
|
|
|
|
if (isUpdatingProfilePicture) {
|
|
|
|
|
binding.profilePictureView.root.recycle() // Clear the cached image before updating
|
|
|
|
|
binding.profilePictureView.root.update()
|
|
|
|
|
}
|
|
|
|
|
displayNameToBeUploaded = null
|
|
|
|
|
profilePictureToBeUploaded = null
|
|
|
|
|
binding.loader.isVisible = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -244,8 +250,7 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
Toast.makeText(this, R.string.activity_settings_display_name_too_long_error, Toast.LENGTH_SHORT).show()
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
displayNameToBeUploaded = displayName
|
|
|
|
|
updateProfile(false)
|
|
|
|
|
updateProfile(false, displayName = displayName)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -255,6 +260,28 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showEditProfilePictureUI() {
|
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
|
.setTitle(R.string.activity_settings_set_display_picture)
|
|
|
|
|
.setView(R.layout.dialog_change_avatar)
|
|
|
|
|
.setPositiveButton(R.string.activity_settings_upload) { _, _ ->
|
|
|
|
|
startAvatarSelection()
|
|
|
|
|
}
|
|
|
|
|
.setNegativeButton(R.string.cancel) { _, _ -> }
|
|
|
|
|
.apply {
|
|
|
|
|
if (TextSecurePreferences.getProfileAvatarId(context) != 0) {
|
|
|
|
|
setNeutralButton(R.string.activity_settings_remove) { _, _ -> removeAvatar() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.show().apply {
|
|
|
|
|
findViewById<ProfilePictureView>(R.id.profile_picture_view)?.let(::setupProfilePictureView)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun removeAvatar() {
|
|
|
|
|
updateProfile(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun startAvatarSelection() {
|
|
|
|
|
// Ask for an optional camera permission.
|
|
|
|
|
Permissions.with(this)
|
|
|
|
|
.request(Manifest.permission.CAMERA)
|
|
|
|
|