diff --git a/src/com/android/settings/accessibility/VibrationMainSwitchPreferenceController.java b/src/com/android/settings/accessibility/VibrationMainSwitchPreferenceController.java index 0f2fb77a98a..9e58560ceb8 100644 --- a/src/com/android/settings/accessibility/VibrationMainSwitchPreferenceController.java +++ b/src/com/android/settings/accessibility/VibrationMainSwitchPreferenceController.java @@ -28,8 +28,13 @@ import android.os.VibrationAttributes; import android.os.Vibrator; import android.provider.Settings; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; + import com.android.settings.R; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStop; @@ -42,11 +47,12 @@ import com.android.settingslib.core.lifecycle.events.OnStop; * be disabled by this setting, except the flagged alerts and accessibility touch feedback. */ // LINT.IfChange -public class VibrationMainSwitchPreferenceController extends SettingsMainSwitchPreferenceController +public class VibrationMainSwitchPreferenceController extends TogglePreferenceController implements LifecycleObserver, OnStart, OnStop { private final ContentObserver mSettingObserver; private final Vibrator mVibrator; + private @Nullable Preference mPreference; public VibrationMainSwitchPreferenceController(Context context, String preferenceKey) { super(context, preferenceKey); @@ -55,7 +61,9 @@ public class VibrationMainSwitchPreferenceController extends SettingsMainSwitchP mSettingObserver = new ContentObserver(handler) { @Override public void onChange(boolean selfChange, Uri uri) { - updateState(mSwitchPreference); + if (mPreference != null) { + updateState(mPreference); + } } }; } @@ -65,6 +73,12 @@ public class VibrationMainSwitchPreferenceController extends SettingsMainSwitchP return AVAILABLE; } + @Override + public void displayPreference(@NonNull PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = screen.findPreference(getPreferenceKey()); + } + @Override public void onStart() { mContext.getContentResolver().registerContentObserver( diff --git a/src/com/android/settings/gestures/DoubleTapPowerMainSwitchPreferenceController.java b/src/com/android/settings/gestures/DoubleTapPowerMainSwitchPreferenceController.java index 4a6d328b59e..8ce6d58213d 100644 --- a/src/com/android/settings/gestures/DoubleTapPowerMainSwitchPreferenceController.java +++ b/src/com/android/settings/gestures/DoubleTapPowerMainSwitchPreferenceController.java @@ -27,34 +27,44 @@ import android.os.Looper; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; import com.android.settings.R; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStop; /** The controller to handle double tap power button main switch enable or disable state. */ public class DoubleTapPowerMainSwitchPreferenceController - extends SettingsMainSwitchPreferenceController + extends TogglePreferenceController implements LifecycleObserver, OnStart, OnStop { private final ContentObserver mSettingsObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { @Override public void onChange(boolean selfChange, @Nullable Uri uri) { - if (mSwitchPreference == null || uri == null) { + if (mPreference == null || uri == null) { return; } - updateState(mSwitchPreference); + updateState(mPreference); } }; + private @Nullable Preference mPreference; + public DoubleTapPowerMainSwitchPreferenceController( @NonNull Context context, @NonNull String preferenceKey) { super(context, preferenceKey); } + @Override + public void displayPreference(@NonNull PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = screen.findPreference(getPreferenceKey()); + } + @Override public int getAvailabilityStatus() { return DoubleTapPowerSettingsUtils diff --git a/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java b/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java index e1a3fb437ee..70473bf42a5 100644 --- a/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java +++ b/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java @@ -23,15 +23,14 @@ import android.provider.Settings; import androidx.preference.PreferenceScreen; import com.android.settings.R; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; /** - * {@link SettingsMainSwitchPreferenceController} + * {@link TogglePreferenceController} * that controls whether Adaptive connectivity option is enabled. */ // LINT.IfChange -public class AdaptiveConnectivityTogglePreferenceController extends - SettingsMainSwitchPreferenceController { +public class AdaptiveConnectivityTogglePreferenceController extends TogglePreferenceController { private final WifiManager mWifiManager; diff --git a/src/com/android/settings/notification/BubbleNotificationPreferenceController.java b/src/com/android/settings/notification/BubbleNotificationPreferenceController.java index d11e2e965d4..67ac5053eb2 100644 --- a/src/com/android/settings/notification/BubbleNotificationPreferenceController.java +++ b/src/com/android/settings/notification/BubbleNotificationPreferenceController.java @@ -25,10 +25,12 @@ import android.net.Uri; import android.os.Handler; import android.provider.Settings; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.events.OnPause; import com.android.settingslib.core.lifecycle.events.OnResume; @@ -38,10 +40,11 @@ import com.android.settingslib.core.lifecycle.events.OnResume; * Allows user to turn bubbles on or off for the device. */ public class BubbleNotificationPreferenceController extends - SettingsMainSwitchPreferenceController implements LifecycleObserver, OnResume, OnPause { + TogglePreferenceController implements LifecycleObserver, OnResume, OnPause { private static final String TAG = "BubbleNotifPrefContr"; + private @Nullable Preference mPreference; private SettingObserver mSettingObserver; public BubbleNotificationPreferenceController(Context context, String preferenceKey) { @@ -49,10 +52,11 @@ public class BubbleNotificationPreferenceController extends } @Override - public void displayPreference(PreferenceScreen screen) { + public void displayPreference(@NonNull PreferenceScreen screen) { super.displayPreference(screen); - if (mSwitchPreference != null) { - mSettingObserver = new SettingObserver(mSwitchPreference); + mPreference = screen.findPreference(getPreferenceKey()); + if (mPreference != null) { + mSettingObserver = new SettingObserver(mPreference); } } diff --git a/src/com/android/settings/notification/BundleGlobalPreferenceController.java b/src/com/android/settings/notification/BundleGlobalPreferenceController.java index e41e7dd7c63..4df36ca716c 100644 --- a/src/com/android/settings/notification/BundleGlobalPreferenceController.java +++ b/src/com/android/settings/notification/BundleGlobalPreferenceController.java @@ -21,10 +21,9 @@ import android.content.Context; import androidx.annotation.NonNull; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; -public class BundleGlobalPreferenceController extends - SettingsMainSwitchPreferenceController { +public class BundleGlobalPreferenceController extends TogglePreferenceController { NotificationBackend mBackend; diff --git a/src/com/android/settings/notification/BundleTypePreferenceController.java b/src/com/android/settings/notification/BundleTypePreferenceController.java index b9fb2b2d78f..d0786931ed7 100644 --- a/src/com/android/settings/notification/BundleTypePreferenceController.java +++ b/src/com/android/settings/notification/BundleTypePreferenceController.java @@ -22,10 +22,9 @@ import android.service.notification.Adjustment; import androidx.annotation.NonNull; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; -public class BundleTypePreferenceController extends - SettingsMainSwitchPreferenceController { +public class BundleTypePreferenceController extends TogglePreferenceController { static final String PROMO_KEY = "promotions"; static final String NEWS_KEY = "news"; diff --git a/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java b/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java index 7c7664e5da9..9e946a72be9 100644 --- a/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java +++ b/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java @@ -33,10 +33,10 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import com.android.server.notification.Flags; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; public class LockScreenNotificationsGlobalPreferenceController - extends SettingsMainSwitchPreferenceController + extends TogglePreferenceController implements LifecycleEventObserver { public static final int ON = 1; diff --git a/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceController.java b/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceController.java index f25c7849e33..564c6d34845 100644 --- a/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceController.java +++ b/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceController.java @@ -22,10 +22,9 @@ import android.provider.Settings; import androidx.annotation.NonNull; import com.android.server.notification.Flags; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; -public class PoliteNotificationGlobalPreferenceController extends - SettingsMainSwitchPreferenceController { +public class PoliteNotificationGlobalPreferenceController extends TogglePreferenceController { public static final int ON = 1; public static final int OFF = 0; diff --git a/src/com/android/settings/notification/SummarizationGlobalPreferenceController.java b/src/com/android/settings/notification/SummarizationGlobalPreferenceController.java index 0d66f37a91b..f3e5b8fc75a 100644 --- a/src/com/android/settings/notification/SummarizationGlobalPreferenceController.java +++ b/src/com/android/settings/notification/SummarizationGlobalPreferenceController.java @@ -21,10 +21,9 @@ import android.content.Context; import androidx.annotation.NonNull; -import com.android.settings.widget.SettingsMainSwitchPreferenceController; +import com.android.settings.core.TogglePreferenceController; -public class SummarizationGlobalPreferenceController extends - SettingsMainSwitchPreferenceController { +public class SummarizationGlobalPreferenceController extends TogglePreferenceController { NotificationBackend mBackend; diff --git a/src/com/android/settings/widget/SettingsMainSwitchPreferenceController.java b/src/com/android/settings/widget/SettingsMainSwitchPreferenceController.java deleted file mode 100644 index a5307f55f50..00000000000 --- a/src/com/android/settings/widget/SettingsMainSwitchPreferenceController.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.widget; - -import android.content.Context; -import android.widget.CompoundButton; -import android.widget.CompoundButton.OnCheckedChangeListener; - -import androidx.preference.Preference; -import androidx.preference.PreferenceScreen; - -import com.android.settings.core.TogglePreferenceController; -import com.android.settingslib.widget.MainSwitchPreference; - -/** - * Preference controller for MainSwitchPreference. - * - * @deprecated Use {@link TogglePreferenceController} directly - */ -@Deprecated -public abstract class SettingsMainSwitchPreferenceController extends - TogglePreferenceController implements OnCheckedChangeListener { - - protected MainSwitchPreference mSwitchPreference; - - public SettingsMainSwitchPreferenceController(Context context, String preferenceKey) { - super(context, preferenceKey); - } - - @Override - public void displayPreference(PreferenceScreen screen) { - super.displayPreference(screen); - final Preference pref = screen.findPreference(getPreferenceKey()); - if (pref != null && pref instanceof MainSwitchPreference) { - mSwitchPreference = (MainSwitchPreference) pref; - mSwitchPreference.addOnSwitchChangeListener(this); - } - } - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - mSwitchPreference.setChecked(isChecked); - setChecked(isChecked); - } -} diff --git a/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java index dd212b47b58..ab77b4f7f87 100644 --- a/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java @@ -31,7 +31,6 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.content.Context; import android.provider.Settings; -import android.widget.Switch; import androidx.preference.PreferenceScreen; import androidx.test.core.app.ApplicationProvider; @@ -66,9 +65,6 @@ public class BubbleNotificationPreferenceControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private PreferenceScreen mScreen; - @Mock - private Switch mSwitch; - private BubbleNotificationPreferenceController mController; private MainSwitchPreference mPreference; @@ -113,7 +109,7 @@ public class BubbleNotificationPreferenceControllerTest { public void onSwitchChanged_true_settingIsOff_flagShouldOn() { Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF); - mController.onCheckedChanged(mSwitch, true); + mController.setChecked(true); assertThat(Settings.Global.getInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF)).isEqualTo(ON); @@ -123,7 +119,7 @@ public class BubbleNotificationPreferenceControllerTest { public void onSwitchChanged_false_settingIsOn_flagShouldOff() { Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON); - mController.onCheckedChanged(mSwitch, false); + mController.setChecked(false); assertThat(Settings.Global.getInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);