| Stanley Wang | 9b906cb | 2021-04-12 19:46:22 +0800 | [diff] [blame^] | 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.Switch; |
| 21 | |
| 22 | import androidx.preference.Preference; |
| 23 | import androidx.preference.PreferenceScreen; |
| 24 | |
| 25 | import com.android.settings.core.TogglePreferenceController; |
| 26 | import com.android.settingslib.widget.MainSwitchPreference; |
| 27 | import com.android.settingslib.widget.OnMainSwitchChangeListener; |
| 28 | |
| 29 | /** |
| 30 | * Preference controller for MainSwitchPreference. |
| 31 | */ |
| 32 | public 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 | } |