• 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.security;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.os.UserManager;
22 import android.text.TextUtils;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.internal.widget.LockPatternUtils;
28 import com.android.settings.SettingsPreferenceFragment;
29 import com.android.settings.Utils;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settings.dashboard.DashboardFragment;
32 import com.android.settings.overlay.FeatureFactory;
33 import com.android.settings.widget.GearPreference;
34 import com.android.settingslib.RestrictedLockUtils;
35 import com.android.settingslib.RestrictedLockUtilsInternal;
36 import com.android.settingslib.RestrictedPreference;
37 import com.android.settingslib.core.AbstractPreferenceController;
38 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
39 
40 public class ChangeScreenLockPreferenceController extends AbstractPreferenceController implements
41         PreferenceControllerMixin, GearPreference.OnGearClickListener {
42 
43     private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
44 
45     protected final SettingsPreferenceFragment mHost;
46     protected final UserManager mUm;
47     protected final LockPatternUtils mLockPatternUtils;
48 
49     protected final int mUserId = UserHandle.myUserId();
50     protected final int mProfileChallengeUserId;
51     private final MetricsFeatureProvider mMetricsFeatureProvider;
52     private final ScreenLockPreferenceDetailsUtils mScreenLockPreferenceDetailUtils;
53 
54     protected RestrictedPreference mPreference;
55 
ChangeScreenLockPreferenceController(Context context, SettingsPreferenceFragment host)56     public ChangeScreenLockPreferenceController(Context context, SettingsPreferenceFragment host) {
57         super(context);
58         mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
59         mLockPatternUtils = FeatureFactory.getFeatureFactory()
60                 .getSecurityFeatureProvider()
61                 .getLockPatternUtils(context);
62         mHost = host;
63         mProfileChallengeUserId = Utils.getManagedProfileId(mUm, mUserId);
64         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
65         mScreenLockPreferenceDetailUtils = new ScreenLockPreferenceDetailsUtils(context);
66     }
67 
68     @Override
isAvailable()69     public boolean isAvailable() {
70         return mScreenLockPreferenceDetailUtils.isAvailable();
71     }
72 
73     @Override
getPreferenceKey()74     public String getPreferenceKey() {
75         return KEY_UNLOCK_SET_OR_CHANGE;
76     }
77 
78     @Override
displayPreference(PreferenceScreen screen)79     public void displayPreference(PreferenceScreen screen) {
80         super.displayPreference(screen);
81         mPreference = screen.findPreference(getPreferenceKey());
82     }
83 
84     @Override
updateState(Preference preference)85     public void updateState(Preference preference) {
86         if (mPreference != null && mPreference instanceof GearPreference) {
87             if (mScreenLockPreferenceDetailUtils.shouldShowGearMenu()) {
88                 ((GearPreference) mPreference).setOnGearClickListener(this);
89             } else {
90                 ((GearPreference) mPreference).setOnGearClickListener(null);
91             }
92         }
93 
94         updateSummary(preference, mUserId);
95         disableIfPasswordQualityManaged(mUserId);
96         if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)) {
97             // PO may disallow to change password for the profile, but screen lock and managed
98             // profile's lock is the same. Disable main "Screen lock" menu.
99             disableIfPasswordQualityManaged(mProfileChallengeUserId);
100         }
101     }
102 
103     @Override
onGearClick(GearPreference p)104     public void onGearClick(GearPreference p) {
105         if (TextUtils.equals(p.getKey(), getPreferenceKey())) {
106             mMetricsFeatureProvider.logClickedPreference(p,
107                     p.getExtras().getInt(DashboardFragment.CATEGORY));
108             mScreenLockPreferenceDetailUtils.openScreenLockSettings(mHost.getMetricsCategory());
109         }
110     }
111 
112     @Override
handlePreferenceTreeClick(Preference preference)113     public boolean handlePreferenceTreeClick(Preference preference) {
114         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
115             return super.handlePreferenceTreeClick(preference);
116         }
117         return mScreenLockPreferenceDetailUtils.openChooseLockGenericFragment(
118                 mHost.getMetricsCategory());
119     }
120 
updateSummary(Preference preference, int userId)121     protected void updateSummary(Preference preference, int userId) {
122         preference.setSummary(mScreenLockPreferenceDetailUtils.getSummary(userId));
123         mPreference.setEnabled(true);
124     }
125 
126     /**
127      * Sets the preference as disabled by admin if PASSWORD_QUALITY_MANAGED is set.
128      * The preference must be a RestrictedPreference.
129      * <p/>
130      * DO or PO installed in the user may disallow to change password.
131      */
disableIfPasswordQualityManaged(int userId)132     void disableIfPasswordQualityManaged(int userId) {
133         final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtilsInternal
134                 .checkIfPasswordQualityIsSet(mContext, userId);
135         if (mScreenLockPreferenceDetailUtils.isPasswordQualityManaged(userId, admin)) {
136             mPreference.setDisabledByAdmin(admin);
137         }
138     }
139 }
140