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