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.fingerprint.FingerprintManager;
21import android.hardware.fingerprint.IFingerprintDaemon;
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 userId, int groupId,
34            boolean restricted, String owner) {
35        super(context, halDeviceId, token, receiver, userId, groupId, restricted, owner);
36    }
37
38    @Override
39    public int start() {
40        IFingerprintDaemon 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);
49                return result;
50            }
51        } catch (RemoteException e) {
52            Slog.e(TAG, "startRemove failed", e);
53        }
54        return 0;
55    }
56
57    @Override
58    public int stop(boolean initiatedByClient) {
59        IFingerprintDaemon daemon = getFingerprintDaemon();
60        if (daemon == null) {
61            Slog.w(TAG, "stopAuthentication: no fingeprintd!");
62            return ERROR_ESRCH;
63        }
64        try {
65            final int result = daemon.cancelEnumeration();
66            if (result != 0) {
67                Slog.w(TAG, "stop enumeration failed, result=" + result);
68                return result;
69            }
70        } catch (RemoteException e) {
71            Slog.e(TAG, "stop enumeration failed", e);
72            return ERROR_ESRCH;
73        }
74        // We don't actually stop enumerate, but inform the client that the cancel operation
75        // succeeded so we can start the next operation.
76        if (initiatedByClient) {
77            onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED);
78        }
79        return 0; // success
80    }
81
82    @Override
83    public boolean onEnumerationResult(int fingerId, int groupId) {
84        IFingerprintServiceReceiver receiver = getReceiver();
85        if (receiver == null)
86            return true; // client not listening
87        try {
88            receiver.onRemoved(getHalDeviceId(), fingerId, groupId);
89        } catch (RemoteException e) {
90            Slog.w(TAG, "Failed to notify enumerated:", e);
91        }
92        return fingerId == 0; // done when id hits 0
93    }
94}
95