1 /*
2  * Copyright (C) 2022 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.settings.biometrics2.data.repository;
18 
19 import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FINGERPRINT;
20 
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 import android.content.res.Resources;
24 import android.hardware.fingerprint.Fingerprint;
25 import android.hardware.fingerprint.FingerprintManager;
26 import android.hardware.fingerprint.FingerprintSensorPropertiesInternal;
27 import android.hardware.fingerprint.IFingerprintAuthenticatorsRegisteredCallback;
28 import android.util.Log;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 
33 import com.android.settings.R;
34 import com.android.settings.biometrics.ParentalControlsUtils;
35 import com.android.settingslib.RestrictedLockUtilsInternal;
36 
37 import java.util.List;
38 
39 /**
40  * This repository is used to call all APIs in {@link FingerprintManager}
41  */
42 public class FingerprintRepository {
43 
44     private static final String TAG = "FingerprintRepository";
45     @NonNull
46     private final FingerprintManager mFingerprintManager;
47 
48     private List<FingerprintSensorPropertiesInternal> mSensorPropertiesCache;
49 
FingerprintRepository(@onNull FingerprintManager fingerprintManager)50     public FingerprintRepository(@NonNull FingerprintManager fingerprintManager) {
51         mFingerprintManager = fingerprintManager;
52         mFingerprintManager.addAuthenticatorsRegisteredCallback(
53                 new IFingerprintAuthenticatorsRegisteredCallback.Stub() {
54                     @Override
55                     public void onAllAuthenticatorsRegistered(
56                             List<FingerprintSensorPropertiesInternal> sensors) {
57                         mSensorPropertiesCache = sensors;
58                     }
59                 });
60     }
61 
62     /**
63      * The first sensor type is UDFPS sensor or not
64      */
canAssumeUdfps()65     public boolean canAssumeUdfps() {
66         FingerprintSensorPropertiesInternal prop = getFirstFingerprintSensorPropertiesInternal();
67         return prop != null && prop.isAnyUdfpsType();
68     }
69 
70     /**
71      * The first sensor type is Side fps sensor or not
72      */
canAssumeSfps()73     public boolean canAssumeSfps() {
74         FingerprintSensorPropertiesInternal prop = getFirstFingerprintSensorPropertiesInternal();
75         return prop != null && prop.isAnySidefpsType();
76     }
77 
78     /**
79      * Get max possible number of fingerprints for a user
80      */
getMaxFingerprints()81     public int getMaxFingerprints() {
82         FingerprintSensorPropertiesInternal prop = getFirstFingerprintSensorPropertiesInternal();
83         return prop != null ? prop.maxEnrollmentsPerUser : 0;
84     }
85 
86     /**
87      * Get number of fingerprints that this user enrolled.
88      */
getNumOfEnrolledFingerprintsSize(int userId)89     public int getNumOfEnrolledFingerprintsSize(int userId) {
90         final List<Fingerprint> list = mFingerprintManager.getEnrolledFingerprints(userId);
91         return list != null ? list.size() : 0;
92     }
93 
94     /**
95      * Get maximum possible fingerprints in setup wizard flow
96      */
getMaxFingerprintsInSuw(@onNull Resources resources)97     public int getMaxFingerprintsInSuw(@NonNull Resources resources) {
98         return resources.getInteger(R.integer.suw_max_fingerprints_enrollable);
99     }
100 
101     /**
102      * Gets the first FingerprintSensorPropertiesInternal from FingerprintManager
103      */
104     @Nullable
getFirstFingerprintSensorPropertiesInternal()105     public FingerprintSensorPropertiesInternal getFirstFingerprintSensorPropertiesInternal() {
106         final List<FingerprintSensorPropertiesInternal> props = mSensorPropertiesCache;
107         if (props == null) {
108             // Handle this case if it really happens
109             Log.e(TAG, "Sensor properties cache is null");
110             return null;
111         }
112         return props.size() > 0 ? props.get(0) : null;
113     }
114 
115     /**
116      * Call FingerprintManager to generate challenge for first sensor
117      */
generateChallenge(int userId, @NonNull FingerprintManager.GenerateChallengeCallback callback)118     public void generateChallenge(int userId,
119             @NonNull FingerprintManager.GenerateChallengeCallback callback) {
120         mFingerprintManager.generateChallenge(userId, callback);
121     }
122 
123     /**
124      * Get parental consent required or not during enrollment process
125      */
isParentalConsentRequired(@onNull Context context)126     public boolean isParentalConsentRequired(@NonNull Context context) {
127         return ParentalControlsUtils.parentConsentRequired(context, TYPE_FINGERPRINT) != null;
128     }
129 
130     /**
131      * Get fingerprint is disable by admin or not
132      */
isDisabledByAdmin(@onNull Context context, int userId)133     public boolean isDisabledByAdmin(@NonNull Context context, int userId) {
134         return RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
135                 context, DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT, userId) != null;
136     }
137 
138     /**
139      * Get fingerprint enroll stage threshold
140      */
getEnrollStageThreshold(int index)141     public float getEnrollStageThreshold(int index) {
142         return mFingerprintManager.getEnrollStageThreshold(index);
143     }
144 
145     /**
146      * Get fingerprint enroll stage count
147      */
getEnrollStageCount()148     public int getEnrollStageCount() {
149         return mFingerprintManager.getEnrollStageCount();
150     }
151 }
152