android.hardware.biometrics.BiometricPrompt Java Examples

The following examples show how to use android.hardware.biometrics.BiometricPrompt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthenticationClient.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override // binder call
public void onDialogDismissed(int reason) {
    if (mBundle != null && mDialogReceiverFromClient != null) {
        try {
            mDialogReceiverFromClient.onDialogDismissed(reason);
            if (reason == BiometricPrompt.DISMISSED_REASON_USER_CANCEL) {
                onError(FingerprintManager.FINGERPRINT_ERROR_USER_CANCELED,
                        0 /* vendorCode */);
            }
            mDialogDismissed = true;
        } catch (RemoteException e) {
            Slog.e(TAG, "Unable to notify dialog dismissed", e);
        }
        stop(true /* initiatedByClient */);
    }
}
 
Example #2
Source File: FingerprintManager.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override // binder call
public void onError(long deviceId, int error, int vendorCode) {
    if (mExecutor != null) {
        // BiometricPrompt case
        if (error == FingerprintManager.FINGERPRINT_ERROR_USER_CANCELED
                || error == FingerprintManager.FINGERPRINT_ERROR_CANCELED) {
            // User tapped somewhere to cancel, or authentication was cancelled by the app
            // or got kicked out. The prompt is already gone, so send the error immediately.
            mExecutor.execute(() -> {
                sendErrorResult(deviceId, error, vendorCode);
            });
        } else {
            // User got an error that needs to be displayed on the dialog, post a delayed
            // runnable on the FingerprintManager handler that sends the error message after
            // FingerprintDialog.HIDE_DIALOG_DELAY to send the error to the application.
            mHandler.postDelayed(() -> {
                mExecutor.execute(() -> {
                    sendErrorResult(deviceId, error, vendorCode);
                });
            }, BiometricPrompt.HIDE_DIALOG_DELAY);
        }
    } else {
        mHandler.obtainMessage(MSG_ERROR, error, vendorCode, deviceId).sendToTarget();
    }
}
 
Example #3
Source File: FingerprintDialogBuilder.java    From FingerprintDialogCompat with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.P)
private void showFingerprintDialog(@NonNull final AuthenticationCallback authenticationCallback) {
    new BiometricPrompt.Builder(mContext)
            .setTitle(mTitle)
            .setSubtitle(mSubTitle)
            .setDescription(mDescription)
            .setNegativeButton(mButtonTitle,
                    mContext.getMainExecutor(),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(final DialogInterface dialogInterface, final int i) {
                            authenticationCallback.authenticationCanceledByUser();
                        }
                    })
            .build()
            .authenticate(new CancellationSignal(),
                    mContext.getMainExecutor(),
                    new AuthenticationCallbackV28(authenticationCallback));
}
 
Example #4
Source File: BiometricManager.java    From smart-farmer-android with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.P)
private void displayBiometricPrompt(final BiometricCallback biometricCallback) {

    if (initCipher()) {
        bioCryptoObject = new BiometricPrompt.CryptoObject(cipher);

        new BiometricPrompt.Builder(context)
                .setTitle(title)
                .setSubtitle(subtitle)
                .setDescription(description)
                .setNegativeButton(negativeButtonText, context.getMainExecutor(), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        biometricCallback.onAuthenticationCancelled();
                    }
                })
                .build()
                .authenticate(bioCryptoObject, mCancellationSignal, context.getMainExecutor(),
                        new BiometricCallbackV28(biometricCallback));
    }
}
 
