下面列出了android.hardware.fingerprint.FingerprintManager#AuthenticationCallback ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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();
}
};
}
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();
}
};
}
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();
}
};
}
@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);
}
@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));
}
@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);
}
@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));
}
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();
}
};
}
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();
}
};
}
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);
}
}
@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);
}
};
}
@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);
}
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);
}
}
/**
* 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();
}
}