Alex Chau | d6d3327 | 2018-02-27 09:44:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.preference.Preference; |
| 23 | import android.preference.PreferenceScreen; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.View; |
| 26 | import android.widget.TextView; |
| 27 | |
| 28 | import com.android.settingslib.RestrictedLockUtils; |
| 29 | |
| 30 | /** |
| 31 | * Preference class that supports being disabled by a device admin. |
| 32 | * |
| 33 | * <p>This class is a mimic of ../../../frameworks/base/packages/SettingsLib/src/com/android |
| 34 | * /settingslib/RestrictedPreference.java, |
| 35 | * but support framework {@link Preference}. |
| 36 | */ |
| 37 | public class RestrictedPreference extends Preference { |
| 38 | private final Context mContext; |
| 39 | |
| 40 | private boolean mDisabledByAdmin; |
| 41 | private EnforcedAdmin mEnforcedAdmin; |
| 42 | |
| 43 | public RestrictedPreference(Context context, AttributeSet attrs, |
| 44 | int defStyleAttr, int defStyleRes) { |
| 45 | super(context, attrs, defStyleAttr, defStyleRes); |
| 46 | mContext = context; |
| 47 | |
| 48 | setLayoutResource(com.android.settingslib.R.layout.preference_two_target); |
| 49 | setWidgetLayoutResource(R.layout.restricted_icon); |
| 50 | } |
| 51 | |
| 52 | public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 53 | this(context, attrs, defStyleAttr, 0); |
| 54 | } |
| 55 | |
| 56 | public RestrictedPreference(Context context, AttributeSet attrs) { |
| 57 | this(context, attrs, android.R.attr.preferenceStyle); |
| 58 | } |
| 59 | |
| 60 | public RestrictedPreference(Context context) { |
| 61 | this(context, null); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void performClick(PreferenceScreen preferenceScreen) { |
| 66 | if (mDisabledByAdmin) { |
| 67 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin); |
| 68 | } else { |
| 69 | super.performClick(preferenceScreen); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected void onBindView(View view) { |
| 75 | super.onBindView(view); |
| 76 | |
| 77 | final View divider = view.findViewById(com.android.settingslib.R.id.two_target_divider); |
| 78 | final View widgetFrame = view.findViewById(android.R.id.widget_frame); |
| 79 | final View restrictedIcon = view.findViewById(R.id.restricted_icon); |
| 80 | final TextView summaryView = view.findViewById(android.R.id.summary); |
| 81 | if (divider != null) { |
| 82 | divider.setVisibility(mDisabledByAdmin ? View.VISIBLE : View.GONE); |
| 83 | } |
| 84 | if (widgetFrame != null) { |
| 85 | widgetFrame.setVisibility(mDisabledByAdmin ? View.VISIBLE : View.GONE); |
| 86 | } |
| 87 | if (restrictedIcon != null) { |
| 88 | restrictedIcon.setVisibility(mDisabledByAdmin ? View.VISIBLE : View.GONE); |
| 89 | } |
| 90 | if (summaryView != null && mDisabledByAdmin) { |
| 91 | summaryView.setText(com.android.settingslib.R.string.disabled_by_admin_summary_text); |
| 92 | summaryView.setVisibility(View.VISIBLE); |
| 93 | } |
| 94 | |
| 95 | if (mDisabledByAdmin) { |
| 96 | view.setEnabled(true); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void setEnabled(boolean enabled) { |
| 102 | if (enabled && mDisabledByAdmin) { |
| 103 | setDisabledByAdmin(null); |
| 104 | return; |
| 105 | } |
| 106 | super.setEnabled(enabled); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Disable this preference based on the enforce admin. |
| 111 | * |
| 112 | * @param admin Details of the admin who enforced the restriction. If it is {@code null}, then |
| 113 | * this preference will be enabled. Otherwise, it will be disabled. |
| 114 | */ |
| 115 | public void setDisabledByAdmin(EnforcedAdmin admin) { |
| 116 | final boolean disabled = admin != null; |
| 117 | mEnforcedAdmin = admin; |
| 118 | boolean changed = false; |
| 119 | if (mDisabledByAdmin != disabled) { |
| 120 | mDisabledByAdmin = disabled; |
| 121 | changed = true; |
| 122 | } |
| 123 | setEnabled(!disabled); |
| 124 | if (changed) { |
| 125 | notifyChanged(); |
| 126 | } |
| 127 | } |
| 128 | } |