生物识别解锁独立化:可无 PIN 单独开启,关闭验证按有无 PIN 分派,生物失效时锁屏提示
This commit is contained in:
@@ -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,6 +68,9 @@ fun LockScreen(
|
||||
|
||||
LaunchedEffect(isBiometricEnabled) {
|
||||
if (isBiometricEnabled) {
|
||||
if (biometricAuthenticator.getAvailability(BiometricAuthMode.AppConfirmation) != null) {
|
||||
biometricUnavailable = true
|
||||
} else {
|
||||
biometricAuthenticator.authenticate(
|
||||
mode = BiometricAuthMode.AppConfirmation,
|
||||
titleRes = R.string.unlock_2fair,
|
||||
@@ -79,6 +84,7 @@ fun LockScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun submitPin(candidate: String) {
|
||||
if (pinCode != null && candidate == pinCode) {
|
||||
@@ -119,10 +125,24 @@ fun LockScreen(
|
||||
)
|
||||
Spacer(modifier = Modifier.height(32.dp))
|
||||
|
||||
if (isBiometricEnabled && biometricUnavailable) {
|
||||
Text(
|
||||
text = stringResource(R.string.enter_pin),
|
||||
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 = if (hasPin) stringResource(R.string.enter_pin)
|
||||
else stringResource(R.string.unlock_2fair),
|
||||
style = MaterialTheme.typography.headlineSmall
|
||||
)
|
||||
|
||||
if (hasPin) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
PinIndicatorRow(
|
||||
@@ -180,8 +200,9 @@ fun LockScreen(
|
||||
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()
|
||||
|
||||
@@ -44,9 +44,6 @@
|
||||
<string name="group_rename">重命名分组</string>
|
||||
<string name="group_delete_confirm">删除该分组?组内令牌将移入未分组。</string>
|
||||
<string name="rename">重命名</string>
|
||||
<<<<<<< HEAD
|
||||
<string name="drag_handle_desc">拖动调整分组顺序</string>
|
||||
=======
|
||||
<string name="drag_handle_desc">拖动排序</string>
|
||||
<string name="biometric_unavailable_no_pin">生物识别不可用且未设置 PIN,请在系统设置中恢复指纹后再解锁应用</string>
|
||||
<string name="account_name_required">账户名称(必填)</string>
|
||||
|
||||
@@ -44,9 +44,6 @@
|
||||
<string name="group_rename">重新命名分組</string>
|
||||
<string name="group_delete_confirm">刪除此分組?組內權杖將移入未分組。</string>
|
||||
<string name="rename">重新命名</string>
|
||||
<<<<<<< HEAD
|
||||
<string name="drag_handle_desc">拖動調整分組順序</string>
|
||||
=======
|
||||
<string name="drag_handle_desc">拖動排序</string>
|
||||
<string name="biometric_unavailable_no_pin">生物辨識不可用且未設定 PIN,請在系統設定中恢復指紋後再解鎖應用程式</string>
|
||||
<string name="account_name_required">帳戶名稱(必填)</string>
|
||||
|
||||
@@ -44,9 +44,6 @@
|
||||
<string name="group_rename">Rename group</string>
|
||||
<string name="group_delete_confirm">Delete this group? Its tokens will move to Uncategorized.</string>
|
||||
<string name="rename">Rename</string>
|
||||
<<<<<<< HEAD
|
||||
<string name="drag_handle_desc">Drag to reorder groups</string>
|
||||
=======
|
||||
<string name="drag_handle_desc">Drag to reorder</string>
|
||||
<string name="biometric_unavailable_no_pin">Biometric unlock is unavailable and no PIN is set. Restore your fingerprint in system settings to unlock the app.</string>
|
||||
<string name="account_name_required">Account Name (Required)</string>
|
||||
|
||||
Reference in New Issue
Block a user