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.UserHandle;
23 import android.util.AttributeSet;
24 import android.view.View;
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)39     public RestrictedPreference(Context context, AttributeSet attrs,
40             int defStyleAttr, int defStyleRes) {
41         super(context, attrs, defStyleAttr, defStyleRes);
42         mHelper = new RestrictedPreferenceHelper(context, this, attrs);
43     }
44 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr)45     public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
46         this(context, attrs, defStyleAttr, 0);
47     }
48 
RestrictedPreference(Context context, AttributeSet attrs)49     public RestrictedPreference(Context context, AttributeSet attrs) {
50         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
51                 android.R.attr.preferenceStyle));
52     }
53 
RestrictedPreference(Context context)54     public RestrictedPreference(Context context) {
55         this(context, null);
56     }
57 
58     @Override
getSecondTargetResId()59     protected int getSecondTargetResId() {
60         return R.layout.restricted_icon;
61     }
62 
63     @Override
shouldHideSecondTarget()64     protected boolean shouldHideSecondTarget() {
65         return !isDisabledByAdmin();
66     }
67 
68     @Override
onBindViewHolder(PreferenceViewHolder holder)69     public void onBindViewHolder(PreferenceViewHolder holder) {
70         super.onBindViewHolder(holder);
71         mHelper.onBindViewHolder(holder);
72         final View restrictedIcon = holder.findViewById(R.id.restricted_icon);
73         if (restrictedIcon != null) {
74             restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE);
75         }
76     }
77 
78     @Override
performClick()79     public void performClick() {
80         if (!mHelper.performClick()) {
81             super.performClick();
82         }
83     }
84 
useAdminDisabledSummary(boolean useSummary)85     public void useAdminDisabledSummary(boolean useSummary) {
86         mHelper.useAdminDisabledSummary(useSummary);
87     }
88 
89     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)90     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
91         mHelper.onAttachedToHierarchy();
92         super.onAttachedToHierarchy(preferenceManager);
93     }
94 
checkRestrictionAndSetDisabled(String userRestriction)95     public void checkRestrictionAndSetDisabled(String userRestriction) {
96         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
97     }
98 
checkRestrictionAndSetDisabled(String userRestriction, int userId)99     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
100         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
101     }
102 
103     @Override
setEnabled(boolean enabled)104     public void setEnabled(boolean enabled) {
105         if (enabled && isDisabledByAdmin()) {
106             mHelper.setDisabledByAdmin(null);
107             return;
108         }
109         super.setEnabled(enabled);
110     }
111 
setDisabledByAdmin(EnforcedAdmin admin)112     public void setDisabledByAdmin(EnforcedAdmin admin) {
113         if (mHelper.setDisabledByAdmin(admin)) {
114             notifyChanged();
115         }
116     }
117 
isDisabledByAdmin()118     public boolean isDisabledByAdmin() {
119         return mHelper.isDisabledByAdmin();
120     }
121 }
122