Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 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.phone; |
| 18 | |
| 19 | import android.content.Context; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 20 | import android.os.UserManager; |
| 21 | import android.preference.PreferenceScreen; |
| 22 | import android.preference.SwitchPreference; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.view.View; |
| 25 | import android.widget.TextView; |
| 26 | |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 27 | import com.android.settingslib.RestrictedLockUtils; |
| 28 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 29 | |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 30 | public class RestrictedSwitchPreference extends SwitchPreference { |
| 31 | private final Context mContext; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 32 | private boolean mDisabledByAdmin; |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 33 | private EnforcedAdmin mEnforcedAdmin; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 34 | |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 35 | public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, |
| 36 | int defStyleRes) { |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 37 | super(context, attrs, defStyleAttr, defStyleRes); |
| 38 | mContext = context; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 39 | } |
| 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 Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 58 | RestrictedLockUtils.setTextViewPadlock(mContext, titleView, mDisabledByAdmin); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 59 | if (mDisabledByAdmin) { |
| 60 | view.setEnabled(true); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void checkRestrictionAndSetDisabled(String userRestriction) { |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 66 | 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 Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 74 | } else { |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 75 | 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 Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void performClick(PreferenceScreen preferenceScreen) { |
| 90 | if (mDisabledByAdmin) { |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame^] | 91 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 92 | } else { |
| 93 | super.performClick(preferenceScreen); |
| 94 | } |
| 95 | } |
| 96 | } |