生物识别解锁独立化:可无 PIN 单独开启,关闭验证按有无 PIN 分派,生物失效时锁屏提示
This commit is contained in:
@@ -41,11 +41,12 @@ data class AppLockSettings(
|
|||||||
val isBiometricEnabled: Boolean = false,
|
val isBiometricEnabled: Boolean = false,
|
||||||
val lockTrigger: AppLockTrigger = AppLockTrigger.DEFAULT
|
val lockTrigger: AppLockTrigger = AppLockTrigger.DEFAULT
|
||||||
) {
|
) {
|
||||||
|
/** App locks when PIN is active or biometric unlock is enabled on its own. */
|
||||||
val isAppLockEnabled: Boolean
|
val isAppLockEnabled: Boolean
|
||||||
get() = isPinEnabled && hasPinCode
|
get() = (isPinEnabled && hasPinCode) || isBiometricEnabled
|
||||||
|
|
||||||
val canUseBiometric: Boolean
|
val canUseBiometric: Boolean
|
||||||
get() = isAppLockEnabled && isBiometricEnabled
|
get() = isBiometricEnabled
|
||||||
}
|
}
|
||||||
|
|
||||||
class SettingsRepository(private val context: Context, private val cryptoManager: CryptoManager) {
|
class SettingsRepository(private val context: Context, private val cryptoManager: CryptoManager) {
|
||||||
@@ -86,11 +87,10 @@ class SettingsRepository(private val context: Context, private val cryptoManager
|
|||||||
storedLockTrigger
|
storedLockTrigger
|
||||||
) { pinEnabled, decryptedPinCode, biometricEnabled, lockTrigger ->
|
) { pinEnabled, decryptedPinCode, biometricEnabled, lockTrigger ->
|
||||||
val hasPinCode = decryptedPinCode?.isValidPin() == true
|
val hasPinCode = decryptedPinCode?.isValidPin() == true
|
||||||
val isAppLockEnabled = pinEnabled && hasPinCode
|
|
||||||
AppLockSettings(
|
AppLockSettings(
|
||||||
isPinEnabled = isAppLockEnabled,
|
isPinEnabled = pinEnabled && hasPinCode,
|
||||||
hasPinCode = hasPinCode,
|
hasPinCode = hasPinCode,
|
||||||
isBiometricEnabled = isAppLockEnabled && biometricEnabled,
|
isBiometricEnabled = biometricEnabled,
|
||||||
lockTrigger = lockTrigger
|
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) {
|
suspend fun enablePin(pin: String) {
|
||||||
require(pin.isValidPin()) { "PIN must be exactly 6 digits." }
|
require(pin.isValidPin()) { "PIN must be exactly 6 digits." }
|
||||||
dataStore.edit { preferences ->
|
dataStore.edit { preferences ->
|
||||||
@@ -134,7 +121,6 @@ class SettingsRepository(private val context: Context, private val cryptoManager
|
|||||||
suspend fun disablePin() {
|
suspend fun disablePin() {
|
||||||
dataStore.edit { preferences ->
|
dataStore.edit { preferences ->
|
||||||
preferences[PreferencesKeys.PIN_ENABLED] = false
|
preferences[PreferencesKeys.PIN_ENABLED] = false
|
||||||
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = false
|
|
||||||
preferences.remove(PreferencesKeys.PIN_CODE)
|
preferences.remove(PreferencesKeys.PIN_CODE)
|
||||||
preferences[PreferencesKeys.FAILED_ATTEMPTS] = 0
|
preferences[PreferencesKeys.FAILED_ATTEMPTS] = 0
|
||||||
preferences[PreferencesKeys.LOCKOUT_END_TIME] = 0L
|
preferences[PreferencesKeys.LOCKOUT_END_TIME] = 0L
|
||||||
@@ -147,9 +133,7 @@ class SettingsRepository(private val context: Context, private val cryptoManager
|
|||||||
|
|
||||||
suspend fun setBiometricEnabled(enabled: Boolean) {
|
suspend fun setBiometricEnabled(enabled: Boolean) {
|
||||||
dataStore.edit { preferences ->
|
dataStore.edit { preferences ->
|
||||||
val hasPinCode = preferences[PreferencesKeys.PIN_CODE] != null
|
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = enabled
|
||||||
val isPinEnabled = preferences[PreferencesKeys.PIN_ENABLED] ?: false
|
|
||||||
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = enabled && hasPinCode && isPinEnabled
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ fun LockScreen(
|
|||||||
val pinCode by settingsRepo.pinCode.collectAsStateWithLifecycle(initialValue = null)
|
val pinCode by settingsRepo.pinCode.collectAsStateWithLifecycle(initialValue = null)
|
||||||
|
|
||||||
val isBiometricEnabled = appLockSettings.canUseBiometric
|
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 failedAttempts by settingsRepo.failedAttempts.collectAsStateWithLifecycle(initialValue = 0)
|
||||||
val lockoutEndTime by settingsRepo.lockoutEndTime.collectAsStateWithLifecycle(initialValue = 0L)
|
val lockoutEndTime by settingsRepo.lockoutEndTime.collectAsStateWithLifecycle(initialValue = 0L)
|
||||||
@@ -66,15 +68,19 @@ fun LockScreen(
|
|||||||
|
|
||||||
LaunchedEffect(isBiometricEnabled) {
|
LaunchedEffect(isBiometricEnabled) {
|
||||||
if (isBiometricEnabled) {
|
if (isBiometricEnabled) {
|
||||||
biometricAuthenticator.authenticate(
|
if (biometricAuthenticator.getAvailability(BiometricAuthMode.AppConfirmation) != null) {
|
||||||
mode = BiometricAuthMode.AppConfirmation,
|
biometricUnavailable = true
|
||||||
titleRes = R.string.unlock_2fair,
|
} else {
|
||||||
subtitleRes = R.string.biometric_prompt_subtitle
|
biometricAuthenticator.authenticate(
|
||||||
) { result ->
|
mode = BiometricAuthMode.AppConfirmation,
|
||||||
when (result) {
|
titleRes = R.string.unlock_2fair,
|
||||||
BiometricAuthResult.Success -> onUnlock()
|
subtitleRes = R.string.biometric_prompt_subtitle
|
||||||
BiometricAuthResult.Failed -> errorMessage = biometricFailedMsg
|
) { result ->
|
||||||
else -> Unit
|
when (result) {
|
||||||
|
BiometricAuthResult.Success -> onUnlock()
|
||||||
|
BiometricAuthResult.Failed -> errorMessage = biometricFailedMsg
|
||||||
|
else -> Unit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,69 +125,84 @@ fun LockScreen(
|
|||||||
)
|
)
|
||||||
Spacer(modifier = Modifier.height(32.dp))
|
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(
|
||||||
text = stringResource(R.string.enter_pin),
|
text = if (hasPin) stringResource(R.string.enter_pin)
|
||||||
|
else stringResource(R.string.unlock_2fair),
|
||||||
style = MaterialTheme.typography.headlineSmall
|
style = MaterialTheme.typography.headlineSmall
|
||||||
)
|
)
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
|
|
||||||
PinIndicatorRow(
|
if (hasPin) {
|
||||||
filledCount = enteredPin.length,
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
isError = errorMessage != null || remainingLockOutTime > 0L,
|
|
||||||
isSuccess = isSuccess,
|
|
||||||
modifier = Modifier.fillMaxWidth(0.8f)
|
|
||||||
)
|
|
||||||
Spacer(modifier = Modifier.height(12.dp))
|
|
||||||
|
|
||||||
Text(
|
PinIndicatorRow(
|
||||||
text = when {
|
filledCount = enteredPin.length,
|
||||||
remainingLockOutTime > 0L -> stringResource(R.string.try_again_in, remainingLockOutTime)
|
isError = errorMessage != null || remainingLockOutTime > 0L,
|
||||||
errorMessage != null -> errorMessage!!
|
isSuccess = isSuccess,
|
||||||
enteredPin.isEmpty() -> pinHintMsg
|
modifier = Modifier.fillMaxWidth(0.8f)
|
||||||
else -> stringResource(R.string.pin_digits_remaining, PIN_LENGTH - enteredPin.length)
|
)
|
||||||
},
|
Spacer(modifier = Modifier.height(12.dp))
|
||||||
color = if (errorMessage != null || remainingLockOutTime > 0L) {
|
|
||||||
MaterialTheme.colorScheme.error
|
|
||||||
} else {
|
|
||||||
MaterialTheme.colorScheme.onSurfaceVariant
|
|
||||||
},
|
|
||||||
style = MaterialTheme.typography.bodyMedium
|
|
||||||
)
|
|
||||||
Spacer(modifier = Modifier.height(24.dp))
|
|
||||||
|
|
||||||
PinNumberPad(
|
Text(
|
||||||
resetLabel = stringResource(R.string.clear_pin_input),
|
text = when {
|
||||||
deleteLabel = stringResource(R.string.delete_digit),
|
remainingLockOutTime > 0L -> stringResource(R.string.try_again_in, remainingLockOutTime)
|
||||||
onDigit = { digit ->
|
errorMessage != null -> errorMessage!!
|
||||||
if (remainingLockOutTime > 0L || isSuccess || enteredPin.length >= PIN_LENGTH) {
|
enteredPin.isEmpty() -> pinHintMsg
|
||||||
return@PinNumberPad
|
else -> stringResource(R.string.pin_digits_remaining, PIN_LENGTH - enteredPin.length)
|
||||||
}
|
},
|
||||||
val updatedPin = enteredPin + digit
|
color = if (errorMessage != null || remainingLockOutTime > 0L) {
|
||||||
enteredPin = updatedPin
|
MaterialTheme.colorScheme.error
|
||||||
errorMessage = null
|
} else {
|
||||||
if (updatedPin.length == PIN_LENGTH) {
|
MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
submitPin(updatedPin)
|
},
|
||||||
}
|
style = MaterialTheme.typography.bodyMedium
|
||||||
},
|
)
|
||||||
onDelete = {
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
|
|
||||||
enteredPin = enteredPin.dropLast(1)
|
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
|
errorMessage = null
|
||||||
}
|
if (updatedPin.length == PIN_LENGTH) {
|
||||||
},
|
submitPin(updatedPin)
|
||||||
onReset = {
|
}
|
||||||
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
|
},
|
||||||
enteredPin = ""
|
onDelete = {
|
||||||
errorMessage = null
|
if (remainingLockOutTime == 0L && enteredPin.isNotEmpty()) {
|
||||||
}
|
enteredPin = enteredPin.dropLast(1)
|
||||||
},
|
errorMessage = null
|
||||||
modifier = Modifier.fillMaxWidth(0.9f),
|
}
|
||||||
isEnabled = remainingLockOutTime == 0L,
|
},
|
||||||
isDeleteEnabled = enteredPin.isNotEmpty(),
|
onReset = {
|
||||||
isResetEnabled = enteredPin.isNotEmpty()
|
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))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
FilledTonalButton(
|
FilledTonalButton(
|
||||||
onClick = {
|
onClick = {
|
||||||
|
|||||||
@@ -125,7 +125,6 @@ fun SecuritySettingsScreen(
|
|||||||
}
|
}
|
||||||
Switch(
|
Switch(
|
||||||
checked = isBiometricEnabled,
|
checked = isBiometricEnabled,
|
||||||
enabled = isPinEnabled,
|
|
||||||
onCheckedChange = { checked ->
|
onCheckedChange = { checked ->
|
||||||
if (checked) {
|
if (checked) {
|
||||||
biometricAuthenticator.authenticate(
|
biometricAuthenticator.authenticate(
|
||||||
@@ -163,8 +162,25 @@ fun SecuritySettingsScreen(
|
|||||||
BiometricAuthResult.Cancelled -> Unit
|
BiometricAuthResult.Cancelled -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if (isPinEnabled) {
|
||||||
|
// PIN available: confirm downgrade with PIN
|
||||||
verificationAction = SecurityVerificationAction.DisableBiometric
|
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))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
if (isPinEnabled) {
|
if (isPinEnabled || isBiometricEnabled) {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
|
|||||||
@@ -44,9 +44,6 @@
|
|||||||
<string name="group_rename">重命名分组</string>
|
<string name="group_rename">重命名分组</string>
|
||||||
<string name="group_delete_confirm">删除该分组?组内令牌将移入未分组。</string>
|
<string name="group_delete_confirm">删除该分组?组内令牌将移入未分组。</string>
|
||||||
<string name="rename">重命名</string>
|
<string name="rename">重命名</string>
|
||||||
<<<<<<< HEAD
|
|
||||||
<string name="drag_handle_desc">拖动调整分组顺序</string>
|
|
||||||
=======
|
|
||||||
<string name="drag_handle_desc">拖动排序</string>
|
<string name="drag_handle_desc">拖动排序</string>
|
||||||
<string name="biometric_unavailable_no_pin">生物识别不可用且未设置 PIN,请在系统设置中恢复指纹后再解锁应用</string>
|
<string name="biometric_unavailable_no_pin">生物识别不可用且未设置 PIN,请在系统设置中恢复指纹后再解锁应用</string>
|
||||||
<string name="account_name_required">账户名称(必填)</string>
|
<string name="account_name_required">账户名称(必填)</string>
|
||||||
|
|||||||
@@ -44,9 +44,6 @@
|
|||||||
<string name="group_rename">重新命名分組</string>
|
<string name="group_rename">重新命名分組</string>
|
||||||
<string name="group_delete_confirm">刪除此分組?組內權杖將移入未分組。</string>
|
<string name="group_delete_confirm">刪除此分組?組內權杖將移入未分組。</string>
|
||||||
<string name="rename">重新命名</string>
|
<string name="rename">重新命名</string>
|
||||||
<<<<<<< HEAD
|
|
||||||
<string name="drag_handle_desc">拖動調整分組順序</string>
|
|
||||||
=======
|
|
||||||
<string name="drag_handle_desc">拖動排序</string>
|
<string name="drag_handle_desc">拖動排序</string>
|
||||||
<string name="biometric_unavailable_no_pin">生物辨識不可用且未設定 PIN,請在系統設定中恢復指紋後再解鎖應用程式</string>
|
<string name="biometric_unavailable_no_pin">生物辨識不可用且未設定 PIN,請在系統設定中恢復指紋後再解鎖應用程式</string>
|
||||||
<string name="account_name_required">帳戶名稱(必填)</string>
|
<string name="account_name_required">帳戶名稱(必填)</string>
|
||||||
|
|||||||
@@ -44,9 +44,6 @@
|
|||||||
<string name="group_rename">Rename group</string>
|
<string name="group_rename">Rename group</string>
|
||||||
<string name="group_delete_confirm">Delete this group? Its tokens will move to Uncategorized.</string>
|
<string name="group_delete_confirm">Delete this group? Its tokens will move to Uncategorized.</string>
|
||||||
<string name="rename">Rename</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="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="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>
|
<string name="account_name_required">Account Name (Required)</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user