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.content.Context; |
| 20 | import android.hardware.fingerprint.FingerprintManager; |
| 21 | import android.hardware.fingerprint.IFingerprintDaemon; |
| 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; |
Tamas Berghammer | cbd3f0c | 2016-06-22 15:21:38 +0100 | [diff] [blame^] | 28 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 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) { |
Tony Mak | ff715ac | 2016-04-19 20:44:12 +0100 | [diff] [blame] | 49 | if (groupId != getGroupId()) { |
| 50 | Slog.w(TAG, "groupId != getGroupId(), groupId: " + groupId + |
| 51 | " getGroupId():" + getGroupId()); |
| 52 | } |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 53 | if (remaining == 0) { |
Jim Miller | c12eca8 | 2016-04-28 15:12:53 -0700 | [diff] [blame] | 54 | FingerprintUtils.getInstance().addFingerprintForUser(getContext(), fingerId, |
| 55 | getTargetUserId()); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 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 | 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 Miller | c57c8d9 | 2016-09-30 17:17:59 -0700 | [diff] [blame] | 91 | MetricsLogger.histogram(getContext(), "fingerprintd_enroll_start_error", result); |
Jim Miller | cb2ce6f | 2016-04-13 20:28:18 -0700 | [diff] [blame] | 92 | 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 | } |