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