Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2018 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.hardware.face; |
| 18 | |
| 19 | import static android.Manifest.permission.INTERACT_ACROSS_USERS; |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.MANAGE_BIOMETRIC; |
| 21 | import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 22 | |
| 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
| 25 | import android.annotation.RequiresPermission; |
| 26 | import android.annotation.SystemService; |
| 27 | import android.app.ActivityManager; |
| 28 | import android.content.Context; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 29 | import android.hardware.biometrics.BiometricAuthenticator; |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 30 | import android.hardware.biometrics.BiometricConstants; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 31 | import android.hardware.biometrics.BiometricFaceConstants; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 32 | import android.hardware.biometrics.CryptoObject; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 33 | import android.hardware.biometrics.IBiometricServiceLockoutResetCallback; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 34 | import android.os.Binder; |
| 35 | import android.os.CancellationSignal; |
| 36 | import android.os.CancellationSignal.OnCancelListener; |
| 37 | import android.os.Handler; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.IRemoteCallback; |
| 40 | import android.os.Looper; |
| 41 | import android.os.PowerManager; |
| 42 | import android.os.RemoteException; |
| 43 | import android.os.UserHandle; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 44 | import android.util.Log; |
| 45 | import android.util.Slog; |
| 46 | |
| 47 | import com.android.internal.R; |
| 48 | |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 49 | import java.util.List; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * A class that coordinates access to the face authentication hardware. |
| 53 | * @hide |
| 54 | */ |
| 55 | @SystemService(Context.FACE_SERVICE) |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 56 | public class FaceManager implements BiometricAuthenticator, BiometricFaceConstants { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 57 | |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 58 | private static final String TAG = "FaceManager"; |
| 59 | private static final boolean DEBUG = true; |
| 60 | private static final int MSG_ENROLL_RESULT = 100; |
| 61 | private static final int MSG_ACQUIRED = 101; |
| 62 | private static final int MSG_AUTHENTICATION_SUCCEEDED = 102; |
| 63 | private static final int MSG_AUTHENTICATION_FAILED = 103; |
| 64 | private static final int MSG_ERROR = 104; |
| 65 | private static final int MSG_REMOVED = 105; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 66 | |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 67 | private IFaceService mService; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 68 | private final Context mContext; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 69 | private IBinder mToken = new Binder(); |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 70 | private AuthenticationCallback mAuthenticationCallback; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 71 | private EnrollmentCallback mEnrollmentCallback; |
| 72 | private RemovalCallback mRemovalCallback; |
| 73 | private CryptoObject mCryptoObject; |
| 74 | private Face mRemovalFace; |
| 75 | private Handler mHandler; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 76 | |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 77 | private IFaceServiceReceiver mServiceReceiver = new IFaceServiceReceiver.Stub() { |
| 78 | |
| 79 | @Override // binder call |
| 80 | public void onEnrollResult(long deviceId, int faceId, int remaining) { |
| 81 | mHandler.obtainMessage(MSG_ENROLL_RESULT, remaining, 0, |
| 82 | new Face(null, faceId, deviceId)).sendToTarget(); |
| 83 | } |
| 84 | |
| 85 | @Override // binder call |
| 86 | public void onAcquired(long deviceId, int acquireInfo, int vendorCode) { |
| 87 | mHandler.obtainMessage(MSG_ACQUIRED, acquireInfo, vendorCode, deviceId).sendToTarget(); |
| 88 | } |
| 89 | |
| 90 | @Override // binder call |
| 91 | public void onAuthenticationSucceeded(long deviceId, Face face) { |
| 92 | mHandler.obtainMessage(MSG_AUTHENTICATION_SUCCEEDED, face).sendToTarget(); |
| 93 | } |
| 94 | |
| 95 | @Override // binder call |
| 96 | public void onAuthenticationFailed(long deviceId) { |
| 97 | mHandler.obtainMessage(MSG_AUTHENTICATION_FAILED).sendToTarget(); |
| 98 | } |
| 99 | |
| 100 | @Override // binder call |
| 101 | public void onError(long deviceId, int error, int vendorCode) { |
| 102 | mHandler.obtainMessage(MSG_ERROR, error, vendorCode, deviceId).sendToTarget(); |
| 103 | } |
| 104 | |
| 105 | @Override // binder call |
| 106 | public void onRemoved(long deviceId, int faceId, int remaining) { |
| 107 | mHandler.obtainMessage(MSG_REMOVED, remaining, 0, |
| 108 | new Face(null, faceId, deviceId)).sendToTarget(); |
| 109 | } |
Kevin Chyn | 6737c57 | 2019-02-08 16:10:54 -0800 | [diff] [blame] | 110 | |
| 111 | @Override |
| 112 | public void onEnumerated(long deviceId, int faceId, int remaining) { |
| 113 | // TODO: Finish. Low priority since it's not used. |
| 114 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * @hide |
| 119 | */ |
| 120 | public FaceManager(Context context, IFaceService service) { |
| 121 | mContext = context; |
| 122 | mService = service; |
| 123 | if (mService == null) { |
| 124 | Slog.v(TAG, "FaceAuthenticationManagerService was null"); |
| 125 | } |
| 126 | mHandler = new MyHandler(context); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Request authentication of a crypto object. This call operates the face recognition hardware |
| 131 | * and starts capturing images. It terminates when |
| 132 | * {@link AuthenticationCallback#onAuthenticationError(int, CharSequence)} or |
| 133 | * {@link AuthenticationCallback#onAuthenticationSucceeded(AuthenticationResult)} is called, at |
| 134 | * which point the object is no longer valid. The operation can be canceled by using the |
| 135 | * provided cancel object. |
| 136 | * |
| 137 | * @param crypto object associated with the call or null if none required. |
| 138 | * @param cancel an object that can be used to cancel authentication |
| 139 | * @param flags optional flags; should be 0 |
| 140 | * @param callback an object to receive authentication events |
| 141 | * @param handler an optional handler to handle callback events |
| 142 | * @throws IllegalArgumentException if the crypto operation is not supported or is not backed |
| 143 | * by |
| 144 | * <a href="{@docRoot}training/articles/keystore.html">Android |
| 145 | * Keystore facility</a>. |
| 146 | * @throws IllegalStateException if the crypto primitive is not initialized. |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 147 | * @hide |
| 148 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 149 | @RequiresPermission(USE_BIOMETRIC_INTERNAL) |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 150 | public void authenticate(@Nullable CryptoObject crypto, @Nullable CancellationSignal cancel, |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 151 | int flags, @NonNull AuthenticationCallback callback, @Nullable Handler handler) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 152 | if (callback == null) { |
| 153 | throw new IllegalArgumentException("Must supply an authentication callback"); |
| 154 | } |
| 155 | |
| 156 | if (cancel != null) { |
| 157 | if (cancel.isCanceled()) { |
| 158 | Log.w(TAG, "authentication already canceled"); |
| 159 | return; |
| 160 | } else { |
| 161 | cancel.setOnCancelListener(new OnAuthenticationCancelListener(crypto)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (mService != null) { |
| 166 | try { |
| 167 | useHandler(handler); |
| 168 | mAuthenticationCallback = callback; |
| 169 | mCryptoObject = crypto; |
| 170 | long sessionId = crypto != null ? crypto.getOpId() : 0; |
Kevin Chyn | 747e29b | 2019-01-11 17:01:53 -0800 | [diff] [blame] | 171 | mService.authenticate(mToken, sessionId, mContext.getUserId(), mServiceReceiver, |
| 172 | flags, mContext.getOpPackageName()); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 173 | } catch (RemoteException e) { |
| 174 | Log.w(TAG, "Remote exception while authenticating: ", e); |
| 175 | if (callback != null) { |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 176 | // Though this may not be a hardware issue, it will cause apps to give up or |
| 177 | // try again later. |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 178 | callback.onAuthenticationError(FACE_ERROR_HW_UNAVAILABLE, |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 179 | getErrorString(mContext, FACE_ERROR_HW_UNAVAILABLE, |
| 180 | 0 /* vendorCode */)); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 187 | * Use the provided handler thread for events. |
| 188 | */ |
| 189 | private void useHandler(Handler handler) { |
| 190 | if (handler != null) { |
| 191 | mHandler = new MyHandler(handler.getLooper()); |
| 192 | } else if (mHandler.getLooper() != mContext.getMainLooper()) { |
| 193 | mHandler = new MyHandler(mContext.getMainLooper()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 198 | * Request face authentication enrollment. This call operates the face authentication hardware |
| 199 | * and starts capturing images. Progress will be indicated by callbacks to the |
| 200 | * {@link EnrollmentCallback} object. It terminates when |
| 201 | * {@link EnrollmentCallback#onEnrollmentError(int, CharSequence)} or |
| 202 | * {@link EnrollmentCallback#onEnrollmentProgress(int) is called with remaining == 0, at |
| 203 | * which point the object is no longer valid. The operation can be canceled by using the |
| 204 | * provided cancel object. |
| 205 | * |
| 206 | * @param token a unique token provided by a recent creation or verification of device |
| 207 | * credentials (e.g. pin, pattern or password). |
| 208 | * @param cancel an object that can be used to cancel enrollment |
| 209 | * @param flags optional flags |
| 210 | * @param userId the user to whom this face will belong to |
| 211 | * @param callback an object to receive enrollment events |
| 212 | * @hide |
| 213 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 214 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 215 | public void enroll(byte[] token, CancellationSignal cancel, |
| 216 | EnrollmentCallback callback, int[] disabledFeatures) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 217 | if (callback == null) { |
| 218 | throw new IllegalArgumentException("Must supply an enrollment callback"); |
| 219 | } |
| 220 | |
| 221 | if (cancel != null) { |
| 222 | if (cancel.isCanceled()) { |
| 223 | Log.w(TAG, "enrollment already canceled"); |
| 224 | return; |
| 225 | } else { |
| 226 | cancel.setOnCancelListener(new OnEnrollCancelListener()); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (mService != null) { |
| 231 | try { |
| 232 | mEnrollmentCallback = callback; |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 233 | mService.enroll(mToken, token, mServiceReceiver, |
| 234 | mContext.getOpPackageName(), disabledFeatures); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 235 | } catch (RemoteException e) { |
| 236 | Log.w(TAG, "Remote exception in enroll: ", e); |
| 237 | if (callback != null) { |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 238 | // Though this may not be a hardware issue, it will cause apps to give up or |
| 239 | // try again later. |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 240 | callback.onEnrollmentError(FACE_ERROR_HW_UNAVAILABLE, |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 241 | getErrorString(mContext, FACE_ERROR_HW_UNAVAILABLE, |
| 242 | 0 /* vendorCode */)); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 249 | * Requests an auth token to tie sensitive operations to the confirmation of |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 250 | * existing device credentials (e.g. pin/pattern/password). |
| 251 | * |
| 252 | * @hide |
| 253 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 254 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 255 | public long generateChallenge() { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 256 | long result = 0; |
| 257 | if (mService != null) { |
| 258 | try { |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 259 | result = mService.generateChallenge(mToken); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 260 | } catch (RemoteException e) { |
| 261 | throw e.rethrowFromSystemServer(); |
| 262 | } |
| 263 | } |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | /** |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 268 | * Invalidates the current auth token. |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 269 | * |
| 270 | * @hide |
| 271 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 272 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 273 | public int revokeChallenge() { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 274 | int result = 0; |
| 275 | if (mService != null) { |
| 276 | try { |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 277 | result = mService.revokeChallenge(mToken); |
| 278 | } catch (RemoteException e) { |
| 279 | throw e.rethrowFromSystemServer(); |
| 280 | } |
| 281 | } |
| 282 | return result; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @hide |
| 287 | */ |
| 288 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | b95f152 | 2019-03-04 16:45:15 -0800 | [diff] [blame] | 289 | public boolean setFeature(int feature, boolean enabled, byte[] token) { |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 290 | if (mService != null) { |
| 291 | try { |
Kevin Chyn | b95f152 | 2019-03-04 16:45:15 -0800 | [diff] [blame] | 292 | return mService.setFeature(feature, enabled, token); |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 293 | } catch (RemoteException e) { |
| 294 | throw e.rethrowFromSystemServer(); |
| 295 | } |
| 296 | } |
Kevin Chyn | b95f152 | 2019-03-04 16:45:15 -0800 | [diff] [blame] | 297 | return false; |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
| 301 | * @hide |
| 302 | */ |
| 303 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 304 | public boolean getFeature(int feature) { |
Kevin Chyn | d79e24e | 2018-09-25 12:06:59 -0700 | [diff] [blame] | 305 | boolean result = true; |
| 306 | if (mService != null) { |
| 307 | try { |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 308 | result = mService.getFeature(feature); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 309 | } catch (RemoteException e) { |
| 310 | throw e.rethrowFromSystemServer(); |
| 311 | } |
| 312 | } |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | /** |
Kevin Chyn | 57f119b | 2018-10-25 12:03:41 -0700 | [diff] [blame] | 317 | * Pokes the the driver to have it start looking for faces again. |
| 318 | * @hide |
| 319 | */ |
| 320 | @RequiresPermission(MANAGE_BIOMETRIC) |
| 321 | public void userActivity() { |
| 322 | if (mService != null) { |
| 323 | try { |
| 324 | mService.userActivity(); |
| 325 | } catch (RemoteException e) { |
| 326 | throw e.rethrowFromSystemServer(); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 332 | * Sets the active user. This is meant to be used to select the current profile for enrollment |
| 333 | * to allow separate enrolled faces for a work profile |
| 334 | * |
| 335 | * @hide |
| 336 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 337 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | bf830a3 | 2018-10-07 15:58:46 -0700 | [diff] [blame] | 338 | @Override |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 339 | public void setActiveUser(int userId) { |
| 340 | if (mService != null) { |
| 341 | try { |
| 342 | mService.setActiveUser(userId); |
| 343 | } catch (RemoteException e) { |
| 344 | throw e.rethrowFromSystemServer(); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Remove given face template from face hardware and/or protected storage. |
| 351 | * |
| 352 | * @param face the face item to remove |
| 353 | * @param userId the user who this face belongs to |
| 354 | * @param callback an optional callback to verify that face templates have been |
| 355 | * successfully removed. May be null if no callback is required. |
| 356 | * @hide |
| 357 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 358 | @RequiresPermission(MANAGE_BIOMETRIC) |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 359 | public void remove(Face face, int userId, RemovalCallback callback) { |
| 360 | if (mService != null) { |
| 361 | try { |
| 362 | mRemovalCallback = callback; |
| 363 | mRemovalFace = face; |
Kevin Chyn | b528d69 | 2018-07-20 11:53:14 -0700 | [diff] [blame] | 364 | mService.remove(mToken, face.getBiometricId(), userId, mServiceReceiver); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 365 | } catch (RemoteException e) { |
| 366 | Log.w(TAG, "Remote exception in remove: ", e); |
| 367 | if (callback != null) { |
| 368 | callback.onRemovalError(face, FACE_ERROR_HW_UNAVAILABLE, |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 369 | getErrorString(mContext, FACE_ERROR_HW_UNAVAILABLE, |
| 370 | 0 /* vendorCode */)); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Obtain the enrolled face template. |
| 378 | * |
| 379 | * @return the current face item |
| 380 | * @hide |
| 381 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 382 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 383 | public List<Face> getEnrolledFaces(int userId) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 384 | if (mService != null) { |
| 385 | try { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 386 | return mService.getEnrolledFaces(userId, mContext.getOpPackageName()); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 387 | } catch (RemoteException e) { |
| 388 | throw e.rethrowFromSystemServer(); |
| 389 | } |
| 390 | } |
| 391 | return null; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Obtain the enrolled face template. |
| 396 | * |
| 397 | * @return the current face item |
| 398 | * @hide |
| 399 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 400 | @RequiresPermission(MANAGE_BIOMETRIC) |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 401 | public List<Face> getEnrolledFaces() { |
| 402 | return getEnrolledFaces(UserHandle.myUserId()); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Determine if there is a face enrolled. |
| 407 | * |
| 408 | * @return true if a face is enrolled, false otherwise |
| 409 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 410 | @RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 411 | @Override |
| 412 | public boolean hasEnrolledTemplates() { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 413 | if (mService != null) { |
| 414 | try { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 415 | return mService.hasEnrolledFaces( |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 416 | UserHandle.myUserId(), mContext.getOpPackageName()); |
| 417 | } catch (RemoteException e) { |
| 418 | throw e.rethrowFromSystemServer(); |
| 419 | } |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * @hide |
| 426 | */ |
| 427 | @RequiresPermission(allOf = { |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 428 | USE_BIOMETRIC_INTERNAL, |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 429 | INTERACT_ACROSS_USERS}) |
Kevin Chyn | 75dbb83 | 2018-10-05 17:32:57 -0700 | [diff] [blame] | 430 | @Override |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 431 | public boolean hasEnrolledTemplates(int userId) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 432 | if (mService != null) { |
| 433 | try { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 434 | return mService.hasEnrolledFaces(userId, mContext.getOpPackageName()); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 435 | } catch (RemoteException e) { |
| 436 | throw e.rethrowFromSystemServer(); |
| 437 | } |
| 438 | } |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Determine if face authentication sensor hardware is present and functional. |
| 444 | * |
| 445 | * @return true if hardware is present and functional, false otherwise. |
| 446 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 447 | @RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 448 | @Override |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 449 | public boolean isHardwareDetected() { |
| 450 | if (mService != null) { |
| 451 | try { |
| 452 | long deviceId = 0; /* TODO: plumb hardware id to FPMS */ |
| 453 | return mService.isHardwareDetected(deviceId, mContext.getOpPackageName()); |
| 454 | } catch (RemoteException e) { |
| 455 | throw e.rethrowFromSystemServer(); |
| 456 | } |
| 457 | } else { |
| 458 | Log.w(TAG, "isFaceHardwareDetected(): Service not connected!"); |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Retrieves the authenticator token for binding keys to the lifecycle |
| 465 | * of the calling user's face. Used only by internal clients. |
| 466 | * |
| 467 | * @hide |
| 468 | */ |
| 469 | public long getAuthenticatorId() { |
| 470 | if (mService != null) { |
| 471 | try { |
| 472 | return mService.getAuthenticatorId(mContext.getOpPackageName()); |
| 473 | } catch (RemoteException e) { |
| 474 | throw e.rethrowFromSystemServer(); |
| 475 | } |
| 476 | } else { |
| 477 | Log.w(TAG, "getAuthenticatorId(): Service not connected!"); |
| 478 | } |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /** |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 483 | * @hide |
| 484 | */ |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 485 | @RequiresPermission(USE_BIOMETRIC_INTERNAL) |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 486 | public void addLockoutResetCallback(final LockoutResetCallback callback) { |
| 487 | if (mService != null) { |
| 488 | try { |
| 489 | final PowerManager powerManager = mContext.getSystemService(PowerManager.class); |
| 490 | mService.addLockoutResetCallback( |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 491 | new IBiometricServiceLockoutResetCallback.Stub() { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 492 | |
| 493 | @Override |
| 494 | public void onLockoutReset(long deviceId, |
| 495 | IRemoteCallback serverCallback) |
| 496 | throws RemoteException { |
| 497 | try { |
| 498 | final PowerManager.WakeLock wakeLock = powerManager.newWakeLock( |
| 499 | PowerManager.PARTIAL_WAKE_LOCK, |
| 500 | "faceLockoutResetCallback"); |
| 501 | wakeLock.acquire(); |
| 502 | mHandler.post(() -> { |
| 503 | try { |
| 504 | callback.onLockoutReset(); |
| 505 | } finally { |
| 506 | wakeLock.release(); |
| 507 | } |
| 508 | }); |
| 509 | } finally { |
| 510 | serverCallback.sendResult(null /* data */); |
| 511 | } |
| 512 | } |
| 513 | }); |
| 514 | } catch (RemoteException e) { |
| 515 | throw e.rethrowFromSystemServer(); |
| 516 | } |
| 517 | } else { |
| 518 | Log.w(TAG, "addLockoutResetCallback(): Service not connected!"); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | private int getCurrentUserId() { |
| 523 | try { |
| 524 | return ActivityManager.getService().getCurrentUser().id; |
| 525 | } catch (RemoteException e) { |
| 526 | throw e.rethrowFromSystemServer(); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | private void cancelEnrollment() { |
| 531 | if (mService != null) { |
| 532 | try { |
| 533 | mService.cancelEnrollment(mToken); |
| 534 | } catch (RemoteException e) { |
| 535 | throw e.rethrowFromSystemServer(); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | private void cancelAuthentication(CryptoObject cryptoObject) { |
| 541 | if (mService != null) { |
| 542 | try { |
| 543 | mService.cancelAuthentication(mToken, mContext.getOpPackageName()); |
| 544 | } catch (RemoteException e) { |
| 545 | throw e.rethrowFromSystemServer(); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 550 | /** |
| 551 | * @hide |
| 552 | */ |
| 553 | public static String getErrorString(Context context, int errMsg, int vendorCode) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 554 | switch (errMsg) { |
Kevin Chyn | 6cf54e8 | 2018-09-18 19:13:27 -0700 | [diff] [blame] | 555 | case FACE_ERROR_HW_UNAVAILABLE: |
| 556 | return context.getString( |
| 557 | com.android.internal.R.string.face_error_hw_not_available); |
Kevin Chyn | 68823b0 | 2019-02-06 08:59:16 -0800 | [diff] [blame] | 558 | case FACE_ERROR_UNABLE_TO_PROCESS: |
| 559 | return context.getString( |
| 560 | com.android.internal.R.string.face_error_unable_to_process); |
Kevin Chyn | 6cf54e8 | 2018-09-18 19:13:27 -0700 | [diff] [blame] | 561 | case FACE_ERROR_TIMEOUT: |
| 562 | return context.getString(com.android.internal.R.string.face_error_timeout); |
Kevin Chyn | 68823b0 | 2019-02-06 08:59:16 -0800 | [diff] [blame] | 563 | case FACE_ERROR_NO_SPACE: |
| 564 | return context.getString(com.android.internal.R.string.face_error_no_space); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 565 | case FACE_ERROR_CANCELED: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 566 | return context.getString(com.android.internal.R.string.face_error_canceled); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 567 | case FACE_ERROR_LOCKOUT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 568 | return context.getString(com.android.internal.R.string.face_error_lockout); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 569 | case FACE_ERROR_LOCKOUT_PERMANENT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 570 | return context.getString( |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 571 | com.android.internal.R.string.face_error_lockout_permanent); |
Kevin Chyn | 6cf54e8 | 2018-09-18 19:13:27 -0700 | [diff] [blame] | 572 | case FACE_ERROR_USER_CANCELED: |
| 573 | return context.getString(com.android.internal.R.string.face_error_user_canceled); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 574 | case FACE_ERROR_NOT_ENROLLED: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 575 | return context.getString(com.android.internal.R.string.face_error_not_enrolled); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 576 | case FACE_ERROR_HW_NOT_PRESENT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 577 | return context.getString(com.android.internal.R.string.face_error_hw_not_present); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 578 | case FACE_ERROR_VENDOR: { |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 579 | String[] msgArray = context.getResources().getStringArray( |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 580 | com.android.internal.R.array.face_error_vendor); |
| 581 | if (vendorCode < msgArray.length) { |
| 582 | return msgArray[vendorCode]; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | Slog.w(TAG, "Invalid error message: " + errMsg + ", " + vendorCode); |
| 587 | return null; |
| 588 | } |
| 589 | |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 590 | /** |
| 591 | * @hide |
| 592 | */ |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 593 | public static String getAcquiredString(Context context, int acquireInfo, int vendorCode) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 594 | switch (acquireInfo) { |
| 595 | case FACE_ACQUIRED_GOOD: |
| 596 | return null; |
| 597 | case FACE_ACQUIRED_INSUFFICIENT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 598 | return context.getString(R.string.face_acquired_insufficient); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 599 | case FACE_ACQUIRED_TOO_BRIGHT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 600 | return context.getString(R.string.face_acquired_too_bright); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 601 | case FACE_ACQUIRED_TOO_DARK: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 602 | return context.getString(R.string.face_acquired_too_dark); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 603 | case FACE_ACQUIRED_TOO_CLOSE: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 604 | return context.getString(R.string.face_acquired_too_close); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 605 | case FACE_ACQUIRED_TOO_FAR: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 606 | return context.getString(R.string.face_acquired_too_far); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 607 | case FACE_ACQUIRED_TOO_HIGH: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 608 | return context.getString(R.string.face_acquired_too_high); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 609 | case FACE_ACQUIRED_TOO_LOW: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 610 | return context.getString(R.string.face_acquired_too_low); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 611 | case FACE_ACQUIRED_TOO_RIGHT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 612 | return context.getString(R.string.face_acquired_too_right); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 613 | case FACE_ACQUIRED_TOO_LEFT: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 614 | return context.getString(R.string.face_acquired_too_left); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 615 | case FACE_ACQUIRED_POOR_GAZE: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 616 | return context.getString(R.string.face_acquired_poor_gaze); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 617 | case FACE_ACQUIRED_NOT_DETECTED: |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 618 | return context.getString(R.string.face_acquired_not_detected); |
Kevin Chyn | 68823b0 | 2019-02-06 08:59:16 -0800 | [diff] [blame] | 619 | case FACE_ACQUIRED_TOO_MUCH_MOTION: |
| 620 | return context.getString(R.string.face_acquired_too_much_motion); |
| 621 | case FACE_ACQUIRED_RECALIBRATE: |
| 622 | return context.getString(R.string.face_acquired_recalibrate); |
| 623 | case FACE_ACQUIRED_TOO_DIFFERENT: |
| 624 | return context.getString(R.string.face_acquired_too_different); |
| 625 | case FACE_ACQUIRED_TOO_SIMILAR: |
| 626 | return context.getString(R.string.face_acquired_too_similar); |
| 627 | case FACE_ACQUIRED_PAN_TOO_EXTREME: |
| 628 | return context.getString(R.string.face_acquired_pan_too_extreme); |
| 629 | case FACE_ACQUIRED_TILT_TOO_EXTREME: |
| 630 | return context.getString(R.string.face_acquired_tilt_too_extreme); |
| 631 | case FACE_ACQUIRED_ROLL_TOO_EXTREME: |
| 632 | return context.getString(R.string.face_acquired_roll_too_extreme); |
| 633 | case FACE_ACQUIRED_FACE_OBSCURED: |
| 634 | return context.getString(R.string.face_acquired_obscured); |
| 635 | case FACE_ACQUIRED_START: |
| 636 | return null; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 637 | case FACE_ACQUIRED_VENDOR: { |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 638 | String[] msgArray = context.getResources().getStringArray( |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 639 | R.array.face_acquired_vendor); |
| 640 | if (vendorCode < msgArray.length) { |
| 641 | return msgArray[vendorCode]; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | Slog.w(TAG, "Invalid acquired message: " + acquireInfo + ", " + vendorCode); |
| 646 | return null; |
| 647 | } |
| 648 | |
| 649 | /** |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 650 | * Used so BiometricPrompt can map the face ones onto existing public constants. |
| 651 | * @hide |
| 652 | */ |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 653 | public static int getMappedAcquiredInfo(int acquireInfo, int vendorCode) { |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 654 | switch (acquireInfo) { |
| 655 | case FACE_ACQUIRED_GOOD: |
| 656 | return BiometricConstants.BIOMETRIC_ACQUIRED_GOOD; |
| 657 | case FACE_ACQUIRED_INSUFFICIENT: |
| 658 | case FACE_ACQUIRED_TOO_BRIGHT: |
| 659 | case FACE_ACQUIRED_TOO_DARK: |
| 660 | return BiometricConstants.BIOMETRIC_ACQUIRED_INSUFFICIENT; |
| 661 | case FACE_ACQUIRED_TOO_CLOSE: |
| 662 | case FACE_ACQUIRED_TOO_FAR: |
| 663 | case FACE_ACQUIRED_TOO_HIGH: |
| 664 | case FACE_ACQUIRED_TOO_LOW: |
| 665 | case FACE_ACQUIRED_TOO_RIGHT: |
| 666 | case FACE_ACQUIRED_TOO_LEFT: |
| 667 | return BiometricConstants.BIOMETRIC_ACQUIRED_PARTIAL; |
| 668 | case FACE_ACQUIRED_POOR_GAZE: |
| 669 | case FACE_ACQUIRED_NOT_DETECTED: |
| 670 | case FACE_ACQUIRED_TOO_MUCH_MOTION: |
| 671 | case FACE_ACQUIRED_RECALIBRATE: |
| 672 | return BiometricConstants.BIOMETRIC_ACQUIRED_INSUFFICIENT; |
| 673 | case FACE_ACQUIRED_VENDOR: |
| 674 | return BiometricConstants.BIOMETRIC_ACQUIRED_VENDOR_BASE + vendorCode; |
| 675 | default: |
| 676 | return BiometricConstants.BIOMETRIC_ACQUIRED_GOOD; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /** |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 681 | * Container for callback data from {@link FaceManager#authenticate(CryptoObject, |
| 682 | * CancellationSignal, int, AuthenticationCallback, Handler)}. |
| 683 | */ |
| 684 | public static class AuthenticationResult { |
| 685 | private Face mFace; |
| 686 | private CryptoObject mCryptoObject; |
| 687 | private int mUserId; |
| 688 | |
| 689 | /** |
| 690 | * Authentication result |
| 691 | * |
| 692 | * @param crypto the crypto object |
| 693 | * @param face the recognized face data, if allowed. |
| 694 | * @hide |
| 695 | */ |
| 696 | public AuthenticationResult(CryptoObject crypto, Face face, int userId) { |
| 697 | mCryptoObject = crypto; |
| 698 | mFace = face; |
| 699 | mUserId = userId; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Obtain the crypto object associated with this transaction |
| 704 | * |
| 705 | * @return crypto object provided to {@link FaceManager#authenticate |
| 706 | * (CryptoObject, |
| 707 | * CancellationSignal, int, AuthenticationCallback, Handler)}. |
| 708 | */ |
| 709 | public CryptoObject getCryptoObject() { |
| 710 | return mCryptoObject; |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Obtain the Face associated with this operation. Applications are strongly |
| 715 | * discouraged from associating specific faces with specific applications or operations. |
| 716 | * |
| 717 | * @hide |
| 718 | */ |
| 719 | public Face getFace() { |
| 720 | return mFace; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Obtain the userId for which this face was authenticated. |
| 725 | * |
| 726 | * @hide |
| 727 | */ |
| 728 | public int getUserId() { |
| 729 | return mUserId; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Callback structure provided to {@link FaceManager#authenticate(CryptoObject, |
| 735 | * CancellationSignal, int, AuthenticationCallback, Handler)}. Users of {@link |
| 736 | * FaceManager#authenticate(CryptoObject, CancellationSignal, |
| 737 | * int, AuthenticationCallback, Handler) } must provide an implementation of this for listening |
| 738 | * to face events. |
| 739 | */ |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 740 | public abstract static class AuthenticationCallback |
| 741 | extends BiometricAuthenticator.AuthenticationCallback { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 742 | |
| 743 | /** |
| 744 | * Called when an unrecoverable error has been encountered and the operation is complete. |
| 745 | * No further callbacks will be made on this object. |
| 746 | * |
| 747 | * @param errorCode An integer identifying the error message |
| 748 | * @param errString A human-readable error string that can be shown in UI |
| 749 | */ |
| 750 | public void onAuthenticationError(int errorCode, CharSequence errString) { |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Called when a recoverable error has been encountered during authentication. The help |
| 755 | * string is provided to give the user guidance for what went wrong, such as |
| 756 | * "Sensor dirty, please clean it." |
| 757 | * |
| 758 | * @param helpCode An integer identifying the error message |
| 759 | * @param helpString A human-readable string that can be shown in UI |
| 760 | */ |
| 761 | public void onAuthenticationHelp(int helpCode, CharSequence helpString) { |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Called when a face is recognized. |
| 766 | * |
| 767 | * @param result An object containing authentication-related data |
| 768 | */ |
| 769 | public void onAuthenticationSucceeded(AuthenticationResult result) { |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Called when a face is detected but not recognized. |
| 774 | */ |
| 775 | public void onAuthenticationFailed() { |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Called when a face image has been acquired, but wasn't processed yet. |
| 780 | * |
| 781 | * @param acquireInfo one of FACE_ACQUIRED_* constants |
| 782 | * @hide |
| 783 | */ |
| 784 | public void onAuthenticationAcquired(int acquireInfo) { |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Callback structure provided to {@link FaceManager#enroll(long, |
| 790 | * EnrollmentCallback, CancellationSignal, int). Users of {@link #FaceAuthenticationManager()} |
| 791 | * must provide an implementation of this to {@link FaceManager#enroll(long, |
| 792 | * CancellationSignal, int, EnrollmentCallback) for listening to face enrollment events. |
| 793 | * |
| 794 | * @hide |
| 795 | */ |
| 796 | public abstract static class EnrollmentCallback { |
| 797 | |
| 798 | /** |
| 799 | * Called when an unrecoverable error has been encountered and the operation is complete. |
| 800 | * No further callbacks will be made on this object. |
| 801 | * |
| 802 | * @param errMsgId An integer identifying the error message |
| 803 | * @param errString A human-readable error string that can be shown in UI |
| 804 | */ |
| 805 | public void onEnrollmentError(int errMsgId, CharSequence errString) { |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Called when a recoverable error has been encountered during enrollment. The help |
| 810 | * string is provided to give the user guidance for what went wrong, such as |
| 811 | * "Image too dark, uncover light source" or what they need to do next, such as |
| 812 | * "Rotate face up / down." |
| 813 | * |
| 814 | * @param helpMsgId An integer identifying the error message |
| 815 | * @param helpString A human-readable string that can be shown in UI |
| 816 | */ |
| 817 | public void onEnrollmentHelp(int helpMsgId, CharSequence helpString) { |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Called as each enrollment step progresses. Enrollment is considered complete when |
| 822 | * remaining reaches 0. This function will not be called if enrollment fails. See |
| 823 | * {@link EnrollmentCallback#onEnrollmentError(int, CharSequence)} |
| 824 | * |
| 825 | * @param remaining The number of remaining steps |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 826 | */ |
Kevin Chyn | 8277f00 | 2018-07-02 15:14:44 -0700 | [diff] [blame] | 827 | public void onEnrollmentProgress(int remaining) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Callback structure provided to {@link #remove}. Users of {@link FaceManager} |
| 833 | * may |
| 834 | * optionally provide an implementation of this to |
| 835 | * {@link #remove(Face, int, RemovalCallback)} for listening to face template |
| 836 | * removal events. |
| 837 | * |
| 838 | * @hide |
| 839 | */ |
| 840 | public abstract static class RemovalCallback { |
| 841 | |
| 842 | /** |
| 843 | * Called when the given face can't be removed. |
| 844 | * |
| 845 | * @param face The face that the call attempted to remove |
| 846 | * @param errMsgId An associated error message id |
| 847 | * @param errString An error message indicating why the face id can't be removed |
| 848 | */ |
| 849 | public void onRemovalError(Face face, int errMsgId, CharSequence errString) { |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * Called when a given face is successfully removed. |
| 854 | * |
| 855 | * @param face The face template that was removed. |
| 856 | */ |
Kevin Chyn | 7e88d113 | 2018-06-29 12:55:22 -0700 | [diff] [blame] | 857 | public void onRemovalSucceeded(Face face, int remaining) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * @hide |
| 863 | */ |
| 864 | public abstract static class LockoutResetCallback { |
| 865 | |
| 866 | /** |
| 867 | * Called when lockout period expired and clients are allowed to listen for face |
| 868 | * authentication |
| 869 | * again. |
| 870 | */ |
| 871 | public void onLockoutReset() { |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | private class OnEnrollCancelListener implements OnCancelListener { |
| 876 | @Override |
| 877 | public void onCancel() { |
| 878 | cancelEnrollment(); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | private class OnAuthenticationCancelListener implements OnCancelListener { |
| 883 | private CryptoObject mCrypto; |
| 884 | |
| 885 | OnAuthenticationCancelListener(CryptoObject crypto) { |
| 886 | mCrypto = crypto; |
| 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public void onCancel() { |
| 891 | cancelAuthentication(mCrypto); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | private class MyHandler extends Handler { |
| 896 | private MyHandler(Context context) { |
| 897 | super(context.getMainLooper()); |
| 898 | } |
| 899 | |
| 900 | private MyHandler(Looper looper) { |
| 901 | super(looper); |
| 902 | } |
| 903 | |
| 904 | @Override |
| 905 | public void handleMessage(android.os.Message msg) { |
| 906 | switch (msg.what) { |
| 907 | case MSG_ENROLL_RESULT: |
Kevin Chyn | 8277f00 | 2018-07-02 15:14:44 -0700 | [diff] [blame] | 908 | sendEnrollResult((Face) msg.obj, msg.arg1 /* remaining */); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 909 | break; |
| 910 | case MSG_ACQUIRED: |
| 911 | sendAcquiredResult((Long) msg.obj /* deviceId */, msg.arg1 /* acquire info */, |
| 912 | msg.arg2 /* vendorCode */); |
| 913 | break; |
| 914 | case MSG_AUTHENTICATION_SUCCEEDED: |
| 915 | sendAuthenticatedSucceeded((Face) msg.obj, msg.arg1 /* userId */); |
| 916 | break; |
| 917 | case MSG_AUTHENTICATION_FAILED: |
| 918 | sendAuthenticatedFailed(); |
| 919 | break; |
| 920 | case MSG_ERROR: |
| 921 | sendErrorResult((Long) msg.obj /* deviceId */, msg.arg1 /* errMsgId */, |
| 922 | msg.arg2 /* vendorCode */); |
| 923 | break; |
| 924 | case MSG_REMOVED: |
Kevin Chyn | 7e88d113 | 2018-06-29 12:55:22 -0700 | [diff] [blame] | 925 | sendRemovedResult((Face) msg.obj, msg.arg1 /* remaining */); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 926 | break; |
| 927 | } |
| 928 | } |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 929 | }; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 930 | |
Kevin Chyn | 7e88d113 | 2018-06-29 12:55:22 -0700 | [diff] [blame] | 931 | private void sendRemovedResult(Face face, int remaining) { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 932 | if (mRemovalCallback == null) { |
| 933 | return; |
| 934 | } |
| 935 | if (face == null) { |
| 936 | Log.e(TAG, "Received MSG_REMOVED, but face is null"); |
| 937 | return; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 938 | } |
Kevin Chyn | 7e88d113 | 2018-06-29 12:55:22 -0700 | [diff] [blame] | 939 | mRemovalCallback.onRemovalSucceeded(face, remaining); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 940 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 941 | |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 942 | private void sendErrorResult(long deviceId, int errMsgId, int vendorCode) { |
| 943 | // emulate HAL 2.1 behavior and send real errMsgId |
| 944 | final int clientErrMsgId = errMsgId == FACE_ERROR_VENDOR |
| 945 | ? (vendorCode + FACE_ERROR_VENDOR_BASE) : errMsgId; |
| 946 | if (mEnrollmentCallback != null) { |
| 947 | mEnrollmentCallback.onEnrollmentError(clientErrMsgId, |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 948 | getErrorString(mContext, errMsgId, vendorCode)); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 949 | } else if (mAuthenticationCallback != null) { |
| 950 | mAuthenticationCallback.onAuthenticationError(clientErrMsgId, |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 951 | getErrorString(mContext, errMsgId, vendorCode)); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 952 | } else if (mRemovalCallback != null) { |
| 953 | mRemovalCallback.onRemovalError(mRemovalFace, clientErrMsgId, |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 954 | getErrorString(mContext, errMsgId, vendorCode)); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 955 | } |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 956 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 957 | |
Kevin Chyn | 8277f00 | 2018-07-02 15:14:44 -0700 | [diff] [blame] | 958 | private void sendEnrollResult(Face face, int remaining) { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 959 | if (mEnrollmentCallback != null) { |
Kevin Chyn | 8277f00 | 2018-07-02 15:14:44 -0700 | [diff] [blame] | 960 | mEnrollmentCallback.onEnrollmentProgress(remaining); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 961 | } |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 962 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 963 | |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 964 | private void sendAuthenticatedSucceeded(Face face, int userId) { |
| 965 | if (mAuthenticationCallback != null) { |
Kevin Chyn | a24e9fd | 2018-08-27 12:39:17 -0700 | [diff] [blame] | 966 | final AuthenticationResult result = |
| 967 | new AuthenticationResult(mCryptoObject, face, userId); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 968 | mAuthenticationCallback.onAuthenticationSucceeded(result); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | private void sendAuthenticatedFailed() { |
| 973 | if (mAuthenticationCallback != null) { |
| 974 | mAuthenticationCallback.onAuthenticationFailed(); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | private void sendAcquiredResult(long deviceId, int acquireInfo, int vendorCode) { |
| 979 | if (mAuthenticationCallback != null) { |
| 980 | mAuthenticationCallback.onAuthenticationAcquired(acquireInfo); |
| 981 | } |
Kevin Chyn | 8b7a037 | 2018-09-17 15:06:05 -0700 | [diff] [blame] | 982 | final String msg = getAcquiredString(mContext, acquireInfo, vendorCode); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 983 | final int clientInfo = acquireInfo == FACE_ACQUIRED_VENDOR |
| 984 | ? (vendorCode + FACE_ACQUIRED_VENDOR_BASE) : acquireInfo; |
| 985 | if (mEnrollmentCallback != null) { |
| 986 | mEnrollmentCallback.onEnrollmentHelp(clientInfo, msg); |
Kevin Chyn | ffef714 | 2018-12-27 16:28:48 -0800 | [diff] [blame] | 987 | } else if (mAuthenticationCallback != null && msg != null) { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 988 | mAuthenticationCallback.onAuthenticationHelp(clientInfo, msg); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 989 | } |
| 990 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 991 | } |