1 /* 2 * Copyright (C) 2017 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 com.android.settings.biometrics.fingerprint; 18 19 import android.app.settings.SettingsEnums; 20 import android.hardware.fingerprint.FingerprintManager; 21 import android.hardware.fingerprint.FingerprintManager.AuthenticationResult; 22 import android.os.CancellationSignal; 23 24 import com.android.settings.core.InstrumentedFragment; 25 26 /** 27 * Sidecar fragment to handle the state around fingerprint authentication 28 */ 29 public class FingerprintAuthenticateSidecar extends InstrumentedFragment { 30 31 private static final String TAG = "FingerprintAuthenticateSidecar"; 32 33 private FingerprintManager mFingerprintManager; 34 private Listener mListener; 35 private AuthenticationResult mAuthenticationResult; 36 private CancellationSignal mCancellationSignal; 37 private AuthenticationError mAuthenticationError; 38 39 public interface Listener { onAuthenticationSucceeded(AuthenticationResult result)40 void onAuthenticationSucceeded(AuthenticationResult result); onAuthenticationFailed()41 void onAuthenticationFailed(); onAuthenticationError(int errMsgId, CharSequence errString)42 void onAuthenticationError(int errMsgId, CharSequence errString); onAuthenticationHelp(int helpMsgId, CharSequence helpString)43 void onAuthenticationHelp(int helpMsgId, CharSequence helpString); 44 } 45 46 private class AuthenticationError { 47 int error; 48 CharSequence errorString; 49 AuthenticationError(int errMsgId, CharSequence errString)50 public AuthenticationError(int errMsgId, CharSequence errString) { 51 error = errMsgId; 52 errorString = errString; 53 } 54 } 55 56 @Override getMetricsCategory()57 public int getMetricsCategory() { 58 return SettingsEnums.FINGERPRINT_AUTHENTICATE_SIDECAR; 59 } 60 61 private FingerprintManager.AuthenticationCallback mAuthenticationCallback = 62 new FingerprintManager.AuthenticationCallback() { 63 @Override 64 public void onAuthenticationSucceeded(AuthenticationResult result) { 65 mCancellationSignal = null; 66 if (mListener != null) { 67 mListener.onAuthenticationSucceeded(result); 68 } else { 69 mAuthenticationResult = result; 70 mAuthenticationError = null; 71 } 72 } 73 74 @Override 75 public void onAuthenticationFailed() { 76 if (mListener != null) { 77 mListener.onAuthenticationFailed(); 78 } 79 } 80 81 @Override 82 public void onAuthenticationError(int errMsgId, CharSequence errString) { 83 mCancellationSignal = null; 84 if (mListener != null) { 85 mListener.onAuthenticationError(errMsgId, errString); 86 } else { 87 mAuthenticationError = new AuthenticationError(errMsgId, errString); 88 mAuthenticationResult = null; 89 } 90 } 91 92 @Override 93 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { 94 if (mListener != null) { 95 mListener.onAuthenticationHelp(helpMsgId, helpString); 96 } 97 } 98 }; 99 setFingerprintManager(FingerprintManager fingerprintManager)100 public void setFingerprintManager(FingerprintManager fingerprintManager) { 101 mFingerprintManager = fingerprintManager; 102 } 103 startAuthentication(int userId)104 public void startAuthentication(int userId) { 105 mCancellationSignal = new CancellationSignal(); 106 mFingerprintManager.authenticate(null /* crypto */, mCancellationSignal, 107 mAuthenticationCallback, null /* handler */, userId); 108 } 109 stopAuthentication()110 public void stopAuthentication() { 111 if (mCancellationSignal != null && !mCancellationSignal.isCanceled()) { 112 mCancellationSignal.cancel(); 113 } 114 mCancellationSignal = null; 115 } 116 setListener(Listener listener)117 public void setListener(Listener listener) { 118 if (mListener == null && listener != null) { 119 if (mAuthenticationResult != null) { 120 listener.onAuthenticationSucceeded(mAuthenticationResult); 121 mAuthenticationResult = null; 122 } 123 if (mAuthenticationError != null && 124 mAuthenticationError.error != FingerprintManager.FINGERPRINT_ERROR_CANCELED) { 125 listener.onAuthenticationError(mAuthenticationError.error, 126 mAuthenticationError.errorString); 127 mAuthenticationError = null; 128 } 129 } 130 mListener = listener; 131 } 132 }