jeffreyhuang | 12ba446 | 2017-10-06 11:40:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.development; |
| 18 | |
| 19 | import android.content.Context; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 20 | |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 21 | import androidx.preference.Preference; |
| 22 | import androidx.preference.PreferenceScreen; |
jeffreyhuang | 12ba446 | 2017-10-06 11:40:59 -0700 | [diff] [blame] | 23 | |
| 24 | import com.android.settingslib.core.AbstractPreferenceController; |
| 25 | |
| 26 | /** |
| 27 | * This controller is used handle changes for the master switch in the developer options page. |
| 28 | * |
| 29 | * All Preference Controllers that are a part of the developer options page should inherit this |
| 30 | * class. |
| 31 | */ |
Doris Ling | 7e91932 | 2018-02-28 16:06:19 -0800 | [diff] [blame] | 32 | public abstract class DeveloperOptionsPreferenceController extends AbstractPreferenceController { |
| 33 | |
| 34 | protected Preference mPreference; |
jeffreyhuang | 12ba446 | 2017-10-06 11:40:59 -0700 | [diff] [blame] | 35 | |
| 36 | public DeveloperOptionsPreferenceController(Context context) { |
| 37 | super(context); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Child classes should override this method to create custom logic for hiding preferences. |
| 42 | * |
| 43 | * @return true if the preference is to be displayed. |
| 44 | */ |
| 45 | @Override |
| 46 | public boolean isAvailable() { |
| 47 | return true; |
| 48 | } |
| 49 | |
Doris Ling | 7e91932 | 2018-02-28 16:06:19 -0800 | [diff] [blame] | 50 | @Override |
| 51 | public void displayPreference(PreferenceScreen screen) { |
| 52 | super.displayPreference(screen); |
| 53 | mPreference = screen.findPreference(getPreferenceKey()); |
| 54 | } |
| 55 | |
jeffreyhuang | 12ba446 | 2017-10-06 11:40:59 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Called when developer options is enabled |
| 58 | */ |
| 59 | public void onDeveloperOptionsEnabled() { |
| 60 | if (isAvailable()) { |
| 61 | onDeveloperOptionsSwitchEnabled(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Called when developer options is disabled |
| 67 | */ |
| 68 | public void onDeveloperOptionsDisabled() { |
| 69 | if (isAvailable()) { |
| 70 | onDeveloperOptionsSwitchDisabled(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Called when developer options is enabled and the preference is available |
| 76 | */ |
jeffreyhuang | 5c4d82a | 2017-10-09 10:24:09 -0700 | [diff] [blame] | 77 | protected void onDeveloperOptionsSwitchEnabled() { |
Doris Ling | 7e91932 | 2018-02-28 16:06:19 -0800 | [diff] [blame] | 78 | mPreference.setEnabled(true); |
jeffreyhuang | 5c4d82a | 2017-10-09 10:24:09 -0700 | [diff] [blame] | 79 | } |
jeffreyhuang | 12ba446 | 2017-10-06 11:40:59 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Called when developer options is disabled and the preference is available |
| 83 | */ |
jeffreyhuang | 5c4d82a | 2017-10-09 10:24:09 -0700 | [diff] [blame] | 84 | protected void onDeveloperOptionsSwitchDisabled() { |
Doris Ling | 7e91932 | 2018-02-28 16:06:19 -0800 | [diff] [blame] | 85 | mPreference.setEnabled(false); |
jeffreyhuang | 5c4d82a | 2017-10-09 10:24:09 -0700 | [diff] [blame] | 86 | } |
jeffreyhuang | 12ba446 | 2017-10-06 11:40:59 -0700 | [diff] [blame] | 87 | |
| 88 | } |