我正在使用FingerprintManager / BiometricPrompt(取决于Android版本)来访问KeyStore。然而,在具有用于“家庭”和扫描仪的单个电容键的电话(如华为P10)中,提到的指纹扫描功能会触发“家庭”操作!
对此有任何解决方法吗?可能是暂时封锁首页?
此代码或多或少:
@RequiresApi(Build.VERSION_CODES.M)
private fun fingerptintDoOnEncrypt(context: Context?, onSuccess: () -> Unit, onDismiss: () -> Unit) {
try {
"test".toByteArray().encryptWithAes(bioCipher, bioKey, "test")
onSuccess()
} catch (ex: UserNotAuthenticatedException) {
val mFingerprintManager = context.getSystemService(Context.FINGERPRINT_SERVICE) as FingerprintManager
val cancellationSignal = CancellationSignal()
val callback = object : FingerprintManager.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult) {
currentDialog?.dismiss()
lastBiometrySuccessful = true
"test".toByteArray().encryptWithAes(bioCipher, bioKey, "test")
onSuccess()
}
override fun onAuthenticationFailed() {
Toast.makeText(context, s(R.string.biometry_failed), Toast.LENGTH_SHORT).show()
super.onAuthenticationFailed()
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
currentDialog?.dismiss()
cancellationSignal.cancel()
lastBiometrySuccessful = false
if (errorCode == FingerprintManager.FINGERPRINT_ERROR_CANCELED) {
Toast.makeText(context, s(R.string.biometry_failed), Toast.LENGTH_SHORT).show()
}
}
}
context?.let {
showAlert(context, cancellationSignal, onDismiss)
// FingerprintManager.CryptoObject(mCipher)
mFingerprintManager.authenticate(null, cancellationSignal, 0, callback, null)
}
}
}
@RequiresApi(Build.VERSION_CODES.P)
private fun biometryDoOnEncrypt(context: Context?, onSuccess: () -> Unit, onDismiss: () -> Unit) {
try {
// TODO - potencjalnie może powodować problemy, jeśli hasło wygaśnie krótko po tym
"test".toByteArray().encryptWithAes(bioCipher, bioKey, "test")
onSuccess()
} catch (ex: UserNotAuthenticatedException) {
if (context == null)
return
val biometricManager = BiometricPrompt.Builder(context)
.setTitle(context.getString(R.string.biometry_dialog_title))
.setDescription(context.getString(R.string.biometry_dialog_desc))
.setNegativeButton(context.getString(android.R.string.cancel), context.mainExecutor, DialogInterface.OnClickListener { dialogInterface, i -> onDismiss() })
.build()
val cancellationSignal = CancellationSignal()
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
lastBiometrySuccessful = true
"test".toByteArray().encryptWithAes(bioCipher, bioKey, "test")
onSuccess()
}
override fun onAuthenticationFailed() {
lastBiometrySuccessful = false
Toast.makeText(context, s(R.string.biometry_failed), Toast.LENGTH_SHORT).show()
super.onAuthenticationFailed()
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
// nierozwiązywalny błęd, nie będzie więcej callbacków
cancellationSignal.cancel()
lastBiometrySuccessful = false
if (errorCode == BiometricPrompt.BIOMETRIC_ERROR_CANCELED) {
Toast.makeText(context, s(R.string.biometry_failed), Toast.LENGTH_SHORT).show()
}
}
}
biometricManager.authenticate(cancellationSignal, context.mainExecutor, callback)
}
}