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 import android.os.UserManager; 24 25 import androidx.annotation.Nullable; 26 27 import com.android.settings.R; 28 import com.android.settings.Settings; 29 import com.android.settings.Utils; 30 import com.android.settings.biometrics.ParentalControlsUtils; 31 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 32 import com.android.settingslib.utils.StringUtil; 33 34 /** 35 * Utilities for combined biometric details shared between Security Settings and Safety Center. 36 */ 37 public class CombinedBiometricStatusUtils { 38 39 /** 40 * An intent extra indicates that the enrollment process is launched from biometric 41 * SafetySourceIssue action. 42 */ 43 public static final String EXTRA_LAUNCH_FROM_SAFETY_SOURCE_ISSUE = 44 "launch_from_safety_source_issue"; 45 46 private final int mUserId; 47 private final Context mContext; 48 @Nullable 49 FingerprintManager mFingerprintManager; 50 @Nullable 51 FaceManager mFaceManager; 52 CombinedBiometricStatusUtils(Context context, int userId)53 public CombinedBiometricStatusUtils(Context context, int userId) { 54 mContext = context; 55 mFingerprintManager = Utils.getFingerprintManagerOrNull(context); 56 mFaceManager = Utils.getFaceManagerOrNull(context); 57 mUserId = userId; 58 } 59 60 /** 61 * Returns whether the combined biometric settings entity should be shown. 62 */ isAvailable()63 public boolean isAvailable() { 64 return Utils.hasFingerprintHardware(mContext) && Utils.hasFaceHardware(mContext); 65 } 66 67 /** 68 * Returns whether at least one face template or fingerprint has been enrolled. 69 */ hasEnrolled()70 public boolean hasEnrolled() { 71 return hasEnrolledFingerprints() || hasEnrolledFace(); 72 } 73 74 /** 75 * Returns the {@link EnforcedAdmin} in case parental consent is required to change both 76 * face and fingerprint settings. 77 * 78 * @return null if either face or fingerprint settings do not require a parental consent. 79 */ getDisablingAdmin()80 public EnforcedAdmin getDisablingAdmin() { 81 // This controller currently is shown if fingerprint&face exist on the device. If this 82 // changes in the future, the modalities passed into the below will need to be updated. 83 84 final EnforcedAdmin faceAdmin = ParentalControlsUtils 85 .parentConsentRequired(mContext, BiometricAuthenticator.TYPE_FACE); 86 final EnforcedAdmin fpAdmin = ParentalControlsUtils 87 .parentConsentRequired(mContext, BiometricAuthenticator.TYPE_FINGERPRINT); 88 89 final boolean faceConsentRequired = faceAdmin != null; 90 final boolean fpConsentRequired = fpAdmin != null; 91 92 // Result is only required if all modalities require consent. 93 // If the admins are non-null, they are actually always the same. 94 return faceConsentRequired && fpConsentRequired ? faceAdmin : null; 95 } 96 97 /** 98 * Returns the title of combined biometric settings entity. 99 */ getTitle()100 public String getTitle() { 101 UserManager userManager = mContext.getSystemService(UserManager.class); 102 if (userManager != null && userManager.isProfile()) { 103 return mContext.getString( 104 Utils.isPrivateProfile(mUserId, mContext) 105 ? R.string.private_space_biometric_unlock_title 106 : R.string.security_settings_work_biometric_preference_title); 107 } else { 108 return mContext.getString(R.string.security_settings_biometric_preference_title); 109 } 110 } 111 112 /** 113 * Returns the summary of combined biometric settings entity. 114 */ getSummary()115 public String getSummary() { 116 final int numFingerprintsEnrolled = mFingerprintManager != null 117 ? mFingerprintManager.getEnrolledFingerprints(mUserId).size() : 0; 118 final boolean faceEnrolled = hasEnrolledFace(); 119 120 if (faceEnrolled && numFingerprintsEnrolled > 1) { 121 return mContext.getString( 122 R.string.security_settings_biometric_preference_summary_both_fp_multiple); 123 } else if (faceEnrolled && numFingerprintsEnrolled == 1) { 124 return mContext.getString( 125 R.string.security_settings_biometric_preference_summary_both_fp_single); 126 } else if (faceEnrolled) { 127 return mContext.getString(R.string.security_settings_face_preference_summary); 128 } else if (numFingerprintsEnrolled > 0) { 129 return StringUtil.getIcuPluralsString(mContext, numFingerprintsEnrolled, 130 R.string.security_settings_fingerprint_preference_summary); 131 } else { 132 return mContext.getString( 133 R.string.security_settings_biometric_preference_summary_none_enrolled); 134 } 135 } 136 hasEnrolledFingerprints()137 private boolean hasEnrolledFingerprints() { 138 return mFingerprintManager != null && mFingerprintManager.hasEnrolledFingerprints(mUserId); 139 } 140 hasEnrolledFace()141 private boolean hasEnrolledFace() { 142 return mFaceManager != null && mFaceManager.hasEnrolledTemplates(mUserId); 143 } 144 145 /** 146 * Returns the class name of the Settings page corresponding to combined biometric settings 147 * based on the current user. 148 */ getSettingsClassNameBasedOnUser()149 public String getSettingsClassNameBasedOnUser() { 150 UserManager userManager = mContext.getSystemService(UserManager.class); 151 if (userManager != null && userManager.isProfile()) { 152 return getProfileSettingsClassName(); 153 } else { 154 return getSettingsClassName(); 155 } 156 } 157 158 /** 159 * Returns the class name of the Settings page corresponding to combined biometric settings. 160 */ getSettingsClassName()161 public String getSettingsClassName() { 162 return Settings.CombinedBiometricSettingsActivity.class.getName(); 163 } 164 165 /** 166 * Returns the class name of the Settings page corresponding to combined biometric settings 167 * for work profile. 168 */ getProfileSettingsClassName()169 public String getProfileSettingsClassName() { 170 return Settings.CombinedBiometricProfileSettingsActivity.class.getName(); 171 } 172 173 /** 174 * Returns the class name of the Settings page corresponding to combined biometric settings for 175 * Private profile. 176 */ getPrivateProfileSettingsClassName()177 public String getPrivateProfileSettingsClassName() { 178 return Settings.PrivateSpaceBiometricSettingsActivity.class.getName(); 179 } 180 } 181