• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
20 import static com.android.settings.biometrics.BiometricEnrollBase.EXTRA_FROM_SETTINGS_SUMMARY;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.internal.widget.LockPatternUtils;
30 import com.android.settings.Utils;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settings.overlay.FeatureFactory;
33 
34 public abstract class BiometricStatusPreferenceController extends BasePreferenceController {
35 
36     protected final UserManager mUm;
37     protected final LockPatternUtils mLockPatternUtils;
38 
39     private final int mUserId = UserHandle.myUserId();
40     protected final int mProfileChallengeUserId;
41 
42     /**
43      * @return true if the manager is not null and the hardware is detected.
44      */
isDeviceSupported()45     protected abstract boolean isDeviceSupported();
46 
47     /**
48      * @return true if the user has enrolled biometrics of the subclassed type.
49      */
hasEnrolledBiometrics()50     protected abstract boolean hasEnrolledBiometrics();
51 
52     /**
53      * @return the summary text if biometrics are enrolled.
54      */
getSummaryTextEnrolled()55     protected abstract String getSummaryTextEnrolled();
56 
57     /**
58      * @return the summary text if no biometrics are enrolled.
59      */
getSummaryTextNoneEnrolled()60     protected abstract String getSummaryTextNoneEnrolled();
61 
62     /**
63      * @return the class name for the settings page.
64      */
getSettingsClassName()65     protected abstract String getSettingsClassName();
66 
67     /**
68      * @return the class name for entry to enrollment.
69      */
getEnrollClassName()70     protected abstract String getEnrollClassName();
71 
BiometricStatusPreferenceController(Context context, String key)72     public BiometricStatusPreferenceController(Context context, String key) {
73         super(context, key);
74         mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
75         mLockPatternUtils = FeatureFactory.getFactory(context)
76                 .getSecurityFeatureProvider()
77                 .getLockPatternUtils(context);
78         mProfileChallengeUserId = Utils.getManagedProfileId(mUm, mUserId);
79     }
80 
81     @Override
getAvailabilityStatus()82     public int getAvailabilityStatus() {
83         if (!isDeviceSupported()) {
84             return UNSUPPORTED_ON_DEVICE;
85         }
86         if (isUserSupported()) {
87             return AVAILABLE;
88         } else {
89             return DISABLED_FOR_USER;
90         }
91     }
92 
93     @Override
updateState(Preference preference)94     public void updateState(Preference preference) {
95         if (!isAvailable()) {
96             if (preference != null) {
97                 preference.setVisible(false);
98             }
99             return;
100         } else {
101             preference.setVisible(true);
102         }
103         final int userId = getUserId();
104         final String clazz;
105         if (hasEnrolledBiometrics()) {
106             preference.setSummary(getSummaryTextEnrolled());
107             clazz = getSettingsClassName();
108         } else {
109             preference.setSummary(getSummaryTextNoneEnrolled());
110             clazz = getEnrollClassName();
111         }
112         preference.setOnPreferenceClickListener(target -> {
113             final Context context = target.getContext();
114             final UserManager userManager = UserManager.get(context);
115             if (Utils.startQuietModeDialogIfNecessary(context, userManager,
116                     userId)) {
117                 return false;
118             }
119             Intent intent = new Intent();
120             intent.setClassName(SETTINGS_PACKAGE_NAME, clazz);
121             intent.putExtra(Intent.EXTRA_USER_ID, userId);
122             intent.putExtra(EXTRA_FROM_SETTINGS_SUMMARY, true);
123             context.startActivity(intent);
124             return true;
125         });
126     }
127 
getUserId()128     protected int getUserId() {
129         return mUserId;
130     }
131 
isUserSupported()132     protected boolean isUserSupported() {
133         return true;
134     }
135 }
136