1 /* 2 * Copyright (C) 2018 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.face; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.hardware.biometrics.SensorProperties; 23 import android.hardware.face.FaceManager; 24 import android.hardware.face.FaceSensorPropertiesInternal; 25 import android.hardware.face.IFaceAuthenticatorsRegisteredCallback; 26 import android.util.Log; 27 28 import androidx.annotation.NonNull; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.R; 33 import com.android.settings.Utils; 34 import com.android.settings.core.BasePreferenceController; 35 import com.android.settings.overlay.FeatureFactory; 36 import com.android.settings.utils.AnnotationSpan; 37 import com.android.settingslib.HelpUtils; 38 39 import java.util.List; 40 41 /** 42 * Footer for face settings showing the help text and help link. 43 */ 44 public class FaceSettingsFooterPreferenceController extends BasePreferenceController { 45 private static final String KEY = "security_face_footer"; 46 private static final String TAG = "FaceSettingsFooterPreferenceController"; 47 private static final String ANNOTATION_URL = "url"; 48 private final FaceFeatureProvider mProvider; 49 private Preference mPreference; 50 private boolean mIsFaceStrong; 51 private int mUserId; 52 FaceSettingsFooterPreferenceController(@onNull Context context)53 public FaceSettingsFooterPreferenceController(@NonNull Context context) { 54 this(context, KEY); 55 } FaceSettingsFooterPreferenceController(Context context, String preferenceKey)56 public FaceSettingsFooterPreferenceController(Context context, String preferenceKey) { 57 super(context, preferenceKey); 58 mProvider = FeatureFactory.getFeatureFactory().getFaceFeatureProvider(); 59 } 60 61 @Override displayPreference(PreferenceScreen screen)62 public void displayPreference(PreferenceScreen screen) { 63 super.displayPreference(screen); 64 mPreference = screen.findPreference(mPreferenceKey); 65 if (screen.getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) { 66 addAuthenticatorsRegisteredCallback(screen.getContext()); 67 } else { 68 Log.w(TAG, "Not support FEATURE_FACE"); 69 } 70 } 71 72 @Override getAvailabilityStatus()73 public int getAvailabilityStatus() { 74 return AVAILABLE_UNSEARCHABLE; 75 } 76 77 @Override updateState(Preference preference)78 public void updateState(Preference preference) { 79 super.updateState(preference); 80 81 final Intent helpIntent = HelpUtils.getHelpIntent( 82 mContext, mContext.getString(R.string.help_url_face), getClass().getName()); 83 final AnnotationSpan.LinkInfo linkInfo = 84 new AnnotationSpan.LinkInfo(mContext, ANNOTATION_URL, helpIntent); 85 86 int footerRes; 87 boolean isAttentionSupported = mProvider.isAttentionSupported(mContext); 88 if (Utils.isPrivateProfile(mUserId, mContext)) { 89 footerRes = R.string.private_space_face_settings_footer; 90 } else if (mIsFaceStrong) { 91 footerRes = isAttentionSupported 92 ? R.string.security_settings_face_settings_footer_class3 93 : R.string.security_settings_face_settings_footer_attention_not_supported; 94 } else { 95 footerRes = isAttentionSupported 96 ? R.string.security_settings_face_settings_footer 97 : R.string.security_settings_face_settings_footer_class3_attention_not_supported; 98 } 99 preference.setTitle(AnnotationSpan.linkify( 100 mContext.getText(footerRes), linkInfo)); 101 } 102 setUserId(int userId)103 public void setUserId(int userId) { 104 mUserId = userId; 105 } 106 addAuthenticatorsRegisteredCallback(Context context)107 private void addAuthenticatorsRegisteredCallback(Context context) { 108 final FaceManager faceManager = context.getSystemService(FaceManager.class); 109 faceManager.addAuthenticatorsRegisteredCallback( 110 new IFaceAuthenticatorsRegisteredCallback.Stub() { 111 @Override 112 public void onAllAuthenticatorsRegistered( 113 @NonNull List<FaceSensorPropertiesInternal> sensors) { 114 if (sensors.isEmpty()) { 115 Log.e(TAG, "No sensors"); 116 return; 117 } 118 119 boolean isFaceStrong = sensors.get(0).sensorStrength 120 == SensorProperties.STRENGTH_STRONG; 121 if (mIsFaceStrong == isFaceStrong) { 122 return; 123 } 124 mIsFaceStrong = isFaceStrong; 125 updateState(mPreference); 126 } 127 }); 128 } 129 } 130