1 /*
2  * Copyright (C) 2017 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.screenlock;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.text.TextUtils;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.TwoStatePreference;
25 
26 import com.android.internal.widget.LockPatternUtils;
27 import com.android.settings.R;
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settings.overlay.FeatureFactory;
30 import com.android.settings.security.trustagent.TrustAgentManager;
31 import com.android.settingslib.core.AbstractPreferenceController;
32 
33 public class PowerButtonInstantLockPreferenceController extends AbstractPreferenceController
34         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
35 
36     private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
37 
38     private final int mUserId;
39     private final LockPatternUtils mLockPatternUtils;
40     private final TrustAgentManager mTrustAgentManager;
41 
PowerButtonInstantLockPreferenceController(Context context, int userId, LockPatternUtils lockPatternUtils)42     public PowerButtonInstantLockPreferenceController(Context context, int userId,
43             LockPatternUtils lockPatternUtils) {
44         super(context);
45         mUserId = userId;
46         mLockPatternUtils = lockPatternUtils;
47         mTrustAgentManager = FeatureFactory.getFactory(context)
48                 .getSecurityFeatureProvider().getTrustAgentManager();
49     }
50 
51     @Override
isAvailable()52     public boolean isAvailable() {
53         if (!mLockPatternUtils.isSecure(mUserId)) {
54             return false;
55         }
56         switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(mUserId)) {
57             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
58             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
59             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
60             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
61             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
62             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
63             case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
64                 return true;
65             default:
66                 return false;
67         }
68     }
69 
70     @Override
updateState(Preference preference)71     public void updateState(Preference preference) {
72         ((TwoStatePreference) preference).setChecked(
73                 mLockPatternUtils.getPowerButtonInstantlyLocks(mUserId));
74         final CharSequence trustAgentLabel = mTrustAgentManager.getActiveTrustAgentLabel(
75                 mContext, mLockPatternUtils);
76         if (!TextUtils.isEmpty(trustAgentLabel)) {
77             preference.setSummary(mContext.getString(
78                     R.string.lockpattern_settings_power_button_instantly_locks_summary,
79                     trustAgentLabel));
80         } else {
81             preference.setSummary(R.string.summary_placeholder);
82         }
83     }
84 
85     @Override
getPreferenceKey()86     public String getPreferenceKey() {
87         return KEY_POWER_INSTANTLY_LOCKS;
88     }
89 
90     @Override
onPreferenceChange(Preference preference, Object newValue)91     public boolean onPreferenceChange(Preference preference, Object newValue) {
92         mLockPatternUtils.setPowerButtonInstantlyLocks((Boolean) newValue, mUserId);
93         return true;
94     }
95 }
96