RestrictedPreferenceHelper.java revision 502bc4e7a340b75829afdd85803005fe3dacbb88
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 17package com.android.settingslib; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.drawable.Drawable; 22import android.os.UserHandle; 23import android.support.v7.preference.Preference; 24import android.support.v7.preference.PreferenceViewHolder; 25import android.text.Spanned; 26import android.text.SpannableStringBuilder; 27import android.text.style.ImageSpan; 28import android.util.AttributeSet; 29import android.util.TypedValue; 30import android.view.View; 31import android.widget.TextView; 32 33import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 34 35/** 36 * Helper class for managing settings preferences that can be disabled 37 * by device admins via user restrictions. 38 */ 39public class RestrictedPreferenceHelper { 40 private final Context mContext; 41 private final Preference mPreference; 42 private final Drawable mRestrictedPadlock; 43 private final int mRestrictedPadlockPadding; 44 45 private boolean mDisabledByAdmin; 46 private EnforcedAdmin mEnforcedAdmin; 47 private String mAttrUserRestriction = null; 48 private boolean mUseAdminDisabledSummary = false; 49 50 public RestrictedPreferenceHelper(Context context, Preference preference, 51 AttributeSet attrs) { 52 mContext = context; 53 mPreference = preference; 54 55 mRestrictedPadlock = RestrictedLockUtils.getRestrictedPadlock(mContext); 56 mRestrictedPadlockPadding = mContext.getResources().getDimensionPixelSize( 57 R.dimen.restricted_icon_padding); 58 59 if (attrs != null) { 60 final TypedArray attributes = context.obtainStyledAttributes(attrs, 61 R.styleable.RestrictedPreference); 62 final TypedValue userRestriction = 63 attributes.peekValue(R.styleable.RestrictedPreference_userRestriction); 64 CharSequence data = null; 65 if (userRestriction != null && userRestriction.type == TypedValue.TYPE_STRING) { 66 if (userRestriction.resourceId != 0) { 67 data = context.getText(userRestriction.resourceId); 68 } else { 69 data = userRestriction.string; 70 } 71 } 72 mAttrUserRestriction = data == null ? null : data.toString(); 73 // If the system has set the user restriction, then we shouldn't add the padlock. 74 if (RestrictedLockUtils.hasBaseUserRestriction(mContext, mAttrUserRestriction, 75 UserHandle.myUserId())) { 76 mAttrUserRestriction = null; 77 return; 78 } 79 80 final TypedValue useAdminDisabledSummary = 81 attributes.peekValue(R.styleable.RestrictedPreference_useAdminDisabledSummary); 82 if (useAdminDisabledSummary != null) { 83 mUseAdminDisabledSummary = 84 (useAdminDisabledSummary.type == TypedValue.TYPE_INT_BOOLEAN 85 && useAdminDisabledSummary.data != 0); 86 } 87 } 88 } 89 90 /** 91 * Modify PreferenceViewHolder to add padlock if restriction is disabled. 92 */ 93 public void onBindViewHolder(PreferenceViewHolder holder) { 94 if (mDisabledByAdmin) { 95 holder.itemView.setEnabled(true); 96 } 97 if (mUseAdminDisabledSummary) { 98 final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary); 99 if (summaryView != null) { 100 if (mDisabledByAdmin) { 101 summaryView.setText(R.string.disabled_by_admin_summary_text); 102 summaryView.setVisibility(View.VISIBLE); 103 } else { 104 summaryView.setVisibility(View.GONE); 105 } 106 } 107 } 108 } 109 110 public void useAdminDisabledSummary(boolean useSummary) { 111 mUseAdminDisabledSummary = useSummary; 112 } 113 114 /** 115 * Check if the preference is disabled if so handle the click by informing the user. 116 * 117 * @return true if the method handled the click. 118 */ 119 public boolean performClick() { 120 if (mDisabledByAdmin) { 121 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin); 122 return true; 123 } 124 return false; 125 } 126 127 /** 128 * Disable / enable if we have been passed the restriction in the xml. 129 */ 130 public void onAttachedToHierarchy() { 131 if (mAttrUserRestriction != null) { 132 checkRestrictionAndSetDisabled(mAttrUserRestriction, UserHandle.myUserId()); 133 } 134 } 135 136 /** 137 * Set the user restriction that is used to disable this preference. 138 * 139 * @param userRestriction constant from {@link android.os.UserManager} 140 * @param userId user to check the restriction for. 141 */ 142 public void checkRestrictionAndSetDisabled(String userRestriction, int userId) { 143 EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, 144 userRestriction, userId); 145 setDisabledByAdmin(admin); 146 } 147 148 /** 149 * Disable this preference based on the enforce admin. 150 * 151 * @param EnforcedAdmin Details of the admin who enforced the restriction. If it 152 * is {@code null}, then this preference will be enabled. Otherwise, it will be disabled. 153 * @return true if the disabled state was changed. 154 */ 155 public boolean setDisabledByAdmin(EnforcedAdmin admin) { 156 final boolean disabled = (admin != null ? true : false); 157 mEnforcedAdmin = (disabled ? admin : null); 158 mPreference.setEnabled(!disabled); 159 if (mDisabledByAdmin != disabled) { 160 mDisabledByAdmin = disabled; 161 return true; 162 } 163 return false; 164 } 165 166 public boolean isDisabledByAdmin() { 167 return mDisabledByAdmin; 168 } 169} 170