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;
18 import androidx.preference.Preference;
19 import androidx.preference.TwoStatePreference;
21 import com.android.settings.slices.SliceData;
22 import com.android.settings.widget.MasterSwitchPreference;
25 * Abstract class that consolidates logic for updating toggle controllers.
26 * It automatically handles the getting and setting of the switch UI element.
27 * Children of this class implement methods to get and set the underlying value of the setting.
29 public abstract class TogglePreferenceController extends BasePreferenceController implements
30 Preference.OnPreferenceChangeListener {
32 private static final String TAG = "TogglePrefController";
34 public TogglePreferenceController(Context context, String preferenceKey) {
35 super(context, preferenceKey);
39 * @return {@code true} if the Setting is enabled.
41 public abstract boolean isChecked();
44 * Set the Setting to {@param isChecked}
46 * @param isChecked Is {@code true} when the setting should be enabled.
47 * @return {@code true} if the underlying setting is updated.
49 public abstract boolean setChecked(boolean isChecked);
52 public void updateState(Preference preference) {
53 if (preference instanceof TwoStatePreference) {
54 ((TwoStatePreference) preference).setChecked(isChecked());
55 } else if (preference instanceof MasterSwitchPreference) {
56 ((MasterSwitchPreference) preference).setChecked(isChecked());
58 refreshSummary(preference);
63 public final boolean onPreferenceChange(Preference preference, Object newValue) {
64 return setChecked((boolean) newValue);
69 public int getSliceType() {
70 return SliceData.SliceType.SWITCH;
74 public boolean isSliceable() {
79 public boolean isPublicSlice() {