From 343820579cfb05abc03bd19f8da0d09e0d1cd211 Mon Sep 17 00:00:00 2001 From: Al Lansley Date: Fri, 19 Jan 2024 15:23:47 +1100 Subject: [PATCH] SA1390 - Session ID regeneration on focus loss (#1392) * Fixes #1390 * Clean up prior to PR * Removed unused logging imports introduced during debugging * Added mechanism to store the session ID seed temporarily during account creation * Implemented keeping session ID on lose/regain focus via Hariss' better way using the savedInstanceState Bundle * Removed now unused imports --- .../securesms/onboarding/LandingActivity.kt | 5 +++++ .../securesms/onboarding/RegisterActivity.kt | 20 +++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) 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 f67f0fbaa6..c878a79eef 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LandingActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LandingActivity.kt @@ -14,6 +14,11 @@ class LandingActivity : BaseActionBarActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + // We always hit this LandingActivity on launch - but if there is a previous instance of + // Session then close this activity to resume the last activity from the previous instance. + if (!isTaskRoot) { finish(); return } + val binding = ActivityLandingBinding.inflate(layoutInflater) setContentView(binding.root) setUpActionBarSessionLogo(true) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/RegisterActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/RegisterActivity.kt index 6e082e0008..13e5b51f0e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/RegisterActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/RegisterActivity.kt @@ -35,6 +35,8 @@ import javax.inject.Inject @AndroidEntryPoint class RegisterActivity : BaseActionBarActivity() { + private val temporarySeedKey = "TEMPORARY_SEED_KEY" + @Inject lateinit var configFactory: ConfigFactory @@ -77,16 +79,23 @@ class RegisterActivity : BaseActionBarActivity() { }, 61, 75, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) binding.termsTextView.movementMethod = LinkMovementMethod.getInstance() binding.termsTextView.text = termsExplanation - updateKeyPair() + updateKeyPair(savedInstanceState?.getByteArray(temporarySeedKey)) + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + seed?.let { tempSeed -> + outState.putByteArray(temporarySeedKey, tempSeed) + } } // endregion // region Updating - private fun updateKeyPair() { - val keyPairGenerationResult = KeyPairUtilities.generate() - seed = keyPairGenerationResult.seed + private fun updateKeyPair(temporaryKey: ByteArray?) { + val keyPairGenerationResult = temporaryKey?.let(KeyPairUtilities::generate) ?: KeyPairUtilities.generate() + seed = keyPairGenerationResult.seed ed25519KeyPair = keyPairGenerationResult.ed25519KeyPair - x25519KeyPair = keyPairGenerationResult.x25519KeyPair + x25519KeyPair = keyPairGenerationResult.x25519KeyPair } private fun updatePublicKeyTextView() { @@ -125,7 +134,6 @@ class RegisterActivity : BaseActionBarActivity() { // which can result in an invalid database state database.clearAllLastMessageHashes() database.clearReceivedMessageHashValues() - KeyPairUtilities.store(this, seed!!, ed25519KeyPair!!, x25519KeyPair!!) configFactory.keyPairChanged() val userHexEncodedPublicKey = x25519KeyPair!!.hexEncodedPublicKey