1 /*
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 
17 package com.android.settings.widget;
18 
19 import android.content.Context;
20 import android.widget.CompoundButton;
21 import android.widget.CompoundButton.OnCheckedChangeListener;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settingslib.widget.MainSwitchPreference;
28 
29 /**
30  * Preference controller for MainSwitchPreference.
31  */
32 public abstract class SettingsMainSwitchPreferenceController extends
33         TogglePreferenceController implements OnCheckedChangeListener {
34 
35     protected MainSwitchPreference mSwitchPreference;
36 
SettingsMainSwitchPreferenceController(Context context, String preferenceKey)37     public SettingsMainSwitchPreferenceController(Context context, String preferenceKey) {
38         super(context, preferenceKey);
39     }
40 
41     @Override
displayPreference(PreferenceScreen screen)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
onCheckedChanged(CompoundButton buttonView, boolean isChecked)52     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
53         mSwitchPreference.setChecked(isChecked);
54         setChecked(isChecked);
55     }
56 }
57