1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 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 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 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. 13 */ 14 package com.android.settings.core; 15 16 import android.content.Context; 17 18 import androidx.preference.Preference; 19 import androidx.preference.PreferenceScreen; 20 import androidx.preference.TwoStatePreference; 21 22 import com.android.settings.overlay.FeatureFactory; 23 import com.android.settings.slices.SliceData; 24 import com.android.settings.onboarding.OnboardingFeatureProvider; 25 import com.android.settings.widget.TwoStateButtonPreference; 26 import com.android.settingslib.PrimarySwitchPreference; 27 import com.android.settingslib.core.instrumentation.SettingsJankMonitor; 28 import com.android.settingslib.widget.MainSwitchPreference; 29 30 /** 31 * Abstract class that consolidates logic for updating toggle controllers. 32 * It automatically handles the getting and setting of the switch UI element. 33 * Children of this class implement methods to get and set the underlying value of the setting. 34 */ 35 public abstract class TogglePreferenceController extends BasePreferenceController implements 36 Preference.OnPreferenceChangeListener { 37 38 private static final String TAG = "TogglePrefController"; 39 TogglePreferenceController(Context context, String preferenceKey)40 public TogglePreferenceController(Context context, String preferenceKey) { 41 super(context, preferenceKey); 42 } 43 44 /** 45 * @return {@code true} if the Setting is enabled. 46 */ isChecked()47 public abstract boolean isChecked(); 48 49 /** 50 * Set the Setting to {@param isChecked} 51 * 52 * @param isChecked Is {@code true} when the setting should be enabled. 53 * @return {@code true} if the underlying setting is updated. 54 */ setChecked(boolean isChecked)55 public abstract boolean setChecked(boolean isChecked); 56 57 @Override displayPreference(PreferenceScreen screen)58 public void displayPreference(PreferenceScreen screen) { 59 super.displayPreference(screen); 60 Preference preference = screen.findPreference(getPreferenceKey()); 61 if (preference instanceof MainSwitchPreference) { 62 ((MainSwitchPreference) preference).addOnSwitchChangeListener((switchView, isChecked) -> 63 SettingsJankMonitor.detectToggleJank(getPreferenceKey(), switchView)); 64 } 65 } 66 67 @Override updateState(Preference preference)68 public void updateState(Preference preference) { 69 if (preference instanceof TwoStatePreference) { 70 ((TwoStatePreference) preference).setChecked(isChecked()); 71 } else if (preference instanceof PrimarySwitchPreference) { 72 ((PrimarySwitchPreference) preference).setChecked(isChecked()); 73 } else if (preference instanceof TwoStateButtonPreference) { 74 ((TwoStateButtonPreference) preference).setChecked(isChecked()); 75 } else { 76 refreshSummary(preference); 77 } 78 } 79 80 @Override onPreferenceChange(Preference preference, Object newValue)81 public final boolean onPreferenceChange(Preference preference, Object newValue) { 82 // TwoStatePreference is a regular preference and can be handled by DashboardFragment 83 if (preference instanceof PrimarySwitchPreference 84 || preference instanceof TwoStateButtonPreference) { 85 FeatureFactory.getFeatureFactory().getMetricsFeatureProvider() 86 .logClickedPreference(preference, getMetricsCategory()); 87 } 88 OnboardingFeatureProvider onboardingFeatureProvider = 89 FeatureFactory.getFeatureFactory().getOnboardingFeatureProvider(); 90 if (onboardingFeatureProvider != null) { 91 onboardingFeatureProvider.markPreferenceHasChanged(mContext, mPreferenceKey); 92 } 93 return setChecked((boolean) newValue); 94 } 95 96 @Override 97 @SliceData.SliceType getSliceType()98 public int getSliceType() { 99 return SliceData.SliceType.SWITCH; 100 } 101 102 @Override isSliceable()103 public boolean isSliceable() { 104 return true; 105 } 106 107 @Override isPublicSlice()108 public boolean isPublicSlice() { 109 return false; 110 } 111 112 @Override getSliceHighlightMenuRes()113 public abstract int getSliceHighlightMenuRes(); 114 }