• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.biometrics.combination;
18 
19 import android.content.Context;
20 import android.hardware.biometrics.BiometricAuthenticator;
21 import android.hardware.face.FaceManager;
22 import android.hardware.fingerprint.FingerprintManager;
23 
24 import androidx.annotation.Nullable;
25 
26 import com.android.settings.R;
27 import com.android.settings.Settings;
28 import com.android.settings.Utils;
29 import com.android.settings.biometrics.ParentalControlsUtils;
30 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
31 
32 /**
33  * Utilities for combined biometric details shared between Security Settings and Safety Center.
34  */
35 public class CombinedBiometricStatusUtils {
36 
37     private final int mUserId;
38     private final Context mContext;
39     @Nullable
40     FingerprintManager mFingerprintManager;
41     @Nullable
42     FaceManager mFaceManager;
43 
CombinedBiometricStatusUtils(Context context, int userId)44     public CombinedBiometricStatusUtils(Context context, int userId) {
45         mContext = context;
46         mFingerprintManager = Utils.getFingerprintManagerOrNull(context);
47         mFaceManager = Utils.getFaceManagerOrNull(context);
48         mUserId = userId;
49     }
50 
51     /**
52      * Returns whether the combined biometric settings entity should be shown.
53      */
isAvailable()54     public boolean isAvailable() {
55         return Utils.hasFingerprintHardware(mContext) && Utils.hasFaceHardware(mContext);
56     }
57 
58     /**
59      * Returns whether at least one face template or fingerprint has been enrolled.
60      */
hasEnrolled()61     public boolean hasEnrolled() {
62         return hasEnrolledFingerprints() || hasEnrolledFace();
63     }
64 
65     /**
66      * Returns the {@link EnforcedAdmin} in case parental consent is required to change both
67      * face and fingerprint settings.
68      *
69      * @return null if either face or fingerprint settings do not require a parental consent.
70      */
getDisablingAdmin()71     public EnforcedAdmin getDisablingAdmin() {
72         // This controller currently is shown if fingerprint&face exist on the device. If this
73         // changes in the future, the modalities passed into the below will need to be updated.
74 
75         final EnforcedAdmin faceAdmin = ParentalControlsUtils
76                 .parentConsentRequired(mContext, BiometricAuthenticator.TYPE_FACE);
77         final EnforcedAdmin fpAdmin = ParentalControlsUtils
78                 .parentConsentRequired(mContext, BiometricAuthenticator.TYPE_FINGERPRINT);
79 
80         final boolean faceConsentRequired = faceAdmin != null;
81         final boolean fpConsentRequired = fpAdmin != null;
82 
83         // Result is only required if all modalities require consent.
84         // If the admins are non-null, they are actually always the same.
85         return faceConsentRequired && fpConsentRequired ? faceAdmin : null;
86     }
87 
88     /**
89      * Returns the summary of combined biometric settings entity.
90      */
getSummary()91     public String getSummary() {
92         final int numFingerprintsEnrolled = mFingerprintManager != null
93                 ? mFingerprintManager.getEnrolledFingerprints(mUserId).size() : 0;
94         final boolean faceEnrolled = hasEnrolledFace();
95 
96         if (faceEnrolled && numFingerprintsEnrolled > 1) {
97             return mContext.getString(
98                     R.string.security_settings_biometric_preference_summary_both_fp_multiple);
99         } else if (faceEnrolled && numFingerprintsEnrolled == 1) {
100             return mContext.getString(
101                     R.string.security_settings_biometric_preference_summary_both_fp_single);
102         } else if (faceEnrolled) {
103             return mContext.getString(R.string.security_settings_face_preference_summary);
104         } else if (numFingerprintsEnrolled > 0) {
105             return mContext.getResources().getQuantityString(
106                     R.plurals.security_settings_fingerprint_preference_summary,
107                     numFingerprintsEnrolled, numFingerprintsEnrolled);
108         } else {
109             return mContext.getString(
110                     R.string.security_settings_biometric_preference_summary_none_enrolled);
111         }
112     }
113 
hasEnrolledFingerprints()114     private boolean hasEnrolledFingerprints() {
115         return mFingerprintManager != null && mFingerprintManager.hasEnrolledFingerprints(mUserId);
116     }
117 
hasEnrolledFace()118     private boolean hasEnrolledFace() {
119         return mFaceManager != null && mFaceManager.hasEnrolledTemplates(mUserId);
120     }
121 
122     /**
123      * Returns the class name of the Settings page corresponding to combined biometric settings.
124      */
getSettingsClassName()125     public String getSettingsClassName() {
126         return Settings.CombinedBiometricSettingsActivity.class.getName();
127     }
128 
129     /**
130      * Returns the class name of the Settings page corresponding to combined biometric settings
131      * for work profile.
132      */
getProfileSettingsClassName()133     public String getProfileSettingsClassName() {
134         return Settings.CombinedBiometricProfileSettingsActivity.class.getName();
135     }
136 }
137