1 /* 2 * Copyright (C) 2023 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.content.Context; 20 21 import androidx.annotation.NonNull; 22 import androidx.preference.DropDownPreference; 23 import androidx.preference.PreferenceViewHolder; 24 25 public class RestrictedDropDownPreference extends DropDownPreference { 26 RestrictedPreferenceHelper mHelper; 27 RestrictedDropDownPreference(@onNull Context context)28 public RestrictedDropDownPreference(@NonNull Context context) { 29 super(context); 30 mHelper = new RestrictedPreferenceHelper(context, this, null); 31 } 32 33 /** 34 * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this 35 * package. Marks the preference as disabled if so. 36 * @param settingIdentifier The key identifying the setting 37 * @param packageName the package to check the settingIdentifier for 38 */ checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName)39 public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier, 40 @NonNull String packageName) { 41 mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName); 42 } 43 44 @Override onBindViewHolder(@onNull PreferenceViewHolder holder)45 public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { 46 super.onBindViewHolder(holder); 47 mHelper.onBindViewHolder(holder); 48 } 49 50 @Override setEnabled(boolean enabled)51 public void setEnabled(boolean enabled) { 52 if (enabled && isDisabledByEcm()) { 53 mHelper.setDisabledByEcm(null); 54 return; 55 } 56 57 super.setEnabled(enabled); 58 } 59 60 @Override performClick()61 public void performClick() { 62 if (!mHelper.performClick()) { 63 super.performClick(); 64 } 65 } 66 isDisabledByEcm()67 public boolean isDisabledByEcm() { 68 return mHelper.isDisabledByEcm(); 69 } 70 } 71