blob: 1fdf22f524d5c229cfe864e0d3a3771f6e1bee5d [file] [log] [blame]
Mahaver Chopra8ce12192015-12-23 16:13:32 +00001/*
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.phone;
18
19import android.content.Context;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000020import android.os.UserManager;
21import android.preference.PreferenceScreen;
22import android.preference.SwitchPreference;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000023import android.util.AttributeSet;
24import android.view.View;
25import android.widget.TextView;
26
Mahaver Choprac738a382016-02-05 15:46:37 +000027import com.android.settingslib.RestrictedLockUtils;
28import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
29
Mahaver Chopra8ce12192015-12-23 16:13:32 +000030public class RestrictedSwitchPreference extends SwitchPreference {
31 private final Context mContext;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000032 private boolean mDisabledByAdmin;
Mahaver Choprac738a382016-02-05 15:46:37 +000033 private EnforcedAdmin mEnforcedAdmin;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000034
Mahaver Choprac738a382016-02-05 15:46:37 +000035 public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr,
36 int defStyleRes) {
Mahaver Chopra8ce12192015-12-23 16:13:32 +000037 super(context, attrs, defStyleAttr, defStyleRes);
38 mContext = context;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000039 }
40
41 public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
42 this(context, attrs, defStyleAttr, 0);
43 }
44
45 public RestrictedSwitchPreference(Context context, AttributeSet attrs) {
46 this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
47 }
48
49 public RestrictedSwitchPreference(Context context) {
50 this(context, null);
51 }
52
53 @Override
54 public void onBindView(View view) {
55 super.onBindView(view);
56 final TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title);
57 if (titleView != null) {
Mahaver Choprac738a382016-02-05 15:46:37 +000058 RestrictedLockUtils.setTextViewPadlock(mContext, titleView, mDisabledByAdmin);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000059 if (mDisabledByAdmin) {
60 view.setEnabled(true);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000061 }
62 }
63 }
64
65 public void checkRestrictionAndSetDisabled(String userRestriction) {
Mahaver Choprac738a382016-02-05 15:46:37 +000066 setDisabledByAdmin(RestrictedLockUtils.checkIfRestrictionEnforced(mContext, userRestriction,
67 UserManager.get(mContext).getUserHandle()));
68 }
69
70 @Override
71 public void setEnabled(boolean enabled) {
72 if (enabled && mDisabledByAdmin) {
73 setDisabledByAdmin(null);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000074 } else {
Mahaver Choprac738a382016-02-05 15:46:37 +000075 super.setEnabled(enabled);
76 }
77 }
78
79 public void setDisabledByAdmin(EnforcedAdmin admin) {
80 final boolean disabled = (admin != null ? true : false);
81 mEnforcedAdmin = admin;
82 if (mDisabledByAdmin != disabled) {
83 mDisabledByAdmin = disabled;
84 setEnabled(!disabled);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000085 }
86 }
87
88 @Override
89 public void performClick(PreferenceScreen preferenceScreen) {
90 if (mDisabledByAdmin) {
Mahaver Choprac738a382016-02-05 15:46:37 +000091 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000092 } else {
93 super.performClick(preferenceScreen);
94 }
95 }
96}