ClientMonitor.java revision 7e1cb55230055b04e5c6c86deae5412dfe0799a7
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 17package com.android.server.fingerprint; 18 19import android.Manifest; 20import android.content.Context; 21import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprint; 22import android.hardware.fingerprint.FingerprintManager; 23import android.hardware.fingerprint.IFingerprintServiceReceiver; 24import android.os.IBinder; 25import android.os.RemoteException; 26import android.util.Slog; 27 28import java.util.NoSuchElementException; 29 30/** 31 * Abstract base class for keeping track and dispatching events from fingerprintd to the 32 * the current client. Subclasses are responsible for coordinating the interaction with 33 * fingerprintd for the specific action (e.g. authenticate, enroll, enumerate, etc.). 34 */ 35public abstract class ClientMonitor implements IBinder.DeathRecipient { 36 protected static final String TAG = FingerprintService.TAG; // TODO: get specific name 37 protected static final int ERROR_ESRCH = 3; // Likely fingerprintd is dead. See errno.h. 38 protected static final boolean DEBUG = FingerprintService.DEBUG; 39 private IBinder mToken; 40 private IFingerprintServiceReceiver mReceiver; 41 private int mTargetUserId; 42 private int mGroupId; 43 private boolean mIsRestricted; // True if client does not have MANAGE_FINGERPRINT permission 44 private String mOwner; 45 private Context mContext; 46 private long mHalDeviceId; 47 48 /** 49 * @param context context of FingerprintService 50 * @param halDeviceId the HAL device ID of the associated fingerprint hardware 51 * @param token a unique token for the client 52 * @param receiver recipient of related events (e.g. authentication) 53 * @param userId target user id for operation 54 * @param groupId groupId for the fingerprint set 55 * @param restricted whether or not client has the {@link Manifest#MANAGE_FINGERPRINT} 56 * permission 57 * @param owner name of the client that owns this 58 */ 59 public ClientMonitor(Context context, long halDeviceId, IBinder token, 60 IFingerprintServiceReceiver receiver, int userId, int groupId,boolean restricted, 61 String owner) { 62 mContext = context; 63 mHalDeviceId = halDeviceId; 64 mToken = token; 65 mReceiver = receiver; 66 mTargetUserId = userId; 67 mGroupId = groupId; 68 mIsRestricted = restricted; 69 mOwner = owner; 70 try { 71 if (token != null) { 72 token.linkToDeath(this, 0); 73 } 74 } catch (RemoteException e) { 75 Slog.w(TAG, "caught remote exception in linkToDeath: ", e); 76 } 77 } 78 79 /** 80 * Contacts fingerprintd to start the client. 81 * @return 0 on succes, errno from driver on failure 82 */ 83 public abstract int start(); 84 85 /** 86 * Contacts fingerprintd to stop the client. 87 * @param initiatedByClient whether the operation is at the request of a client 88 */ 89 public abstract int stop(boolean initiatedByClient); 90 91 /** 92 * Method to explicitly poke powermanager on events 93 */ 94 public abstract void notifyUserActivity(); 95 96 /** 97 * Gets the fingerprint daemon from the cached state in the container class. 98 */ 99 public abstract IBiometricsFingerprint getFingerprintDaemon(); 100 101 // Event callbacks from driver. Inappropriate calls is flagged/logged by the 102 // respective client (e.g. enrolling shouldn't get authenticate events). 103 // All of these return 'true' if the operation is completed and it's ok to move 104 // to the next client (e.g. authentication accepts or rejects a fingerprint). 105 public abstract boolean onEnrollResult(int fingerId, int groupId, int rem); 106 public abstract boolean onAuthenticated(int fingerId, int groupId); 107 public abstract boolean onRemoved(int fingerId, int groupId, int remaining); 108 public abstract boolean onEnumerationResult(int fingerId, int groupId, int remaining); 109 110 /** 111 * Called when we get notification from fingerprintd that an image has been acquired. 112 * Common to authenticate and enroll. 113 * @param acquiredInfo info about the current image acquisition 114 * @return true if client should be removed 115 */ 116 public boolean onAcquired(int acquiredInfo, int vendorCode) { 117 if (mReceiver == null) 118 return true; // client not connected 119 try { 120 mReceiver.onAcquired(getHalDeviceId(), acquiredInfo, vendorCode); 121 return false; // acquisition continues... 122 } catch (RemoteException e) { 123 Slog.w(TAG, "Failed to invoke sendAcquired:", e); 124 return true; // client failed 125 } finally { 126 // Good scans will keep the device awake 127 if (acquiredInfo == FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) { 128 notifyUserActivity(); 129 } 130 } 131 } 132 133 /** 134 * Called when we get notification from fingerprintd that an error has occurred with the 135 * current operation. Common to authenticate, enroll, enumerate and remove. 136 * @param error 137 * @return true if client should be removed 138 */ 139 public boolean onError(int error, int vendorCode) { 140 if (mReceiver != null) { 141 try { 142 mReceiver.onError(getHalDeviceId(), error, vendorCode); 143 } catch (RemoteException e) { 144 Slog.w(TAG, "Failed to invoke sendError:", e); 145 } 146 } 147 return true; // errors always remove current client 148 } 149 150 public void destroy() { 151 if (mToken != null) { 152 try { 153 mToken.unlinkToDeath(this, 0); 154 } catch (NoSuchElementException e) { 155 // TODO: remove when duplicate call bug is found 156 Slog.e(TAG, "destroy(): " + this + ":", new Exception("here")); 157 } 158 mToken = null; 159 } 160 mReceiver = null; 161 } 162 163 @Override 164 public void binderDied() { 165 mToken = null; 166 mReceiver = null; 167 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */); 168 } 169 170 @Override 171 protected void finalize() throws Throwable { 172 try { 173 if (mToken != null) { 174 if (DEBUG) Slog.w(TAG, "removing leaked reference: " + mToken); 175 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */); 176 } 177 } finally { 178 super.finalize(); 179 } 180 } 181 182 public final Context getContext() { 183 return mContext; 184 } 185 186 public final long getHalDeviceId() { 187 return mHalDeviceId; 188 } 189 190 public final String getOwnerString() { 191 return mOwner; 192 } 193 194 public final IFingerprintServiceReceiver getReceiver() { 195 return mReceiver; 196 } 197 198 public final boolean getIsRestricted() { 199 return mIsRestricted; 200 } 201 202 public final int getTargetUserId() { 203 return mTargetUserId; 204 } 205 206 public final int getGroupId() { 207 return mGroupId; 208 } 209 210 public final IBinder getToken() { 211 return mToken; 212 } 213} 214