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.content.Context;
20import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprint;
21import android.hardware.fingerprint.FingerprintManager;
22import android.hardware.fingerprint.IFingerprintServiceReceiver;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.Slog;
26import com.android.internal.logging.MetricsLogger;
27
28/**
29 * A class to keep track of the enumeration state for a given client.
30 */
31public abstract class EnumerateClient extends ClientMonitor {
32    public EnumerateClient(Context context, long halDeviceId, IBinder token,
33        IFingerprintServiceReceiver receiver, int groupId, int userId,
34        boolean restricted, String owner) {
35        super(context, halDeviceId, token, receiver, userId, groupId, restricted, owner);
36    }
37
38    @Override
39    public int start() {
40        IBiometricsFingerprint daemon = getFingerprintDaemon();
41        // The fingerprint template ids will be removed when we get confirmation from the HAL
42        try {
43            final int result = daemon.enumerate();
44            if (result != 0) {
45                Slog.w(TAG, "start enumerate for user " + getTargetUserId()
46                    + " failed, result=" + result);
47                MetricsLogger.histogram(getContext(), "fingerprintd_enum_start_error", result);
48                onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */);
49                return result;
50            }
51        } catch (RemoteException e) {
52            Slog.e(TAG, "startEnumeration failed", e);
53        }
54        return 0;
55    }
56
57    @Override
58    public int stop(boolean initiatedByClient) {
59        if (mAlreadyCancelled) {
60            Slog.w(TAG, "stopEnumerate: already cancelled!");
61            return 0;
62        }
63        IBiometricsFingerprint daemon = getFingerprintDaemon();
64        if (daemon == null) {
65            Slog.w(TAG, "stopEnumeration: no fingerprint HAL!");
66            return ERROR_ESRCH;
67        }
68        try {
69            final int result = daemon.cancel();
70            if (result != 0) {
71                Slog.w(TAG, "stop enumeration failed, result=" + result);
72                return result;
73            }
74        } catch (RemoteException e) {
75            Slog.e(TAG, "stopEnumeration failed", e);
76            return ERROR_ESRCH;
77        }
78
79        // We don't actually stop enumerate, but inform the client that the cancel operation
80        // succeeded so we can start the next operation.
81        if (initiatedByClient) {
82            onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED, 0 /* vendorCode */);
83        }
84        mAlreadyCancelled = true;
85        return 0; // success
86    }
87
88    @Override
89    public boolean onEnumerationResult(int fingerId, int groupId, int remaining) {
90        IFingerprintServiceReceiver receiver = getReceiver();
91        if (receiver == null)
92            return true; // client not listening
93        try {
94            receiver.onEnumerated(getHalDeviceId(), fingerId, groupId, remaining);
95        } catch (RemoteException e) {
96            Slog.w(TAG, "Failed to notify enumerated:", e);
97        }
98        return remaining == 0;
99    }
100
101    @Override
102    public boolean onAuthenticated(int fingerId, int groupId) {
103        if (DEBUG) Slog.w(TAG, "onAuthenticated() called for enumerate!");
104        return true; // Invalid for Enumerate.
105    }
106
107    @Override
108    public boolean onEnrollResult(int fingerId, int groupId, int rem) {
109        if (DEBUG) Slog.w(TAG, "onEnrollResult() called for enumerate!");
110        return true; // Invalid for Enumerate.
111    }
112
113    @Override
114    public boolean onRemoved(int fingerId, int groupId, int remaining) {
115        if (DEBUG) Slog.w(TAG, "onRemoved() called for enumerate!");
116        return true; // Invalid for Enumerate.
117    }
118}
119