diff --git a/app/src/main/java/cn/airnan/a2fair/data/repository/SettingsRepository.kt b/app/src/main/java/cn/airnan/a2fair/data/repository/SettingsRepository.kt index 36a4c85..890ea25 100644 --- a/app/src/main/java/cn/airnan/a2fair/data/repository/SettingsRepository.kt +++ b/app/src/main/java/cn/airnan/a2fair/data/repository/SettingsRepository.kt @@ -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 } } diff --git a/app/src/main/java/cn/airnan/a2fair/ui/settings/security/LockScreen.kt b/app/src/main/java/cn/airnan/a2fair/ui/settings/security/LockScreen.kt index 56f6e91..8cbd635 100644 --- a/app/src/main/java/cn/airnan/a2fair/ui/settings/security/LockScreen.kt +++ b/app/src/main/java/cn/airnan/a2fair/ui/settings/security/LockScreen.kt @@ -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 = { diff --git a/app/src/main/java/cn/airnan/a2fair/ui/settings/security/SecuritySettingsScreen.kt b/app/src/main/java/cn/airnan/a2fair/ui/settings/security/SecuritySettingsScreen.kt index 3894519..f1dea97 100644 --- a/app/src/main/java/cn/airnan/a2fair/ui/settings/security/SecuritySettingsScreen.kt +++ b/app/src/main/java/cn/airnan/a2fair/ui/settings/security/SecuritySettingsScreen.kt @@ -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() diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index cc49c11..f70f811 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -44,9 +44,6 @@ 重命名分组 删除该分组?组内令牌将移入未分组。 重命名 -<<<<<<< HEAD - 拖动调整分组顺序 -======= 拖动排序 生物识别不可用且未设置 PIN,请在系统设置中恢复指纹后再解锁应用 账户名称(必填) diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 0ab0afb..2fd31d4 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -44,9 +44,6 @@ 重新命名分組 刪除此分組?組內權杖將移入未分組。 重新命名 -<<<<<<< HEAD - 拖動調整分組順序 -======= 拖動排序 生物辨識不可用且未設定 PIN,請在系統設定中恢復指紋後再解鎖應用程式 帳戶名稱(必填) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ae6a412..6f1fdc2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -44,9 +44,6 @@ Rename group Delete this group? Its tokens will move to Uncategorized. Rename -<<<<<<< HEAD - Drag to reorder groups -======= Drag to reorder Biometric unlock is unavailable and no PIN is set. Restore your fingerprint in system settings to unlock the app. Account Name (Required)