Example #5
Source File: LoginActivity.java    From secure-quick-reliable-login with MIT License 5 votes vote down vote up
private void doLoginBiometric() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return;

    BioAuthenticationCallback biometricCallback =
            new BioAuthenticationCallback(LoginActivity.this.getApplicationContext(), () ->
                    handler.post(() -> doLogin(false, useCps, false))
            );

    BiometricPrompt bioPrompt = new BiometricPrompt.Builder(this)
            .setTitle(getString(R.string.login_title))
            .setSubtitle(mSqrlMatcher.group(1))
            .setDescription(getString(R.string.login_verify_domain_text))
            .setNegativeButton(
                    getString(R.string.button_cps_cancel),
                    this.getMainExecutor(),
                    (dialogInterface, i) -> {}
            ).build();

    CancellationSignal cancelSign = new CancellationSignal();
    cancelSign.setOnCancelListener(() -> {});

    try {
        KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
        keyStore.load(null);
        KeyStore.Entry entry = keyStore.getEntry("quickPass", null);
        Cipher decCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); //or try with "RSA"
        decCipher.init(Cipher.DECRYPT_MODE, ((KeyStore.PrivateKeyEntry) entry).getPrivateKey());
        bioPrompt.authenticate(new BiometricPrompt.CryptoObject(decCipher), cancelSign, this.getMainExecutor(), biometricCallback);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: BioAuthenticationCallback.java    From secure-quick-reliable-login with MIT License 5 votes vote down vote up
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
    super.onAuthenticationSucceeded(result);
    try {
        BiometricPrompt.CryptoObject co = result.getCryptoObject();
        SQRLStorage.getInstance(context).decryptIdentityKeyBiometric(co.getCipher());

        new Thread(doneCallback).start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #7
Source File: AuthenticationCallbackV28.java    From FingerprintDialogCompat with Apache License 2.0 5 votes vote down vote up
/**
 * @see BiometricPrompt.AuthenticationCallback#onAuthenticationError(int, CharSequence)
 */
@Override
public void onAuthenticationError(final int errorCode, final CharSequence errString) {
    super.onAuthenticationError(errorCode, errString);

    switch (errorCode) {

        //User canceled the scanning process by pressing the negative button.
        case BiometricPrompt.BIOMETRIC_ERROR_USER_CANCELED:
            mCallback.authenticationCanceledByUser();
            break;

        // Device doesn't have the supported fingerprint hardware.
        case  BiometricPrompt.BIOMETRIC_ERROR_HW_NOT_PRESENT:
        case BiometricPrompt.BIOMETRIC_ERROR_HW_UNAVAILABLE:
            mCallback.fingerprintAuthenticationNotSupported();
            break;

        //User did not register any fingerprints.
        case BiometricPrompt.BIOMETRIC_ERROR_NO_BIOMETRICS:
            mCallback.hasNoFingerprintEnrolled();
            break;

            //Any other unrecoverable error
        default:
            mCallback.onAuthenticationError(errorCode, errString);
    }
}
 
Example #8
Source File: BiometricPromptCompat.java    From BiometricPromptCompat with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@NonNull
public BiometricPromptCompat build() {
    if (title == null) {
        throw new IllegalArgumentException("You should set a title for BiometricPrompt.");
    }
    if (isApiPSupported()) {
        BiometricPrompt.Builder builder = new BiometricPrompt.Builder(context);
        builder.setTitle(title);
        if (subtitle != null) {
            builder.setSubtitle(subtitle);
        }
        if (description != null) {
            builder.setDescription(description);
        }
        if (negativeButtonText != null) {
            builder.setNegativeButton(
                    negativeButtonText, context.getMainExecutor(), negativeButtonListener);
        }
        return new BiometricPromptCompat(
                new BiometricPromptApi28Impl(context, builder.build())
        );
    } else {
        return new BiometricPromptCompat(
                new BiometricPromptApi23Impl(
                        context, title, subtitle, description,
                        negativeButtonText, negativeButtonListener
                )
        );
    }
}
 
Example #9
Source File: BiometricPromptApi28Impl.java    From BiometricPromptCompat with Apache License 2.0 5 votes vote down vote up
private static BiometricPrompt.CryptoObject toCryptoObjectApi28(
        @Nullable BiometricPromptCompat.ICryptoObject ico
) {
    if (ico == null) {
       return null;
    } else if (ico.getCipher() != null) {
        return new BiometricPrompt.CryptoObject(ico.getCipher());
    } else if (ico.getMac() != null) {
        return new BiometricPrompt.CryptoObject(ico.getMac());
    } else if (ico.getSignature() != null) {
        return new BiometricPrompt.CryptoObject(ico.getSignature());
    } else {
        throw new IllegalArgumentException("ICryptoObject doesn\'t include any data.");
    }
}
 
Example #10
Source File: SmartLockHelper.java    From samples-android with Apache License 2.0 5 votes vote down vote up
private void showBiometricPromptCompat(FragmentActivity activity, FingerprintDialogCallbacks callback) {
    androidx.biometric.BiometricPrompt.PromptInfo promptInfo = new androidx.biometric.BiometricPrompt.PromptInfo.Builder()
            .setTitle(activity.getString(R.string.fingerprint_alert_title))
            .setNegativeButtonText(activity.getString(R.string.cancel))
            .build();

    androidx.biometric.BiometricPrompt biometricPrompt = new androidx.biometric.BiometricPrompt(activity, Executors.newSingleThreadExecutor(), new androidx.biometric.BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            if (errorCode == androidx.biometric.BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
                callback.onFingerprintCancel();
            } else {
                callback.onFingerprintError(errString.toString());
            }
        }

        @Override
        public void onAuthenticationSucceeded(@NonNull androidx.biometric.BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            callback.onFingerprintSuccess(null);
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            activity.runOnUiThread(() -> Toast.makeText(activity, "Fingerprint not recognized. Try again", Toast.LENGTH_SHORT).show());

        }
    });
    biometricPrompt.authenticate(promptInfo);
}
 
Example #11
Source File: DebugInterface.java    From rebootmenu with GNU General Public License v3.0 5 votes vote down vote up
private void biometricPromptTest() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
        BiometricPrompt bp = new BiometricPrompt.Builder(this)
                .setTitle("Authenticate Test")
                .setDescription("setDescription")
                .setNegativeButton(getString(android.R.string.cancel), getMainExecutor(), (dialogInterface, i) -> print("canceled"))
                .setSubtitle("setSubtitle")
                .build();
        CancellationSignal signal = new CancellationSignal();
        signal.setOnCancelListener(() -> print("canceled from CancellationSignal"));
        bp.authenticate(signal, getMainExecutor(), new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, CharSequence errString) {
                print("onAuthenticationError:" + varArgsToString(errorCode, errString));
            }

            @Override
            public void onAuthenticationFailed() {
                print("onAuthenticationFailed");
                finish();
            }

            @Override
            public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
                print("onAuthenticationSucceeded");
            }

            @Override
            public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
                print("onAuthenticationHelp:" + varArgsToString(helpCode, helpString));
            }
        });
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(signal::cancel);
            }
        }, 5000);
    }
}
 
Example #12
Source File: AuthenticationCallbackV28.java    From FingerprintDialogCompat with Apache License 2.0 4 votes vote down vote up
/**
 * @see BiometricPrompt.AuthenticationCallback#onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult)
 */
@Override
public void onAuthenticationSucceeded(final BiometricPrompt.AuthenticationResult result) {
    super.onAuthenticationSucceeded(result);
    mCallback.onAuthenticationSucceeded();
}
 
Example #13
Source File: BiometricPromptApi28Impl.java    From BiometricPromptCompat with Apache License 2.0 4 votes vote down vote up
BiometricPromptApi28Impl(@NonNull Context context, @NonNull BiometricPrompt prompt) {
    this.context = context;
    this.biometricPrompt = prompt;
}
 
Example #14
Source File: BiometricPromptApi28Impl.java    From BiometricPromptCompat with Apache License 2.0 4 votes vote down vote up
CryptoObjectApi28Impl(BiometricPrompt.CryptoObject cryptoObject) {
    this.cryptoObject = cryptoObject;
}
 
Example #15
Source File: SmartLockHelper.java    From samples-android with Apache License 2.0 4 votes vote down vote up
@TargetApi(P)
@Deprecated
private void showBiometricPrompt(FragmentActivity activity, FingerprintDialogCallbacks callback, Cipher cipher) {
    CancellationSignal mCancellationSignal = new CancellationSignal();
    BiometricPrompt biometricPrompt = new BiometricPrompt.Builder(activity)
            .setTitle(activity.getString(R.string.fingerprint_alert_title))
            .setNegativeButton(activity.getString(R.string.cancel), activity.getMainExecutor(), (dialogInterface, i) -> {
                callback.onFingerprintCancel();
            })
            .build();

    BiometricPrompt.AuthenticationCallback authenticationCallback = new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            callback.onFingerprintCancel();
        }

        @Override
        public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
            super.onAuthenticationHelp(helpCode, helpString);
            callback.onFingerprintCancel();
        }

        @Override
        public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);

            if (result.getCryptoObject() != null) {
                callback.onFingerprintSuccess(result.getCryptoObject().getCipher());
            } else {
                callback.onFingerprintSuccess(null);
            }
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            callback.onFingerprintCancel();
        }
    };

    biometricPrompt.authenticate(mCancellationSignal, activity.getMainExecutor(), authenticationCallback);
}
 
Example #16
Source File: BiometricCallbackV28.java    From smart-farmer-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
    super.onAuthenticationSucceeded(result);
    biometricCallback.onAuthenticationSuccessful(result.getCryptoObject().getCipher());
}