From 140b3ed1385631291f622c038bad56ddc02bf502 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 11 Oct 2023 17:18:55 +1030 Subject: [PATCH] Reinstate key generation on continue pick name --- app/src/main/AndroidManifest.xml | 2 +- .../securesms/onboarding/LandingActivity.kt | 9 +--- .../securesms/onboarding/LoadingActivity.kt | 4 +- .../securesms/onboarding/PNModeActivity.kt | 8 +++- .../PickDisplayNameActivity.kt} | 36 ++++++++-------- .../PickDisplayNameViewModel.kt} | 41 +++++++++++++++++-- 6 files changed, 66 insertions(+), 34 deletions(-) rename app/src/main/java/org/thoughtcrime/securesms/onboarding/{name/DisplayNameActivity.kt => pickname/PickDisplayNameActivity.kt} (80%) rename app/src/main/java/org/thoughtcrime/securesms/onboarding/{name/DisplayNameViewModel.kt => pickname/PickDisplayNameViewModel.kt} (54%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8382c5006f..54b888489b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -115,7 +115,7 @@ android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.Session.DayNight.FlatActionBar" /> diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LandingActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LandingActivity.kt index 666c4c79db..c23d2adc22 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LandingActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LandingActivity.kt @@ -6,7 +6,7 @@ import network.loki.messenger.databinding.ActivityLandingBinding import org.session.libsession.utilities.TextSecurePreferences import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.crypto.IdentityKeyUtil -import org.thoughtcrime.securesms.onboarding.name.DisplayNameActivity +import org.thoughtcrime.securesms.onboarding.pickname.startPickDisplayNameActivity import org.thoughtcrime.securesms.service.KeyCachingService import org.thoughtcrime.securesms.util.push import org.thoughtcrime.securesms.util.setUpActionBarSessionLogo @@ -20,7 +20,7 @@ class LandingActivity : BaseActionBarActivity() { setUpActionBarSessionLogo(true) with(binding) { fakeChatView.startAnimating() - registerButton.setOnClickListener { register() } + registerButton.setOnClickListener { startPickDisplayNameActivity() } restoreButton.setOnClickListener { link() } linkButton.setOnClickListener { link() } } @@ -30,11 +30,6 @@ class LandingActivity : BaseActionBarActivity() { KeyCachingService.setMasterSecret(applicationContext, Object()) } - private fun register() { - val intent = Intent(this, DisplayNameActivity::class.java) - push(intent) - } - private fun link() { val intent = Intent(this, LinkDeviceActivity::class.java) push(intent) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt index 293069f497..7fcc1fb54a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt @@ -24,7 +24,7 @@ import kotlinx.coroutines.launch import org.session.libsession.utilities.TextSecurePreferences import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.dependencies.ConfigFactory -import org.thoughtcrime.securesms.onboarding.name.DisplayNameActivity +import org.thoughtcrime.securesms.onboarding.pickname.PickDisplayNameActivity import org.thoughtcrime.securesms.ui.AppTheme import org.thoughtcrime.securesms.ui.ProgressArc import org.thoughtcrime.securesms.util.push @@ -50,7 +50,7 @@ class LoadingActivity: BaseActionBarActivity() { private fun register(skipped: Boolean) { prefs.setLastConfigurationSyncTime(System.currentTimeMillis()) - Intent(this, if (skipped) DisplayNameActivity::class.java else PNModeActivity::class.java) + Intent(this, if (skipped) PickDisplayNameActivity::class.java else PNModeActivity::class.java) .apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } .also(::push) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt index e4e8e6a9a6..aec3c1cdff 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt @@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.onboarding import android.animation.ArgbEvaluator import android.animation.ValueAnimator +import android.content.Context import android.content.Intent import android.graphics.drawable.TransitionDrawable import android.net.Uri @@ -28,6 +29,7 @@ import org.thoughtcrime.securesms.util.PNModeView import org.thoughtcrime.securesms.util.disableClipping import org.thoughtcrime.securesms.util.getAccentColor import org.thoughtcrime.securesms.util.getColorWithID +import org.thoughtcrime.securesms.util.push import org.thoughtcrime.securesms.util.setUpActionBarSessionLogo import org.thoughtcrime.securesms.util.show import javax.inject.Inject @@ -176,4 +178,8 @@ class PNModeActivity : BaseActionBarActivity() { show(intent) } // endregion -} \ No newline at end of file +} + +fun Context.startPNModeActivity() { + Intent(this, PNModeActivity::class.java).also(::startActivity) +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/name/DisplayNameActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt similarity index 80% rename from app/src/main/java/org/thoughtcrime/securesms/onboarding/name/DisplayNameActivity.kt rename to app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt index 59a605e711..2db88147f0 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/name/DisplayNameActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt @@ -1,8 +1,8 @@ -package org.thoughtcrime.securesms.onboarding.name +package org.thoughtcrime.securesms.onboarding.pickname +import android.content.Context import android.content.Intent import android.os.Bundle -import android.view.inputmethod.InputMethodManager import androidx.activity.viewModels import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -11,7 +11,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardActions -import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.ContentAlpha import androidx.compose.material.LocalContentAlpha import androidx.compose.material.LocalContentColor @@ -32,21 +31,19 @@ import androidx.lifecycle.lifecycleScope import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch import network.loki.messenger.R -import network.loki.messenger.databinding.ActivityDisplayNameBinding import org.thoughtcrime.securesms.BaseActionBarActivity -import org.thoughtcrime.securesms.onboarding.PNModeActivity +import org.thoughtcrime.securesms.onboarding.startPNModeActivity import org.thoughtcrime.securesms.ui.AppTheme import org.thoughtcrime.securesms.ui.OutlineButton import org.thoughtcrime.securesms.ui.PreviewTheme import org.thoughtcrime.securesms.ui.base import org.thoughtcrime.securesms.ui.baseBold import org.thoughtcrime.securesms.ui.colorDestructive -import org.thoughtcrime.securesms.util.push import org.thoughtcrime.securesms.util.setUpActionBarSessionLogo @AndroidEntryPoint -class DisplayNameActivity : BaseActionBarActivity() { - private val viewModel: DisplayNameViewModel by viewModels() +class PickDisplayNameActivity : BaseActionBarActivity() { + private val viewModel: PickDisplayNameViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -58,20 +55,17 @@ class DisplayNameActivity : BaseActionBarActivity() { lifecycleScope.launch { viewModel.eventFlow.collect { -// val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager -// inputMethodManager.hideSoftInputFromWindow(content.displayNameEditText.windowToken, 0) - - Intent(this@DisplayNameActivity, PNModeActivity::class.java).also(::push) + startPNModeActivity() } } } @Composable - private fun DisplayNameScreen(viewModel: DisplayNameViewModel) { + private fun DisplayNameScreen(viewModel: PickDisplayNameViewModel) { val state = viewModel.stateFlow.collectAsState() AppTheme { - DisplayName(state.value, viewModel::onChange, viewModel::onContinue) + DisplayName(state.value, viewModel::onChange) { viewModel.onContinue(this) } } } @@ -113,10 +107,10 @@ class DisplayNameActivity : BaseActionBarActivity() { ), singleLine = true, keyboardActions = KeyboardActions( - onDone = { viewModel.onContinue() }, - onGo = { viewModel.onContinue() }, - onSearch = { viewModel.onContinue() }, - onSend = { viewModel.onContinue() }, + onDone = { onContinue() }, + onGo = { onContinue() }, + onSearch = { onContinue() }, + onSend = { onContinue() }, ), isError = state.error != null, shape = RoundedCornerShape(12.dp) @@ -136,4 +130,8 @@ class DisplayNameActivity : BaseActionBarActivity() { ) { onContinue() } } } -} \ No newline at end of file +} + +fun Context.startPickDisplayNameActivity() { + Intent(this, PickDisplayNameActivity::class.java).also(::startActivity) +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/name/DisplayNameViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt similarity index 54% rename from app/src/main/java/org/thoughtcrime/securesms/onboarding/name/DisplayNameViewModel.kt rename to app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt index e76ce8dfd2..49939c257f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/name/DisplayNameViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt @@ -1,5 +1,6 @@ -package org.thoughtcrime.securesms.onboarding.name +package org.thoughtcrime.securesms.onboarding.pickname +import android.content.Context import androidx.annotation.StringRes import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope @@ -11,12 +12,18 @@ import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import network.loki.messenger.R +import org.session.libsession.snode.SnodeModule import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol.Companion.NAME_PADDED_LENGTH import org.session.libsession.utilities.TextSecurePreferences +import org.session.libsignal.database.LokiAPIDatabaseProtocol +import org.session.libsignal.utilities.KeyHelper +import org.session.libsignal.utilities.hexEncodedPublicKey +import org.thoughtcrime.securesms.crypto.KeyPairUtilities +import org.thoughtcrime.securesms.dependencies.ConfigFactory import javax.inject.Inject @HiltViewModel -class DisplayNameViewModel @Inject constructor( +class PickDisplayNameViewModel @Inject constructor( private val prefs: TextSecurePreferences ): ViewModel() { @@ -26,16 +33,42 @@ class DisplayNameViewModel @Inject constructor( private val event = Channel() val eventFlow = event.receiveAsFlow() - fun onContinue() { + @Inject + lateinit var configFactory: ConfigFactory + + private val database: LokiAPIDatabaseProtocol + get() = SnodeModule.shared.storage + + fun onContinue(context: Context) { state.update { it.copy(displayName = it.displayName.trim()) } val displayName = state.value.displayName + val keyPairGenerationResult = KeyPairUtilities.generate() + val seed = keyPairGenerationResult.seed + val ed25519KeyPair = keyPairGenerationResult.ed25519KeyPair + val x25519KeyPair = keyPairGenerationResult.x25519KeyPair + when { displayName.isEmpty() -> { state.update { it.copy(error = R.string.activity_display_name_display_name_missing_error) } } displayName.length > NAME_PADDED_LENGTH -> { state.update { it.copy(error = R.string.activity_display_name_display_name_too_long_error) } } else -> { prefs.setProfileName(displayName) + + // This is here to resolve a case where the app restarts before a user completes onboarding + // which can result in an invalid database state + database.clearAllLastMessageHashes() + database.clearReceivedMessageHashValues() + + KeyPairUtilities.store(context, seed, ed25519KeyPair, x25519KeyPair) + configFactory.keyPairChanged() + val userHexEncodedPublicKey = x25519KeyPair.hexEncodedPublicKey + val registrationID = KeyHelper.generateRegistrationId(false) + prefs.setLocalRegistrationId(registrationID) + prefs.setLocalNumber(userHexEncodedPublicKey) + prefs.setRestorationTime(0) + prefs.setHasViewedSeed(false) + viewModelScope.launch { event.send(Event.DONE) } } } @@ -60,4 +93,4 @@ data class State( sealed interface Event { object DONE: Event -} \ No newline at end of file +}