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; |
Sudheer Shanka | 25a6cfe | 2016-03-21 12:53:29 -0700 | [diff] [blame^] | 20 | import android.os.UserHandle; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 21 | import android.os.UserManager; |
| 22 | import android.preference.PreferenceScreen; |
| 23 | import android.preference.SwitchPreference; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 24 | import android.util.AttributeSet; |
| 25 | import android.view.View; |
| 26 | import android.widget.TextView; |
| 27 | |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame] | 28 | import com.android.settingslib.RestrictedLockUtils; |
| 29 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 30 | |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 31 | public class RestrictedSwitchPreference extends SwitchPreference { |
| 32 | private final Context mContext; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 33 | private boolean mDisabledByAdmin; |
Sudheer Shanka | c2d0c65 | 2016-03-01 18:12:16 -0800 | [diff] [blame] | 34 | private final int mSwitchWidgetResId; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 35 | |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame] | 36 | public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, |
| 37 | int defStyleRes) { |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 38 | super(context, attrs, defStyleAttr, defStyleRes); |
Sudheer Shanka | c2d0c65 | 2016-03-01 18:12:16 -0800 | [diff] [blame] | 39 | mSwitchWidgetResId = getWidgetLayoutResource(); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 40 | mContext = context; |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 41 | } |
| 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 Shanka | c2d0c65 | 2016-03-01 18:12:16 -0800 | [diff] [blame] | 58 | 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 Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | public void checkRestrictionAndSetDisabled(String userRestriction) { |
Sudheer Shanka | 25a6cfe | 2016-03-21 12:53:29 -0700 | [diff] [blame^] | 70 | UserManager um = UserManager.get(mContext); |
| 71 | UserHandle user = UserHandle.of(um.getUserHandle()); |
| 72 | boolean disabledByAdmin = um.hasUserRestriction(userRestriction, user) |
| 73 | && !um.hasBaseUserRestriction(userRestriction, user); |
| 74 | setDisabledByAdmin(disabledByAdmin); |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void setEnabled(boolean enabled) { |
| 79 | if (enabled && mDisabledByAdmin) { |
Sudheer Shanka | 25a6cfe | 2016-03-21 12:53:29 -0700 | [diff] [blame^] | 80 | setDisabledByAdmin(false); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 81 | } else { |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame] | 82 | super.setEnabled(enabled); |
| 83 | } |
| 84 | } |
| 85 | |
Sudheer Shanka | 25a6cfe | 2016-03-21 12:53:29 -0700 | [diff] [blame^] | 86 | public void setDisabledByAdmin(boolean disabled) { |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame] | 87 | if (mDisabledByAdmin != disabled) { |
| 88 | mDisabledByAdmin = disabled; |
Sudheer Shanka | c2d0c65 | 2016-03-01 18:12:16 -0800 | [diff] [blame] | 89 | setWidgetLayoutResource(disabled ? R.layout.restricted_icon : mSwitchWidgetResId); |
Mahaver Chopra | c738a38 | 2016-02-05 15:46:37 +0000 | [diff] [blame] | 90 | setEnabled(!disabled); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void performClick(PreferenceScreen preferenceScreen) { |
| 96 | if (mDisabledByAdmin) { |
Sudheer Shanka | 25a6cfe | 2016-03-21 12:53:29 -0700 | [diff] [blame^] | 97 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, |
| 98 | new EnforcedAdmin()); |
Mahaver Chopra | 8ce1219 | 2015-12-23 16:13:32 +0000 | [diff] [blame] | 99 | } else { |
| 100 | super.performClick(preferenceScreen); |
| 101 | } |
| 102 | } |
| 103 | } |