Home | History | Annotate | Download | only in development
      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 
     21 import androidx.preference.Preference;
     22 import androidx.preference.PreferenceScreen;
     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  */
     32 public abstract class DeveloperOptionsPreferenceController extends AbstractPreferenceController {
     33 
     34     protected Preference mPreference;
     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 
     50     @Override
     51     public void displayPreference(PreferenceScreen screen) {
     52         super.displayPreference(screen);
     53         mPreference = screen.findPreference(getPreferenceKey());
     54     }
     55 
     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      */
     77     protected void onDeveloperOptionsSwitchEnabled() {
     78         mPreference.setEnabled(true);
     79     }
     80 
     81     /**
     82      * Called when developer options is disabled and the preference is available
     83      */
     84     protected void onDeveloperOptionsSwitchDisabled() {
     85         mPreference.setEnabled(false);
     86     }
     87 
     88 }
     89