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 android.content.Context;
20 import android.os.UserHandle;
21 import android.support.v4.content.res.TypedArrayUtils;
22 import android.support.v7.preference.PreferenceManager;
23 import android.support.v7.preference.PreferenceViewHolder;
24 import android.util.AttributeSet;
25 import android.view.View;
26 
27 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
28 
29 /**
30  * Preference class that supports being disabled by a user restriction
31  * set by a device admin.
32  */
33 public class RestrictedPreference extends TwoTargetPreference {
34     RestrictedPreferenceHelper mHelper;
35 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)36     public RestrictedPreference(Context context, AttributeSet attrs,
37             int defStyleAttr, int defStyleRes) {
38         super(context, attrs, defStyleAttr, defStyleRes);
39         mHelper = new RestrictedPreferenceHelper(context, this, attrs);
40     }
41 
RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr)42     public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
43         this(context, attrs, defStyleAttr, 0);
44     }
45 
RestrictedPreference(Context context, AttributeSet attrs)46     public RestrictedPreference(Context context, AttributeSet attrs) {
47         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
48                 android.R.attr.preferenceStyle));
49     }
50 
RestrictedPreference(Context context)51     public RestrictedPreference(Context context) {
52         this(context, null);
53     }
54 
55     @Override
getSecondTargetResId()56     protected int getSecondTargetResId() {
57         return R.layout.restricted_icon;
58     }
59 
60     @Override
shouldHideSecondTarget()61     protected boolean shouldHideSecondTarget() {
62         return !isDisabledByAdmin();
63     }
64 
65     @Override
onBindViewHolder(PreferenceViewHolder holder)66     public void onBindViewHolder(PreferenceViewHolder holder) {
67         super.onBindViewHolder(holder);
68         mHelper.onBindViewHolder(holder);
69         final View restrictedIcon = holder.findViewById(R.id.restricted_icon);
70         if (restrictedIcon != null) {
71             restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE);
72         }
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     @Override
setEnabled(boolean enabled)101     public void setEnabled(boolean enabled) {
102         if (enabled && isDisabledByAdmin()) {
103             mHelper.setDisabledByAdmin(null);
104             return;
105         }
106         super.setEnabled(enabled);
107     }
108 
setDisabledByAdmin(EnforcedAdmin admin)109     public void setDisabledByAdmin(EnforcedAdmin admin) {
110         if (mHelper.setDisabledByAdmin(admin)) {
111             notifyChanged();
112         }
113     }
114 
isDisabledByAdmin()115     public boolean isDisabledByAdmin() {
116         return mHelper.isDisabledByAdmin();
117     }
118 }
119