session-android/src/org/thoughtcrime/securesms/loki/redesign/activities/SettingsActivity.kt

137 lines
5.8 KiB
Kotlin
Raw Normal View History

2020-01-06 04:26:52 +01:00
package org.thoughtcrime.securesms.loki.redesign.activities
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
2020-01-07 02:00:30 +01:00
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
2020-01-06 04:26:52 +01:00
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_settings.*
import network.loki.messenger.R
2020-01-07 02:00:30 +01:00
import org.thoughtcrime.securesms.ApplicationContext
2020-01-06 04:26:52 +01:00
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.database.DatabaseFactory
2020-01-06 06:05:57 +01:00
import org.thoughtcrime.securesms.loki.redesign.utilities.push
2020-01-07 02:00:30 +01:00
import org.thoughtcrime.securesms.loki.toPx
2020-01-06 04:26:52 +01:00
import org.thoughtcrime.securesms.mms.GlideApp
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.util.TextSecurePreferences
class SettingsActivity : PassphraseRequiredActionBarActivity() {
private lateinit var glide: GlideRequests
2020-01-07 02:00:30 +01:00
private var isEditingDisplayName = false
set(value) { field = value; handleIsEditingDisplayNameChanged() }
private var displayNameToBeUploaded: String? = null
2020-01-06 04:26:52 +01:00
private val hexEncodedPublicKey: String
get() {
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this)
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
return masterHexEncodedPublicKey ?: userHexEncodedPublicKey
}
// region Lifecycle
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)
// Set content view
setContentView(R.layout.activity_settings)
2020-01-07 02:00:30 +01:00
// Set custom toolbar
setSupportActionBar(toolbar)
cancelButton.setOnClickListener { cancelEditingDisplayName() }
saveButton.setOnClickListener { saveDisplayName() }
showQRCodeButton.setOnClickListener { showQRCode() }
2020-01-06 04:26:52 +01:00
// Set up Glide
glide = GlideApp.with(this)
// Set up profile picture view
profilePictureView.glide = glide
profilePictureView.hexEncodedPublicKey = hexEncodedPublicKey
profilePictureView.isLarge = true
profilePictureView.update()
2020-01-07 02:00:30 +01:00
// Set up display name container
displayNameContainer.setOnClickListener { showEditDisplayNameUI() }
2020-01-06 04:26:52 +01:00
// Set up display name text view
displayNameTextView.text = DatabaseFactory.getLokiUserDatabase(this).getDisplayName(hexEncodedPublicKey)
// Set up public key text view
publicKeyTextView.text = hexEncodedPublicKey
// Set up copy button
copyButton.setOnClickListener { copyPublicKey() }
// Set up share button
shareButton.setOnClickListener { sharePublicKey() }
}
2020-01-07 02:00:30 +01:00
// endregion
2020-01-06 04:26:52 +01:00
2020-01-07 02:00:30 +01:00
// region Updating
private fun handleIsEditingDisplayNameChanged() {
cancelButton.visibility = if (isEditingDisplayName) View.VISIBLE else View.GONE
showQRCodeButton.visibility = if (isEditingDisplayName) View.GONE else View.VISIBLE
saveButton.visibility = if (isEditingDisplayName) View.VISIBLE else View.GONE
displayNameTextView.visibility = if (isEditingDisplayName) View.INVISIBLE else View.VISIBLE
displayNameEditText.visibility = if (isEditingDisplayName) View.VISIBLE else View.INVISIBLE
val titleTextViewLayoutParams = titleTextView.layoutParams as LinearLayout.LayoutParams
titleTextViewLayoutParams.leftMargin = if (isEditingDisplayName) toPx(16, resources) else 0
titleTextView.layoutParams = titleTextViewLayoutParams
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (isEditingDisplayName) {
displayNameEditText.requestFocus()
inputMethodManager.showSoftInput(displayNameEditText, 0)
} else {
inputMethodManager.hideSoftInputFromWindow(displayNameEditText.windowToken, 0)
}
}
private fun updateProfile(isUpdatingDisplayName: Boolean, isUpdatingProfilePicture: Boolean) {
val displayName = displayNameToBeUploaded ?: TextSecurePreferences.getProfileName(this)
TextSecurePreferences.setProfileName(this, displayName)
val publicChatAPI = ApplicationContext.getInstance(this).lokiPublicChatAPI
if (publicChatAPI != null) {
val servers = DatabaseFactory.getLokiThreadDatabase(this).getAllPublicChatServers()
for (server in servers) {
publicChatAPI.setDisplayName(displayName, server)
}
}
displayNameTextView.text = displayName
displayNameToBeUploaded = null
2020-01-06 04:26:52 +01:00
}
// endregion
// region Interaction
2020-01-07 02:00:30 +01:00
private fun cancelEditingDisplayName() {
isEditingDisplayName = false
}
private fun saveDisplayName() {
val displayName = displayNameEditText.text.trim().toString()
// TODO: Validation
isEditingDisplayName = false
displayNameToBeUploaded = displayName
updateProfile(true, false)
2020-01-06 04:26:52 +01:00
}
private fun showQRCode() {
2020-01-06 06:05:57 +01:00
val intent = Intent(this, QRCodeActivity::class.java)
push(intent)
2020-01-06 04:26:52 +01:00
}
2020-01-07 02:00:30 +01:00
private fun showEditDisplayNameUI() {
isEditingDisplayName = true
}
2020-01-06 04:26:52 +01:00
private fun copyPublicKey() {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Session ID", hexEncodedPublicKey)
clipboard.primaryClip = clip
Toast.makeText(this, R.string.activity_register_public_key_copied_message, Toast.LENGTH_SHORT).show()
}
private fun sharePublicKey() {
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT, hexEncodedPublicKey)
intent.type = "text/plain"
startActivity(intent)
}
// endregion
}