blob: c70ca7f5b28b50f1db7a50c480ec0e8a44c836c4 [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;
20import android.hardware.fingerprint.FingerprintManager;
21import android.hardware.fingerprint.IFingerprintDaemon;
22import android.hardware.fingerprint.IFingerprintServiceReceiver;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.Slog;
26
27import com.android.internal.logging.MetricsLogger;
Tamas Berghammercbd3f0c2016-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
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 IFingerprintDaemon daemon = getFingerprintDaemon();
82 if (daemon == null) {
83 Slog.w(TAG, "enroll: no fingeprintd!");
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);
Jim Millerc57c8d92016-09-30 17:17:59 -070091 MetricsLogger.histogram(getContext(), "fingerprintd_enroll_start_error", result);
Jim Millercb2ce6f2016-04-13 20:28:18 -070092 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
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 IFingerprintDaemon daemon = getFingerprintDaemon();
104 if (daemon == null) {
105 Slog.w(TAG, "stopEnrollment: no fingeprintd!");
106 return ERROR_ESRCH;
107 }
108 try {
109 final int result = daemon.cancelEnrollment();
110 if (result != 0) {
111 Slog.w(TAG, "startEnrollCancel failed, result = " + result);
112 return result;
113 }
114 } catch (RemoteException e) {
115 Slog.e(TAG, "stopEnrollment failed", e);
116 }
117 if (initiatedByClient) {
118 onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED);
119 }
120 return 0;
121 }
122
123 @Override
124 public boolean onRemoved(int fingerId, int groupId) {
125 if (DEBUG) Slog.w(TAG, "onRemoved() called for enroll!");
126 return true; // Invalid for EnrollClient
127 }
128
129 @Override
130 public boolean onEnumerationResult(int fingerId, int groupId) {
131 if (DEBUG) Slog.w(TAG, "onEnumerationResult() called for enroll!");
132 return true; // Invalid for EnrollClient
133 }
134
135 @Override
136 public boolean onAuthenticated(int fingerId, int groupId) {
137 if (DEBUG) Slog.w(TAG, "onAuthenticated() called for enroll!");
138 return true; // Invalid for EnrollClient
139 }
140
141}