blob: 5fa9259ab9ec73c9567f6015f527500acb324b3c [file] [log] [blame]
Stanley Wang9b906cb2021-04-12 19:46:22 +08001/*
2 * Copyright (C) 2021 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
17package com.android.settings.widget;
18
19import android.content.Context;
20import android.widget.Switch;
21
22import androidx.preference.Preference;
23import androidx.preference.PreferenceScreen;
24
25import com.android.settings.core.TogglePreferenceController;
26import com.android.settingslib.widget.MainSwitchPreference;
27import com.android.settingslib.widget.OnMainSwitchChangeListener;
28
29/**
30 * Preference controller for MainSwitchPreference.
31 */
32public abstract class SettingsMainSwitchPreferenceController extends
33 TogglePreferenceController implements OnMainSwitchChangeListener {
34
35 protected MainSwitchPreference mSwitchPreference;
36
37 public SettingsMainSwitchPreferenceController(Context context, String preferenceKey) {
38 super(context, preferenceKey);
39 }
40
41 @Override
42 public void displayPreference(PreferenceScreen screen) {
43 super.displayPreference(screen);
44 final Preference pref = screen.findPreference(getPreferenceKey());
45 if (pref != null && pref instanceof MainSwitchPreference) {
46 mSwitchPreference = (MainSwitchPreference) pref;
47 mSwitchPreference.addOnSwitchChangeListener(this);
48 }
49 }
50
51 @Override
52 public void onSwitchChanged(Switch switchView, boolean isChecked) {
53 mSwitchPreference.setChecked(isChecked);
54 setChecked(isChecked);
55 }
56}