LockScreen : refact some code and add secureFlag
This commit is contained in:
parent
64b7c906db
commit
5a417ba498
15 changed files with 128 additions and 47 deletions
|
|
@ -35,8 +35,12 @@ import io.element.android.services.appnavstate.api.AppForegroundStateService
|
|||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
import kotlin.time.Duration
|
||||
|
|
@ -113,14 +117,23 @@ class DefaultLockScreenService @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun isSetupRequired(): Boolean {
|
||||
return lockScreenConfig.isPinMandatory
|
||||
&& featureFlagService.isFeatureEnabled(FeatureFlags.PinUnlock)
|
||||
&& !pinCodeManager.isPinCodeAvailable()
|
||||
override fun isPinSetup(): Flow<Boolean> {
|
||||
return combine(
|
||||
featureFlagService.isFeatureEnabledFlow(FeatureFlags.PinUnlock),
|
||||
pinCodeManager.hasPinCode()
|
||||
) { isEnabled, hasPinCode ->
|
||||
isEnabled && hasPinCode
|
||||
}
|
||||
}
|
||||
|
||||
override fun isSetupRequired(): Flow<Boolean> {
|
||||
return isPinSetup().map { isPinSetup ->
|
||||
!isPinSetup && lockScreenConfig.isPinMandatory
|
||||
}
|
||||
}
|
||||
|
||||
private fun CoroutineScope.lockIfNeeded(gracePeriod: Duration = Duration.ZERO) = launch {
|
||||
if (featureFlagService.isFeatureEnabled(FeatureFlags.PinUnlock) && pinCodeManager.isPinCodeAvailable()) {
|
||||
if (isPinSetup().first()) {
|
||||
delay(gracePeriod)
|
||||
_lockScreenState.value = LockScreenLockState.Locked
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import io.element.android.libraries.cryptography.api.EncryptionResult
|
|||
import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import javax.inject.Inject
|
||||
|
||||
|
|
@ -46,7 +47,7 @@ class DefaultPinCodeManager @Inject constructor(
|
|||
callbacks.remove(callback)
|
||||
}
|
||||
|
||||
override suspend fun isPinCodeAvailable(): Boolean {
|
||||
override fun hasPinCode(): Flow<Boolean> {
|
||||
return lockScreenStore.hasPinCode()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package io.element.android.features.lockscreen.impl.pin
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* This interface is the main interface to manage the pin code.
|
||||
* Implementation should take care of encrypting the pin code and storing it.
|
||||
|
|
@ -55,7 +57,7 @@ interface PinCodeManager {
|
|||
/**
|
||||
* @return true if a pin code is available.
|
||||
*/
|
||||
suspend fun isPinCodeAvailable(): Boolean
|
||||
fun hasPinCode(): Flow<Boolean>
|
||||
|
||||
/**
|
||||
* @return the size of the saved pin code.
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ import io.element.android.libraries.architecture.BackstackNode
|
|||
import io.element.android.libraries.architecture.animation.rememberDefaultTransitionHandler
|
||||
import io.element.android.libraries.architecture.createNode
|
||||
import io.element.android.libraries.di.SessionScope
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
|
|
@ -90,9 +91,11 @@ class LockScreenSettingsFlowNode @AssistedInject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
init {
|
||||
override fun onBuilt() {
|
||||
super.onBuilt()
|
||||
lifecycleScope.launch {
|
||||
if (pinCodeManager.isPinCodeAvailable()) {
|
||||
val hasPinCode = pinCodeManager.hasPinCode().first()
|
||||
if (hasPinCode) {
|
||||
backstack.newRoot(NavTarget.Unlock)
|
||||
} else {
|
||||
backstack.newRoot(NavTarget.Setup)
|
||||
|
|
|
|||
|
|
@ -17,11 +17,10 @@
|
|||
package io.element.android.features.lockscreen.impl.settings
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import io.element.android.appconfig.LockScreenConfig
|
||||
|
|
@ -43,23 +42,15 @@ class LockScreenSettingsPresenter @Inject constructor(
|
|||
|
||||
@Composable
|
||||
override fun present(): LockScreenSettingsState {
|
||||
var triggerComputation by remember {
|
||||
mutableIntStateOf(0)
|
||||
}
|
||||
var showRemovePinOption by remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
var showToggleBiometric by remember {
|
||||
mutableStateOf(false)
|
||||
val showRemovePinOption by produceState(initialValue = false) {
|
||||
pinCodeManager.hasPinCode().collect { hasPinCode ->
|
||||
value = !lockScreenConfig.isPinMandatory && hasPinCode
|
||||
}
|
||||
}
|
||||
val isBiometricEnabled by lockScreenStore.isBiometricUnlockAllowed().collectAsState(initial = false)
|
||||
var showRemovePinConfirmation by remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
LaunchedEffect(triggerComputation) {
|
||||
showRemovePinOption = !lockScreenConfig.isPinMandatory && pinCodeManager.isPinCodeAvailable()
|
||||
showToggleBiometric = biometricUnlockManager.isDeviceSecured
|
||||
}
|
||||
|
||||
fun handleEvents(event: LockScreenSettingsEvents) {
|
||||
when (event) {
|
||||
|
|
@ -69,7 +60,6 @@ class LockScreenSettingsPresenter @Inject constructor(
|
|||
if (showRemovePinConfirmation) {
|
||||
showRemovePinConfirmation = false
|
||||
pinCodeManager.deletePinCode()
|
||||
triggerComputation++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -86,7 +76,7 @@ class LockScreenSettingsPresenter @Inject constructor(
|
|||
showRemovePinOption = showRemovePinOption,
|
||||
isBiometricEnabled = isBiometricEnabled,
|
||||
showRemovePinConfirmation = showRemovePinConfirmation,
|
||||
showToggleBiometric = showToggleBiometric,
|
||||
showToggleBiometric = biometricUnlockManager.isDeviceSecured,
|
||||
eventSink = ::handleEvents
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package io.element.android.features.lockscreen.impl.storage
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Should be implemented by any class that provides access to the encrypted PIN code.
|
||||
* All methods are suspending in case there are async IO operations involved.
|
||||
|
|
@ -39,5 +41,6 @@ interface EncryptedPinCodeStorage {
|
|||
/**
|
||||
* Returns whether the PIN code is stored or not.
|
||||
*/
|
||||
suspend fun hasPinCode(): Boolean
|
||||
fun hasPinCode(): Flow<Boolean>
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,10 +85,10 @@ class PreferencesLockScreenStore @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun hasPinCode(): Boolean {
|
||||
override fun hasPinCode(): Flow<Boolean> {
|
||||
return context.dataStore.data.map { preferences ->
|
||||
preferences[pinCodeKey] != null
|
||||
}.first()
|
||||
}
|
||||
}
|
||||
|
||||
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package io.element.android.features.lockscreen.impl.pin
|
||||
|
||||
import app.cash.turbine.test
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.features.lockscreen.impl.pin.storage.InMemoryLockScreenStore
|
||||
import io.element.android.libraries.cryptography.impl.AESEncryptionDecryptionService
|
||||
|
|
@ -32,10 +33,13 @@ class DefaultPinCodeManagerTest {
|
|||
|
||||
@Test
|
||||
fun `given a pin code when create and delete assert no pin code left`() = runTest {
|
||||
pinCodeManager.createPinCode("1234")
|
||||
assertThat(pinCodeManager.isPinCodeAvailable()).isTrue()
|
||||
pinCodeManager.deletePinCode()
|
||||
assertThat(pinCodeManager.isPinCodeAvailable()).isFalse()
|
||||
pinCodeManager.hasPinCode().test {
|
||||
assertThat(awaitItem()).isFalse()
|
||||
pinCodeManager.createPinCode("1234")
|
||||
assertThat(awaitItem()).isTrue()
|
||||
pinCodeManager.deletePinCode()
|
||||
assertThat(awaitItem()).isFalse()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -24,7 +24,12 @@ private const val DEFAULT_REMAINING_ATTEMPTS = 3
|
|||
|
||||
class InMemoryLockScreenStore : LockScreenStore {
|
||||
|
||||
private val hasPinCode = MutableStateFlow(false)
|
||||
private var pinCode: String? = null
|
||||
set(value) {
|
||||
field = value
|
||||
hasPinCode.value = value != null
|
||||
}
|
||||
private var remainingAttempts: Int = DEFAULT_REMAINING_ATTEMPTS
|
||||
private var isBiometricUnlockAllowed = MutableStateFlow(false)
|
||||
|
||||
|
|
@ -52,8 +57,8 @@ class InMemoryLockScreenStore : LockScreenStore {
|
|||
pinCode = null
|
||||
}
|
||||
|
||||
override suspend fun hasPinCode(): Boolean {
|
||||
return pinCode != null
|
||||
override fun hasPinCode(): Flow<Boolean> {
|
||||
return hasPinCode
|
||||
}
|
||||
|
||||
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue