Java Code Examples for android.hardware.fingerprint.FingerprintManager#AuthenticationCallback

The following examples show how to use android.hardware.fingerprint.FingerprintManager#AuthenticationCallback . 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: NativeFingerprint.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
private void authenticate(final CancellationSignal cancellationSignal,
                  final AuthenticationListener listener,
                  final Fingerprint.KeepSensorActive keepSensorActive,
                  final int retryCount) {

    // Get the FingerPrint Manager, authentication callback, and cancellation signal object (for authentication)
    final FingerprintManager fingerprintManager = getFingerprintManager();
    final FingerprintManager.AuthenticationCallback callback = new AuthenticationCallback(retryCount, keepSensorActive, cancellationSignal, listener);


    if(fingerprintManager == null){
        listener.onFailure(AuthenticationFailureReason.UNKNOWN, true, getString(R.string.hardware_unavailable), TAG, HARDWARE_UNAVAILABLE);
        return;
    }

    try{
        fingerprintManager.authenticate(null, cancellationSignal, 0, callback, null);
    } catch (NullPointerException npe){
        listener.onFailure(AuthenticationFailureReason.UNKNOWN, true, getString(R.string.authentication_failed), TAG, FINGERPRINT_MANAGER_ERROR);
    }
}
 
Example 2
Source File: FingerprintManagerCompatApi23.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private static FingerprintManager.AuthenticationCallback wrapCallback(
        final AuthenticationCallback callback) {
    return new FingerprintManager.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            callback.onAuthenticationError(errMsgId, errString);
        }

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

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
                    unwrapCryptoObject(result.getCryptoObject())));
        }

        @Override
        public void onAuthenticationFailed() {
            callback.onAuthenticationFailed();
        }
    };
}
 
Example 3
Source File: FingerprintManagerCompatApi23.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private static FingerprintManager.AuthenticationCallback wrapCallback(
        final AuthenticationCallback callback) {
    return new FingerprintManager.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            callback.onAuthenticationError(errMsgId, errString);
        }

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

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
                    unwrapCryptoObject(result.getCryptoObject())));
        }

        @Override
        public void onAuthenticationFailed() {
            callback.onAuthenticationFailed();
        }
    };
}
 
Example 4
Source File: FingerprintManagerCompatApi23.java    From FingerprintIdentify with MIT License 6 votes vote down vote up
private static FingerprintManager.AuthenticationCallback wrapCallback(final AuthenticationCallback callback) {
    return new FingerprintManager.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            callback.onAuthenticationError(errMsgId, errString);
        }

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

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            callback.onAuthenticationSucceeded(new AuthenticationResultInternal(unwrapCryptoObject(result.getCryptoObject())));
        }

        @Override
        public void onAuthenticationFailed() {
            callback.onAuthenticationFailed();
        }
    };
}
 
Example 5
Source File: FingerprintAuthenticationTest.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationSuccessful() throws Exception {
    when(fingerprintApiWrapper.isUnavailable()).thenReturn(false);
    when(fingerprintApiWrapper.getFingerprintManager()).thenReturn(fingerprintManager);

    AuthenticationResult result = mock(AuthenticationResult.class);
    TestObserver<FingerprintAuthenticationResult> testObserver = observable.test();

    ArgumentCaptor<FingerprintManager.AuthenticationCallback> callbackCaptor = ArgumentCaptor.forClass(FingerprintManager.AuthenticationCallback.class);
    verify(fingerprintManager).authenticate(any(CryptoObject.class), any(CancellationSignal.class), anyInt(), callbackCaptor.capture(), any(Handler.class));
    callbackCaptor.getValue().onAuthenticationSucceeded(result);

    testObserver.awaitTerminalEvent();
    testObserver.assertNoErrors();
    testObserver.assertComplete();
    testObserver.assertValueCount(1);

    FingerprintAuthenticationResult fingerprintAuthenticationResult = testObserver.values().get(0);
    assertTrue("Authentication should be successful", fingerprintAuthenticationResult.isSuccess());
    assertTrue("Result should be equal AUTHENTICATED", fingerprintAuthenticationResult.getResult().equals(FingerprintResult.AUTHENTICATED));
    assertTrue("Should contain no message", fingerprintAuthenticationResult.getMessage() == null);
}
 
Example 6
Source File: FingerprintAuthenticationTest.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationError() throws Exception {
    when(fingerprintApiWrapper.isUnavailable()).thenReturn(false);
    when(fingerprintApiWrapper.getFingerprintManager()).thenReturn(fingerprintManager);

    TestObserver<FingerprintAuthenticationResult> testObserver = observable.test();

    ArgumentCaptor<FingerprintManager.AuthenticationCallback> callbackCaptor = ArgumentCaptor.forClass(FingerprintManager.AuthenticationCallback.class);
    verify(fingerprintManager).authenticate(any(CryptoObject.class), any(CancellationSignal.class), anyInt(), callbackCaptor.capture(), any(Handler.class));
    callbackCaptor.getValue().onAuthenticationError(0, ERROR_MESSAGE);

    testObserver.awaitTerminalEvent();
    testObserver.assertError(FingerprintAuthenticationException.class);
    testObserver.assertValueCount(0);

    assertTrue("Should contain 1 error", testObserver.errorCount() == 1);

    Throwable throwable = testObserver.errors().get(0);
    assertTrue("Message should equal ERROR_MESSAGE", throwable.getMessage().equals(ERROR_MESSAGE));
}
 
Example 7
Source File: FingerprintAuthenticationTest.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationFailed() throws Exception {
    when(fingerprintApiWrapper.isUnavailable()).thenReturn(false);
    when(fingerprintApiWrapper.getFingerprintManager()).thenReturn(fingerprintManager);

    TestObserver<FingerprintAuthenticationResult> testObserver = observable.test();

    ArgumentCaptor<FingerprintManager.AuthenticationCallback> callbackCaptor = ArgumentCaptor.forClass(FingerprintManager.AuthenticationCallback.class);
    verify(fingerprintManager).authenticate(any(CryptoObject.class), any(CancellationSignal.class), anyInt(), callbackCaptor.capture(), any(Handler.class));
    callbackCaptor.getValue().onAuthenticationFailed();

    testObserver.assertNotTerminated();
    testObserver.assertNoErrors();
    testObserver.assertNotComplete();
    testObserver.assertValueCount(1);

    FingerprintAuthenticationResult fingerprintAuthenticationResult = testObserver.values().get(0);
    assertTrue("Authentication should not be successful", !fingerprintAuthenticationResult.isSuccess());
    assertTrue("Result should be equal FAILED", fingerprintAuthenticationResult.getResult().equals(FingerprintResult.FAILED));
    assertTrue("Should contain no message", fingerprintAuthenticationResult.getMessage() == null);
}
 
Example 8
Source File: FingerprintAuthenticationTest.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationHelp() throws Exception {
    when(fingerprintApiWrapper.isUnavailable()).thenReturn(false);
    when(fingerprintApiWrapper.getFingerprintManager()).thenReturn(fingerprintManager);

    TestObserver<FingerprintAuthenticationResult> testObserver = observable.test();

    ArgumentCaptor<FingerprintManager.AuthenticationCallback> callbackCaptor = ArgumentCaptor.forClass(FingerprintManager.AuthenticationCallback.class);
    verify(fingerprintManager).authenticate(any(CryptoObject.class), any(CancellationSignal.class), anyInt(), callbackCaptor.capture(), any(Handler.class));
    callbackCaptor.getValue().onAuthenticationHelp(0, MESSAGE_HELP);

    testObserver.assertNotTerminated();
    testObserver.assertNoErrors();
    testObserver.assertNotComplete();
    testObserver.assertValueCount(1);

    FingerprintAuthenticationResult fingerprintAuthenticationResult = testObserver.values().get(0);
    assertTrue("Authentication should not be successful", !fingerprintAuthenticationResult.isSuccess());
    assertTrue("Result should be equal HELP", fingerprintAuthenticationResult.getResult().equals(FingerprintResult.HELP));
    assertTrue("Should contain help message", fingerprintAuthenticationResult.getMessage().equals(MESSAGE_HELP));
}
 
Example 9
Source File: FingerprintManagerCompatApi23.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private static FingerprintManager.AuthenticationCallback wrapCallback(
        final AuthenticationCallback callback) {
    return new FingerprintManager.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            callback.onAuthenticationError(errMsgId, errString);
        }

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

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
                    unwrapCryptoObject(result.getCryptoObject())));
        }

        @Override
        public void onAuthenticationFailed() {
            callback.onAuthenticationFailed();
        }
    };
}
 
Example 10
Source File: FingerprintManagerCompatApi23.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private static FingerprintManager.AuthenticationCallback wrapCallback(
        final AuthenticationCallback callback) {
    return new FingerprintManager.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            callback.onAuthenticationError(errMsgId, errString);
        }

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

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
                    unwrapCryptoObject(result.getCryptoObject())));
        }

        @Override
        public void onAuthenticationFailed() {
            callback.onAuthenticationFailed();
        }
    };
}
 
Example 11
Source File: FingerprintHelper.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
public static void authenticate(Context context, CancellationSignal cancelSignal,
                                FingerprintManager.AuthenticationCallback callback) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return;
    }

    FingerprintManager manager = context.getSystemService(FingerprintManager.class);
    if (manager != null) {
        manager.authenticate(null, cancelSignal, 0, callback, null);
    }
}
 
Example 12
Source File: RxFingerPrinter.java    From LLApp with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
private void initManager() {
    mCancellationSignal = new CancellationSignal();
    manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
    mKeyManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    mSelfCancelled = new FingerprintManager.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, CharSequence errString) {
            //多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证
            publishSubject.onError(new FPerException(FINGERPRINTERS_FAILED_ERROR));
            mCancellationSignal.cancel();
        }

        @Override
        public void onAuthenticationHelp(int helpCode, CharSequence helpString) {

        }

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            publishSubject.onNext(true);

        }

        @Override
        public void onAuthenticationFailed() {
            publishSubject.onNext(false);
        }
    };
}
 
Example 13
Source File: FingerprintAuthenticationTest.java    From RxFingerprint with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthenticationSuccessfulOnSecondTry() throws Exception {
    when(fingerprintApiWrapper.isUnavailable()).thenReturn(false);
    when(fingerprintApiWrapper.getFingerprintManager()).thenReturn(fingerprintManager);

    TestObserver<FingerprintAuthenticationResult> testObserver = observable.test();

    ArgumentCaptor<FingerprintManager.AuthenticationCallback> callbackCaptor = ArgumentCaptor.forClass(FingerprintManager.AuthenticationCallback.class);
    verify(fingerprintManager).authenticate(any(CryptoObject.class), any(CancellationSignal.class), anyInt(), callbackCaptor.capture(), any(Handler.class));
    callbackCaptor.getValue().onAuthenticationHelp(0, MESSAGE_HELP);

    testObserver.assertNotTerminated();
    testObserver.assertNoErrors();
    testObserver.assertNotComplete();
    testObserver.assertValueCount(1);

    FingerprintAuthenticationResult helpResult = testObserver.values().get(0);
    assertTrue("Authentication should not be successful", !helpResult.isSuccess());
    assertTrue("Result should be equal HELP", helpResult.getResult().equals(FingerprintResult.HELP));
    assertTrue("Should contain help message", helpResult.getMessage().equals(MESSAGE_HELP));

    callbackCaptor.getValue().onAuthenticationSucceeded(mock(AuthenticationResult.class));

    testObserver.awaitTerminalEvent();
    testObserver.assertNoErrors();
    testObserver.assertComplete();
    testObserver.assertValueCount(2);

    FingerprintAuthenticationResult successResult = testObserver.values().get(1);
    assertTrue("Authentication should be successful", successResult.isSuccess());
    assertTrue("Result should be equal AUTHENTICATED", successResult.getResult().equals(FingerprintResult.AUTHENTICATED));
    assertTrue("Should contain no message", successResult.getMessage() == null);
}
 
Example 14
Source File: MarshmallowReprintModule.java    From reprint with Apache License 2.0 5 votes vote down vote up
void authenticate(final CancellationSignal cancellationSignal,
                          final AuthenticationListener listener,
                          final Reprint.RestartPredicate restartPredicate,
                          final int restartCount) throws SecurityException {
    final FingerprintManager fingerprintManager = fingerprintManager();

    if (fingerprintManager == null) {
        listener.onFailure(AuthenticationFailureReason.UNKNOWN, true,
                context.getString(R.string.fingerprint_error_hw_not_available), TAG, FINGERPRINT_ERROR_CANCELED);
        return;
    }

    final FingerprintManager.AuthenticationCallback callback =
            new AuthCallback(restartCount, restartPredicate, cancellationSignal, listener);

    // Why getCancellationSignalObject returns an Object is unexplained
    final android.os.CancellationSignal signalObject = cancellationSignal == null ? null :
            (android.os.CancellationSignal) cancellationSignal.getCancellationSignalObject();

    // Occasionally, an NPE will bubble up out of FingerprintManager.authenticate
    try {
        fingerprintManager.authenticate(null, signalObject, 0, callback, null);
    } catch (NullPointerException e) {
        logger.logException(e, "MarshmallowReprintModule: authenticate failed unexpectedly");
        listener.onFailure(AuthenticationFailureReason.UNKNOWN, true,
                context.getString(R.string.fingerprint_error_unable_to_process), TAG, FINGERPRINT_ERROR_CANCELED);
    }
}
 
Example 15
Source File: FingerprintDialogCompatV23.java    From FingerprintDialogCompat with Apache License 2.0 4 votes vote down vote up
/**
 * Start the finger print authentication by enabling the finger print sensor.
 * Note: Use this function in the onResume() of the activity/fragment. Never forget to call
 * {@link #stopAuthIfRunning()} in onPause() of the activity/fragment.
 */
@TargetApi(Build.VERSION_CODES.M)
private void startAuth() {
    if (isScanning) stopAuthIfRunning();
    final FingerprintManager fingerprintManager = (FingerprintManager) mContext.getSystemService(Context.FINGERPRINT_SERVICE);

    //Cannot access the fingerprint manager.
    if (fingerprintManager == null) {
        mCallback.fingerprintAuthenticationNotSupported();
        return;
    }

    //No fingerprint enrolled.
    if (!fingerprintManager.hasEnrolledFingerprints()) {
        mCallback.hasNoFingerprintEnrolled();
        return;
    }

    final FingerprintManager.CryptoObject cryptoObject = getCryptoObject();
    if (cryptoObject != null) {
        final FingerprintManager.AuthenticationCallback authCallback = new FingerprintManager.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                displayStatusText(errString.toString(), true);

                switch (errMsgId) {
                    case FingerprintManager.FINGERPRINT_ERROR_CANCELED:
                    case FingerprintManager.FINGERPRINT_ERROR_USER_CANCELED:
                        mCallback.authenticationCanceledByUser();
                        break;
                    case FingerprintManager.FINGERPRINT_ERROR_HW_NOT_PRESENT:
                    case FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE:
                        mCallback.fingerprintAuthenticationNotSupported();
                        break;
                    default:
                        mCallback.onAuthenticationError(errMsgId, errString);
                }
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                displayStatusText(helpString.toString(), false);
                mCallback.onAuthenticationHelp(helpMsgId, helpString);
            }

            @Override
            public void onAuthenticationFailed() {
                displayStatusText(getString(R.string.fingerprint_not_recognised), false);
                mCallback.onAuthenticationFailed();
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                mCallback.onAuthenticationSucceeded();
                closeDialog();
            }
        };

        mCancellationSignal = new CancellationSignal();
        //noinspection MissingPermission
        fingerprintManager.authenticate(cryptoObject,
                mCancellationSignal,
                0,
                authCallback,
                new Handler(Looper.getMainLooper()));
    } else {
        //Cannot access the secure keystore.
        mCallback.fingerprintAuthenticationNotSupported();
        closeDialog();
    }
}