12

I am using BiometricPrompt in my application. It works well and shows the dialog when call the authenticate() method. But this dialog gets closing when I click outside the dialog. How to prevent it? How to make BiometricPrompt's dialog non-cancelable? Here is no method like biometricPrompt.setCancelable(false).

BArtWell
  • 4,176
  • 10
  • 63
  • 106
  • seems the feature is not available, i suggest to use custom dialog for same purpose. – MadLeo Jun 06 '19 at 10:27
  • Have you tried `setCanceledOnTouchOutside(false)`? – Mehul Solanki Jun 06 '19 at 10:33
  • 2
    @MadLeo From documentation: "In Android 9 and higher, the FingerprintManager API is deprecated. If your bundled and system apps use this API, update them to use BiometricPrompt instead". https://source.android.com/security/biometric – BArtWell Jun 06 '19 at 10:59
  • @MehulSolanki As I see, there is no method available with this name: https://developer.android.google.cn/reference/kotlin/androidx/biometric/BiometricPrompt – BArtWell Jun 06 '19 at 11:01
  • U can handle it in onAuthenticationError with error code BiometricPrompt.ERROR_USER_CANCELED – DAC84 Nov 27 '19 at 13:35

4 Answers4

3

BiometricPrompt does not allow that. So you won't be able to make the system-provided biometric prompt non-cancelable. But you can detect whenever user cancels the dialog.

So an option would be, to show again the biometric prompt after user cancel it (which I think would be a bad user experience) or use alternate user authentication:

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
        if (errorCode == BiometricConstants.ERROR_USER_CANCELED) {
            // User canceled the operation

            // you can either show the dialog again here

            // or use alternate authentication (e.g. a password) - recommended way
        }
    }
mgcaguioa
  • 1,443
  • 16
  • 19
  • can we provide an alternate authentication if the fingerprint fails ? Like an android device pin to retrieve the password from keystore? – user2234 Aug 14 '19 at 06:07
  • 1
    Yes you can. You can open password/pin/pattern screen and validate user device credentials. Check this SO answer: https://stackoverflow.com/a/52504424/3955965 – mgcaguioa Aug 15 '19 at 09:52
  • You should probably use setDeviceCredentialAllowed instead: https://developer.android.com/reference/androidx/biometric/BiometricPrompt.PromptInfo.Builder.html#setDeviceCredentialAllowed(boolean) – Kevin Sep 21 '19 at 19:12
0

check it out

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
    supportFragmentManager.fragments.forEach {
        if(it is DialogFragment) {
            it.dialog?.setCanceledOnTouchOutside(false)
        }
    }
}
黄朝阳
  • 9
  • 1
  • The biometric prompt is not necessarily a dialog depending on the device. For instance, on OnePlus devices, a fullscreen view may be displayed. – Louis Dec 22 '20 at 10:09
0

There are some devices that still have this issue. An work around will be to get root view and add an overlay view with clickable method set to false.

    ViewGroup  viewGroup =  ((ViewGroup) yourActivity.findViewById(android.R.id.content)).getChildAt(0);

    //create your view
    Display display = mActivity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    View view = new View(yourActivity);
    view.setId(R.id.overlay_view);
    view.setLayoutParams(new ViewGroup.LayoutParams(size.x, size.y));
    view.setBackgroundColor(ContextCompat.getColor(yourActivity, R.color.black));
    view.setOnClickListener(v -> {
        //do nothing prevent click under this overlay
    });

    //add your view on top of the screen
    viewGroup.addView(view);

    //call your biometric dialog
    ....

    //on callbacks even if it is error or success call remove view
    viewGroup.removeView(view);
Lucian Novac
  • 1,255
  • 12
  • 18
-4

You have to use the version 1.0.0-beta01 or later.

Now it is the default behavior:
Touches outside no longer cancel authentication. Back button cancel authentication still.

You can see the changelog:

Changed behavior to not allow BiometricPrompt to be cancelled by a touch event outside the prompt.

You can check also the rewiew report.
No new API.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841