Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2016 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.server.fingerprint; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.content.Context; |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 21 | import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprint; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 22 | import android.hardware.fingerprint.FingerprintManager; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 23 | import android.hardware.fingerprint.IFingerprintServiceReceiver; |
Beverly | d9ec6df | 2018-03-20 17:19:07 -0400 | [diff] [blame] | 24 | import android.media.AudioAttributes; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 27 | import android.os.VibrationEffect; |
| 28 | import android.os.Vibrator; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
| 30 | |
| 31 | import java.util.NoSuchElementException; |
| 32 | |
| 33 | /** |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 34 | * Abstract base class for keeping track and dispatching events from fingerprint HAL to the |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 35 | * the current client. Subclasses are responsible for coordinating the interaction with |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 36 | * fingerprint HAL for the specific action (e.g. authenticate, enroll, enumerate, etc.). |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 37 | */ |
| 38 | public abstract class ClientMonitor implements IBinder.DeathRecipient { |
| 39 | protected static final String TAG = FingerprintService.TAG; // TODO: get specific name |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 40 | protected static final int ERROR_ESRCH = 3; // Likely fingerprint HAL is dead. See errno.h. |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 41 | protected static final boolean DEBUG = FingerprintService.DEBUG; |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 42 | private static final long[] DEFAULT_SUCCESS_VIBRATION_PATTERN = new long[] {0, 30}; |
Beverly | d9ec6df | 2018-03-20 17:19:07 -0400 | [diff] [blame] | 43 | private static final AudioAttributes FINGERPRINT_SONFICATION_ATTRIBUTES = |
| 44 | new AudioAttributes.Builder() |
| 45 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 46 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 47 | .build(); |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 48 | private final Context mContext; |
| 49 | private final long mHalDeviceId; |
| 50 | private final int mTargetUserId; |
| 51 | private final int mGroupId; |
| 52 | // True if client does not have MANAGE_FINGERPRINT permission |
| 53 | private final boolean mIsRestricted; |
| 54 | private final String mOwner; |
| 55 | private final VibrationEffect mSuccessVibrationEffect; |
| 56 | private final VibrationEffect mErrorVibrationEffect; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 57 | private IBinder mToken; |
| 58 | private IFingerprintServiceReceiver mReceiver; |
Kevin Chyn | 3736858 | 2017-05-19 17:15:38 -0700 | [diff] [blame] | 59 | protected boolean mAlreadyCancelled; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * @param context context of FingerprintService |
| 63 | * @param halDeviceId the HAL device ID of the associated fingerprint hardware |
| 64 | * @param token a unique token for the client |
| 65 | * @param receiver recipient of related events (e.g. authentication) |
Jim Miller | 8f2aca0 | 2016-04-20 13:34:11 -0700 | [diff] [blame] | 66 | * @param userId target user id for operation |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 67 | * @param groupId groupId for the fingerprint set |
| 68 | * @param restricted whether or not client has the {@link Manifest#MANAGE_FINGERPRINT} |
| 69 | * permission |
| 70 | * @param owner name of the client that owns this |
| 71 | */ |
| 72 | public ClientMonitor(Context context, long halDeviceId, IBinder token, |
Jim Miller | 8f2aca0 | 2016-04-20 13:34:11 -0700 | [diff] [blame] | 73 | IFingerprintServiceReceiver receiver, int userId, int groupId,boolean restricted, |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 74 | String owner) { |
| 75 | mContext = context; |
| 76 | mHalDeviceId = halDeviceId; |
| 77 | mToken = token; |
| 78 | mReceiver = receiver; |
Jim Miller | 8f2aca0 | 2016-04-20 13:34:11 -0700 | [diff] [blame] | 79 | mTargetUserId = userId; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 80 | mGroupId = groupId; |
| 81 | mIsRestricted = restricted; |
| 82 | mOwner = owner; |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 83 | mSuccessVibrationEffect = getSuccessVibrationEffect(context); |
| 84 | mErrorVibrationEffect = VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 85 | try { |
Jim Miller | 7e1cb55 | 2017-02-27 17:37:32 -0800 | [diff] [blame] | 86 | if (token != null) { |
| 87 | token.linkToDeath(this, 0); |
| 88 | } |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 89 | } catch (RemoteException e) { |
| 90 | Slog.w(TAG, "caught remote exception in linkToDeath: ", e); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 95 | * Contacts fingerprint HAL to start the client. |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 96 | * @return 0 on success, errno from driver on failure |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 97 | */ |
| 98 | public abstract int start(); |
| 99 | |
| 100 | /** |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 101 | * Contacts fingerprint HAL to stop the client. |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 102 | * @param initiatedByClient whether the operation is at the request of a client |
| 103 | */ |
| 104 | public abstract int stop(boolean initiatedByClient); |
| 105 | |
| 106 | /** |
| 107 | * Method to explicitly poke powermanager on events |
| 108 | */ |
| 109 | public abstract void notifyUserActivity(); |
| 110 | |
| 111 | /** |
| 112 | * Gets the fingerprint daemon from the cached state in the container class. |
| 113 | */ |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 114 | public abstract IBiometricsFingerprint getFingerprintDaemon(); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 115 | |
| 116 | // Event callbacks from driver. Inappropriate calls is flagged/logged by the |
| 117 | // respective client (e.g. enrolling shouldn't get authenticate events). |
| 118 | // All of these return 'true' if the operation is completed and it's ok to move |
| 119 | // to the next client (e.g. authentication accepts or rejects a fingerprint). |
| 120 | public abstract boolean onEnrollResult(int fingerId, int groupId, int rem); |
| 121 | public abstract boolean onAuthenticated(int fingerId, int groupId); |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 122 | public abstract boolean onRemoved(int fingerId, int groupId, int remaining); |
| 123 | public abstract boolean onEnumerationResult(int fingerId, int groupId, int remaining); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 126 | * Called when we get notification from fingerprint HAL that an image has been acquired. |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 127 | * Common to authenticate and enroll. |
| 128 | * @param acquiredInfo info about the current image acquisition |
| 129 | * @return true if client should be removed |
| 130 | */ |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 131 | public boolean onAcquired(int acquiredInfo, int vendorCode) { |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 132 | if (mReceiver == null) |
| 133 | return true; // client not connected |
| 134 | try { |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 135 | mReceiver.onAcquired(getHalDeviceId(), acquiredInfo, vendorCode); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 136 | return false; // acquisition continues... |
| 137 | } catch (RemoteException e) { |
| 138 | Slog.w(TAG, "Failed to invoke sendAcquired:", e); |
| 139 | return true; // client failed |
| 140 | } finally { |
| 141 | // Good scans will keep the device awake |
| 142 | if (acquiredInfo == FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) { |
| 143 | notifyUserActivity(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
Kevin Chyn | 80e40cc | 2017-03-14 12:31:17 -0700 | [diff] [blame] | 149 | * Called when we get notification from fingerprint HAL that an error has occurred with the |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 150 | * current operation. Common to authenticate, enroll, enumerate and remove. |
| 151 | * @param error |
| 152 | * @return true if client should be removed |
| 153 | */ |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 154 | public boolean onError(int error, int vendorCode) { |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 155 | if (mReceiver != null) { |
| 156 | try { |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 157 | mReceiver.onError(getHalDeviceId(), error, vendorCode); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 158 | } catch (RemoteException e) { |
| 159 | Slog.w(TAG, "Failed to invoke sendError:", e); |
| 160 | } |
| 161 | } |
| 162 | return true; // errors always remove current client |
| 163 | } |
| 164 | |
| 165 | public void destroy() { |
| 166 | if (mToken != null) { |
| 167 | try { |
| 168 | mToken.unlinkToDeath(this, 0); |
| 169 | } catch (NoSuchElementException e) { |
| 170 | // TODO: remove when duplicate call bug is found |
| 171 | Slog.e(TAG, "destroy(): " + this + ":", new Exception("here")); |
| 172 | } |
| 173 | mToken = null; |
| 174 | } |
| 175 | mReceiver = null; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void binderDied() { |
| 180 | mToken = null; |
| 181 | mReceiver = null; |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 182 | onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | protected void finalize() throws Throwable { |
| 187 | try { |
| 188 | if (mToken != null) { |
| 189 | if (DEBUG) Slog.w(TAG, "removing leaked reference: " + mToken); |
Jim Miller | 40e4645 | 2016-12-16 18:38:53 -0800 | [diff] [blame] | 190 | onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 191 | } |
| 192 | } finally { |
| 193 | super.finalize(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public final Context getContext() { |
| 198 | return mContext; |
| 199 | } |
| 200 | |
| 201 | public final long getHalDeviceId() { |
| 202 | return mHalDeviceId; |
| 203 | } |
| 204 | |
| 205 | public final String getOwnerString() { |
| 206 | return mOwner; |
| 207 | } |
| 208 | |
| 209 | public final IFingerprintServiceReceiver getReceiver() { |
| 210 | return mReceiver; |
| 211 | } |
| 212 | |
| 213 | public final boolean getIsRestricted() { |
| 214 | return mIsRestricted; |
| 215 | } |
| 216 | |
Jim Miller | 8f2aca0 | 2016-04-20 13:34:11 -0700 | [diff] [blame] | 217 | public final int getTargetUserId() { |
| 218 | return mTargetUserId; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public final int getGroupId() { |
| 222 | return mGroupId; |
| 223 | } |
| 224 | |
| 225 | public final IBinder getToken() { |
| 226 | return mToken; |
| 227 | } |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 228 | |
| 229 | public final void vibrateSuccess() { |
| 230 | Vibrator vibrator = mContext.getSystemService(Vibrator.class); |
| 231 | if (vibrator != null) { |
Beverly | d9ec6df | 2018-03-20 17:19:07 -0400 | [diff] [blame] | 232 | vibrator.vibrate(mSuccessVibrationEffect, FINGERPRINT_SONFICATION_ATTRIBUTES); |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | public final void vibrateError() { |
| 237 | Vibrator vibrator = mContext.getSystemService(Vibrator.class); |
| 238 | if (vibrator != null) { |
Beverly | d9ec6df | 2018-03-20 17:19:07 -0400 | [diff] [blame] | 239 | vibrator.vibrate(mErrorVibrationEffect, FINGERPRINT_SONFICATION_ATTRIBUTES); |
Michael Wright | 6726fd5 | 2017-06-27 00:41:45 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | private static VibrationEffect getSuccessVibrationEffect(Context ctx) { |
| 244 | int[] arr = ctx.getResources().getIntArray( |
| 245 | com.android.internal.R.array.config_longPressVibePattern); |
| 246 | final long[] vibePattern; |
| 247 | if (arr == null || arr.length == 0) { |
| 248 | vibePattern = DEFAULT_SUCCESS_VIBRATION_PATTERN; |
| 249 | } else { |
| 250 | vibePattern = new long[arr.length]; |
| 251 | for (int i = 0; i < arr.length; i++) { |
| 252 | vibePattern[i] = arr[i]; |
| 253 | } |
| 254 | } |
| 255 | if (vibePattern.length == 1) { |
| 256 | return VibrationEffect.createOneShot( |
| 257 | vibePattern[0], VibrationEffect.DEFAULT_AMPLITUDE); |
| 258 | } else { |
| 259 | return VibrationEffect.createWaveform(vibePattern, -1); |
| 260 | } |
| 261 | } |
| 262 | |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 263 | } |