• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.widget;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 
24 import androidx.annotation.NonNull;
25 import androidx.preference.PreferenceManager;
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settingslib.RestrictedLockUtils;
29 import com.android.settingslib.RestrictedPreferenceHelper;
30 import com.android.settingslib.RestrictedPreferenceHelperProvider;
31 import com.android.settingslib.widget.AppPreference;
32 
33 /**
34  * {@link AppPreference} that implements user restriction utilities using
35  * {@link com.android.settingslib.RestrictedPreferenceHelper}.
36  * Used to show policy transparency on {@link AppPreference}.
37  */
38 public class RestrictedAppPreference extends AppPreference implements
39         RestrictedPreferenceHelperProvider {
40     private RestrictedPreferenceHelper mHelper;
41     private String userRestriction;
42 
RestrictedAppPreference(Context context)43     public RestrictedAppPreference(Context context) {
44         super(context);
45         initialize(null, null);
46     }
47 
RestrictedAppPreference(Context context, String userRestriction)48     public RestrictedAppPreference(Context context, String userRestriction) {
49         super(context);
50         initialize(null, userRestriction);
51     }
52 
RestrictedAppPreference(Context context, AttributeSet attrs, String userRestriction)53     public RestrictedAppPreference(Context context, AttributeSet attrs, String userRestriction) {
54         super(context, attrs);
55         initialize(attrs, userRestriction);
56     }
57 
initialize(AttributeSet attrs, String userRestriction)58     private void initialize(AttributeSet attrs, String userRestriction) {
59         mHelper = new RestrictedPreferenceHelper(getContext(), this, attrs);
60         this.userRestriction = userRestriction;
61     }
62 
63     @Override
getRestrictedPreferenceHelper()64     public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() {
65         return mHelper;
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 
81     @Override
setEnabled(boolean enabled)82     public void setEnabled(boolean enabled) {
83         boolean changed = false;
84         if (enabled && isDisabledByAdmin()) {
85             mHelper.setDisabledByAdmin(null);
86             changed = true;
87         }
88         if (enabled && isDisabledByEcm()) {
89             mHelper.setDisabledByEcm(null);
90             changed = true;
91         }
92         if (!changed) {
93             super.setEnabled(enabled);
94         }
95     }
96 
setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin)97     public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
98         if (mHelper.setDisabledByAdmin(admin)) {
99             notifyChanged();
100         }
101     }
102 
isDisabledByAdmin()103     public boolean isDisabledByAdmin() {
104         return mHelper.isDisabledByAdmin();
105     }
106 
isDisabledByEcm()107     public boolean isDisabledByEcm() {
108         return mHelper.isDisabledByEcm();
109     }
110 
useAdminDisabledSummary(boolean useSummary)111     public void useAdminDisabledSummary(boolean useSummary) {
112         mHelper.useAdminDisabledSummary(useSummary);
113     }
114 
115     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)116     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
117         mHelper.onAttachedToHierarchy();
118         super.onAttachedToHierarchy(preferenceManager);
119     }
120 
checkRestrictionAndSetDisabled()121     public void checkRestrictionAndSetDisabled() {
122         if (TextUtils.isEmpty(userRestriction)) {
123             return;
124         }
125         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
126     }
127 
checkRestrictionAndSetDisabled(String userRestriction)128     public void checkRestrictionAndSetDisabled(String userRestriction) {
129         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
130     }
131 
checkRestrictionAndSetDisabled(String userRestriction, int userId)132     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
133         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
134     }
135 
136     /**
137      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
138      * package. Marks the preference as disabled if so.
139      * TODO b/390196024: remove this and update all callers to use the "settingEnabled" version
140      * @param settingIdentifier The key identifying the setting
141      * @param packageName the package to check the settingIdentifier for
142      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName)143     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
144             @NonNull String packageName) {
145         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName, false);
146     }
147 }
148