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;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
22 
23 import com.android.settingslib.core.AbstractPreferenceController;
24 
25 /**
26  * This controller is used handle changes for the master switch in the developer options page.
27  *
28  * All Preference Controllers that are a part of the developer options page should inherit this
29  * class.
30  */
31 public abstract class DeveloperOptionsPreferenceController extends AbstractPreferenceController {
32 
33     protected Preference mPreference;
34 
DeveloperOptionsPreferenceController(Context context)35     public DeveloperOptionsPreferenceController(Context context) {
36         super(context);
37     }
38 
39     /**
40      * Child classes should override this method to create custom logic for hiding preferences.
41      *
42      * @return true if the preference is to be displayed.
43      */
44     @Override
isAvailable()45     public boolean isAvailable() {
46         return true;
47     }
48 
49     @Override
displayPreference(PreferenceScreen screen)50     public void displayPreference(PreferenceScreen screen) {
51         super.displayPreference(screen);
52         mPreference = screen.findPreference(getPreferenceKey());
53     }
54 
55     /**
56      * Called when developer options is enabled
57      */
onDeveloperOptionsEnabled()58     public void onDeveloperOptionsEnabled() {
59         if (isAvailable()) {
60             onDeveloperOptionsSwitchEnabled();
61         }
62     }
63 
64     /**
65      * Called when developer options is disabled
66      */
onDeveloperOptionsDisabled()67     public void onDeveloperOptionsDisabled() {
68         if (isAvailable()) {
69             onDeveloperOptionsSwitchDisabled();
70         }
71     }
72 
73     /**
74      * Called when developer options is enabled and the preference is available
75      */
onDeveloperOptionsSwitchEnabled()76     protected void onDeveloperOptionsSwitchEnabled() {
77         mPreference.setEnabled(true);
78     }
79 
80     /**
81      * Called when developer options is disabled and the preference is available
82      */
onDeveloperOptionsSwitchDisabled()83     protected void onDeveloperOptionsSwitchDisabled() {
84         mPreference.setEnabled(false);
85     }
86 
87 }
88