• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 implements
26         RestrictedPreferenceHelperProvider {
27     private final RestrictedPreferenceHelper mHelper;
28 
RestrictedDropDownPreference(@onNull Context context)29     public RestrictedDropDownPreference(@NonNull Context context) {
30         super(context);
31         mHelper = new RestrictedPreferenceHelper(context, this, null);
32     }
33 
34     @Override
getRestrictedPreferenceHelper()35     public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() {
36         return mHelper;
37     }
38 
39     /**
40      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
41      * package. Marks the preference as disabled if so.
42      * @param settingIdentifier The key identifying the setting
43      * @param packageName the package to check the settingIdentifier for
44      * @param settingEnabled Whether the setting in question is enabled
45      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName, boolean settingEnabled)46     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
47             @NonNull String packageName, boolean settingEnabled) {
48         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName, settingEnabled);
49     }
50 
51     /**
52      * Checks if the given setting is subject to Enhanced Confirmation Mode restrictions for this
53      * package. Marks the preference as disabled if so.
54      * TODO b/390196024: remove this and update all callers to use the "settingEnabled" version
55      * @param settingIdentifier The key identifying the setting
56      * @param packageName the package to check the settingIdentifier for
57      */
checkEcmRestrictionAndSetDisabled(@onNull String settingIdentifier, @NonNull String packageName)58     public void checkEcmRestrictionAndSetDisabled(@NonNull String settingIdentifier,
59             @NonNull String packageName) {
60         mHelper.checkEcmRestrictionAndSetDisabled(settingIdentifier, packageName, false);
61     }
62 
63     @Override
onBindViewHolder(@onNull PreferenceViewHolder holder)64     public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
65         super.onBindViewHolder(holder);
66         mHelper.onBindViewHolder(holder);
67     }
68 
69     @Override
setEnabled(boolean enabled)70     public void setEnabled(boolean enabled) {
71         if (enabled && isDisabledByEcm()) {
72             mHelper.setDisabledByEcm(null);
73             return;
74         }
75 
76         super.setEnabled(enabled);
77     }
78 
79     @Override
performClick()80     public void performClick() {
81         if (!mHelper.performClick()) {
82             super.performClick();
83         }
84     }
85 
isDisabledByEcm()86     public boolean isDisabledByEcm() {
87         return mHelper.isDisabledByEcm();
88     }
89 }
90