1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.server.biometrics.fingerprint; 18 19 import android.app.Activity; 20 import android.hardware.fingerprint.FingerprintManager; 21 import android.os.Bundle; 22 import android.server.wm.TestJournalProvider; 23 24 import androidx.annotation.NonNull; 25 26 import java.util.ArrayList; 27 28 /** 29 * Authentication callback helper that allows easy transfer between test activities and 30 * CTS via {@link android.server.wm.TestJournalProvider.TestJournal}, as well as serialization 31 * and deserialization. 32 * 33 * Note that generally a single instance of this helper should only be used for a single 34 * authentication. 35 */ 36 @SuppressWarnings("deprecation") 37 public class FingerprintCallbackHelper extends FingerprintManager.AuthenticationCallback { 38 39 public static final String KEY = "key_auth_callback"; 40 41 public static class State { 42 private static final String KEY_ERRORS_RECEIVED = "key_errors_received"; 43 private static final String KEY_ACQUIRED_RECEIVED = "key_acquired_received"; 44 private static final String KEY_NUM_ACCEPTED = "key_num_accepted"; 45 private static final String KEY_NUM_REJECTED = "key_num_rejected"; 46 47 public final ArrayList<Integer> mErrorsReceived; 48 public final ArrayList<Integer> mAcquiredReceived; 49 public int mNumAuthAccepted; 50 public int mNumAuthRejected; 51 State()52 public State() { 53 mErrorsReceived = new ArrayList<>(); 54 mAcquiredReceived = new ArrayList<>(); 55 } 56 toBundle()57 public Bundle toBundle() { 58 final Bundle bundle = new Bundle(); 59 bundle.putIntegerArrayList(KEY_ERRORS_RECEIVED, mErrorsReceived); 60 bundle.putIntegerArrayList(KEY_ACQUIRED_RECEIVED, mAcquiredReceived); 61 bundle.putInt(KEY_NUM_ACCEPTED, mNumAuthAccepted); 62 bundle.putInt(KEY_NUM_REJECTED, mNumAuthRejected); 63 return bundle; 64 } 65 State(ArrayList<Integer> errorsReceived, ArrayList<Integer> acquiredReceived, int numAuthAccepted, int numAuthRejected)66 private State(ArrayList<Integer> errorsReceived, ArrayList<Integer> acquiredReceived, 67 int numAuthAccepted, int numAuthRejected) { 68 mErrorsReceived = errorsReceived; 69 mAcquiredReceived = acquiredReceived; 70 mNumAuthAccepted = numAuthAccepted; 71 mNumAuthRejected = numAuthRejected; 72 } 73 fromBundle(@onNull Bundle bundle)74 public static State fromBundle(@NonNull Bundle bundle) { 75 return new State( 76 bundle.getIntegerArrayList(KEY_ERRORS_RECEIVED), 77 bundle.getIntegerArrayList(KEY_ACQUIRED_RECEIVED), 78 bundle.getInt(KEY_NUM_ACCEPTED), 79 bundle.getInt(KEY_NUM_REJECTED)); 80 } 81 } 82 83 private final Activity mActivity; 84 private final State mState; 85 86 @Override onAuthenticationError(int errorCode, CharSequence errString)87 public void onAuthenticationError(int errorCode, CharSequence errString) { 88 mState.mErrorsReceived.add(errorCode); 89 updateJournal(); 90 } 91 92 @Override onAuthenticationHelp(int helpCode, CharSequence helpString)93 public void onAuthenticationHelp(int helpCode, CharSequence helpString) { 94 mState.mAcquiredReceived.add(helpCode); 95 updateJournal(); 96 } 97 98 @Override onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result)99 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { 100 mState.mNumAuthAccepted++; 101 updateJournal(); 102 } 103 104 @Override onAuthenticationFailed()105 public void onAuthenticationFailed() { 106 mState.mNumAuthRejected++; 107 updateJournal(); 108 } 109 FingerprintCallbackHelper(@onNull Activity activity)110 public FingerprintCallbackHelper(@NonNull Activity activity) { 111 mActivity = activity; 112 mState = new State(); 113 114 // Update with empty state. It's faster than waiting/retrying for null on CTS-side. 115 updateJournal(); 116 } 117 updateJournal()118 private void updateJournal() { 119 TestJournalProvider.putExtras(mActivity, 120 bundle -> bundle.putBundle(KEY, mState.toBundle())); 121 } 122 } 123