| yuemingw | 3bf4b93 | 2018-02-01 13:57:52 +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.settings.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.UserHandle; |
| yuemingw | 3bf4b93 | 2018-02-01 13:57:52 +0000 | [diff] [blame] | 21 | import android.text.TextUtils; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 25 | import androidx.preference.PreferenceManager; |
| 26 | import androidx.preference.PreferenceViewHolder; |
| 27 | |
| yuemingw | 3bf4b93 | 2018-02-01 13:57:52 +0000 | [diff] [blame] | 28 | import com.android.settings.R; |
| 29 | import com.android.settingslib.RestrictedLockUtils; |
| 30 | import com.android.settingslib.RestrictedPreferenceHelper; |
| 31 | |
| 32 | /** |
| 33 | * {@link AppPreference} that implements user restriction utilities using |
| 34 | * {@link com.android.settingslib.RestrictedPreferenceHelper}. |
| 35 | * Used to show policy transparency on {@link AppPreference}. |
| 36 | */ |
| 37 | public class RestrictedAppPreference extends AppPreference { |
| 38 | private RestrictedPreferenceHelper mHelper; |
| 39 | private String userRestriction; |
| 40 | |
| 41 | public RestrictedAppPreference(Context context) { |
| 42 | super(context); |
| 43 | initialize(null, null); |
| 44 | } |
| 45 | |
| 46 | public RestrictedAppPreference(Context context, String userRestriction) { |
| 47 | super(context); |
| 48 | initialize(null, userRestriction); |
| 49 | } |
| 50 | |
| 51 | public RestrictedAppPreference(Context context, AttributeSet attrs, String userRestriction) { |
| 52 | super(context, attrs); |
| 53 | initialize(attrs, userRestriction); |
| 54 | } |
| 55 | |
| 56 | private void initialize(AttributeSet attrs, String userRestriction) { |
| 57 | setWidgetLayoutResource(R.layout.restricted_icon); |
| 58 | mHelper = new RestrictedPreferenceHelper(getContext(), this, attrs); |
| 59 | this.userRestriction = userRestriction; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onBindViewHolder(PreferenceViewHolder holder) { |
| 64 | super.onBindViewHolder(holder); |
| 65 | mHelper.onBindViewHolder(holder); |
| 66 | final View restrictedIcon = holder.findViewById(R.id.restricted_icon); |
| 67 | if (restrictedIcon != null) { |
| 68 | restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void performClick() { |
| 74 | if (!mHelper.performClick()) { |
| 75 | super.performClick(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void setEnabled(boolean enabled) { |
| 81 | if (isDisabledByAdmin() && enabled) { |
| 82 | return; |
| 83 | } |
| 84 | super.setEnabled(enabled); |
| 85 | } |
| 86 | |
| 87 | public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) { |
| 88 | if (mHelper.setDisabledByAdmin(admin)) { |
| 89 | notifyChanged(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public boolean isDisabledByAdmin() { |
| 94 | return mHelper.isDisabledByAdmin(); |
| 95 | } |
| 96 | |
| 97 | public void useAdminDisabledSummary(boolean useSummary) { |
| 98 | mHelper.useAdminDisabledSummary(useSummary); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | protected void onAttachedToHierarchy(PreferenceManager preferenceManager) { |
| 103 | mHelper.onAttachedToHierarchy(); |
| 104 | super.onAttachedToHierarchy(preferenceManager); |
| 105 | } |
| 106 | |
| 107 | public void checkRestrictionAndSetDisabled() { |
| 108 | if (TextUtils.isEmpty(userRestriction)) { |
| 109 | return; |
| 110 | } |
| 111 | mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId()); |
| 112 | } |
| 113 | |
| 114 | public void checkRestrictionAndSetDisabled(String userRestriction) { |
| 115 | mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId()); |
| 116 | } |
| 117 | |
| 118 | public void checkRestrictionAndSetDisabled(String userRestriction, int userId) { |
| 119 | mHelper.checkRestrictionAndSetDisabled(userRestriction, userId); |
| 120 | } |
| 121 | } |