blob: 60e3388b2641ce4ee366483c1bb3779726571996 [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;
Chaohui Wang08a1c982023-11-07 20:41:11 +080020import android.widget.CompoundButton;
21import android.widget.CompoundButton.OnCheckedChangeListener;
Stanley Wang9b906cb2021-04-12 19:46:22 +080022
23import androidx.preference.Preference;
24import androidx.preference.PreferenceScreen;
25
26import com.android.settings.core.TogglePreferenceController;
27import com.android.settingslib.widget.MainSwitchPreference;
Stanley Wang9b906cb2021-04-12 19:46:22 +080028
29/**
30 * Preference controller for MainSwitchPreference.
31 */
32public abstract class SettingsMainSwitchPreferenceController extends
Chaohui Wang08a1c982023-11-07 20:41:11 +080033 TogglePreferenceController implements OnCheckedChangeListener {
Stanley Wang9b906cb2021-04-12 19:46:22 +080034
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
Chaohui Wang08a1c982023-11-07 20:41:11 +080052 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Stanley Wang9b906cb2021-04-12 19:46:22 +080053 mSwitchPreference.setChecked(isChecked);
54 setChecked(isChecked);
55 }
56}