关于Android上BiometricPrompt的一些问题

问题描述 投票:0回答:1

我正在尝试找出(如果可能的话)如何修改biometricPrompt的正常行为,特别是当身份验证失败时,我想显示Gandalf。我当前正在使用自定义的alertDialog显示它,但它仍在后台,而biometricPrompt片段位于前景exactly like this上,并且它失去了所有的笨拙...最好的解决方案可能是在前景上同时显示alertDialog和biometricPrompt,仅在屏幕的上半部分显示图像,但是目前我不知道该怎么做,或者更好的是,我没有想出如何将布局链接在一起以管理尺寸/边距以及其他所有内容。

我当时想的另一件事是删除biometricPrompt,因此警报对话框将放在前台,但是我尝试过的任何解决方案都失败了。

欢迎任何类型的帮助/想法。

无论如何,这是代码:

class BiometricPromptManager(private val activity: FragmentActivity) {

private val cryptoManager = CryptoManager(activity)

fun authenticateAndDecrypt(failedAction: () -> Unit, successAction: (String) -> Unit) {

    //  display biometric prompt, if the user is authenticated, the decryption will start
    // if biometric related decryption gives positives results, the successAction will start services data decryption

    val executor = Executors.newSingleThreadExecutor()
    val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)

            cryptoManager.startDecrypt(failedAction,successAction)
        }

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)

            activity.runOnUiThread { failedAction() }
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()

            activity.runOnUiThread { failedAction() }
        }
    })

    val promptInfo = biometricPromptInfo()
    biometricPrompt.authenticate(promptInfo)
}

    private fun biometricPromptInfo(): BiometricPrompt.PromptInfo {
        return BiometricPrompt.PromptInfo.Builder()
            .setTitle("Fingerprint Authenticator")
            .setNegativeButtonText(activity.getString(android.R.string.cancel))
            .build()
    }
}

从活动中打开生物特征认证:

private fun openBiometricAuth(){

    if(sharedPreferences.getBoolean("fingerPrintEnabled",false)) {
        if (BiometricManager.from(this).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { // check for hardware/permission
            biometric.visibility = View.VISIBLE
            BiometricPromptManager(this).authenticateAndDecrypt(::failure, ::callDecryption)
        }
    }
}

无法识别用户时该怎么办:

private fun failure(){

    val view = layoutInflater.inflate(R.layout.gandalf, null)

    val builder = AlertDialog.Builder(this)

    builder.setView(view)

    builder.setPositiveButton("Dismiss") { dialog: DialogInterface, id: Int -> dialog.cancel() }

    val alertDialog = builder.create()

    alertDialog.show()
}
android kotlin android-alertdialog android-biometric-prompt
1个回答
0
投票

Biometric API本身通过以下方式处理身份验证失败的尝试:

  1. 对于每次失败的尝试,都会调用onAuthenticationFailed()回调。
  2. 用户获得了5次尝试,在第5次尝试失败之后,onAuthenticationError()回调收到错误代码ERROR_LOCKOUT,用户必须等待30秒才能再次尝试。

onAuthenticationFailed()中显示对话框可能太急了,可能会导致不良的用户体验。因此,获得ERROR_LOCKOUT之后可能是个好地方。 AndroidX生物特征库的工作方式是在发送错误时将其关闭。因此,此时显示自己的对话框应该没有什么问题。

在任何情况下(即,超出这些意见的范围,更通用的方法是调用cancelAuthentication()消除提示,然后继续以这种方式显示您自己的对话框。

还请遵循blogpost1blogpost2了解BiometricPrompt的推荐设计模式。

© www.soinside.com 2019 - 2024. All rights reserved.