1 /* 2 * Copyright (C) 2021 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.display; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.os.UserHandle; 22 import android.provider.Settings; 23 24 import androidx.preference.Preference; 25 26 import com.android.internal.widget.LockPatternUtils; 27 import com.android.settings.R; 28 import com.android.settings.core.TogglePreferenceController; 29 import com.android.settings.overlay.FeatureFactory; 30 31 /** 32 * Preference for showing/hiding sensitive device controls content while the device is locked. 33 */ 34 public class ControlsPrivacyPreferenceController extends TogglePreferenceController { 35 36 private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_CONTROLS; 37 ControlsPrivacyPreferenceController(Context context, String preferenceKey)38 public ControlsPrivacyPreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 } 41 42 @Override isChecked()43 public boolean isChecked() { 44 return Settings.Secure.getInt(mContext.getContentResolver(), SETTING_KEY, 0) != 0; 45 } 46 47 @Override setChecked(boolean isChecked)48 public boolean setChecked(boolean isChecked) { 49 return Settings.Secure.putInt(mContext.getContentResolver(), SETTING_KEY, 50 isChecked ? 1 : 0); 51 } 52 53 @Override getSummary()54 public CharSequence getSummary() { 55 final int res = isSecure() ? R.string.lockscreen_privacy_controls_summary : 56 R.string.lockscreen_privacy_not_secure; 57 return mContext.getText(res); 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 // hide if lockscreen isn't secure for this user 63 return isEnabled() && isSecure() ? AVAILABLE : DISABLED_DEPENDENT_SETTING; 64 } 65 66 @Override updateState(Preference preference)67 public void updateState(Preference preference) { 68 super.updateState(preference); 69 preference.setEnabled(getAvailabilityStatus() != DISABLED_DEPENDENT_SETTING); 70 refreshSummary(preference); 71 } 72 isEnabled()73 private boolean isEnabled() { 74 return isControlsAvailable(); 75 } 76 isSecure()77 private boolean isSecure() { 78 final LockPatternUtils utils = FeatureFactory.getFactory(mContext) 79 .getSecurityFeatureProvider() 80 .getLockPatternUtils(mContext); 81 final int userId = UserHandle.myUserId(); 82 return utils.isSecure(userId); 83 } 84 isControlsAvailable()85 private boolean isControlsAvailable() { 86 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS); 87 } 88 } 89