1 /* 2 * Copyright (C) 2017 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 androidx.preference.Preference; 20 21 import com.android.settings.dashboard.DashboardFragment; 22 import com.android.settings.overlay.FeatureFactory; 23 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 24 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 25 26 /* 27 * The switch controller that is used to update the switch widget in the MasterSwitchPreference 28 * layout. 29 */ 30 public class MasterSwitchController extends SwitchWidgetController implements 31 Preference.OnPreferenceChangeListener { 32 33 private final MasterSwitchPreference mPreference; 34 private final MetricsFeatureProvider mMetricsFeatureProvider; 35 MasterSwitchController(MasterSwitchPreference preference)36 public MasterSwitchController(MasterSwitchPreference preference) { 37 mPreference = preference; 38 mMetricsFeatureProvider = FeatureFactory.getFactory(preference.getContext()) 39 .getMetricsFeatureProvider(); 40 } 41 42 @Override updateTitle(boolean isChecked)43 public void updateTitle(boolean isChecked) { 44 } 45 46 @Override startListening()47 public void startListening() { 48 mPreference.setOnPreferenceChangeListener(this); 49 } 50 51 @Override stopListening()52 public void stopListening() { 53 mPreference.setOnPreferenceChangeListener(null); 54 } 55 56 @Override setChecked(boolean checked)57 public void setChecked(boolean checked) { 58 mPreference.setChecked(checked); 59 } 60 61 @Override isChecked()62 public boolean isChecked() { 63 return mPreference.isChecked(); 64 } 65 66 @Override setEnabled(boolean enabled)67 public void setEnabled(boolean enabled) { 68 mPreference.setSwitchEnabled(enabled); 69 } 70 71 @Override onPreferenceChange(Preference preference, Object newValue)72 public boolean onPreferenceChange(Preference preference, Object newValue) { 73 if (mListener != null) { 74 final boolean result = mListener.onSwitchToggled((Boolean) newValue); 75 if (result) { 76 mMetricsFeatureProvider.logClickedPreference(preference, 77 preference.getExtras().getInt(DashboardFragment.CATEGORY)); 78 } 79 return result; 80 } 81 return false; 82 } 83 84 @Override setDisabledByAdmin(EnforcedAdmin admin)85 public void setDisabledByAdmin(EnforcedAdmin admin) { 86 mPreference.setDisabledByAdmin(admin); 87 } 88 } 89