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.Context;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settingslib.RestrictedLockUtils;
32 import com.android.settingslib.RestrictedSwitchPreference;
33 
34 /** Preference controller for content protection work profile switch bar. */
35 public class ContentProtectionWorkSwitchController extends TogglePreferenceController {
36 
37     @Nullable private UserHandle mManagedProfile;
38 
39     @DevicePolicyManager.ContentProtectionPolicy
40     private int mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
41 
ContentProtectionWorkSwitchController( @onNull Context context, @NonNull String preferenceKey)42     public ContentProtectionWorkSwitchController(
43             @NonNull Context context, @NonNull String preferenceKey) {
44         super(context, preferenceKey);
45 
46         if (manageDevicePolicyEnabled()) {
47             mManagedProfile = getManagedProfile();
48             if (mManagedProfile != null) {
49                 mContentProtectionPolicy = getContentProtectionPolicy(mManagedProfile);
50             }
51         }
52     }
53 
54     @Override
getAvailabilityStatus()55     public int getAvailabilityStatus() {
56         if (!manageDevicePolicyEnabled()) {
57             return getManagedProfile() != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
58         }
59         if (mManagedProfile == null) {
60             return CONDITIONALLY_UNAVAILABLE;
61         }
62         if (mContentProtectionPolicy
63                 == DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY) {
64             return CONDITIONALLY_UNAVAILABLE;
65         }
66         return AVAILABLE;
67     }
68 
69     @Override
isChecked()70     public boolean isChecked() {
71         if (!manageDevicePolicyEnabled()) {
72             return false;
73         }
74         return mContentProtectionPolicy == DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
75     }
76 
77     @Override
setChecked(boolean isChecked)78     public boolean setChecked(boolean isChecked) {
79         // Controlled by the admin API
80         return false;
81     }
82 
83     @Override
displayPreference(PreferenceScreen screen)84     public void displayPreference(PreferenceScreen screen) {
85         super.displayPreference(screen);
86 
87         UserHandle managedProfile =
88                 manageDevicePolicyEnabled() ? mManagedProfile : getManagedProfile();
89         if (managedProfile != null) {
90             RestrictedSwitchPreference switchPreference = screen.findPreference(getPreferenceKey());
91             if (switchPreference != null) {
92                 switchPreference.setDisabledByAdmin(getEnforcedAdmin(managedProfile));
93             }
94         }
95     }
96 
97     @Override
getSliceHighlightMenuRes()98     public int getSliceHighlightMenuRes() {
99         return R.string.menu_key_security;
100     }
101 
102     @VisibleForTesting
103     @Nullable
getManagedProfile()104     protected UserHandle getManagedProfile() {
105         return ContentProtectionPreferenceUtils.getManagedProfile(mContext);
106     }
107 
108     @VisibleForTesting
109     @Nullable
getEnforcedAdmin(@onNull UserHandle userHandle)110     protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(@NonNull UserHandle userHandle) {
111         return RestrictedLockUtils.getProfileOrDeviceOwner(mContext, userHandle);
112     }
113 
114     @VisibleForTesting
115     @DevicePolicyManager.ContentProtectionPolicy
getContentProtectionPolicy(@ullable UserHandle userHandle)116     protected int getContentProtectionPolicy(@Nullable UserHandle userHandle) {
117         return ContentProtectionPreferenceUtils.getContentProtectionPolicy(mContext, userHandle);
118     }
119 }
120