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  * Note that ControlsTrivialPrivacyPreferenceController depends on the preferenceKey
35  * of this controller.
36  */
37 public class ControlsPrivacyPreferenceController extends TogglePreferenceController {
38 
39     private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_CONTROLS;
40 
ControlsPrivacyPreferenceController(Context context, String preferenceKey)41     public ControlsPrivacyPreferenceController(Context context, String preferenceKey) {
42         super(context, preferenceKey);
43     }
44 
45     @Override
isChecked()46     public boolean isChecked() {
47         return Settings.Secure.getInt(mContext.getContentResolver(), SETTING_KEY, 0) != 0;
48     }
49 
50     @Override
setChecked(boolean isChecked)51     public boolean setChecked(boolean isChecked) {
52         return Settings.Secure.putInt(mContext.getContentResolver(), SETTING_KEY,
53                 isChecked ? 1 : 0);
54     }
55 
56     @Override
getSummary()57     public CharSequence getSummary() {
58         final int res = isSecure() ? R.string.lockscreen_privacy_controls_summary :
59                 R.string.lockscreen_privacy_not_secure;
60         return mContext.getText(res);
61     }
62 
63     @Override
getAvailabilityStatus()64     public int getAvailabilityStatus() {
65         // hide if we should use customizable lock screen quick affordances
66         if (CustomizableLockScreenUtils.isFeatureEnabled(mContext)) {
67             return UNSUPPORTED_ON_DEVICE;
68         }
69 
70         // hide if lockscreen isn't secure for this user
71         return isEnabled() && isSecure() ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
72     }
73 
74     @Override
updateState(Preference preference)75     public void updateState(Preference preference) {
76         super.updateState(preference);
77         preference.setEnabled(getAvailabilityStatus() != DISABLED_DEPENDENT_SETTING);
78         refreshSummary(preference);
79     }
80 
81     @Override
getSliceHighlightMenuRes()82     public int getSliceHighlightMenuRes() {
83         return R.string.menu_key_display;
84     }
85 
isEnabled()86     private boolean isEnabled() {
87         return isControlsAvailable();
88     }
89 
isSecure()90     private boolean isSecure() {
91         final LockPatternUtils utils = FeatureFactory.getFeatureFactory()
92                 .getSecurityFeatureProvider()
93                 .getLockPatternUtils(mContext);
94         final int userId = UserHandle.myUserId();
95         return utils.isSecure(userId);
96     }
97 
isControlsAvailable()98     private boolean isControlsAvailable() {
99         return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS);
100     }
101 }
102