1 /*
2  * Copyright (C) 2016 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.Process;
23 import android.os.UserHandle;
24 import android.util.AttributeSet;
25 
26 import androidx.core.content.res.TypedArrayUtils;
27 import androidx.preference.PreferenceManager;
28 import androidx.preference.PreferenceViewHolder;
29 
30 import com.android.settingslib.widget.TwoTargetPreference;
31 
32 /**
33  * Preference class that supports being disabled by a user restriction
34  * set by a device admin.
35  */
36 public class RestrictedPreference extends TwoTargetPreference {
37     RestrictedPreferenceHelper mHelper;
38 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, String packageName, int uid)39     public RestrictedPreference(Context context, AttributeSet attrs,
40             int defStyleAttr, int defStyleRes, String packageName, int uid) {
41         super(context, attrs, defStyleAttr, defStyleRes);
42         mHelper = new RestrictedPreferenceHelper(context, this, attrs, packageName, uid);
43     }
44 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)45     public RestrictedPreference(Context context, AttributeSet attrs,
46             int defStyleAttr, int defStyleRes) {
47         this(context, attrs, defStyleAttr, defStyleRes, null, Process.INVALID_UID);
48     }
49 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr)50     public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
51         this(context, attrs, defStyleAttr, 0);
52     }
53 
RestrictedPreference(Context context, AttributeSet attrs)54     public RestrictedPreference(Context context, AttributeSet attrs) {
55         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
56                 android.R.attr.preferenceStyle));
57     }
58 
RestrictedPreference(Context context)59     public RestrictedPreference(Context context) {
60         this(context, null);
61     }
62 
RestrictedPreference(Context context, String packageName, int uid)63     public RestrictedPreference(Context context, String packageName, int uid) {
64         this(context, null, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
65                 android.R.attr.preferenceStyle), 0, packageName, uid);
66     }
67 
68     @Override
onBindViewHolder(PreferenceViewHolder holder)69     public void onBindViewHolder(PreferenceViewHolder holder) {
70         super.onBindViewHolder(holder);
71         mHelper.onBindViewHolder(holder);
72     }
73 
74     @Override
performClick()75     public void performClick() {
76         if (!mHelper.performClick()) {
77             super.performClick();
78         }
79     }
80 
useAdminDisabledSummary(boolean useSummary)81     public void useAdminDisabledSummary(boolean useSummary) {
82         mHelper.useAdminDisabledSummary(useSummary);
83     }
84 
85     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)86     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
87         mHelper.onAttachedToHierarchy();
88         super.onAttachedToHierarchy(preferenceManager);
89     }
90 
checkRestrictionAndSetDisabled(String userRestriction)91     public void checkRestrictionAndSetDisabled(String userRestriction) {
92         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
93     }
94 
checkRestrictionAndSetDisabled(String userRestriction, int userId)95     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
96         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
97     }
98 
99     @Override
setEnabled(boolean enabled)100     public void setEnabled(boolean enabled) {
101         if (enabled && isDisabledByAdmin()) {
102             mHelper.setDisabledByAdmin(null);
103             return;
104         }
105         super.setEnabled(enabled);
106     }
107 
setDisabledByAdmin(EnforcedAdmin admin)108     public void setDisabledByAdmin(EnforcedAdmin admin) {
109         if (mHelper.setDisabledByAdmin(admin)) {
110             notifyChanged();
111         }
112     }
113 
setDisabledByAppOps(boolean disabled)114     public void setDisabledByAppOps(boolean disabled) {
115         if (mHelper.setDisabledByAppOps(disabled)) {
116             notifyChanged();
117         }
118     }
119 
isDisabledByAdmin()120     public boolean isDisabledByAdmin() {
121         return mHelper.isDisabledByAdmin();
122     }
123 
getUid()124     public int getUid() {
125         return mHelper != null ? mHelper.uid : Process.INVALID_UID;
126     }
127 
getPackageName()128     public String getPackageName() {
129         return mHelper != null ? mHelper.packageName : null;
130     }
131 }
132