• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.preference.PreferenceManager;
28 import androidx.preference.PreferenceViewHolder;
29 
30 import com.android.settingslib.widget.SelectorWithWidgetPreference;
31 
32 /**
33  * Selector with widget preference that can be disabled by a device admin using a user restriction.
34  */
35 public class RestrictedSelectorWithWidgetPreference extends SelectorWithWidgetPreference implements
36         RestrictedPreferenceHelperProvider {
37     private final RestrictedPreferenceHelper mHelper;
38 
39     /**
40      * Perform inflation from XML and apply a class-specific base style.
41      *
42      * @param context The {@link Context} this is associated with, through which it can access the
43      *     current theme, resources, {@link SharedPreferences}, etc.
44      * @param attrs The attributes of the XML tag that is inflating the preference
45      * @param defStyle An attribute in the current theme that contains a reference to a style
46      *     resource that supplies default values for the view. Can be 0 to not look for defaults.
47      */
RestrictedSelectorWithWidgetPreference( @onNull Context context, @NonNull AttributeSet attrs, int defStyle)48     public RestrictedSelectorWithWidgetPreference(
49             @NonNull Context context, @NonNull AttributeSet attrs, int defStyle) {
50         super(context, attrs, defStyle);
51         mHelper = new RestrictedPreferenceHelper(context, /* preference= */ this, attrs);
52     }
53 
54     /**
55      * Perform inflation from XML and apply a class-specific base style.
56      *
57      * @param context The {@link Context} this is associated with, through which it can access the
58      *     current theme, resources, {@link SharedPreferences}, etc.
59      * @param attrs The attributes of the XML tag that is inflating the preference
60      */
RestrictedSelectorWithWidgetPreference( @onNull Context context, @NonNull AttributeSet attrs)61     public RestrictedSelectorWithWidgetPreference(
62             @NonNull Context context, @NonNull AttributeSet attrs) {
63         super(context, attrs);
64         mHelper = new RestrictedPreferenceHelper(context, /* preference= */ this, attrs);
65     }
66 
67     /**
68      * Constructor to create a preference, which will display with a checkbox style.
69      *
70      * @param context The {@link Context} this is associated with.
71      * @param isCheckbox Whether this preference should display as a checkbox.
72      */
RestrictedSelectorWithWidgetPreference(@onNull Context context, boolean isCheckbox)73     public RestrictedSelectorWithWidgetPreference(@NonNull Context context, boolean isCheckbox) {
74         super(context, null);
75         mHelper =
76                 new RestrictedPreferenceHelper(context, /* preference= */ this, /* attrs= */ null);
77     }
78 
79     /**
80      * Constructor to create a preference.
81      *
82      * @param context The Context this is associated with.
83      */
RestrictedSelectorWithWidgetPreference(@onNull Context context)84     public RestrictedSelectorWithWidgetPreference(@NonNull Context context) {
85         this(context, null);
86     }
87 
88     @Override
getRestrictedPreferenceHelper()89     public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() {
90         return mHelper;
91     }
92 
93     @Override
onBindViewHolder(@onNull PreferenceViewHolder holder)94     public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
95         super.onBindViewHolder(holder);
96         mHelper.onBindViewHolder(holder);
97     }
98 
99     @Override
performClick()100     public void performClick() {
101         if (!mHelper.performClick()) {
102             super.performClick();
103         }
104     }
105 
106     @Override
onAttachedToHierarchy(@onNull PreferenceManager preferenceManager)107     protected void onAttachedToHierarchy(@NonNull PreferenceManager preferenceManager) {
108         mHelper.onAttachedToHierarchy();
109         super.onAttachedToHierarchy(preferenceManager);
110     }
111 
112     /**
113      * Set the user restriction and disable this preference.
114      *
115      * @param userRestriction constant from {@link android.os.UserManager}
116      */
checkRestrictionAndSetDisabled(@onNull String userRestriction)117     public void checkRestrictionAndSetDisabled(@NonNull String userRestriction) {
118         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
119     }
120 
121     /**
122      * Set the user restriction and disable this preference for the given user.
123      *
124      * @param userRestriction constant from {@link android.os.UserManager}
125      * @param userId user to check the restriction for.
126      */
checkRestrictionAndSetDisabled(@onNull String userRestriction, int userId)127     public void checkRestrictionAndSetDisabled(@NonNull String userRestriction, int userId) {
128         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
129     }
130 
131     /**
132      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
133      * package. Marks the preference as disabled if so.
134      *
135      * @param settingIdentifier The key identifying the setting
136      * @param packageName the package to check the settingIdentifier for
137      * @param settingEnabled Whether the setting in question is enabled
138      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName, boolean settingEnabled)139     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
140             @NonNull String packageName, boolean settingEnabled) {
141         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName, settingEnabled);
142     }
143 
144     @Override
setEnabled(boolean enabled)145     public void setEnabled(boolean enabled) {
146         if (enabled && isDisabledByAdmin()) {
147             mHelper.setDisabledByAdmin(/* admin= */ null);
148             return;
149         }
150         super.setEnabled(enabled);
151     }
152 
153     /**
154      * Check whether this preference is disabled by admin.
155      *
156      * @return true if this preference is disabled by admin.
157      */
isDisabledByAdmin()158     public boolean isDisabledByAdmin() {
159         return mHelper.isDisabledByAdmin();
160     }
161 
162     /**
163      * Disable preference based on the enforce admin.
164      *
165      * @param admin details of the admin who enforced the restriction. If it is {@code null}, then
166      *     this preference will be enabled. Otherwise, it will be disabled.
167      */
setDisabledByAdmin(@ullable EnforcedAdmin admin)168     public void setDisabledByAdmin(@Nullable EnforcedAdmin admin) {
169         if (mHelper.setDisabledByAdmin(admin)) {
170             notifyChanged();
171         }
172     }
173 }
174