• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 package com.android.settings.security;
17 
18 import static android.view.contentprotection.flags.Flags.manageDevicePolicyEnabled;
19 
20 import android.app.admin.DevicePolicyManager;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.provider.Settings;
26 import android.widget.CompoundButton;
27 import android.widget.CompoundButton.OnCheckedChangeListener;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.annotation.VisibleForTesting;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceScreen;
34 
35 import com.android.settings.R;
36 import com.android.settings.core.TogglePreferenceController;
37 import com.android.settings.widget.SettingsMainSwitchPreference;
38 import com.android.settingslib.RestrictedLockUtils;
39 import com.android.settingslib.RestrictedLockUtilsInternal;
40 
41 /** Preference controller for content protection toggle switch bar. */
42 public class ContentProtectionTogglePreferenceController extends TogglePreferenceController
43         implements OnCheckedChangeListener {
44 
45     @VisibleForTesting
46     static final String KEY_CONTENT_PROTECTION_PREFERENCE = "content_protection_user_consent";
47 
48     @Nullable private SettingsMainSwitchPreference mSwitchBar;
49 
50     @Nullable private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
51 
52     @NonNull private final ContentResolver mContentResolver;
53 
54     @DevicePolicyManager.ContentProtectionPolicy
55     private int mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
56 
ContentProtectionTogglePreferenceController(Context context, String preferenceKey)57     public ContentProtectionTogglePreferenceController(Context context, String preferenceKey) {
58         super(context, preferenceKey);
59         mContentResolver = context.getContentResolver();
60 
61         if (manageDevicePolicyEnabled()) {
62             mEnforcedAdmin = getEnforcedAdmin();
63             mContentProtectionPolicy = getContentProtectionPolicy(getManagedProfile());
64         }
65     }
66 
67     @Override
getAvailabilityStatus()68     public int getAvailabilityStatus() {
69         return AVAILABLE;
70     }
71 
72     @Override
isChecked()73     public boolean isChecked() {
74         if (mEnforcedAdmin != null) {
75             if (!manageDevicePolicyEnabled()) {
76                 // If fully managed device, it should always unchecked
77                 return false;
78             }
79 
80             if (mContentProtectionPolicy == DevicePolicyManager.CONTENT_PROTECTION_DISABLED) {
81                 return false;
82             }
83             if (mContentProtectionPolicy == DevicePolicyManager.CONTENT_PROTECTION_ENABLED) {
84                 return true;
85             }
86         }
87         return Settings.Global.getInt(mContentResolver, KEY_CONTENT_PROTECTION_PREFERENCE, 0) >= 0;
88     }
89 
90     @Override
setChecked(boolean isChecked)91     public boolean setChecked(boolean isChecked) {
92         if (manageDevicePolicyEnabled()) {
93             if (mEnforcedAdmin != null
94                     && mContentProtectionPolicy
95                             != DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY) {
96                 return false;
97             }
98         }
99         Settings.Global.putInt(
100                 mContentResolver, KEY_CONTENT_PROTECTION_PREFERENCE, isChecked ? 1 : -1);
101         return true;
102     }
103 
104     @Override
displayPreference(PreferenceScreen screen)105     public void displayPreference(PreferenceScreen screen) {
106         super.displayPreference(screen);
107         final Preference preference = screen.findPreference(getPreferenceKey());
108 
109         if (preference instanceof SettingsMainSwitchPreference) {
110             mSwitchBar = (SettingsMainSwitchPreference) preference;
111             mSwitchBar.addOnSwitchChangeListener(this);
112         }
113     }
114 
115     // Workaround for SettingsMainSwitchPreference.setDisabledByAdmin without user restriction.
116     @Override
updateState(Preference preference)117     public void updateState(Preference preference) {
118         super.updateState(preference);
119 
120         if (!manageDevicePolicyEnabled()) {
121             // Assign the value to mEnforcedAdmin since it's needed in isChecked()
122             mEnforcedAdmin = getEnforcedAdmin();
123             mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
124         }
125         if (mSwitchBar != null
126                 && mEnforcedAdmin != null
127                 && mContentProtectionPolicy
128                         != DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY) {
129             mSwitchBar.setDisabledByAdmin(mEnforcedAdmin);
130             return;
131         }
132 
133         UserManager userManager = mContext.getSystemService(UserManager.class);
134         if (userManager != null
135                 && userManager.isGuestUser()
136                 && mSwitchBar != null) {
137             mSwitchBar.setEnabled(false);
138         }
139     }
140 
141     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)142     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
143         if (isChecked != isChecked()) {
144             setChecked(isChecked);
145         }
146     }
147 
148     @Override
getSliceHighlightMenuRes()149     public int getSliceHighlightMenuRes() {
150         return R.string.menu_key_security;
151     }
152 
153     @VisibleForTesting
154     @Nullable
getManagedProfile()155     protected UserHandle getManagedProfile() {
156         return ContentProtectionPreferenceUtils.getManagedProfile(mContext);
157     }
158 
159     @VisibleForTesting
160     @Nullable
getEnforcedAdmin()161     protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
162         return RestrictedLockUtilsInternal.getDeviceOwner(mContext);
163     }
164 
165     @VisibleForTesting
166     @DevicePolicyManager.ContentProtectionPolicy
getContentProtectionPolicy(@ullable UserHandle userHandle)167     protected int getContentProtectionPolicy(@Nullable UserHandle userHandle) {
168         return ContentProtectionPreferenceUtils.getContentProtectionPolicy(mContext, userHandle);
169     }
170 }
171