From a8e77659b3e1c4c78fa26dfe9133404e20a874ac Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 22 May 2023 14:53:58 +0930 Subject: [PATCH] Add remove avatar --- .../securesms/preferences/SettingsActivity.kt | 77 +++++++++++++------ .../main/res/layout/dialog_change_avatar.xml | 17 ++++ app/src/main/res/values/strings.xml | 3 + 3 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 app/src/main/res/layout/dialog_change_avatar.xml diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt index 0a56c5058d..e7e3c0d268 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt @@ -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() @@ -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>() - 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(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) diff --git a/app/src/main/res/layout/dialog_change_avatar.xml b/app/src/main/res/layout/dialog_change_avatar.xml new file mode 100644 index 0000000000..afe3672f15 --- /dev/null +++ b/app/src/main/res/layout/dialog_change_avatar.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1609f7e2af..d7948c4f2d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -769,6 +769,9 @@ Scan the QR code of the open group you\'d like to join Enter an open group URL Settings + Set display picture + Upload + Remove Enter a display name Please pick a display name Please pick a shorter display name