blob: c9efcf2fd68745a005c949e048458967ae171b23 [file] [log] [blame]
Jim Millercb2ce6f2016-04-13 20:28:18 -07001/**
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.content.Context;
Jim Miller40e46452016-12-16 18:38:53 -080020import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprint;
Jim Millercb2ce6f2016-04-13 20:28:18 -070021import android.hardware.fingerprint.FingerprintManager;
Jim Millercb2ce6f2016-04-13 20:28:18 -070022import android.hardware.fingerprint.IFingerprintServiceReceiver;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.Slog;
26
27import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010028import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jim Millercb2ce6f2016-04-13 20:28:18 -070029
30import java.util.Arrays;
31
32/**
33 * A class to keep track of the enrollment state for a given client.
34 */
35public 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) {
Tony Makff715ac2016-04-19 20:44:12 +010049 if (groupId != getGroupId()) {
50 Slog.w(TAG, "groupId != getGroupId(), groupId: " + groupId +
51 " getGroupId():" + getGroupId());
52 }
Jim Millercb2ce6f2016-04-13 20:28:18 -070053 if (remaining == 0) {
Jim Millerc12eca82016-04-28 15:12:53 -070054 FingerprintUtils.getInstance().addFingerprintForUser(getContext(), fingerId,
55 getTargetUserId());
Jim Millercb2ce6f2016-04-13 20:28:18 -070056 }
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
Michael Wright6726fd52017-06-27 00:41:45 +010068 vibrateSuccess();
Jim Millercb2ce6f2016-04-13 20:28:18 -070069 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() {
Jim Miller40e46452016-12-16 18:38:53 -080081 IBiometricsFingerprint daemon = getFingerprintDaemon();
Jim Millercb2ce6f2016-04-13 20:28:18 -070082 if (daemon == null) {
Kevin Chyn80e40cc2017-03-14 12:31:17 -070083 Slog.w(TAG, "enroll: no fingerprint HAL!");
Jim Millercb2ce6f2016-04-13 20:28:18 -070084 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);
Jim Millerc57c8d92016-09-30 17:17:59 -070091 MetricsLogger.histogram(getContext(), "fingerprintd_enroll_start_error", result);
Jim Miller40e46452016-12-16 18:38:53 -080092 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */);
Jim Millercb2ce6f2016-04-13 20:28:18 -070093 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) {
Kevin Chyn37368582017-05-19 17:15:38 -0700103 if (mAlreadyCancelled) {
104 Slog.w(TAG, "stopEnroll: already cancelled!");
105 return 0;
106 }
Jim Miller40e46452016-12-16 18:38:53 -0800107 IBiometricsFingerprint daemon = getFingerprintDaemon();
Jim Millercb2ce6f2016-04-13 20:28:18 -0700108 if (daemon == null) {
Kevin Chyn80e40cc2017-03-14 12:31:17 -0700109 Slog.w(TAG, "stopEnrollment: no fingerprint HAL!");
Jim Millercb2ce6f2016-04-13 20:28:18 -0700110 return ERROR_ESRCH;
111 }
112 try {
Jim Miller40e46452016-12-16 18:38:53 -0800113 final int result = daemon.cancel();
Jim Millercb2ce6f2016-04-13 20:28:18 -0700114 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) {
Jim Miller40e46452016-12-16 18:38:53 -0800122 onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED, 0 /* vendorCode */);
Jim Millercb2ce6f2016-04-13 20:28:18 -0700123 }
Kevin Chyn37368582017-05-19 17:15:38 -0700124 mAlreadyCancelled = true;
Jim Millercb2ce6f2016-04-13 20:28:18 -0700125 return 0;
126 }
127
128 @Override
Jim Miller40e46452016-12-16 18:38:53 -0800129 public boolean onRemoved(int fingerId, int groupId, int remaining) {
Jim Millercb2ce6f2016-04-13 20:28:18 -0700130 if (DEBUG) Slog.w(TAG, "onRemoved() called for enroll!");
131 return true; // Invalid for EnrollClient
132 }
133
134 @Override
Jim Miller40e46452016-12-16 18:38:53 -0800135 public boolean onEnumerationResult(int fingerId, int groupId, int remaining) {
Jim Millercb2ce6f2016-04-13 20:28:18 -0700136 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}