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.annotation.NonNull;
27 import androidx.core.content.res.TypedArrayUtils;
28 import androidx.preference.PreferenceManager;
29 import androidx.preference.PreferenceViewHolder;
30 
31 import com.android.settingslib.widget.TwoTargetPreference;
32 
33 /**
34  * Preference class that supports being disabled by a user restriction
35  * set by a device admin.
36  */
37 public class RestrictedPreference extends TwoTargetPreference {
38     RestrictedPreferenceHelper mHelper;
39 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, String packageName, int uid)40     public RestrictedPreference(Context context, AttributeSet attrs,
41             int defStyleAttr, int defStyleRes, String packageName, int uid) {
42         super(context, attrs, defStyleAttr, defStyleRes);
43         mHelper = new RestrictedPreferenceHelper(context, this, attrs, packageName, uid);
44     }
45 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)46     public RestrictedPreference(Context context, AttributeSet attrs,
47             int defStyleAttr, int defStyleRes) {
48         this(context, attrs, defStyleAttr, defStyleRes, null, Process.INVALID_UID);
49     }
50 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr)51     public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
52         this(context, attrs, defStyleAttr, 0);
53     }
54 
RestrictedPreference(Context context, AttributeSet attrs)55     public RestrictedPreference(Context context, AttributeSet attrs) {
56         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
57                 android.R.attr.preferenceStyle));
58     }
59 
RestrictedPreference(Context context)60     public RestrictedPreference(Context context) {
61         this(context, null);
62     }
63 
RestrictedPreference(Context context, String packageName, int uid)64     public RestrictedPreference(Context context, String packageName, int uid) {
65         this(context, null, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
66                 android.R.attr.preferenceStyle), 0, packageName, uid);
67     }
68 
69     @Override
onBindViewHolder(PreferenceViewHolder holder)70     public void onBindViewHolder(PreferenceViewHolder holder) {
71         super.onBindViewHolder(holder);
72         mHelper.onBindViewHolder(holder);
73     }
74 
75     @Override
performClick()76     public void performClick() {
77         if (!mHelper.performClick()) {
78             super.performClick();
79         }
80     }
81 
useAdminDisabledSummary(boolean useSummary)82     public void useAdminDisabledSummary(boolean useSummary) {
83         mHelper.useAdminDisabledSummary(useSummary);
84     }
85 
86     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)87     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
88         mHelper.onAttachedToHierarchy();
89         super.onAttachedToHierarchy(preferenceManager);
90     }
91 
checkRestrictionAndSetDisabled(String userRestriction)92     public void checkRestrictionAndSetDisabled(String userRestriction) {
93         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
94     }
95 
checkRestrictionAndSetDisabled(String userRestriction, int userId)96     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
97         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
98     }
99 
100     /**
101      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
102      * package. Marks the preference as disabled if so.
103      * @param settingIdentifier The key identifying the setting
104      * @param packageName the package to check the settingIdentifier for
105      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName)106     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
107             @NonNull String packageName) {
108         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName);
109     }
110 
111     @Override
setEnabled(boolean enabled)112     public void setEnabled(boolean enabled) {
113         if (enabled && isDisabledByAdmin()) {
114             mHelper.setDisabledByAdmin(null);
115             return;
116         }
117 
118         if (enabled && isDisabledByEcm()) {
119             mHelper.setDisabledByEcm(null);
120             return;
121         }
122 
123         super.setEnabled(enabled);
124     }
125 
setDisabledByAdmin(EnforcedAdmin admin)126     public void setDisabledByAdmin(EnforcedAdmin admin) {
127         if (mHelper.setDisabledByAdmin(admin)) {
128             notifyChanged();
129         }
130     }
131 
isDisabledByAdmin()132     public boolean isDisabledByAdmin() {
133         return mHelper.isDisabledByAdmin();
134     }
135 
isDisabledByEcm()136     public boolean isDisabledByEcm() {
137         return mHelper.isDisabledByEcm();
138     }
139 
getUid()140     public int getUid() {
141         return mHelper != null ? mHelper.uid : Process.INVALID_UID;
142     }
143 
getPackageName()144     public String getPackageName() {
145         return mHelper != null ? mHelper.packageName : null;
146     }
147 
148     /**
149      * @deprecated TODO(b/308921175): This will be deleted with the
150      * {@link android.security.Flags#extendEcmToAllSettings} feature flag. Do not use for any new
151      * code.
152      */
153     @Deprecated
setDisabledByAppOps(boolean disabled)154     public void setDisabledByAppOps(boolean disabled) {
155         if (mHelper.setDisabledByAppOps(disabled)) {
156             notifyChanged();
157         }
158     }
159 }
160