1 /* 2 * Copyright (C) 2020 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.settingslib; 18 19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 20 21 import android.content.Context; 22 import android.os.UserHandle; 23 import android.util.AttributeSet; 24 25 import androidx.annotation.NonNull; 26 import androidx.core.content.res.TypedArrayUtils; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceManager; 29 import androidx.preference.PreferenceViewHolder; 30 31 /** Top level preference that can be disabled by a device admin using a user restriction. */ 32 public class RestrictedTopLevelPreference extends Preference implements 33 RestrictedPreferenceHelperProvider { 34 private final RestrictedPreferenceHelper mHelper; 35 RestrictedTopLevelPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)36 public RestrictedTopLevelPreference(Context context, AttributeSet attrs, 37 int defStyleAttr, int defStyleRes) { 38 super(context, attrs, defStyleAttr, defStyleRes); 39 mHelper = new RestrictedPreferenceHelper(context, /* preference= */ this, attrs); 40 } 41 RestrictedTopLevelPreference(Context context, AttributeSet attrs, int defStyleAttr)42 public RestrictedTopLevelPreference(Context context, AttributeSet attrs, int defStyleAttr) { 43 this(context, attrs, defStyleAttr, /* defStyleRes= */ 0); 44 } 45 RestrictedTopLevelPreference(Context context, AttributeSet attrs)46 public RestrictedTopLevelPreference(Context context, AttributeSet attrs) { 47 this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle, 48 android.R.attr.preferenceStyle)); 49 } 50 RestrictedTopLevelPreference(Context context)51 public RestrictedTopLevelPreference(Context context) { 52 this(context, /* attrs= */ null); 53 } 54 55 @Override getRestrictedPreferenceHelper()56 public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() { 57 return mHelper; 58 } 59 60 @Override onBindViewHolder(PreferenceViewHolder holder)61 public void onBindViewHolder(PreferenceViewHolder holder) { 62 super.onBindViewHolder(holder); 63 mHelper.onBindViewHolder(holder); 64 } 65 66 @Override performClick()67 public void performClick() { 68 if (!mHelper.performClick()) { 69 super.performClick(); 70 } 71 } 72 73 @Override onAttachedToHierarchy(PreferenceManager preferenceManager)74 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) { 75 mHelper.onAttachedToHierarchy(); 76 super.onAttachedToHierarchy(preferenceManager); 77 } 78 79 /** 80 * Set the user restriction and disable this preference. 81 * 82 * @param userRestriction constant from {@link android.os.UserManager} 83 */ checkRestrictionAndSetDisabled(String userRestriction)84 public void checkRestrictionAndSetDisabled(String userRestriction) { 85 mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId()); 86 } 87 88 /** 89 * Set the user restriction and disable this preference for the given user. 90 * 91 * @param userRestriction constant from {@link android.os.UserManager} 92 * @param userId user to check the restriction for. 93 */ checkRestrictionAndSetDisabled(String userRestriction, int userId)94 public void checkRestrictionAndSetDisabled(String userRestriction, int userId) { 95 mHelper.checkRestrictionAndSetDisabled(userRestriction, userId); 96 } 97 98 @Override setEnabled(boolean enabled)99 public void setEnabled(boolean enabled) { 100 if (enabled && isDisabledByAdmin()) { 101 mHelper.setDisabledByAdmin(/* admin= */ null); 102 return; 103 } 104 super.setEnabled(enabled); 105 } 106 107 /** 108 * Check whether this preference is disabled by admin. 109 * 110 * @return true if this preference is disabled by admin. 111 */ isDisabledByAdmin()112 public boolean isDisabledByAdmin() { 113 return mHelper.isDisabledByAdmin(); 114 } 115 116 /** 117 * Disable preference based on the enforce admin. 118 * 119 * @param admin details of the admin who enforced the restriction. If it is {@code null}, then 120 * this preference will be enabled. Otherwise, it will be disabled. 121 */ setDisabledByAdmin(EnforcedAdmin admin)122 public void setDisabledByAdmin(EnforcedAdmin admin) { 123 if (mHelper.setDisabledByAdmin(admin)) { 124 notifyChanged(); 125 } 126 } 127 } 128