• 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.content.res.TypedArray;
21 import android.os.UserHandle;
22 import android.support.v4.content.res.TypedArrayUtils;
23 import android.support.v7.preference.PreferenceManager;
24 import android.support.v7.preference.PreferenceViewHolder;
25 import android.support.v14.preference.SwitchPreference;
26 import android.util.AttributeSet;
27 import android.util.TypedValue;
28 import android.view.View;
29 import android.widget.TextView;
30 
31 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
32 
33 /**
34  * Version of SwitchPreference that can be disabled by a device admin
35  * using a user restriction.
36  */
37 public class RestrictedSwitchPreference extends SwitchPreference {
38     RestrictedPreferenceHelper mHelper;
39     boolean mUseAdditionalSummary = false;
40     String mRestrictedSwitchSummary = null;
41 
RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)42     public RestrictedSwitchPreference(Context context, AttributeSet attrs,
43             int defStyleAttr, int defStyleRes) {
44         super(context, attrs, defStyleAttr, defStyleRes);
45         setWidgetLayoutResource(R.layout.restricted_switch_widget);
46         mHelper = new RestrictedPreferenceHelper(context, this, attrs);
47         if (attrs != null) {
48             final TypedArray attributes = context.obtainStyledAttributes(attrs,
49                     R.styleable.RestrictedSwitchPreference);
50             final TypedValue useAdditionalSummary = attributes.peekValue(
51                     R.styleable.RestrictedSwitchPreference_useAdditionalSummary);
52             if (useAdditionalSummary != null) {
53                 mUseAdditionalSummary =
54                         (useAdditionalSummary.type == TypedValue.TYPE_INT_BOOLEAN
55                                 && useAdditionalSummary.data != 0);
56             }
57 
58             final TypedValue restrictedSwitchSummary = attributes.peekValue(
59                     R.styleable.RestrictedSwitchPreference_restrictedSwitchSummary);
60             CharSequence data = null;
61             if (restrictedSwitchSummary != null
62                     && restrictedSwitchSummary.type == TypedValue.TYPE_STRING) {
63                 if (restrictedSwitchSummary.resourceId != 0) {
64                     data = context.getString(restrictedSwitchSummary.resourceId);
65                 } else {
66                     data = restrictedSwitchSummary.string;
67                 }
68             }
69             mRestrictedSwitchSummary = data == null ? null : data.toString();
70         }
71         if (mRestrictedSwitchSummary == null) {
72             mRestrictedSwitchSummary = context.getString(R.string.disabled_by_admin);
73         }
74         if (mUseAdditionalSummary) {
75             setLayoutResource(R.layout.restricted_switch_preference);
76             useAdminDisabledSummary(false);
77         }
78     }
79 
RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)80     public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
81         this(context, attrs, defStyleAttr, 0);
82     }
83 
RestrictedSwitchPreference(Context context, AttributeSet attrs)84     public RestrictedSwitchPreference(Context context, AttributeSet attrs) {
85         this(context, attrs, TypedArrayUtils.getAttr(context,
86                 android.support.v7.preference.R.attr.switchPreferenceStyle,
87                 android.R.attr.switchPreferenceStyle));
88     }
89 
RestrictedSwitchPreference(Context context)90     public RestrictedSwitchPreference(Context context) {
91         this(context, null);
92     }
93 
94     @Override
onBindViewHolder(PreferenceViewHolder holder)95     public void onBindViewHolder(PreferenceViewHolder holder) {
96         super.onBindViewHolder(holder);
97         mHelper.onBindViewHolder(holder);
98         final View restrictedIcon = holder.findViewById(R.id.restricted_icon);
99         final View switchWidget = holder.findViewById(android.R.id.switch_widget);
100         if (restrictedIcon != null) {
101             restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE);
102         }
103         if (switchWidget != null) {
104             switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE);
105         }
106         if (mUseAdditionalSummary) {
107             final TextView additionalSummaryView = (TextView) holder.findViewById(
108                     R.id.additional_summary);
109             if (additionalSummaryView != null) {
110                 if (isDisabledByAdmin()) {
111                     additionalSummaryView.setText(mRestrictedSwitchSummary);
112                     additionalSummaryView.setVisibility(View.VISIBLE);
113                 } else {
114                     additionalSummaryView.setVisibility(View.GONE);
115                 }
116             }
117         } else {
118             final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
119             if (summaryView != null) {
120                 if (isDisabledByAdmin()) {
121                     summaryView.setText(mRestrictedSwitchSummary);
122                     summaryView.setVisibility(View.VISIBLE);
123                 }
124                 // No need to change the visibility to GONE in the else case here since Preference
125                 // class would have already changed it if there is no summary to display.
126             }
127         }
128     }
129 
130     @Override
performClick()131     public void performClick() {
132         if (!mHelper.performClick()) {
133             super.performClick();
134         }
135     }
136 
useAdminDisabledSummary(boolean useSummary)137     public void useAdminDisabledSummary(boolean useSummary) {
138         mHelper.useAdminDisabledSummary(useSummary);
139     }
140 
141     @Override
onAttachedToHierarchy(PreferenceManager preferenceManager)142     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
143         mHelper.onAttachedToHierarchy();
144         super.onAttachedToHierarchy(preferenceManager);
145     }
146 
checkRestrictionAndSetDisabled(String userRestriction)147     public void checkRestrictionAndSetDisabled(String userRestriction) {
148         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
149     }
150 
checkRestrictionAndSetDisabled(String userRestriction, int userId)151     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
152         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
153     }
154 
155     @Override
setEnabled(boolean enabled)156     public void setEnabled(boolean enabled) {
157         if (enabled && isDisabledByAdmin()) {
158             mHelper.setDisabledByAdmin(null);
159             return;
160         }
161         super.setEnabled(enabled);
162     }
163 
setDisabledByAdmin(EnforcedAdmin admin)164     public void setDisabledByAdmin(EnforcedAdmin admin) {
165         if (mHelper.setDisabledByAdmin(admin)) {
166             notifyChanged();
167         }
168     }
169 
isDisabledByAdmin()170     public boolean isDisabledByAdmin() {
171         return mHelper.isDisabledByAdmin();
172     }
173 }
174