blob: fe006045bf0fb2e91ba59c6a94a2e69e37c84adc [file] [log] [blame]
Kevin Chyn05c21502018-09-18 13:07:19 -07001/*
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
17package android.hardware.biometrics;
18
19import static android.Manifest.permission.USE_BIOMETRIC;
Kevin Chynb7b54a62018-09-28 18:48:12 -070020import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL;
Kevin Chyn05c21502018-09-18 13:07:19 -070021
Kevin Chynb97e9c92018-10-03 20:45:27 -070022import android.annotation.IntDef;
Kevin Chyn05c21502018-09-18 13:07:19 -070023import android.annotation.RequiresPermission;
24import android.content.Context;
25import android.os.RemoteException;
Kevin Chyne7411422018-09-27 17:28:20 -070026import android.util.Slog;
Kevin Chyn05c21502018-09-18 13:07:19 -070027
28/**
29 * A class that contains biometric utilities. For authentication, see {@link BiometricPrompt}.
30 */
31public class BiometricManager {
32
Kevin Chyne7411422018-09-27 17:28:20 -070033 private static final String TAG = "BiometricManager";
34
35 /**
36 * No error detected.
37 */
Kevin Chyna8b57ef2018-10-25 11:09:23 -070038 public static final int BIOMETRIC_SUCCESS =
39 BiometricConstants.BIOMETRIC_SUCCESS;
Kevin Chyne7411422018-09-27 17:28:20 -070040
41 /**
42 * The hardware is unavailable. Try again later.
43 */
Kevin Chyna8b57ef2018-10-25 11:09:23 -070044 public static final int BIOMETRIC_ERROR_UNAVAILABLE =
45 BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE;
Kevin Chyne7411422018-09-27 17:28:20 -070046
47 /**
48 * The user does not have any biometrics enrolled.
49 */
Kevin Chyna8b57ef2018-10-25 11:09:23 -070050 public static final int BIOMETRIC_ERROR_NO_BIOMETRICS =
51 BiometricConstants.BIOMETRIC_ERROR_NO_BIOMETRICS;
Kevin Chyne7411422018-09-27 17:28:20 -070052
Kevin Chyn811ae8b2018-10-03 23:24:03 -070053 /**
54 * There is no biometric hardware.
55 */
Kevin Chyna8b57ef2018-10-25 11:09:23 -070056 public static final int BIOMETRIC_ERROR_NO_HARDWARE =
57 BiometricConstants.BIOMETRIC_ERROR_HW_NOT_PRESENT;
Kevin Chyn811ae8b2018-10-03 23:24:03 -070058
Kevin Chyna8b57ef2018-10-25 11:09:23 -070059 @IntDef({BIOMETRIC_SUCCESS,
60 BIOMETRIC_ERROR_UNAVAILABLE,
61 BIOMETRIC_ERROR_NO_BIOMETRICS,
62 BIOMETRIC_ERROR_NO_HARDWARE})
Kevin Chynb97e9c92018-10-03 20:45:27 -070063 @interface BiometricError {}
64
Kevin Chyn05c21502018-09-18 13:07:19 -070065 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 Chyne7411422018-09-27 17:28:20 -070079 * 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 Chyn05c21502018-09-18 13:07:19 -070081 *
Kevin Chyna8b57ef2018-10-25 11:09:23 -070082 * @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 Chyn05c21502018-09-18 13:07:19 -070086 */
87 @RequiresPermission(USE_BIOMETRIC)
Kevin Chynb97e9c92018-10-03 20:45:27 -070088 public @BiometricError int canAuthenticate() {
Kevin Chyne7411422018-09-27 17:28:20 -070089 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 Chyna8b57ef2018-10-25 11:09:23 -070097 return BIOMETRIC_ERROR_UNAVAILABLE;
Kevin Chyn05c21502018-09-18 13:07:19 -070098 }
99 }
Kevin Chynb7b54a62018-09-28 18:48:12 -0700100
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 Chynbf830a32018-10-07 15:58:46 -0700118
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 Chyn05c21502018-09-18 13:07:19 -0700135}
Kevin Chynbf830a32018-10-07 15:58:46 -0700136