· 5 years ago · May 03, 2020, 09:12 AM
1 // ---------------------------------------------------------------------------------------------
2 private void showBiometricPrompt() throws Exception {
3 BiometricPrompt.AuthenticationCallback authenticationCallback = getAuthenticationCallback();
4 // ATTENZIONE viene eseguita sul UIThread
5 biometricPrompt = new BiometricPrompt(this, bioMainExecutor, authenticationCallback);
6
7 // Set prompt info
8 // If device credential is supported, promptInfo need setDeviceCredentialAllowed set to true
9 if (password) {
10 promptInfo = new BiometricPrompt.PromptInfo.Builder()
11 .setDescription(getString(R.string.prompt_desc_biometric))
12 .setTitle(getString(R.string.prompt_title))
13 .setSubtitle("\n")
14 // Cannot call setNegativeButtonText() and
15 // setDeviceCredentialAllowed() at the same time.
16 .setDeviceCredentialAllowed(true)
17 .build();
18
19 Log.i(TAG, "Show biometric prompt");
20 biometricPrompt.authenticate(promptInfo);
21 }
22 // If device credential not supported, use setNegativeButtonText
23 else {
24 promptInfo = new BiometricPrompt.PromptInfo.Builder()
25 .setDescription(getString(R.string.prompt_desc_biometric))
26 .setTitle(getString(R.string.prompt_title))
27 .setSubtitle("\n")
28 // The negative button will return the errCode 5
29 // You can handle the negative button
30 .setNegativeButtonText(getString(R.string.prompt_neg_btn))
31 .build();
32
33 // Get cipher for cryptography
34 // Device credential not supported with crypto
35 cryptoManager.genCrypt();
36 cipher = cryptoManager.getCipher();
37 SecretKey secretKey = cryptoManager.getSecretKey();
38 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
39
40 Log.i(TAG, "Show biometric prompt");
41 biometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
42 }
43 }