2 * Copyright (C) 2017 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
14 package com.android.settings.core;
16 import android.content.Context;
17 import android.support.v14.preference.SwitchPreference;
18 import android.support.v7.preference.Preference;
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.
25 public abstract class TogglePreferenceController extends BasePreferenceController implements
26 Preference.OnPreferenceChangeListener {
28 private static final String TAG = "TogglePrefController";
30 public TogglePreferenceController(Context context, String preferenceKey) {
31 super(context, preferenceKey);
35 * @return {@code true} if the Setting is enabled.
37 public abstract boolean isChecked();
40 * Set the Setting to {@param isChecked}
42 * @param isChecked Is {@true} when the setting should be enabled.
44 public abstract void setChecked(boolean isChecked);
47 public final void updateState(Preference preference) {
48 ((SwitchPreference) preference).setChecked(isChecked());
52 public final boolean onPreferenceChange(Preference preference, Object newValue) {
53 boolean auto = (Boolean) newValue;