Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [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.biometrics; |
| 18 | |
| 19 | import static android.Manifest.permission.USE_BIOMETRIC; |
Kevin Chyn | b7b54a6 | 2018-09-28 18:48:12 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL; |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 21 | |
Kevin Chyn | b97e9c9 | 2018-10-03 20:45:27 -0700 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 23 | import android.annotation.RequiresPermission; |
| 24 | import android.content.Context; |
| 25 | import android.os.RemoteException; |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A class that contains biometric utilities. For authentication, see {@link BiometricPrompt}. |
| 30 | */ |
| 31 | public class BiometricManager { |
| 32 | |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 33 | private static final String TAG = "BiometricManager"; |
| 34 | |
| 35 | /** |
| 36 | * No error detected. |
| 37 | */ |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 38 | public static final int BIOMETRIC_SUCCESS = |
| 39 | BiometricConstants.BIOMETRIC_SUCCESS; |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * The hardware is unavailable. Try again later. |
| 43 | */ |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 44 | public static final int BIOMETRIC_ERROR_UNAVAILABLE = |
| 45 | BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE; |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * The user does not have any biometrics enrolled. |
| 49 | */ |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 50 | public static final int BIOMETRIC_ERROR_NO_BIOMETRICS = |
| 51 | BiometricConstants.BIOMETRIC_ERROR_NO_BIOMETRICS; |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 52 | |
Kevin Chyn | 811ae8b | 2018-10-03 23:24:03 -0700 | [diff] [blame] | 53 | /** |
| 54 | * There is no biometric hardware. |
| 55 | */ |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 56 | public static final int BIOMETRIC_ERROR_NO_HARDWARE = |
| 57 | BiometricConstants.BIOMETRIC_ERROR_HW_NOT_PRESENT; |
Kevin Chyn | 811ae8b | 2018-10-03 23:24:03 -0700 | [diff] [blame] | 58 | |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 59 | @IntDef({BIOMETRIC_SUCCESS, |
| 60 | BIOMETRIC_ERROR_UNAVAILABLE, |
| 61 | BIOMETRIC_ERROR_NO_BIOMETRICS, |
| 62 | BIOMETRIC_ERROR_NO_HARDWARE}) |
Kevin Chyn | b97e9c9 | 2018-10-03 20:45:27 -0700 | [diff] [blame] | 63 | @interface BiometricError {} |
| 64 | |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 65 | private final Context mContext; |
| 66 | private final IBiometricService mService; |
| 67 | |
| 68 | /** |
| 69 | * @hide |
| 70 | * @param context |
| 71 | * @param service |
| 72 | */ |
| 73 | public BiometricManager(Context context, IBiometricService service) { |
| 74 | mContext = context; |
| 75 | mService = service; |
| 76 | } |
| 77 | |
| 78 | /** |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 79 | * Determine if biometrics can be used. In other words, determine if {@link BiometricPrompt} |
| 80 | * can be expected to be shown (hardware available, templates enrolled, user-enabled). |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 81 | * |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 82 | * @return Returns {@link #BIOMETRIC_ERROR_NO_BIOMETRICS} if the user does not have any |
| 83 | * enrolled, or {@link #BIOMETRIC_ERROR_UNAVAILABLE} if none are currently |
| 84 | * supported/enabled. Returns {@link #BIOMETRIC_SUCCESS} if a biometric can currently be |
| 85 | * used (enrolled and available). |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 86 | */ |
| 87 | @RequiresPermission(USE_BIOMETRIC) |
Kevin Chyn | b97e9c9 | 2018-10-03 20:45:27 -0700 | [diff] [blame] | 88 | public @BiometricError int canAuthenticate() { |
Kevin Chyn | e741142 | 2018-09-27 17:28:20 -0700 | [diff] [blame] | 89 | if (mService != null) { |
| 90 | try { |
| 91 | return mService.canAuthenticate(mContext.getOpPackageName()); |
| 92 | } catch (RemoteException e) { |
| 93 | throw e.rethrowFromSystemServer(); |
| 94 | } |
| 95 | } else { |
| 96 | Slog.w(TAG, "hasEnrolledBiometrics(): Service not connected"); |
Kevin Chyn | a8b57ef | 2018-10-25 11:09:23 -0700 | [diff] [blame^] | 97 | return BIOMETRIC_ERROR_UNAVAILABLE; |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
Kevin Chyn | b7b54a6 | 2018-09-28 18:48:12 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Listens for changes to biometric eligibility on keyguard from user settings. |
| 103 | * @param callback |
| 104 | * @hide |
| 105 | */ |
| 106 | @RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 107 | public void registerEnabledOnKeyguardCallback(IBiometricEnabledOnKeyguardCallback callback) { |
| 108 | if (mService != null) { |
| 109 | try { |
| 110 | mService.registerEnabledOnKeyguardCallback(callback); |
| 111 | } catch (RemoteException e) { |
| 112 | throw e.rethrowFromSystemServer(); |
| 113 | } |
| 114 | } else { |
| 115 | Slog.w(TAG, "registerEnabledOnKeyguardCallback(): Service not connected"); |
| 116 | } |
| 117 | } |
Kevin Chyn | bf830a3 | 2018-10-07 15:58:46 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Sets the active user. |
| 121 | * @hide |
| 122 | */ |
| 123 | @RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 124 | public void setActiveUser(int userId) { |
| 125 | if (mService != null) { |
| 126 | try { |
| 127 | mService.setActiveUser(userId); |
| 128 | } catch (RemoteException e) { |
| 129 | throw e.rethrowFromSystemServer(); |
| 130 | } |
| 131 | } else { |
| 132 | Slog.w(TAG, "setActiveUser(): Service not connected"); |
| 133 | } |
| 134 | } |
Kevin Chyn | 05c2150 | 2018-09-18 13:07:19 -0700 | [diff] [blame] | 135 | } |
Kevin Chyn | bf830a3 | 2018-10-07 15:58:46 -0700 | [diff] [blame] | 136 | |