1 /*
2  * Copyright (C) 2020 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.settingslib;
18 
19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20 
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.util.AttributeSet;
24 
25 import androidx.core.content.res.TypedArrayUtils;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceManager;
28 import androidx.preference.PreferenceViewHolder;
29 
30 /** Top level preference that can be disabled by a device admin using a user restriction. */
31 public class RestrictedTopLevelPreference extends Preference {
32     private RestrictedPreferenceHelper mHelper;
33 
RestrictedTopLevelPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)34     public RestrictedTopLevelPreference(Context context, AttributeSet attrs,
35             int defStyleAttr, int defStyleRes) {
36         super(context, attrs, defStyleAttr, defStyleRes);
37         mHelper = new RestrictedPreferenceHelper(context, /* preference= */ this, attrs);
38     }
39 
RestrictedTopLevelPreference(Context context, AttributeSet attrs, int defStyleAttr)40     public RestrictedTopLevelPreference(Context context, AttributeSet attrs, int defStyleAttr) {
41         this(context, attrs, defStyleAttr, /* defStyleRes= */ 0);
42     }
43 
RestrictedTopLevelPreference(Context context, AttributeSet attrs)44     public RestrictedTopLevelPreference(Context context, AttributeSet attrs) {
45         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
46                 android.R.attr.preferenceStyle));
47     }
48 
RestrictedTopLevelPreference(Context context)49     public RestrictedTopLevelPreference(Context context) {
50         this(context, /* attrs= */ null);
51     }
52 
53     @Override
onBindViewHolder(PreferenceViewHolder holder)54     public void onBindViewHolder(PreferenceViewHolder holder) {
55         super.onBindViewHolder(holder);
56         mHelper.onBindViewHolder(holder);
57     }
58 
59     @Override
performClick()60     public void performClick() {
61         if (!mHelper.performClick()) {
62             super.performClick();
63         }
64     }
65 
66     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)67     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
68         mHelper.onAttachedToHierarchy();
69         super.onAttachedToHierarchy(preferenceManager);
70     }
71 
72     /**
73      * Set the user restriction and disable this preference.
74      *
75      * @param userRestriction constant from {@link android.os.UserManager}
76      */
checkRestrictionAndSetDisabled(String userRestriction)77     public void checkRestrictionAndSetDisabled(String userRestriction) {
78         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
79     }
80 
81     /**
82      * Set the user restriction and disable this preference for the given user.
83      *
84      * @param userRestriction constant from {@link android.os.UserManager}
85      * @param userId          user to check the restriction for.
86      */
checkRestrictionAndSetDisabled(String userRestriction, int userId)87     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
88         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
89     }
90 
91     @Override
setEnabled(boolean enabled)92     public void setEnabled(boolean enabled) {
93         if (enabled && isDisabledByAdmin()) {
94             mHelper.setDisabledByAdmin(/* admin= */ null);
95             return;
96         }
97         super.setEnabled(enabled);
98     }
99 
100     /**
101      * Check whether this preference is disabled by admin.
102      *
103      * @return true if this preference is disabled by admin.
104      */
isDisabledByAdmin()105     public boolean isDisabledByAdmin() {
106         return mHelper.isDisabledByAdmin();
107     }
108 
109     /**
110      * Disable preference based on the enforce admin.
111      *
112      * @param admin details of the admin who enforced the restriction. If it is {@code null}, then
113      *              this preference will be enabled. Otherwise, it will be disabled.
114      */
setDisabledByAdmin(EnforcedAdmin admin)115     public void setDisabledByAdmin(EnforcedAdmin admin) {
116         if (mHelper.setDisabledByAdmin(admin)) {
117             notifyChanged();
118         }
119     }
120 }
121