生物识别解锁独立化:可无 PIN 单独开启,关闭验证按有无 PIN 分派,生物失效时锁屏提示

This commit is contained in:
2026-10-15 10:26:32 +08:00
parent d3459e69c1
commit 01c619e925
6 changed files with 110 additions and 98 deletions
@@ -41,11 +41,12 @@ data class AppLockSettings(
val isBiometricEnabled: Boolean = false,
val lockTrigger: AppLockTrigger = AppLockTrigger.DEFAULT
) {
/** App locks when PIN is active or biometric unlock is enabled on its own. */
val isAppLockEnabled: Boolean
get() = isPinEnabled && hasPinCode
get() = (isPinEnabled && hasPinCode) || isBiometricEnabled
val canUseBiometric: Boolean
get() = isAppLockEnabled && isBiometricEnabled
get() = isBiometricEnabled
}
class SettingsRepository(private val context: Context, private val cryptoManager: CryptoManager) {
@@ -86,11 +87,10 @@ class SettingsRepository(private val context: Context, private val cryptoManager
storedLockTrigger
) { pinEnabled, decryptedPinCode, biometricEnabled, lockTrigger ->
val hasPinCode = decryptedPinCode?.isValidPin() == true
val isAppLockEnabled = pinEnabled && hasPinCode
AppLockSettings(
isPinEnabled = isAppLockEnabled,
isPinEnabled = pinEnabled && hasPinCode,
hasPinCode = hasPinCode,
isBiometricEnabled = isAppLockEnabled && biometricEnabled,
isBiometricEnabled = biometricEnabled,
lockTrigger = lockTrigger
)
}
@@ -110,19 +110,6 @@ class SettingsRepository(private val context: Context, private val cryptoManager
}
}
suspend fun setPinCode(pin: String?) {
require(pin == null || pin.isValidPin()) { "PIN must be exactly 6 digits." }
dataStore.edit { preferences ->
if (pin == null) {
preferences.remove(PreferencesKeys.PIN_CODE)
preferences[PreferencesKeys.PIN_ENABLED] = false
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = false
} else {
preferences[PreferencesKeys.PIN_CODE] = cryptoManager.encrypt(pin)
}
}
}
suspend fun enablePin(pin: String) {
require(pin.isValidPin()) { "PIN must be exactly 6 digits." }
dataStore.edit { preferences ->
@@ -134,7 +121,6 @@ class SettingsRepository(private val context: Context, private val cryptoManager
suspend fun disablePin() {
dataStore.edit { preferences ->
preferences[PreferencesKeys.PIN_ENABLED] = false
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = false
preferences.remove(PreferencesKeys.PIN_CODE)
preferences[PreferencesKeys.FAILED_ATTEMPTS] = 0
preferences[PreferencesKeys.LOCKOUT_END_TIME] = 0L
@@ -147,9 +133,7 @@ class SettingsRepository(private val context: Context, private val cryptoManager
suspend fun setBiometricEnabled(enabled: Boolean) {
dataStore.edit { preferences ->
val hasPinCode = preferences[PreferencesKeys.PIN_CODE] != null
val isPinEnabled = preferences[PreferencesKeys.PIN_ENABLED] ?: false
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = enabled && hasPinCode && isPinEnabled
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = enabled
}
}
@@ -34,6 +34,8 @@ fun LockScreen(
val pinCode by settingsRepo.pinCode.collectAsStateWithLifecycle(initialValue = null)
val isBiometricEnabled = appLockSettings.canUseBiometric
val hasPin = appLockSettings.isPinEnabled && pinCode != null
var biometricUnavailable by remember { mutableStateOf(false) }
val failedAttempts by settingsRepo.failedAttempts.collectAsStateWithLifecycle(initialValue = 0)
val lockoutEndTime by settingsRepo.lockoutEndTime.collectAsStateWithLifecycle(initialValue = 0L)
@@ -66,15 +68,19 @@ fun LockScreen(
LaunchedEffect(isBiometricEnabled) {
if (isBiometricEnabled) {
biometricAuthenticator.authenticate(
mode = BiometricAuthMode.AppConfirmation,
titleRes = R.string.unlock_2fair,
subtitleRes = R.string.biometric_prompt_subtitle
) { result ->
when (result) {
BiometricAuthResult.Success -> onUnlock()
BiometricAuthResult.Failed -> errorMessage = biometricFailedMsg
else -> Unit
if (biometricAuthenticator.getAvailability(BiometricAuthMode.AppConfirmation) != null) {
biometricUnavailable = true
} else {
biometricAuthenticator.authenticate(
mode = BiometricAuthMode.AppConfirmation,
titleRes = R.string.unlock_2fair,
subtitleRes = R.string.biometric_prompt_subtitle
) { result ->
when (result) {
BiometricAuthResult.Success -> onUnlock()
BiometricAuthResult.Failed -> errorMessage = biometricFailedMsg
else -> Unit
}
}
}
}
@@ -119,69 +125,84 @@ fun LockScreen(
)
Spacer(modifier = Modifier.height(32.dp))
if (isBiometricEnabled && biometricUnavailable) {
Text(
text = stringResource(R.string.biometric_unavailable_no_pin),
color = MaterialTheme.colorScheme.error,
style = MaterialTheme.typography.bodyMedium,
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
modifier = Modifier.padding(horizontal = 16.dp)
)
Spacer(modifier = Modifier.height(24.dp))
}
Text(
text = stringResource(R.string.enter_pin),
text = if (hasPin) stringResource(R.string.enter_pin)
else stringResource(R.string.unlock_2fair),
style = MaterialTheme.typography.headlineSmall
)
Spacer(modifier = Modifier.height(16.dp))
PinIndicatorRow(
filledCount = enteredPin.length,
isError = errorMessage != null || remainingLockOutTime > 0L,
isSuccess = isSuccess,
modifier = Modifier.fillMaxWidth(0.8f)
)
Spacer(modifier = Modifier.height(12.dp))
if (hasPin) {
Spacer(modifier = Modifier.height(16.dp))
Text(
text = when {
remainingLockOutTime > 0L -> stringResource(R.string.try_again_in, remainingLockOutTime)
errorMessage != null -> errorMessage!!
enteredPin.isEmpty() -> pinHintMsg
else -> stringResource(R.string.pin_digits_remaining, PIN_LENGTH - enteredPin.length)
},
color = if (errorMessage != null || remainingLockOutTime > 0L) {
MaterialTheme.colorScheme.error
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
style = MaterialTheme.typography.bodyMedium
)
Spacer(modifier = Modifier.height(24.dp))
PinIndicatorRow(
filledCount = enteredPin.length,
isError = errorMessage != null || remainingLockOutTime > 0L,
isSuccess = isSuccess,
modifier = Modifier.fillMaxWidth(0.8f)
)
Spacer(modifier = Modifier.height(12.dp))
PinNumberPad(
resetLabel = stringResource(R.string.clear_pin_input),
deleteLabel = stringResource(R.string.delete_digit),
onDigit = { digit ->
if (remainingLockOutTime > 0L || isSuccess || enteredPin.length >= PIN_LENGTH) {
return@PinNumberPad
}
val updatedPin = enteredPin + digit
enteredPin = updatedPin
errorMessage = null
if (updatedPin.length == PIN_LENGTH) {
submitPin(updatedPin)
}
},
onDelete = {
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
enteredPin = enteredPin.dropLast(1)
Text(
text = when {
remainingLockOutTime > 0L -> stringResource(R.string.try_again_in, remainingLockOutTime)
errorMessage != null -> errorMessage!!
enteredPin.isEmpty() -> pinHintMsg
else -> stringResource(R.string.pin_digits_remaining, PIN_LENGTH - enteredPin.length)
},
color = if (errorMessage != null || remainingLockOutTime > 0L) {
MaterialTheme.colorScheme.error
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
style = MaterialTheme.typography.bodyMedium
)
Spacer(modifier = Modifier.height(24.dp))
PinNumberPad(
resetLabel = stringResource(R.string.clear_pin_input),
deleteLabel = stringResource(R.string.delete_digit),
onDigit = { digit ->
if (remainingLockOutTime > 0L || isSuccess || enteredPin.length >= PIN_LENGTH) {
return@PinNumberPad
}
val updatedPin = enteredPin + digit
enteredPin = updatedPin
errorMessage = null
}
},
onReset = {
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
enteredPin = ""
errorMessage = null
}
},
modifier = Modifier.fillMaxWidth(0.9f),
isEnabled = remainingLockOutTime == 0L,
isDeleteEnabled = enteredPin.isNotEmpty(),
isResetEnabled = enteredPin.isNotEmpty()
)
if (updatedPin.length == PIN_LENGTH) {
submitPin(updatedPin)
}
},
onDelete = {
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
enteredPin = enteredPin.dropLast(1)
errorMessage = null
}
},
onReset = {
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
enteredPin = ""
errorMessage = null
}
},
modifier = Modifier.fillMaxWidth(0.9f),
isEnabled = remainingLockOutTime == 0L,
isDeleteEnabled = enteredPin.isNotEmpty(),
isResetEnabled = enteredPin.isNotEmpty()
)
}
if (isBiometricEnabled) {
if (isBiometricEnabled && !biometricUnavailable) {
Spacer(modifier = Modifier.height(16.dp))
FilledTonalButton(
onClick = {
@@ -125,7 +125,6 @@ fun SecuritySettingsScreen(
}
Switch(
checked = isBiometricEnabled,
enabled = isPinEnabled,
onCheckedChange = { checked ->
if (checked) {
biometricAuthenticator.authenticate(
@@ -163,8 +162,25 @@ fun SecuritySettingsScreen(
BiometricAuthResult.Cancelled -> Unit
}
}
} else {
} else if (isPinEnabled) {
// PIN available: confirm downgrade with PIN
verificationAction = SecurityVerificationAction.DisableBiometric
} else {
// No PIN: confirm downgrade with biometric itself
biometricAuthenticator.authenticate(
mode = BiometricAuthMode.BiometricOnly,
titleRes = R.string.disable_biometric_verification_title,
subtitleRes = R.string.enable_biometric_verification_desc
) { result ->
when (result) {
BiometricAuthResult.Success -> {
scope.launch {
settingsRepository.setBiometricEnabled(false)
}
}
else -> Unit
}
}
}
}
)
@@ -172,7 +188,7 @@ fun SecuritySettingsScreen(
Spacer(modifier = Modifier.height(16.dp))
if (isPinEnabled) {
if (isPinEnabled || isBiometricEnabled) {
Row(
modifier = Modifier
.fillMaxWidth()