OSDN Git Service

Remove Slice getter from BasePreferenceController
[android-x86/packages-apps-Settings.git] / src / com / android / settings / core / TogglePreferenceController.java
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.v14.preference.SwitchPreference;
18 import android.support.v7.preference.Preference;
19
20 /**
21  * Abstract class that consolidates logic for updating toggle controllers.
22  * It automatically handles the getting and setting of the switch UI element.
23  * Children of this class implement methods to get and set the underlying value of the setting.
24  */
25 public abstract class TogglePreferenceController extends BasePreferenceController implements
26         Preference.OnPreferenceChangeListener {
27
28     private static final String TAG = "TogglePrefController";
29
30     public TogglePreferenceController(Context context, String preferenceKey) {
31         super(context, preferenceKey);
32     }
33
34     /**
35      * @return {@code true} if the Setting is enabled.
36      */
37     public abstract boolean isChecked();
38
39     /**
40      * Set the Setting to {@param isChecked}
41      *
42      * @param isChecked Is {@true} when the setting should be enabled.
43      */
44     public abstract void setChecked(boolean isChecked);
45
46     @Override
47     public final void updateState(Preference preference) {
48         ((SwitchPreference) preference).setChecked(isChecked());
49     }
50
51     @Override
52     public final boolean onPreferenceChange(Preference preference, Object newValue) {
53         boolean auto = (Boolean) newValue;
54         setChecked(auto);
55         return true;
56     }
57 }