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.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 import android.text.TextUtils;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.SettingsPreferenceFragment;
30 import com.android.settings.Utils;
31 import com.android.settings.core.SubSettingLauncher;
32 import com.android.settings.password.ChooseLockGeneric;
33 import com.android.settingslib.transition.SettingsTransitionHelper;
34 
35 public class ChangeProfileScreenLockPreferenceController extends
36         ChangeScreenLockPreferenceController {
37 
38     private static final String KEY_UNLOCK_SET_OR_CHANGE_PROFILE = "unlock_set_or_change_profile";
39 
40     private final String mPreferenceKey;
41 
ChangeProfileScreenLockPreferenceController(Context context, SettingsPreferenceFragment host)42     public ChangeProfileScreenLockPreferenceController(Context context,
43             SettingsPreferenceFragment host) {
44         this(context, host, KEY_UNLOCK_SET_OR_CHANGE_PROFILE);
45     }
46 
ChangeProfileScreenLockPreferenceController(Context context, SettingsPreferenceFragment host, String key)47     public ChangeProfileScreenLockPreferenceController(Context context,
48             SettingsPreferenceFragment host, String key) {
49         super(context, host);
50         this.mPreferenceKey = key;
51     }
52 
isAvailable()53     public boolean isAvailable() {
54         if (mProfileChallengeUserId == UserHandle.USER_NULL ||
55                 !mLockPatternUtils.isSeparateProfileChallengeAllowed(mProfileChallengeUserId)) {
56             return false;
57         }
58         if (!mLockPatternUtils.isSecure(mProfileChallengeUserId)) {
59             return true;
60         }
61         switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId)) {
62             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
63             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
64             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
65             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
66             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
67             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
68             case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
69                 return true;
70         }
71         return false;
72     }
73 
74     @Override
getPreferenceKey()75     public String getPreferenceKey() {
76         return mPreferenceKey;
77     }
78 
79     @Override
handlePreferenceTreeClick(Preference preference)80     public boolean handlePreferenceTreeClick(Preference preference) {
81         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
82             return false;
83         }
84         if (Utils.startQuietModeDialogIfNecessary(mContext, mUm, mProfileChallengeUserId)) {
85             return false;
86         }
87         final Bundle extras = new Bundle();
88         extras.putInt(Intent.EXTRA_USER_ID, mProfileChallengeUserId);
89         new SubSettingLauncher(mContext)
90                 .setDestination(ChooseLockGeneric.ChooseLockGenericFragment.class.getName())
91                 .setSourceMetricsCategory(mHost.getMetricsCategory())
92                 .setArguments(extras)
93                 .setTransitionType(SettingsTransitionHelper.TransitionType.TRANSITION_SLIDE)
94                 .launch();
95 
96         return true;
97     }
98 
99     @Override
updateState(Preference preference)100     public void updateState(Preference preference) {
101         updateSummary(preference, mProfileChallengeUserId);
102 
103         if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)) {
104             final String summary = mContext.getString(
105                     R.string.lock_settings_profile_unified_summary);
106             mPreference.setSummary(summary);
107             mPreference.setEnabled(false);
108         } else {
109             // PO may disallow to change profile password, and the profile's password is
110             // separated from screen lock password. Disable profile specific "Screen lock" menu.
111             disableIfPasswordQualityManaged(mProfileChallengeUserId);
112         }
113     }
114 }
115