• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 implements
38         RestrictedPreferenceHelperProvider {
39 
40     RestrictedPreferenceHelper mHelper;
41 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, String packageName, int uid)42     public RestrictedPreference(Context context, AttributeSet attrs,
43             int defStyleAttr, int defStyleRes, String packageName, int uid) {
44         super(context, attrs, defStyleAttr, defStyleRes);
45         mHelper = new RestrictedPreferenceHelper(context, this, attrs, packageName, uid);
46     }
47 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48     public RestrictedPreference(Context context, AttributeSet attrs,
49             int defStyleAttr, int defStyleRes) {
50         this(context, attrs, defStyleAttr, defStyleRes, null, Process.INVALID_UID);
51     }
52 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr)53     public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
54         this(context, attrs, defStyleAttr, 0);
55     }
56 
RestrictedPreference(Context context, AttributeSet attrs)57     public RestrictedPreference(Context context, AttributeSet attrs) {
58         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
59                 android.R.attr.preferenceStyle));
60     }
61 
RestrictedPreference(Context context)62     public RestrictedPreference(Context context) {
63         this(context, null);
64     }
65 
RestrictedPreference(Context context, String packageName, int uid)66     public RestrictedPreference(Context context, String packageName, int uid) {
67         this(context, null, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
68                 android.R.attr.preferenceStyle), 0, packageName, uid);
69     }
70 
71     @Override
getRestrictedPreferenceHelper()72     public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() {
73         return mHelper;
74     }
75 
76     @Override
onBindViewHolder(PreferenceViewHolder holder)77     public void onBindViewHolder(PreferenceViewHolder holder) {
78         super.onBindViewHolder(holder);
79         mHelper.onBindViewHolder(holder);
80     }
81 
82     @Override
performClick()83     public void performClick() {
84         if (!mHelper.performClick()) {
85             super.performClick();
86         }
87     }
88 
useAdminDisabledSummary(boolean useSummary)89     public void useAdminDisabledSummary(boolean useSummary) {
90         mHelper.useAdminDisabledSummary(useSummary);
91     }
92 
93     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)94     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
95         mHelper.onAttachedToHierarchy();
96         super.onAttachedToHierarchy(preferenceManager);
97     }
98 
checkRestrictionAndSetDisabled(String userRestriction)99     public void checkRestrictionAndSetDisabled(String userRestriction) {
100         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
101     }
102 
checkRestrictionAndSetDisabled(String userRestriction, int userId)103     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
104         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
105     }
106 
107     /**
108      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
109      * package. Marks the preference as disabled if so.
110      * TODO b/390196024: remove this and update all callers to use the "settingEnabled" version
111      * @param settingIdentifier The key identifying the setting
112      * @param packageName the package to check the settingIdentifier for
113      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName)114     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
115             @NonNull String packageName) {
116         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName, false);
117     }
118 
119     /**
120      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
121      * package. Marks the preference as disabled if so.
122      * @param settingIdentifier The key identifying the setting
123      * @param packageName the package to check the settingIdentifier for
124      * @param settingEnabled Whether the setting in question is enabled
125      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName, boolean settingEnabled)126     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
127             @NonNull String packageName, boolean settingEnabled) {
128         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName, settingEnabled);
129     }
130 
131     @Override
setEnabled(boolean enabled)132     public void setEnabled(boolean enabled) {
133         if (enabled && isDisabledByAdmin()) {
134             mHelper.setDisabledByAdmin(null);
135             return;
136         }
137 
138         if (enabled && isDisabledByEcm()) {
139             mHelper.setDisabledByEcm(null);
140             return;
141         }
142 
143         super.setEnabled(enabled);
144     }
145 
setDisabledByAdmin(EnforcedAdmin admin)146     public void setDisabledByAdmin(EnforcedAdmin admin) {
147         if (mHelper.setDisabledByAdmin(admin)) {
148             notifyChanged();
149         }
150     }
151 
isDisabledByAdmin()152     public boolean isDisabledByAdmin() {
153         return mHelper.isDisabledByAdmin();
154     }
155 
isDisabledByEcm()156     public boolean isDisabledByEcm() {
157         return mHelper.isDisabledByEcm();
158     }
159 
getUid()160     public int getUid() {
161         return mHelper != null ? mHelper.uid : Process.INVALID_UID;
162     }
163 
getPackageName()164     public String getPackageName() {
165         return mHelper != null ? mHelper.packageName : null;
166     }
167 
168     /**
169      * @deprecated TODO(b/308921175): This will be deleted with the
170      * {@link android.security.Flags#extendEcmToAllSettings} feature flag. Do not use for any new
171      * code.
172      */
173     @Deprecated
setDisabledByAppOps(boolean disabled)174     public void setDisabledByAppOps(boolean disabled) {
175         if (mHelper.setDisabledByAppOps(disabled)) {
176             notifyChanged();
177         }
178     }
179 }
180