Reinstate key generation on continue pick name

This commit is contained in:
andrew 2023-10-11 17:18:55 +10:30
parent b7ffc9afc9
commit 140b3ed138
6 changed files with 66 additions and 34 deletions

View File

@ -115,7 +115,7 @@
android:windowSoftInputMode="adjustResize"
android:theme="@style/Theme.Session.DayNight.FlatActionBar" />
<activity
android:name="org.thoughtcrime.securesms.onboarding.name.DisplayNameActivity"
android:name="org.thoughtcrime.securesms.onboarding.pickname.PickDisplayNameActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"
android:theme="@style/Theme.Session.DayNight.FlatActionBar" />

View File

@ -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)

View File

@ -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)
}

View File

@ -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
}
}
fun Context.startPNModeActivity() {
Intent(this, PNModeActivity::class.java).also(::startActivity)
}

View File

@ -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() }
}
}
}
}
fun Context.startPickDisplayNameActivity() {
Intent(this, PickDisplayNameActivity::class.java).also(::startActivity)
}

View File

@ -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<Event>()
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
}
}