blob: 650b1e39e2b874646dd6c1504a2a17af6bf0b414 [file] [log] [blame]
Fan Zhang928ddbe2018-01-03 15:02:02 -08001/*
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
17package com.android.settings.security;
18
Fan Zhang928ddbe2018-01-03 15:02:02 -080019import android.app.admin.DevicePolicyManager;
20import android.content.Context;
21import android.os.UserHandle;
22import android.os.UserManager;
23import android.os.storage.StorageManager;
Fan Zhang928ddbe2018-01-03 15:02:02 -080024import android.text.TextUtils;
25
Fan Zhang23f8d592018-08-28 15:11:40 -070026import androidx.preference.Preference;
27import androidx.preference.PreferenceScreen;
28
Fan Zhang928ddbe2018-01-03 15:02:02 -080029import com.android.internal.widget.LockPatternUtils;
30import com.android.settings.R;
31import com.android.settings.Utils;
32import com.android.settings.core.PreferenceControllerMixin;
Fan Zhangb1d6c762018-02-16 16:20:30 -080033import com.android.settings.core.SubSettingLauncher;
Fan Zhang928ddbe2018-01-03 15:02:02 -080034import com.android.settings.overlay.FeatureFactory;
35import com.android.settings.password.ChooseLockGeneric;
36import com.android.settings.security.screenlock.ScreenLockSettings;
37import com.android.settings.widget.GearPreference;
38import com.android.settingslib.RestrictedLockUtils;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070039import com.android.settingslib.RestrictedLockUtilsInternal;
Fan Zhang928ddbe2018-01-03 15:02:02 -080040import com.android.settingslib.RestrictedPreference;
41import com.android.settingslib.core.AbstractPreferenceController;
42
43public class ChangeScreenLockPreferenceController extends AbstractPreferenceController implements
44 PreferenceControllerMixin, GearPreference.OnGearClickListener {
45
46 private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
47
48 protected final DevicePolicyManager mDPM;
Fan Zhang73dc4792018-01-29 12:06:58 -080049 protected final SecuritySettings mHost;
Fan Zhang928ddbe2018-01-03 15:02:02 -080050 protected final UserManager mUm;
51 protected final LockPatternUtils mLockPatternUtils;
52
53 protected final int mUserId = UserHandle.myUserId();
54 protected final int mProfileChallengeUserId;
55
56 protected RestrictedPreference mPreference;
57
Fan Zhang73dc4792018-01-29 12:06:58 -080058 public ChangeScreenLockPreferenceController(Context context, SecuritySettings host) {
Fan Zhang928ddbe2018-01-03 15:02:02 -080059 super(context);
60 mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
61 mDPM = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
62 mLockPatternUtils = FeatureFactory.getFactory(context)
63 .getSecurityFeatureProvider()
64 .getLockPatternUtils(context);
65 mHost = host;
66 mProfileChallengeUserId = Utils.getManagedProfileId(mUm, mUserId);
67 }
68
69 @Override
70 public boolean isAvailable() {
Ben Lindd1e1e12018-01-16 15:51:57 -080071 return mContext.getResources().getBoolean(R.bool.config_show_unlock_set_or_change);
Fan Zhang928ddbe2018-01-03 15:02:02 -080072 }
73
74 @Override
75 public String getPreferenceKey() {
76 return KEY_UNLOCK_SET_OR_CHANGE;
77 }
78
79 @Override
80 public void displayPreference(PreferenceScreen screen) {
81 super.displayPreference(screen);
Fan Zhang7db118e2019-02-14 12:25:29 -080082 mPreference = screen.findPreference(getPreferenceKey());
Fan Zhang928ddbe2018-01-03 15:02:02 -080083 }
84
85 @Override
86 public void updateState(Preference preference) {
Fan Zhang361e55b2018-01-03 15:02:02 -080087 if (mPreference != null && mPreference instanceof GearPreference) {
felkachangc2b67642018-07-19 20:06:50 +080088 if (mLockPatternUtils.isSecure(mUserId)) {
Fan Zhang361e55b2018-01-03 15:02:02 -080089 ((GearPreference) mPreference).setOnGearClickListener(this);
90 } else {
91 ((GearPreference) mPreference).setOnGearClickListener(null);
92 }
93 }
94
Fan Zhang928ddbe2018-01-03 15:02:02 -080095 updateSummary(preference, mUserId);
96 disableIfPasswordQualityManaged(mUserId);
97 if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)) {
98 // PO may disallow to change password for the profile, but screen lock and managed
99 // profile's lock is the same. Disable main "Screen lock" menu.
100 disableIfPasswordQualityManaged(mProfileChallengeUserId);
101 }
102 }
103
104 @Override
105 public void onGearClick(GearPreference p) {
106 if (TextUtils.equals(p.getKey(), getPreferenceKey())) {
Fan Zhangb1d6c762018-02-16 16:20:30 -0800107 new SubSettingLauncher(mContext)
108 .setDestination(ScreenLockSettings.class.getName())
109 .setSourceMetricsCategory(mHost.getMetricsCategory())
110 .launch();
Fan Zhang928ddbe2018-01-03 15:02:02 -0800111 }
112 }
113
114 @Override
115 public boolean handlePreferenceTreeClick(Preference preference) {
116 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
117 return super.handlePreferenceTreeClick(preference);
118 }
119 // TODO(b/35930129): Remove once existing password can be passed into vold directly.
120 // Currently we need this logic to ensure that the QUIET_MODE is off for any work
121 // profile with unified challenge on FBE-enabled devices. Otherwise, vold would not be
122 // able to complete the operation due to the lack of (old) encryption key.
123 if (mProfileChallengeUserId != UserHandle.USER_NULL
124 && !mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)
125 && StorageManager.isFileEncryptedNativeOnly()) {
126 if (Utils.startQuietModeDialogIfNecessary(mContext, mUm, mProfileChallengeUserId)) {
127 return false;
128 }
129 }
Fan Zhangb1d6c762018-02-16 16:20:30 -0800130
131 new SubSettingLauncher(mContext)
132 .setDestination(ChooseLockGeneric.ChooseLockGenericFragment.class.getName())
hjchangliaoe86eec02018-05-02 13:01:07 +0800133 .setTitleRes(R.string.lock_settings_picker_title)
Fan Zhangb1d6c762018-02-16 16:20:30 -0800134 .setSourceMetricsCategory(mHost.getMetricsCategory())
135 .launch();
Fan Zhang928ddbe2018-01-03 15:02:02 -0800136 return true;
137 }
138
139 protected void updateSummary(Preference preference, int userId) {
140 if (!mLockPatternUtils.isSecure(userId)) {
141 if (userId == mProfileChallengeUserId
142 || mLockPatternUtils.isLockScreenDisabled(userId)) {
143 preference.setSummary(R.string.unlock_set_unlock_mode_off);
144 } else {
145 preference.setSummary(R.string.unlock_set_unlock_mode_none);
146 }
147 } else {
148 switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(userId)) {
149 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
150 preference.setSummary(R.string.unlock_set_unlock_mode_pattern);
151 break;
152 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
153 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
154 preference.setSummary(R.string.unlock_set_unlock_mode_pin);
155 break;
156 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
157 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
158 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
159 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
160 preference.setSummary(R.string.unlock_set_unlock_mode_password);
161 break;
162 }
163 }
Fan Zhang361e55b2018-01-03 15:02:02 -0800164 mPreference.setEnabled(true);
Fan Zhang928ddbe2018-01-03 15:02:02 -0800165 }
166
167 /**
168 * Sets the preference as disabled by admin if PASSWORD_QUALITY_MANAGED is set.
169 * The preference must be a RestrictedPreference.
170 * <p/>
171 * DO or PO installed in the user may disallow to change password.
172 */
173 void disableIfPasswordQualityManaged(int userId) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700174 final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtilsInternal
Fan Zhang928ddbe2018-01-03 15:02:02 -0800175 .checkIfPasswordQualityIsSet(mContext, userId);
176 final DevicePolicyManager dpm = (DevicePolicyManager) mContext
177 .getSystemService(Context.DEVICE_POLICY_SERVICE);
178 if (admin != null && dpm.getPasswordQuality(admin.component, userId)
179 == DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
180 mPreference.setDisabledByAdmin(admin);
181 }
182 }
183}