Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 package com.android.settings.core;
     15 
     16 import android.content.Context;
     17 import android.support.v7.preference.Preference;
     18 import android.support.v7.preference.TwoStatePreference;
     19 
     20 import com.android.settings.slices.SliceData;
     21 import com.android.settings.widget.MasterSwitchPreference;
     22 
     23 /**
     24  * Abstract class that consolidates logic for updating toggle controllers.
     25  * It automatically handles the getting and setting of the switch UI element.
     26  * Children of this class implement methods to get and set the underlying value of the setting.
     27  */
     28 public abstract class TogglePreferenceController extends BasePreferenceController implements
     29         Preference.OnPreferenceChangeListener {
     30 
     31     private static final String TAG = "TogglePrefController";
     32 
     33     public TogglePreferenceController(Context context, String preferenceKey) {
     34         super(context, preferenceKey);
     35     }
     36 
     37     /**
     38      * @return {@code true} if the Setting is enabled.
     39      */
     40     public abstract boolean isChecked();
     41 
     42     /**
     43      * Set the Setting to {@param isChecked}
     44      *
     45      * @param isChecked Is {@code true} when the setting should be enabled.
     46      * @return {@code true} if the underlying setting is updated.
     47      */
     48     public abstract boolean setChecked(boolean isChecked);
     49 
     50     @Override
     51     public void updateState(Preference preference) {
     52         if (preference instanceof TwoStatePreference) {
     53             ((TwoStatePreference) preference).setChecked(isChecked());
     54         } else if (preference instanceof MasterSwitchPreference) {
     55             ((MasterSwitchPreference) preference).setChecked(isChecked());
     56         } else {
     57             refreshSummary(preference);
     58         }
     59     }
     60 
     61     @Override
     62     public final boolean onPreferenceChange(Preference preference, Object newValue) {
     63         return setChecked((boolean) newValue);
     64     }
     65 
     66     @Override
     67     @SliceData.SliceType
     68     public int getSliceType() {
     69         return SliceData.SliceType.SWITCH;
     70     }
     71 
     72 }