1 /* 2 * Copyright (C) 2024 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 android.annotation.SuppressLint; 20 import android.content.Context; 21 import android.os.Process; 22 import android.util.AttributeSet; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.core.content.res.TypedArrayUtils; 27 import androidx.preference.PreferenceManager; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.settingslib.widget.SliderPreference; 31 32 /** 33 * Slide Preference that supports being disabled by a user restriction 34 * set by a device admin. 35 */ 36 public class RestrictedSliderPreference extends SliderPreference implements 37 RestrictedPreferenceHelperProvider { 38 39 private final RestrictedPreferenceHelper mHelper; 40 RestrictedSliderPreference(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, @Nullable String packageName, int uid)41 public RestrictedSliderPreference(@NonNull Context context, @Nullable AttributeSet attrs, 42 int defStyleAttr, @Nullable String packageName, int uid) { 43 super(context, attrs, defStyleAttr); 44 mHelper = new RestrictedPreferenceHelper(context, this, attrs, packageName, uid); 45 } 46 RestrictedSliderPreference(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)47 public RestrictedSliderPreference(@NonNull Context context, 48 @Nullable AttributeSet attrs, int defStyleAttr) { 49 this(context, attrs, defStyleAttr, null, Process.INVALID_UID); 50 } 51 52 @SuppressLint("RestrictedApi") RestrictedSliderPreference(@onNull Context context, @Nullable AttributeSet attrs)53 public RestrictedSliderPreference(@NonNull Context context, @Nullable AttributeSet attrs) { 54 this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle, 55 android.R.attr.preferenceStyle)); 56 } 57 RestrictedSliderPreference(@onNull Context context)58 public RestrictedSliderPreference(@NonNull Context context) { 59 this(context, null); 60 } 61 62 @SuppressLint("RestrictedApi") RestrictedSliderPreference(@onNull Context context, @Nullable String packageName, int uid)63 public RestrictedSliderPreference(@NonNull Context context, @Nullable String packageName, 64 int uid) { 65 this(context, null, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle, 66 android.R.attr.preferenceStyle), packageName, uid); 67 } 68 69 @Override getRestrictedPreferenceHelper()70 public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() { 71 return mHelper; 72 } 73 74 @Override onBindViewHolder(@onNull PreferenceViewHolder holder)75 public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { 76 super.onBindViewHolder(holder); 77 mHelper.onBindViewHolder(holder); 78 } 79 80 @SuppressLint("RestrictedApi") 81 @Override performClick()82 public void performClick() { 83 if (!mHelper.performClick()) { 84 super.performClick(); 85 } 86 } 87 88 @Override setEnabled(boolean enabled)89 public void setEnabled(boolean enabled) { 90 if (enabled && mHelper.isDisabledByAdmin()) { 91 mHelper.setDisabledByAdmin(null); 92 return; 93 } 94 95 if (enabled && mHelper.isDisabledByEcm()) { 96 mHelper.setDisabledByEcm(null); 97 return; 98 } 99 100 super.setEnabled(enabled); 101 } 102 103 @Override onAttachedToHierarchy(@onNull PreferenceManager preferenceManager)104 protected void onAttachedToHierarchy(@NonNull PreferenceManager preferenceManager) { 105 mHelper.onAttachedToHierarchy(); 106 super.onAttachedToHierarchy(preferenceManager); 107 } 108 } 109