Clean up seed step

This commit is contained in:
Niels Andriesse 2019-10-08 10:38:22 +11:00
parent 79ec4553aa
commit 95695ff88e
4 changed files with 53 additions and 76 deletions

View File

@ -20,7 +20,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/activity_account_details_title"
android:text="@string/activity_display_name_title"
android:textAlignment="center" />
<TextView
@ -29,7 +29,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/activity_account_details_subtitle"
android:text="@string/activity_display_name_subtitle"
android:textAlignment="center" />
<org.thoughtcrime.securesms.components.LabeledEditText
@ -38,7 +38,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:labeledEditText_background="@color/loki_darkest_gray"
app:labeledEditText_label="@string/activity_account_details_name_edit_text_label"/>
app:labeledEditText_label="@string/activity_display_name_name_edit_text_label"/>
<com.dd.CircularProgressButton
android:id="@+id/nextButton"
@ -53,7 +53,7 @@
app:cpb_colorProgress="@color/textsecure_primary"
app:cpb_cornerRadius="4dp"
app:cpb_selectorIdle="@drawable/progress_button_state"
app:cpb_textIdle="@string/activity_account_details_button_title" />
app:cpb_textIdle="@string/activity_display_name_button_title" />
</LinearLayout>

View File

@ -113,6 +113,7 @@
android:background="@color/transparent"
android:textColor="@color/signal_primary"
android:text="@string/activity_key_pair_toggle_mode_button_title_2"
android:visibility="gone"
android:elevation="0dp"
android:stateListAnimator="@null" />
@ -127,7 +128,7 @@
android:stateListAnimator="@null" />
<com.dd.CircularProgressButton
android:id="@+id/registerOrRestoreButton"
android:id="@+id/mainButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
@ -139,7 +140,7 @@
app:cpb_colorProgress="@color/textsecure_primary"
app:cpb_cornerRadius="4dp"
app:cpb_selectorIdle="@drawable/progress_button_state"
app:cpb_textIdle="@string/activity_key_pair_register_or_restore_button_title_1" />
app:cpb_textIdle="@string/activity_key_pair_main_button_title_1" />
</LinearLayout>

View File

@ -1549,11 +1549,11 @@
<string name="activity_landing_permission_dialog_message">Some features of Loki Messenger (such as automatic message backup) require storage access to work.</string>
<string name="activity_landing_beta_terms">"Loki Messenger is currently in beta. For development purposes the beta version collects basic usage statistics and crash logs. In addition, the beta version doesn't provide full privacy and shouldn\'t be used to transmit sensitive information."</string>
<string name="activity_landing_privacy_policy_button_title">Privacy Policy</string>
<!-- Account details activity -->
<string name="activity_account_details_title">Create Your Loki Messenger Account</string>
<string name="activity_account_details_subtitle">Enter a name to be shown to your contacts</string>
<string name="activity_account_details_name_edit_text_label">Display Name (Optional)</string>
<string name="activity_account_details_button_title">Next</string>
<!-- Display name activity -->
<string name="activity_display_name_title">Create Your Loki Messenger Account</string>
<string name="activity_display_name_subtitle">Enter a name to be shown to your contacts</string>
<string name="activity_display_name_name_edit_text_label">Display Name (Optional)</string>
<string name="activity_display_name_button_title">Next</string>
<!-- Key pair activity -->
<string name="activity_key_pair_title">Create Your Loki Messenger Account</string>
<string name="activity_key_pair_seed_explanation_1">Please save the seed below in a safe location. It can be used to restore your account if you lose access, or to migrate to a new device.</string>
@ -1565,9 +1565,9 @@
<string name="activity_key_pair_toggle_mode_button_title_2">Register a New Account</string>
<string name="activity_key_pair_toggle_mode_button_title_3">Link Device</string>
<string name="activity_key_pair_mnemonic_copied_message">Copied to clipboard</string>
<string name="activity_key_pair_register_or_restore_button_title_1">Register</string>
<string name="activity_key_pair_register_or_restore_button_title_2">Restore</string>
<string name="activity_key_pair_register_or_restore_button_title_3">Link</string>
<string name="activity_key_pair_main_button_title_1">Register</string>
<string name="activity_key_pair_main_button_title_2">Restore</string>
<string name="activity_key_pair_main_button_title_3">Link</string>
<string name="activity_key_pair_public_key_edit_text_label">Your Public Key</string>
<!-- Conversation list activity -->
<string name="activity_conversation_list_empty_state_message">Looks like you don\'t have any conversations yet. Get started by messaging a friend.</string>

