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