0

I'm using FingerprintManager / BiometricPrompt (depending on Android version) to get access to KeyStore. Yet on phones that have single capacitive key for "Home" and scanner (like Huawei P10) calling fingerprint scanning functions mentioned triggers "Home" action!

Is there any fix for this? Temporarily blocking Home maybe?

More or less this code:

@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)
    }
}
ssuukk
  • 8,200
  • 7
  • 35
  • 47

0 Answers0