1 /*
2  * Copyright (C) 2020 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.biometrics.sensors.fingerprint.hidl;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.hardware.biometrics.fingerprint.V2_1.FingerprintError;
23 import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprintClientCallback;
24 import android.hardware.biometrics.fingerprint.V2_3.IBiometricsFingerprint;
25 import android.hardware.fingerprint.Fingerprint;
26 import android.os.RemoteException;
27 import android.util.Slog;
28 
29 import com.android.server.biometrics.sensors.fingerprint.FingerprintUtils;
30 
31 import java.util.List;
32 
33 /**
34  * Test HAL that provides only provides no-ops.
35  */
36 public class TestHal extends IBiometricsFingerprint.Stub {
37     private static final String TAG = "fingerprint.hidl.TestHal";
38 
39     @NonNull
40     private final Context mContext;
41     private final int mSensorId;
42 
43     @Nullable
44     private IBiometricsFingerprintClientCallback mCallback;
45 
TestHal(@onNull Context context, int sensorId)46     TestHal(@NonNull Context context, int sensorId) {
47         mContext = context;
48         mSensorId = sensorId;
49     }
50 
51     @Override
isUdfps(int sensorId)52     public boolean isUdfps(int sensorId) {
53         return false;
54     }
55 
56     @Override
onFingerDown(int x, int y, float minor, float major)57     public void onFingerDown(int x, int y, float minor, float major) {
58 
59     }
60 
61     @Override
onFingerUp()62     public void onFingerUp() {
63 
64     }
65 
66     @Override
setNotify(IBiometricsFingerprintClientCallback clientCallback)67     public long setNotify(IBiometricsFingerprintClientCallback clientCallback) {
68         mCallback = clientCallback;
69         return 0;
70     }
71 
72     @Override
preEnroll()73     public long preEnroll() {
74         return 0;
75     }
76 
77     @Override
enroll(byte[] hat, int gid, int timeoutSec)78     public int enroll(byte[] hat, int gid, int timeoutSec) {
79         Slog.w(TAG, "enroll");
80         return 0;
81     }
82 
83     @Override
postEnroll()84     public int postEnroll() {
85         return 0;
86     }
87 
88     @Override
getAuthenticatorId()89     public long getAuthenticatorId() {
90         return 0;
91     }
92 
93     @Override
cancel()94     public int cancel() throws RemoteException {
95         if (mCallback != null) {
96             mCallback.onError(0, FingerprintError.ERROR_CANCELED, 0 /* vendorCode */);
97         }
98         return 0;
99     }
100 
101     @Override
enumerate()102     public int enumerate() throws RemoteException {
103         Slog.w(TAG, "Enumerate");
104         if (mCallback != null) {
105             mCallback.onEnumerate(0 /* deviceId */, 0 /* fingerId */, 0 /* groupId */,
106                     0 /* remaining */);
107         }
108         return 0;
109     }
110 
111     @Override
remove(int gid, int fid)112     public int remove(int gid, int fid) throws RemoteException {
113         Slog.w(TAG, "Remove");
114         if (mCallback != null) {
115             if (fid == 0) {
116                 // For this HAL interface, remove(0) means to remove all enrollments.
117                 final List<Fingerprint> fingerprints = FingerprintUtils.getInstance(mSensorId)
118                         .getBiometricsForUser(mContext, gid);
119                 for (int i = 0; i < fingerprints.size(); i++) {
120                     final Fingerprint fp = fingerprints.get(i);
121                     mCallback.onRemoved(0 /* deviceId */, fp.getBiometricId(), gid,
122                             fingerprints.size() - i - 1);
123                 }
124             } else {
125                 mCallback.onRemoved(0 /* deviceId */, fid, gid, 0 /* remaining */);
126             }
127         }
128         return 0;
129     }
130 
131     @Override
setActiveGroup(int gid, String storePath)132     public int setActiveGroup(int gid, String storePath) {
133         return 0;
134     }
135 
136     @Override
authenticate(long operationId, int gid)137     public int authenticate(long operationId, int gid) {
138         Slog.w(TAG, "Authenticate");
139         return 0;
140     }
141 }