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.content.Context; 20 import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprint; 21 import android.hardware.fingerprint.FingerprintManager; 22 import android.hardware.fingerprint.IFingerprintServiceReceiver; 23 import android.os.IBinder; 24 import android.os.RemoteException; 25 import android.util.Slog; 26 27 import com.android.internal.logging.MetricsLogger; 28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 29 30 import java.util.Arrays; 31 32 /** 33 * A class to keep track of the enrollment state for a given client. 34 */ 35 public abstract class EnrollClient extends ClientMonitor { 36 private static final long MS_PER_SEC = 1000; 37 private static final int ENROLLMENT_TIMEOUT_MS = 60 * 1000; // 1 minute 38 private byte[] mCryptoToken; 39 40 public EnrollClient(Context context, long halDeviceId, IBinder token, 41 IFingerprintServiceReceiver receiver, int userId, int groupId, byte [] cryptoToken, 42 boolean restricted, String owner) { 43 super(context, halDeviceId, token, receiver, userId, groupId, restricted, owner); 44 mCryptoToken = Arrays.copyOf(cryptoToken, cryptoToken.length); 45 } 46 47 @Override 48 public boolean onEnrollResult(int fingerId, int groupId, int remaining) { 49 if (groupId != getGroupId()) { 50 Slog.w(TAG, "groupId != getGroupId(), groupId: " + groupId + 51 " getGroupId():" + getGroupId()); 52 } 53 if (remaining == 0) { 54 FingerprintUtils.getInstance().addFingerprintForUser(getContext(), fingerId, 55 getTargetUserId()); 56 } 57 return sendEnrollResult(fingerId, groupId, remaining); 58 } 59 60 /* 61 * @return true if we're done. 62 */ 63 private boolean sendEnrollResult(int fpId, int groupId, int remaining) { 64 IFingerprintServiceReceiver receiver = getReceiver(); 65 if (receiver == null) 66 return true; // client not listening 67 68 FingerprintUtils.vibrateFingerprintSuccess(getContext()); 69 MetricsLogger.action(getContext(), MetricsEvent.ACTION_FINGERPRINT_ENROLL); 70 try { 71 receiver.onEnrollResult(getHalDeviceId(), fpId, groupId, remaining); 72 return remaining == 0; 73 } catch (RemoteException e) { 74 Slog.w(TAG, "Failed to notify EnrollResult:", e); 75 return true; 76 } 77 } 78 79 @Override 80 public int start() { 81 IBiometricsFingerprint daemon = getFingerprintDaemon(); 82 if (daemon == null) { 83 Slog.w(TAG, "enroll: no fingerprint HAL!"); 84 return ERROR_ESRCH; 85 } 86 final int timeout = (int) (ENROLLMENT_TIMEOUT_MS / MS_PER_SEC); 87 try { 88 final int result = daemon.enroll(mCryptoToken, getGroupId(), timeout); 89 if (result != 0) { 90 Slog.w(TAG, "startEnroll failed, result=" + result); 91 MetricsLogger.histogram(getContext(), "fingerprintd_enroll_start_error", result); 92 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */); 93 return result; 94 } 95 } catch (RemoteException e) { 96 Slog.e(TAG, "startEnroll failed", e); 97 } 98 return 0; // success 99 } 100 101 @Override 102 public int stop(boolean initiatedByClient) { 103 if (mAlreadyCancelled) { 104 Slog.w(TAG, "stopEnroll: already cancelled!"); 105 return 0; 106 } 107 IBiometricsFingerprint daemon = getFingerprintDaemon(); 108 if (daemon == null) { 109 Slog.w(TAG, "stopEnrollment: no fingerprint HAL!"); 110 return ERROR_ESRCH; 111 } 112 try { 113 final int result = daemon.cancel(); 114 if (result != 0) { 115 Slog.w(TAG, "startEnrollCancel failed, result = " + result); 116 return result; 117 } 118 } catch (RemoteException e) { 119 Slog.e(TAG, "stopEnrollment failed", e); 120 } 121 if (initiatedByClient) { 122 onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED, 0 /* vendorCode */); 123 } 124 mAlreadyCancelled = true; 125 return 0; 126 } 127 128 @Override 129 public boolean onRemoved(int fingerId, int groupId, int remaining) { 130 if (DEBUG) Slog.w(TAG, "onRemoved() called for enroll!"); 131 return true; // Invalid for EnrollClient 132 } 133 134 @Override 135 public boolean onEnumerationResult(int fingerId, int groupId, int remaining) { 136 if (DEBUG) Slog.w(TAG, "onEnumerationResult() called for enroll!"); 137 return true; // Invalid for EnrollClient 138 } 139 140 @Override 141 public boolean onAuthenticated(int fingerId, int groupId) { 142 if (DEBUG) Slog.w(TAG, "onAuthenticated() called for enroll!"); 143 return true; // Invalid for EnrollClient 144 } 145 146 } 147