1 /*
2  * Copyright (C) 2021 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 package com.android.settings.biometrics.combination;
17 
18 import static com.android.settings.biometrics.activeunlock.ActiveUnlockStatusPreferenceController.KEY_ACTIVE_UNLOCK_SETTINGS;
19 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.UserHandle;
26 
27 import androidx.annotation.Nullable;
28 import androidx.preference.Preference;
29 
30 import com.android.settings.R;
31 import com.android.settings.biometrics.activeunlock.ActiveUnlockContentListener.OnContentChangedListener;
32 import com.android.settings.biometrics.activeunlock.ActiveUnlockDeviceNameListener;
33 import com.android.settings.biometrics.activeunlock.ActiveUnlockRequireBiometricSetup;
34 import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settingslib.search.SearchIndexable;
37 
38 /**
39  * Settings screen for multiple biometrics.
40  */
41 @SearchIndexable
42 public class CombinedBiometricSettings extends BiometricsSettingsBase {
43     private static final String TAG = "BiometricSettings";
44     private static final String KEY_FACE_SETTINGS = "biometric_face_settings";
45     private static final String KEY_FINGERPRINT_SETTINGS = "biometric_fingerprint_settings";
46     private static final String KEY_UNLOCK_PHONE = "biometric_settings_biometric_keyguard";
47     private static final String KEY_USE_IN_APPS = "biometric_settings_biometric_app";
48     private static final String KEY_INTRO_PREFERENCE = "biometric_intro";
49     private static final String KEY_USE_BIOMETRIC_PREFERENCE = "biometric_ways_to_use";
50 
51     private ActiveUnlockStatusUtils mActiveUnlockStatusUtils;
52     private CombinedBiometricStatusUtils mCombinedBiometricStatusUtils;
53     @Nullable private ActiveUnlockDeviceNameListener mActiveUnlockDeviceNameListener;
54 
55     @Override
onAttach(Context context)56     public void onAttach(Context context) {
57         super.onAttach(context);
58         use(BiometricSettingsKeyguardPreferenceController.class).setUserId(mUserId);
59         use(BiometricSettingsAppPreferenceController.class).setUserId(mUserId);
60     }
61 
62     @Override
onCreate(Bundle savedInstanceState)63     public void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65         mActiveUnlockStatusUtils = new ActiveUnlockStatusUtils(getActivity());
66         mCombinedBiometricStatusUtils = new CombinedBiometricStatusUtils(getActivity(), mUserId);
67         if (mActiveUnlockStatusUtils.isAvailable()) {
68             updateUiForActiveUnlock();
69         }
70     }
71 
updateUiForActiveUnlock()72     private void updateUiForActiveUnlock() {
73         OnContentChangedListener listener = new OnContentChangedListener() {
74             @Override
75             public void onContentChanged(String newValue) {
76                 updateUnlockPhonePreferenceSummary();
77             }
78         };
79 
80         mActiveUnlockDeviceNameListener =
81                 new ActiveUnlockDeviceNameListener(getActivity(), listener);
82         mActiveUnlockDeviceNameListener.subscribe();
83         final Preference introPreference = findPreference(KEY_INTRO_PREFERENCE);
84         if (introPreference != null) {
85             introPreference.setTitle(mActiveUnlockStatusUtils.getIntroForActiveUnlock());
86         }
87         final Preference useBiometricPreference = findPreference(KEY_USE_BIOMETRIC_PREFERENCE);
88         if (useBiometricPreference != null) {
89             useBiometricPreference.setTitle(
90                     mActiveUnlockStatusUtils.getUseBiometricTitleForActiveUnlock());
91         }
92         getActivity().setTitle(mActiveUnlockStatusUtils.getTitleForActiveUnlock());
93     }
94 
95     @Override
onDestroy()96     public void onDestroy() {
97         if (mActiveUnlockDeviceNameListener != null) {
98             mActiveUnlockDeviceNameListener.unsubscribe();
99         }
100         super.onDestroy();
101     }
102 
103     @Override
getPreferenceScreenResId()104     protected int getPreferenceScreenResId() {
105         return R.xml.security_settings_combined_biometric;
106     }
107 
108     @Override
getFacePreferenceKey()109     public String getFacePreferenceKey() {
110         return KEY_FACE_SETTINGS;
111     }
112 
113     @Override
getFingerprintPreferenceKey()114     public String getFingerprintPreferenceKey() {
115         return KEY_FINGERPRINT_SETTINGS;
116     }
117 
118     @Override
getUnlockPhonePreferenceKey()119     public String getUnlockPhonePreferenceKey() {
120         return KEY_UNLOCK_PHONE;
121     }
122 
123     @Override
getUseInAppsPreferenceKey()124     public String getUseInAppsPreferenceKey() {
125         return KEY_USE_IN_APPS;
126     }
127 
128     @Override
getLogTag()129     protected String getLogTag() {
130         return TAG;
131     }
132 
133     @Override
getMetricsCategory()134     public int getMetricsCategory() {
135         return SettingsEnums.COMBINED_BIOMETRIC;
136     }
137 
138     @Override
onRetryPreferenceTreeClick(Preference preference, final boolean retry)139     protected boolean onRetryPreferenceTreeClick(Preference preference, final boolean retry) {
140         if (!mActiveUnlockStatusUtils.isAvailable()
141                 || !KEY_ACTIVE_UNLOCK_SETTINGS.equals(preference.getKey())) {
142             return super.onRetryPreferenceTreeClick(preference, retry);
143         }
144         mDoNotFinishActivity = true;
145         Intent intent;
146         if (mActiveUnlockStatusUtils.useBiometricFailureLayout()
147                 && mActiveUnlockDeviceNameListener != null
148                 && !mActiveUnlockDeviceNameListener.hasEnrolled()
149                 && !mCombinedBiometricStatusUtils.hasEnrolled()) {
150             intent = new Intent(getActivity(), ActiveUnlockRequireBiometricSetup.class);
151             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
152             int userId = mUserId;
153             if (mUserId != UserHandle.USER_NULL) {
154                 intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
155             }
156             intent.putExtra(EXTRA_KEY_GK_PW_HANDLE, getGkPwHandle());
157         } else {
158             intent = mActiveUnlockStatusUtils.getIntent();
159         }
160         if (intent != null) {
161             startActivityForResult(intent, ACTIVE_UNLOCK_REQUEST);
162         }
163         return true;
164 
165     }
166 
167     @Override
getUseAnyBiometricSummary()168     protected String getUseAnyBiometricSummary() {
169         // either Active Unlock is not enabled or no device is enrolled.
170         if (mActiveUnlockDeviceNameListener == null
171                 || !mActiveUnlockDeviceNameListener.hasEnrolled()) {
172             return super.getUseAnyBiometricSummary();
173         }
174         return mActiveUnlockStatusUtils.getUnlockDeviceSummaryForActiveUnlock();
175     }
176 
177     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
178             new CombinedBiometricSearchIndexProvider(R.xml.security_settings_combined_biometric);
179 }
180