blob: fd2b222b82c0d6a2cc05e55fe9b2b5328104ac54 [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;
Sudheer Shankac2d0c652016-03-01 18:12:16 -080034 private final int mSwitchWidgetResId;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000035
Mahaver Choprac738a382016-02-05 15:46:37 +000036 public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr,
37 int defStyleRes) {
Mahaver Chopra8ce12192015-12-23 16:13:32 +000038 super(context, attrs, defStyleAttr, defStyleRes);
Sudheer Shankac2d0c652016-03-01 18:12:16 -080039 mSwitchWidgetResId = getWidgetLayoutResource();
Mahaver Chopra8ce12192015-12-23 16:13:32 +000040 mContext = context;
Mahaver Chopra8ce12192015-12-23 16:13:32 +000041 }
42
43 public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
44 this(context, attrs, defStyleAttr, 0);
45 }
46
47 public RestrictedSwitchPreference(Context context, AttributeSet attrs) {
48 this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
49 }
50
51 public RestrictedSwitchPreference(Context context) {
52 this(context, null);
53 }
54
55 @Override
56 public void onBindView(View view) {
57 super.onBindView(view);
Sudheer Shankac2d0c652016-03-01 18:12:16 -080058 if (mDisabledByAdmin) {
59 view.setEnabled(true);
60 }
61 final TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
62 if (summaryView != null && mDisabledByAdmin) {
63 summaryView.setText(
64 isChecked() ? R.string.enabled_by_admin : R.string.disabled_by_admin);
65 summaryView.setVisibility(View.VISIBLE);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000066 }
67 }
68
69 public void checkRestrictionAndSetDisabled(String userRestriction) {
Mahaver Choprac738a382016-02-05 15:46:37 +000070 setDisabledByAdmin(RestrictedLockUtils.checkIfRestrictionEnforced(mContext, userRestriction,
71 UserManager.get(mContext).getUserHandle()));
72 }
73
74 @Override
75 public void setEnabled(boolean enabled) {
76 if (enabled && mDisabledByAdmin) {
77 setDisabledByAdmin(null);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000078 } else {
Mahaver Choprac738a382016-02-05 15:46:37 +000079 super.setEnabled(enabled);
80 }
81 }
82
83 public void setDisabledByAdmin(EnforcedAdmin admin) {
84 final boolean disabled = (admin != null ? true : false);
85 mEnforcedAdmin = admin;
86 if (mDisabledByAdmin != disabled) {
87 mDisabledByAdmin = disabled;
Sudheer Shankac2d0c652016-03-01 18:12:16 -080088 setWidgetLayoutResource(disabled ? R.layout.restricted_icon : mSwitchWidgetResId);
Mahaver Choprac738a382016-02-05 15:46:37 +000089 setEnabled(!disabled);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000090 }
91 }
92
93 @Override
94 public void performClick(PreferenceScreen preferenceScreen) {
95 if (mDisabledByAdmin) {
Mahaver Choprac738a382016-02-05 15:46:37 +000096 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin);
Mahaver Chopra8ce12192015-12-23 16:13:32 +000097 } else {
98 super.performClick(preferenceScreen);
99 }
100 }
101}