blob: e33c34287daa118e40905480b80dfa4bdb5abe78 [file] [log] [blame]
yuemingw3bf4b932018-02-01 13:57:52 +00001/*
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
17package com.android.settings.widget;
18
19import android.content.Context;
20import android.os.UserHandle;
yuemingw3bf4b932018-02-01 13:57:52 +000021import android.text.TextUtils;
22import android.util.AttributeSet;
23import android.view.View;
24
Fan Zhangde117042018-09-04 13:52:15 -070025import androidx.preference.PreferenceManager;
26import androidx.preference.PreferenceViewHolder;
27
yuemingw3bf4b932018-02-01 13:57:52 +000028import com.android.settings.R;
29import com.android.settingslib.RestrictedLockUtils;
30import com.android.settingslib.RestrictedPreferenceHelper;
31
32/**
33 * {@link AppPreference} that implements user restriction utilities using
34 * {@link com.android.settingslib.RestrictedPreferenceHelper}.
35 * Used to show policy transparency on {@link AppPreference}.
36 */
37public class RestrictedAppPreference extends AppPreference {
38 private RestrictedPreferenceHelper mHelper;
39 private String userRestriction;
40
41 public RestrictedAppPreference(Context context) {
42 super(context);
43 initialize(null, null);
44 }
45
46 public RestrictedAppPreference(Context context, String userRestriction) {
47 super(context);
48 initialize(null, userRestriction);
49 }
50
51 public RestrictedAppPreference(Context context, AttributeSet attrs, String userRestriction) {
52 super(context, attrs);
53 initialize(attrs, userRestriction);
54 }
55
56 private void initialize(AttributeSet attrs, String userRestriction) {
57 setWidgetLayoutResource(R.layout.restricted_icon);
58 mHelper = new RestrictedPreferenceHelper(getContext(), this, attrs);
59 this.userRestriction = userRestriction;
60 }
61
62 @Override
63 public void onBindViewHolder(PreferenceViewHolder holder) {
64 super.onBindViewHolder(holder);
65 mHelper.onBindViewHolder(holder);
66 final View restrictedIcon = holder.findViewById(R.id.restricted_icon);
67 if (restrictedIcon != null) {
68 restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE);
69 }
70 }
71
72 @Override
73 public void performClick() {
74 if (!mHelper.performClick()) {
75 super.performClick();
76 }
77 }
78
79 @Override
80 public void setEnabled(boolean enabled) {
81 if (isDisabledByAdmin() && enabled) {
82 return;
83 }
84 super.setEnabled(enabled);
85 }
86
87 public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
88 if (mHelper.setDisabledByAdmin(admin)) {
89 notifyChanged();
90 }
91 }
92
93 public boolean isDisabledByAdmin() {
94 return mHelper.isDisabledByAdmin();
95 }
96
97 public void useAdminDisabledSummary(boolean useSummary) {
98 mHelper.useAdminDisabledSummary(useSummary);
99 }
100
101 @Override
102 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
103 mHelper.onAttachedToHierarchy();
104 super.onAttachedToHierarchy(preferenceManager);
105 }
106
107 public void checkRestrictionAndSetDisabled() {
108 if (TextUtils.isEmpty(userRestriction)) {
109 return;
110 }
111 mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
112 }
113
114 public void checkRestrictionAndSetDisabled(String userRestriction) {
115 mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
116 }
117
118 public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
119 mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
120 }
121}