View File

@ -13,9 +13,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import network.loki.messenger.R
import nl.komponents.kovenant.ui.failUi
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.BaseActionBarActivity
import org.thoughtcrime.securesms.ConversationListActivity
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
@ -34,7 +34,7 @@ import org.whispersystems.signalservice.loki.utilities.retryIfNeeded
import java.io.File
import java.io.FileOutputStream
class SeedActivity : BaseActionBarActivity() {
class SeedActivity : BaseActionBarActivity(), DeviceLinkingDialogDelegate {
private lateinit var languageFileDirectory: File
private var mode = Mode.Register
set(newValue) { field = newValue; updateUI() }
@ -53,18 +53,13 @@ class SeedActivity : BaseActionBarActivity() {
setContentView(R.layout.activity_seed)
setUpLanguageFileDirectory()
updateSeed()
updateUI()
copyButton.setOnClickListener { copy() }
toggleRestoreModeButton.setOnClickListener { mode = Mode.Restore }
toggleRegisterModeButton.setOnClickListener { mode = Mode.Register }
toggleRestoreModeButton.setOnClickListener { mode = Mode.Restore }
toggleLinkModeButton.setOnClickListener { mode = Mode.Link }
registerOrRestoreButton.setOnClickListener { registerOrRestore() }
mainButton.setOnClickListener { handleMainButtonTapped() }
Analytics.shared.track("Seed Screen Viewed")
}
override fun onDestroy() {
super.onDestroy()
}
// endregion
// region General
@ -102,29 +97,25 @@ class SeedActivity : BaseActionBarActivity() {
}
private fun updateUI() {
val showOnRegister = if (mode == Mode.Register) View.VISIBLE else View.GONE
val showOnRestore = if (mode == Mode.Restore) View.VISIBLE else View.GONE
val showOnLink = if (mode == Mode.Link) View.VISIBLE else View.GONE
seedExplanationTextView1.visibility = showOnRegister
mnemonicTextView.visibility = showOnRegister
copyButton.visibility = showOnRegister
seedExplanationTextView2.visibility = showOnRestore
mnemonicEditText.visibility = showOnRestore
publicKeyEditText.visibility = showOnLink
linkExplanationTextView.visibility = showOnLink
val registerModeVisibility = if (mode == Mode.Register) View.VISIBLE else View.GONE
val restoreModeVisibility = if (mode == Mode.Restore) View.VISIBLE else View.GONE
val linkModeVisibility = if (mode == Mode.Link) View.VISIBLE else View.GONE
seedExplanationTextView1.visibility = registerModeVisibility
mnemonicTextView.visibility = registerModeVisibility
copyButton.visibility = registerModeVisibility
seedExplanationTextView2.visibility = restoreModeVisibility
mnemonicEditText.visibility = restoreModeVisibility
linkExplanationTextView.visibility = linkModeVisibility
publicKeyEditText.visibility = linkModeVisibility
toggleRegisterModeButton.visibility = if (mode != Mode.Register) View.VISIBLE else View.GONE
toggleRestoreModeButton.visibility = if (mode != Mode.Restore) View.VISIBLE else View.GONE
toggleLinkModeButton.visibility = if (mode != Mode.Link) View.VISIBLE else View.GONE
val registerOrRestoreButtonTitleID = when (mode) {
Mode.Register -> R.string.activity_key_pair_register_or_restore_button_title_1
Mode.Restore -> R.string.activity_key_pair_register_or_restore_button_title_2
Mode.Link -> R.string.activity_key_pair_register_or_restore_button_title_3
val mainButtonTitleID = when (mode) {
Mode.Register -> R.string.activity_key_pair_main_button_title_1
Mode.Restore -> R.string.activity_key_pair_main_button_title_2
Mode.Link -> R.string.activity_key_pair_main_button_title_3
}
registerOrRestoreButton.setText(registerOrRestoreButtonTitleID)
mainButton.setText(mainButtonTitleID)
if (mode == Mode.Restore) {
mnemonicEditText.requestFocus()
} else {
@ -132,7 +123,6 @@ class SeedActivity : BaseActionBarActivity() {
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(mnemonicEditText.windowToken, 0)
}
if (mode == Mode.Link) {
publicKeyEditText.requestFocus()
} else {
@ -160,7 +150,7 @@ class SeedActivity : BaseActionBarActivity() {
Toast.makeText(this, R.string.activity_key_pair_mnemonic_copied_message, Toast.LENGTH_SHORT).show()
}
private fun registerOrRestore() {
private fun handleMainButtonTapped() {
var seed: ByteArray
when (mode) {
Mode.Register -> seed = this.seed!!
@ -175,7 +165,8 @@ class SeedActivity : BaseActionBarActivity() {
}
}
Mode.Link -> {
if (!PublicKeyValidation.isValid(publicKeyEditText.text.trim().toString())) {
val hexEncodedPublicKey = publicKeyEditText.text.trim().toString()
if (!PublicKeyValidation.isValid(hexEncodedPublicKey)) {
return Toast.makeText(this, "Invalid public key", Toast.LENGTH_SHORT).show()
}
seed = this.seed!!
@ -193,7 +184,7 @@ class SeedActivity : BaseActionBarActivity() {
val registrationID = KeyHelper.generateRegistrationId(false)
TextSecurePreferences.setLocalRegistrationId(this, registrationID)
DatabaseFactory.getIdentityDatabase(this).saveIdentity(Address.fromSerialized(hexEncodedPublicKey), publicKey,
IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true)
IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true)
TextSecurePreferences.setLocalNumber(this, hexEncodedPublicKey)
when (mode) {
Mode.Register -> Analytics.shared.track("Seed Created")
@ -203,55 +194,40 @@ class SeedActivity : BaseActionBarActivity() {
if (mode == Mode.Link) {
TextSecurePreferences.setHasSeenWelcomeScreen(this, true)
TextSecurePreferences.setPromptedPushRegistration(this, true)
// Build the pairing request
val primaryDevicePublicKey = publicKeyEditText.text.trim().toString()
val authorisation = PairingAuthorisation(primaryDevicePublicKey, hexEncodedPublicKey).sign(PairingAuthorisation.Type.REQUEST, keyPair.privateKey.serialize())
if (authorisation == null) {
Log.w("Loki", "Failed to sign outgoing pairing request :(")
resetRegistration()
return Toast.makeText(application, "Failed to initialise device pairing", Toast.LENGTH_SHORT).show()
Log.d("Loki", "Failed to sign outgoing pairing request.")
resetForRegistration()
return Toast.makeText(application, "Failed to link device.", Toast.LENGTH_SHORT).show()
}
val application = ApplicationContext.getInstance(this)
application.startLongPollingIfNeeded()
application.setUpP2PAPI()
application.setUpStorageAPIIfNeeded()
// Show the dialog
val dialog = DeviceLinkingDialog.show(this, DeviceLinkingView.Mode.Slave, object: DeviceLinkingDialogDelegate {
override fun handleDeviceLinkAuthorized() {
Analytics.shared.track("Device Linked Successfully")
showAccountDetailsView()
}
override fun handleDeviceLinkingDialogDismissed() {
resetRegistration()
Toast.makeText(this@SeedActivity, "Cancelled Device Linking", Toast.LENGTH_SHORT).show()
}
})
// Send the request to the other user
DeviceLinkingDialog.show(this, DeviceLinkingView.Mode.Slave, this)
CoroutineScope(Dispatchers.Main).launch {
retryIfNeeded(3) {
retryIfNeeded(8) {
sendPairingAuthorisationMessage(this@SeedActivity, authorisation.primaryDevicePublicKey, authorisation).get()
}.failUi {
dialog.dismiss()
resetRegistration()
Toast.makeText(application, "Failed to send device pairing request. Please try again.", Toast.LENGTH_SHORT).show()
}
}
} else {
showAccountDetailsView()
startActivity(Intent(this, DisplayNameActivity::class.java))
finish()
}
}
private fun showAccountDetailsView() {
startActivity(Intent(this, DisplayNameActivity::class.java))
override fun handleDeviceLinkAuthorized() {
Analytics.shared.track("Device Linked Successfully")
startActivity(Intent(this, ConversationListActivity::class.java))
finish()
}
private fun resetRegistration() {
override fun handleDeviceLinkingDialogDismissed() {
resetForRegistration()
}
private fun resetForRegistration() {
IdentityKeyUtil.delete(this, IdentityKeyUtil.lokiSeedKey)
TextSecurePreferences.removeLocalRegistrationId(this)
TextSecurePreferences.removeLocalNumber(this)