blob: db2a6ec2da68f86e255d31c00aee93487ab5545c [file] [log] [blame]
Aurimas Liutikas88c7ff12023-08-10 12:42:26 -07001/*
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
17package com.android.settingslib;
18
19import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20
21import android.content.Context;
22import android.os.Process;
23import android.os.UserHandle;
24import android.util.AttributeSet;
25
26import androidx.core.content.res.TypedArrayUtils;
27import androidx.preference.PreferenceManager;
28import androidx.preference.PreferenceViewHolder;
29
30import 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 */
36public class RestrictedPreference extends TwoTargetPreference {
37 RestrictedPreferenceHelper mHelper;
38
39 public RestrictedPreference(Context context, AttributeSet attrs,
40 int defStyleAttr, int defStyleRes, String packageName, int uid) {
41 super(context, attrs, defStyleAttr, defStyleRes);
42 mHelper = new RestrictedPreferenceHelper(context, this, attrs, packageName, uid);
43 }
44
45 public RestrictedPreference(Context context, AttributeSet attrs,
46 int defStyleAttr, int defStyleRes) {
47 this(context, attrs, defStyleAttr, defStyleRes, null, Process.INVALID_UID);
48 }
49
50 public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
51 this(context, attrs, defStyleAttr, 0);
52 }
53
54 public RestrictedPreference(Context context, AttributeSet attrs) {
55 this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
56 android.R.attr.preferenceStyle));
57 }
58
59 public RestrictedPreference(Context context) {
60 this(context, null);
61 }
62
63 public RestrictedPreference(Context context, String packageName, int uid) {
64 this(context, null, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
65 android.R.attr.preferenceStyle), 0, packageName, uid);
66 }
67
68 @Override
69 public void onBindViewHolder(PreferenceViewHolder holder) {
70 super.onBindViewHolder(holder);
71 mHelper.onBindViewHolder(holder);
72 }
73
74 @Override
75 public void performClick() {
76 if (!mHelper.performClick()) {
77 super.performClick();
78 }
79 }
80
81 public void useAdminDisabledSummary(boolean useSummary) {
82 mHelper.useAdminDisabledSummary(useSummary);
83 }
84
85 @Override
86 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
87 mHelper.onAttachedToHierarchy();
88 super.onAttachedToHierarchy(preferenceManager);
89 }
90
91 public void checkRestrictionAndSetDisabled(String userRestriction) {
92 mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
93 }
94
95 public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
96 mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
97 }
98
99 @Override
100 public void setEnabled(boolean enabled) {
101 if (enabled && isDisabledByAdmin()) {
102 mHelper.setDisabledByAdmin(null);
103 return;
104 }
105 super.setEnabled(enabled);
106 }
107
108 public void setDisabledByAdmin(EnforcedAdmin admin) {
109 if (mHelper.setDisabledByAdmin(admin)) {
110 notifyChanged();
111 }
112 }
113
114 public void setDisabledByAppOps(boolean disabled) {
115 if (mHelper.setDisabledByAppOps(disabled)) {
116 notifyChanged();
117 }
118 }
119
120 public boolean isDisabledByAdmin() {
121 return mHelper.isDisabledByAdmin();
122 }
123
124 public int getUid() {
125 return mHelper != null ? mHelper.uid : Process.INVALID_UID;
126 }
127
128 public String getPackageName() {
129 return mHelper != null ? mHelper.packageName : null;
130 }
131}