diff --git a/res/layout/dream_start_button.xml b/res/layout/dream_start_button.xml
deleted file mode 100644
index a45e56ffa13..00000000000
--- a/res/layout/dream_start_button.xml
+++ /dev/null
@@ -1,38 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 859f411bf5a..742ed97ae2e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3122,8 +3122,6 @@
When to start
Current screen saver
-
- Start now
Settings
@@ -8746,10 +8744,6 @@
On / Conversations can appear as floating icons
-
- Allow apps to show bubbles
-
- Some conversations will appear as floating icons on top of other apps
All conversations can bubble
@@ -13232,6 +13226,10 @@
Use adaptive brightness
Use Wi‑Fi calling
+
+ Use Bubbles
+
+ Use screen saver
See all apps
diff --git a/res/xml/bubble_notification_settings.xml b/res/xml/bubble_notification_settings.xml
index 4827cb6df95..70492b4fd8b 100644
--- a/res/xml/bubble_notification_settings.xml
+++ b/res/xml/bubble_notification_settings.xml
@@ -18,6 +18,11 @@
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/bubbles_app_toggle_title">
+
+
-
-
-
-
\ No newline at end of file
+
diff --git a/res/xml/dream_fragment_overview.xml b/res/xml/dream_fragment_overview.xml
index 3cb49f9cced..6e18a979f3e 100644
--- a/res/xml/dream_fragment_overview.xml
+++ b/res/xml/dream_fragment_overview.xml
@@ -19,6 +19,11 @@
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/screensaver_settings_title">
+
+
-
-
-
-
-
-
diff --git a/res/xml/one_handed_settings.xml b/res/xml/one_handed_settings.xml
index 5a71d48cd4e..b2c7de9788d 100644
--- a/res/xml/one_handed_settings.xml
+++ b/res/xml/one_handed_settings.xml
@@ -21,6 +21,12 @@
android:persistent="false"
android:title="@string/one_handed_title">
+
+
-
-
buildPreferenceControllers(Context context) {
- List controllers = new ArrayList<>();
+ final List controllers = new ArrayList<>();
controllers.add(new WhenToDreamPreferenceController(context));
- controllers.add(new StartNowPreferenceController(context));
return controllers;
}
diff --git a/src/com/android/settings/dream/StartNowPreferenceController.java b/src/com/android/settings/dream/StartNowPreferenceController.java
index fb0581f343a..ed0a4dbeece 100644
--- a/src/com/android/settings/dream/StartNowPreferenceController.java
+++ b/src/com/android/settings/dream/StartNowPreferenceController.java
@@ -17,63 +17,52 @@
package com.android.settings.dream;
import android.content.Context;
-import android.widget.Button;
import androidx.preference.Preference;
-import androidx.preference.PreferenceScreen;
-import com.android.settings.R;
-import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.overlay.FeatureFactory;
-import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settings.widget.SettingsMainSwitchPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.dream.DreamBackend;
-import com.android.settingslib.widget.LayoutPreference;
-public class StartNowPreferenceController extends AbstractPreferenceController implements
- PreferenceControllerMixin {
+/**
+ * Controller that used to enable screen saver
+ */
+public class StartNowPreferenceController extends SettingsMainSwitchPreferenceController {
- private static final String PREF_KEY = "dream_start_now_button_container";
private final DreamBackend mBackend;
private final MetricsFeatureProvider mMetricsFeatureProvider;
- public StartNowPreferenceController(Context context) {
- super(context);
-
+ public StartNowPreferenceController(Context context, String preferenceKey) {
+ super(context, preferenceKey);
mBackend = DreamBackend.getInstance(context);
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
}
@Override
- public boolean isAvailable() {
- return true;
- }
-
- @Override
- public String getPreferenceKey() {
- return PREF_KEY;
- }
-
- @Override
- public void displayPreference(PreferenceScreen screen) {
- super.displayPreference(screen);
-
- LayoutPreference pref = screen.findPreference(getPreferenceKey());
- Button startButton = pref.findViewById(R.id.dream_start_now_button);
- startButton.setOnClickListener(v -> {
- mMetricsFeatureProvider.logClickedPreference(pref,
- pref.getExtras().getInt(DashboardFragment.CATEGORY));
- mBackend.startDreaming();
- });
+ public int getAvailabilityStatus() {
+ return AVAILABLE;
}
@Override
public void updateState(Preference preference) {
- super.updateState(preference);
+ mSwitchPreference.setChecked(false);
+ mSwitchPreference.setEnabled(mBackend.getWhenToDreamSetting() != DreamBackend.NEVER);
+ }
- Button startButton = ((LayoutPreference) preference)
- .findViewById(R.id.dream_start_now_button);
- startButton.setEnabled(mBackend.getWhenToDreamSetting() != DreamBackend.NEVER);
+ @Override
+ public boolean isChecked() {
+ return false;
+ }
+
+ @Override
+ public boolean setChecked(boolean isChecked) {
+ if (isChecked) {
+ mMetricsFeatureProvider.logClickedPreference(mSwitchPreference,
+ mSwitchPreference.getExtras().getInt(DashboardFragment.CATEGORY));
+ mBackend.startDreaming();
+ }
+ return true;
}
}
diff --git a/src/com/android/settings/gestures/OneHandedEnablePreferenceController.java b/src/com/android/settings/gestures/OneHandedEnablePreferenceController.java
index adfa137232e..75018970449 100644
--- a/src/com/android/settings/gestures/OneHandedEnablePreferenceController.java
+++ b/src/com/android/settings/gestures/OneHandedEnablePreferenceController.java
@@ -20,12 +20,12 @@ import android.content.Context;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
-import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.widget.SettingsMainSwitchPreferenceController;
/**
* The controller to handle one-handed mode enable or disable state.
**/
-public class OneHandedEnablePreferenceController extends TogglePreferenceController {
+public class OneHandedEnablePreferenceController extends SettingsMainSwitchPreferenceController {
public OneHandedEnablePreferenceController(Context context, String key) {
super(context, key);
diff --git a/src/com/android/settings/notification/BubbleNotificationPreferenceController.java b/src/com/android/settings/notification/BubbleNotificationPreferenceController.java
index 67f0c7b34ab..37723b13283 100644
--- a/src/com/android/settings/notification/BubbleNotificationPreferenceController.java
+++ b/src/com/android/settings/notification/BubbleNotificationPreferenceController.java
@@ -30,8 +30,7 @@ import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
-import com.android.settings.core.PreferenceControllerMixin;
-import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.widget.SettingsMainSwitchPreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
@@ -40,9 +39,8 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
* Feature level screen for bubbles, available through notification menu.
* Allows user to turn bubbles on or off for the device.
*/
-public class BubbleNotificationPreferenceController extends TogglePreferenceController
- implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
- LifecycleObserver, OnResume, OnPause {
+public class BubbleNotificationPreferenceController extends
+ SettingsMainSwitchPreferenceController implements LifecycleObserver, OnResume, OnPause {
private static final String TAG = "BubbleNotifPrefContr";
@@ -60,9 +58,8 @@ public class BubbleNotificationPreferenceController extends TogglePreferenceCont
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
- Preference preference = screen.findPreference(getPreferenceKey());
- if (preference != null) {
- mSettingObserver = new SettingObserver(preference);
+ if (mSwitchPreference != null) {
+ mSettingObserver = new SettingObserver(mSwitchPreference);
}
}
@@ -86,21 +83,22 @@ public class BubbleNotificationPreferenceController extends TogglePreferenceCont
return am.isLowRamDevice() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
}
+ @Override
+ public boolean isSliceable() {
+ return false;
+ }
+
@Override
public boolean isChecked() {
- return Settings.Secure.getInt(mContext.getContentResolver(),
+ return Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, ON) == ON;
}
@Override
public boolean setChecked(boolean isChecked) {
- return Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES,
- isChecked ? ON : OFF);
- }
-
- @Override
- public boolean isSliceable() {
- return false;
+ Settings.Global.putInt(mContext.getContentResolver(),
+ NOTIFICATION_BUBBLES, isChecked ? ON : OFF);
+ return true;
}
class SettingObserver extends ContentObserver {
diff --git a/src/com/android/settings/widget/SettingsMainSwitchPreferenceController.java b/src/com/android/settings/widget/SettingsMainSwitchPreferenceController.java
new file mode 100644
index 00000000000..5fa9259ab9e
--- /dev/null
+++ b/src/com/android/settings/widget/SettingsMainSwitchPreferenceController.java
@@ -0,0 +1,56 @@
+/*
+ * 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.Switch;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.core.TogglePreferenceController;
+import com.android.settingslib.widget.MainSwitchPreference;
+import com.android.settingslib.widget.OnMainSwitchChangeListener;
+
+/**
+ * Preference controller for MainSwitchPreference.
+ */
+public abstract class SettingsMainSwitchPreferenceController extends
+ TogglePreferenceController implements OnMainSwitchChangeListener {
+
+ 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 onSwitchChanged(Switch switchView, boolean isChecked) {
+ mSwitchPreference.setChecked(isChecked);
+ setChecked(isChecked);
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java
index d58d7e8d5f3..6c8a8f60cbd 100644
--- a/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java
@@ -16,43 +16,37 @@
package com.android.settings.dream;
-import static com.google.common.truth.Truth.assertThat;
-
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
-import android.view.View.OnClickListener;
-import android.widget.Button;
import androidx.preference.PreferenceScreen;
-import com.android.settings.R;
import com.android.settingslib.dream.DreamBackend;
-import com.android.settingslib.widget.LayoutPreference;
+import com.android.settingslib.widget.MainSwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Answers;
-import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class StartNowPreferenceControllerTest {
private StartNowPreferenceController mController;
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
+
@Mock
private PreferenceScreen mScreen;
@Mock
- private LayoutPreference mLayoutPref;
- @Mock
- private Button mButton;
+ private MainSwitchPreference mPref;
@Mock
private DreamBackend mBackend;
@@ -60,36 +54,38 @@ public class StartNowPreferenceControllerTest {
public void setup() {
MockitoAnnotations.initMocks(this);
- mController = new StartNowPreferenceController(mContext);
- when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPref);
- when(mLayoutPref.findViewById(R.id.dream_start_now_button)).thenReturn(mButton);
+ mContext = spy(RuntimeEnvironment.application);
+ mController = new StartNowPreferenceController(mContext, "key");
+ mPref = mock(MainSwitchPreference.class);
+ when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPref);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
- public void setsOnClickListenerForStartNow() {
- ArgumentCaptor captor =
- ArgumentCaptor.forClass(Button.OnClickListener.class);
-
+ public void displayPreference_shouldAddOnSwitchChangeListener() {
mController.displayPreference(mScreen);
- verify(mButton).setOnClickListener(captor.capture());
- assertThat(captor.getValue()).isNotNull();
+
+ verify(mPref).addOnSwitchChangeListener(mController);
}
@Test
- public void buttonIsDisabledWhenNeverDreaming() {
+ public void updateState_neverDreaming_preferenceShouldDidabled() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER);
+ mController.displayPreference(mScreen);
- mController.updateState(mLayoutPref);
- verify(mButton).setEnabled(false);
+ mController.updateState(mPref);
+
+ verify(mPref).setEnabled(false);
}
@Test
- public void buttonIsEnabledWhenDreamIsAvailable() {
+ public void updateState_dreamIsAvailable_preferenceShouldEnabled() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER);
+ mController.displayPreference(mScreen);
- mController.updateState(mLayoutPref);
- verify(mButton).setEnabled(true);
+ mController.updateState(mPref);
+
+ verify(mPref).setEnabled(true);
}
}
diff --git a/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java
index 4f38c461e6f..8d643ad4613 100644
--- a/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/BubbleNotificationPreferenceControllerTest.java
@@ -26,17 +26,15 @@ import static com.android.settings.notification.BadgingNotificationPreferenceCon
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.ActivityManager;
import android.content.Context;
import android.provider.Settings;
-import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
-import androidx.preference.TwoStatePreference;
+
+import com.android.settingslib.widget.MainSwitchPreference;
import org.junit.Before;
import org.junit.Test;
@@ -57,7 +55,7 @@ public class BubbleNotificationPreferenceControllerTest {
private PreferenceScreen mScreen;
private BubbleNotificationPreferenceController mController;
- private Preference mPreference;
+ private MainSwitchPreference mPreference;
private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles";
@@ -67,15 +65,16 @@ public class BubbleNotificationPreferenceControllerTest {
mContext = RuntimeEnvironment.application;
mController = new BubbleNotificationPreferenceController(mContext,
KEY_NOTIFICATION_BUBBLES);
- mPreference = new Preference(RuntimeEnvironment.application);
+ mPreference = new MainSwitchPreference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
+ mController.displayPreference(mScreen);
}
@Test
public void isAvailable_lowRam_returnsUnsupported() {
final ShadowActivityManager activityManager =
- Shadow.extract(mContext.getSystemService(ActivityManager.class));
+ Shadow.extract(mContext.getSystemService(ActivityManager.class));
activityManager.setIsLowRamDevice(true);
assertEquals(UNSUPPORTED_ON_DEVICE, mController.getAvailabilityStatus());
}
@@ -83,45 +82,40 @@ public class BubbleNotificationPreferenceControllerTest {
@Test
public void isAvailable_notLowRam_returnsAvailable() {
final ShadowActivityManager activityManager =
- Shadow.extract(mContext.getSystemService(ActivityManager.class));
+ Shadow.extract(mContext.getSystemService(ActivityManager.class));
activityManager.setIsLowRamDevice(false);
assertEquals(AVAILABLE, mController.getAvailabilityStatus());
}
- @Test
- public void updateState_settingIsOn_preferenceSetChecked() {
- final TwoStatePreference preference = mock(TwoStatePreference.class);
- Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
-
- mController.updateState(preference);
-
- verify(preference).setChecked(true);
- }
-
@Test
public void updateState_settingIsOff_preferenceSetUnchecked() {
- final TwoStatePreference preference = mock(TwoStatePreference.class);
- Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
- assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
+ Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
+ assertThat(Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
- mController.updateState(preference);
+ mPreference.updateStatus(false);
- verify(preference).setChecked(false);
+ assertThat(mPreference.isChecked()).isFalse();
}
@Test
- public void isChecked_settingIsOff_shouldReturnFalse() {
- Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
+ public void onSwitchChanged_true_settingIsOff_flagShouldOn() {
+ Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
- assertThat(mController.isChecked()).isFalse();
+ mController.onSwitchChanged(null, true);
+
+ assertThat(Settings.Global.getInt(mContext.getContentResolver(),
+ NOTIFICATION_BUBBLES, OFF)).isEqualTo(ON);
}
@Test
- public void isChecked_settingIsOn_shouldReturnTrue() {
- Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
+ public void onSwitchChanged_false_settingIsOn_flagShouldOff() {
+ Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
- assertThat(mController.isChecked()).isTrue();
+ mController.onSwitchChanged(null, false);
+
+ assertThat(Settings.Global.getInt(mContext.getContentResolver(),
+ NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
}
@Test
@@ -129,8 +123,8 @@ public class BubbleNotificationPreferenceControllerTest {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
mController.setChecked(false);
- int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
- NOTIFICATION_BUBBLES, -1);
+ int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
+ NOTIFICATION_BUBBLES, ON);
assertThat(updatedValue).isEqualTo(OFF);
}
@@ -140,8 +134,8 @@ public class BubbleNotificationPreferenceControllerTest {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
mController.setChecked(true);
- int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
- NOTIFICATION_BUBBLES, -1);
+ int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
+ NOTIFICATION_BUBBLES, OFF);
assertThat(updatedValue).isEqualTo(ON);
}