1 /*
2  * Copyright (C) 2023 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 android.hardware.fingerprint;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.hardware.biometrics.BiometricAuthenticator;
22 import android.hardware.biometrics.BiometricManager;
23 import android.hardware.biometrics.common.CommonProps;
24 import android.hardware.biometrics.common.SensorStrength;
25 import android.hardware.biometrics.fingerprint.FingerprintSensorType;
26 import android.hardware.biometrics.fingerprint.SensorLocation;
27 import android.hardware.biometrics.fingerprint.SensorProps;
28 
29 import com.android.internal.R;
30 import com.android.internal.util.ArrayUtils;
31 
32 /**
33  * Parse HIDL fingerprint sensor config and map it to SensorProps.aidl to match AIDL.
34  * See core/res/res/values/config.xml config_biometric_sensors
35  * @hide
36  */
37 public final class HidlFingerprintSensorConfig extends SensorProps {
38     private int mSensorId;
39     private int mModality;
40     private int mStrength;
41 
42     /**
43      * Parse through the config string and map it to SensorProps.aidl.
44      * @throws IllegalArgumentException when config string has unexpected format
45      */
parse(@onNull String config, @NonNull Context context)46     public void parse(@NonNull String config, @NonNull Context context)
47             throws IllegalArgumentException {
48         final String[] elems = config.split(":");
49         if (elems.length < 3) {
50             throw new IllegalArgumentException();
51         }
52         mSensorId = Integer.parseInt(elems[0]);
53         mModality = Integer.parseInt(elems[1]);
54         mStrength = Integer.parseInt(elems[2]);
55         mapHidlToAidlSensorConfiguration(context);
56     }
57 
58     @BiometricAuthenticator.Modality
getModality()59     public int getModality() {
60         return mModality;
61     }
62 
mapHidlToAidlSensorConfiguration(@onNull Context context)63     private void mapHidlToAidlSensorConfiguration(@NonNull Context context) {
64         commonProps = new CommonProps();
65         commonProps.componentInfo = null;
66         commonProps.sensorId = mSensorId;
67         commonProps.sensorStrength = authenticatorStrengthToPropertyStrength(mStrength);
68         commonProps.maxEnrollmentsPerUser = context.getResources().getInteger(
69                 R.integer.config_fingerprintMaxTemplatesPerUser);
70         halControlsIllumination = false;
71         sensorLocations = new SensorLocation[1];
72 
73         final int[] udfpsProps = context.getResources().getIntArray(
74                 com.android.internal.R.array.config_udfps_sensor_props);
75         final boolean isUdfps = !ArrayUtils.isEmpty(udfpsProps);
76         // config_is_powerbutton_fps indicates whether device has a power button fingerprint sensor.
77         final boolean isPowerbuttonFps = context.getResources().getBoolean(
78                 R.bool.config_is_powerbutton_fps);
79 
80         if (isUdfps) {
81             sensorType = FingerprintSensorType.UNKNOWN;
82         } else if (isPowerbuttonFps) {
83             sensorType = FingerprintSensorType.POWER_BUTTON;
84         } else {
85             sensorType = FingerprintSensorType.REAR;
86         }
87 
88         if (isUdfps && udfpsProps.length == 3) {
89             setSensorLocation(udfpsProps[0], udfpsProps[1], udfpsProps[2]);
90         } else {
91             setSensorLocation(540 /* sensorLocationX */, 1636 /* sensorLocationY */,
92                     130 /* sensorRadius */);
93         }
94 
95     }
96 
setSensorLocation(int sensorLocationX, int sensorLocationY, int sensorRadius)97     private void setSensorLocation(int sensorLocationX,
98             int sensorLocationY, int sensorRadius) {
99         sensorLocations[0] = new SensorLocation();
100         sensorLocations[0].display = "";
101         sensorLocations[0].sensorLocationX = sensorLocationX;
102         sensorLocations[0].sensorLocationY = sensorLocationY;
103         sensorLocations[0].sensorRadius = sensorRadius;
104     }
105 
authenticatorStrengthToPropertyStrength( @iometricManager.Authenticators.Types int strength)106     private byte authenticatorStrengthToPropertyStrength(
107             @BiometricManager.Authenticators.Types int strength) {
108         switch (strength) {
109             case BiometricManager.Authenticators.BIOMETRIC_CONVENIENCE:
110                 return SensorStrength.CONVENIENCE;
111             case BiometricManager.Authenticators.BIOMETRIC_WEAK:
112                 return SensorStrength.WEAK;
113             case BiometricManager.Authenticators.BIOMETRIC_STRONG:
114                 return SensorStrength.STRONG;
115             default:
116                 throw new IllegalArgumentException("Unknown strength: " + strength);
117         }
118     }
119 }
120