@Override public void onAuthenticationError(int errMsgId, CharSequence errString) { AuthenticationFailureReason failureReason = AuthenticationFailureReason.UNKNOWN; switch (errMsgId) { case FINGERPRINT_ERROR_HW_UNAVAILABLE: failureReason = AuthenticationFailureReason.HARDWARE_UNAVAILABLE; break; case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: case FINGERPRINT_ERROR_NO_SPACE: failureReason = AuthenticationFailureReason.SENSOR_FAILED; break; case FINGERPRINT_ERROR_TIMEOUT: failureReason = AuthenticationFailureReason.TIMEOUT; break; case FINGERPRINT_ERROR_LOCKOUT: failureReason = AuthenticationFailureReason.LOCKED_OUT; break; case FINGERPRINT_ERROR_CANCELED: // Don't send a cancelled message. return; } if (errMsgId == FINGERPRINT_ERROR_TIMEOUT && restartPredicate.invoke(failureReason, restartCount)) { authenticate(cancellationSignal, listener, restartPredicate, restartCount); } else { listener.onFailure(failureReason, true, errString, TAG, errMsgId); } }
@Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { if (!restartPredicate.invoke(AuthenticationFailureReason.SENSOR_FAILED, restartCount++)) { cancellationSignal.cancel(); } listener.onFailure(AuthenticationFailureReason.SENSOR_FAILED, false, helpString, TAG, helpMsgId); }
@Test public void neverRestartPredicate_neverRestarts() throws Exception { Reprint.RestartPredicate predicate = RestartPredicates.neverRestart(); assertFalse(predicate.invoke(TIMEOUT, 0)); assertFalse(predicate.invoke(AUTHENTICATION_FAILED, 1)); assertFalse(predicate.invoke(HARDWARE_UNAVAILABLE, 2)); } }
@Test public void defaultPredicate_passesLimitedTimeout() throws Exception { Reprint.RestartPredicate predicate = RestartPredicates.defaultPredicate(); assertTrue(predicate.invoke(TIMEOUT, 0)); assertTrue(predicate.invoke(TIMEOUT, 1)); assertTrue(predicate.invoke(TIMEOUT, 2)); assertTrue(predicate.invoke(TIMEOUT, 3)); assertTrue(predicate.invoke(TIMEOUT, 4)); assertFalse(predicate.invoke(TIMEOUT, 5)); }
@Test public void defaultPredicate_passesUnlimitedNonFatal() throws Exception { Reprint.RestartPredicate predicate = RestartPredicates.defaultPredicate(); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 0)); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 1)); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 2)); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 3)); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 4)); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 5)); }
@Test public void defaultPredicate_passesMixedAuthFailureAndTimeout() throws Exception { Reprint.RestartPredicate predicate = RestartPredicates.defaultPredicate(); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 0)); assertTrue(predicate.invoke(TIMEOUT, 1)); assertTrue(predicate.invoke(AUTHENTICATION_FAILED, 2)); assertTrue(predicate.invoke(TIMEOUT, 3)); assertTrue(predicate.invoke(TIMEOUT, 4)); assertTrue(predicate.invoke(TIMEOUT, 5)); assertTrue(predicate.invoke(TIMEOUT, 6)); assertFalse(predicate.invoke(TIMEOUT, 7)); }
private void fail(AuthenticationFailureReason reason, boolean fatal, String message, int status) { listener.onFailure(reason, fatal, message, TAG, status); if ((!fatal || reason == TIMEOUT) && restartPredicate.invoke(reason, restartCount)) { authenticate(cancellationSignal, listener, restartPredicate, restartCount + 1); } }