Restore the style of the screen saver widget from switch style to
button style.

The operation of the Switch is not suitable for previewing the
screen saver and will make the user confused. So we change the
style of the screen saver widget back to the button style.

Fix: 189505023
Test: manually test the screen saver
Change-Id: I4a97d91cc0b76111d4cb7b6aa202f2207ee0ca5d
diff --git a/res/layout/dream_start_button.xml b/res/layout/dream_start_button.xml
new file mode 100644
index 0000000..17ba78d
--- /dev/null
+++ b/res/layout/dream_start_button.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  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.
+  -->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:gravity="bottom"
+    android:paddingStart="72dp"
+    android:paddingEnd="72dp"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <Button
+        android:id="@+id/dream_start_now_button"
+        style="@style/ActionPrimaryButton"
+        android:layout_width="0dp"
+        android:layout_weight="1"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:text="@string/screensaver_settings_dream_start"/>
+
+</LinearLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 32ab532..300b3a4 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3210,6 +3210,8 @@
     <string name="screensaver_settings_when_to_dream">When to start</string>
     <!-- Dream settings screen, action label, current selected screen saver -->
     <string name="screensaver_settings_current">Current screen saver</string>
+    <!-- Dream settings screen, button label to start dreaming [CHAR LIMIT=30] -->
+    <string name="screensaver_settings_dream_start">Start now</string>
     <!-- Dream settings screen, button label for settings for a specific screensaver -->
     <string name="screensaver_settings_button">Settings</string>
     <!-- Sound & display settings screen, setting option name to change whether the screen adjusts automatically based on lighting conditions -->
@@ -13531,8 +13533,6 @@
     <string name="adaptive_brightness_main_switch_title">Use adaptive brightness</string>
     <!-- Title for wifi calling main switch preferences. [CHAR LIMIT=50] -->
     <string name="wifi_calling_main_switch_title">Use Wi‑Fi calling</string>
-    <!-- Title for Screen saver main switch preferences. [CHAR LIMIT=50] -->
-    <string name="screen_saver_main_switch_title">Use screen saver</string>
 
     <!-- Default preference title for showing all apps on device [CHAR_LIMIT=50]-->
     <string name="default_see_all_apps_title">See all apps</string>
diff --git a/res/xml/dream_fragment_overview.xml b/res/xml/dream_fragment_overview.xml
index 6e18a97..42b0334 100644
--- a/res/xml/dream_fragment_overview.xml
+++ b/res/xml/dream_fragment_overview.xml
@@ -19,11 +19,6 @@
     xmlns:settings="http://schemas.android.com/apk/res-auto"
     android:title="@string/screensaver_settings_title">
 
-    <com.android.settingslib.widget.MainSwitchPreference
-        android:key="dream_start_now_button_container"
-        android:title="@string/screen_saver_main_switch_title"
-        settings:controller="com.android.settings.dream.StartNowPreferenceController"/>
-
     <com.android.settings.widget.GearPreference
         android:key="current_screensaver"
         android:title="@string/screensaver_settings_current"
@@ -35,4 +30,12 @@
         android:title="@string/screensaver_settings_when_to_dream"
         android:fragment="com.android.settings.dream.WhenToDreamPicker" />
 
+    <PreferenceCategory>
+        <com.android.settingslib.widget.LayoutPreference
+            android:key="dream_start_now_button_container"
+            android:layout="@layout/dream_start_button"
+            android:selectable="false"
+            settings:controller="com.android.settings.dream.StartNowPreferenceController" />
+    </PreferenceCategory>
+
 </PreferenceScreen>
diff --git a/src/com/android/settings/dream/StartNowPreferenceController.java b/src/com/android/settings/dream/StartNowPreferenceController.java
index add1b58f..f270496 100644
--- a/src/com/android/settings/dream/StartNowPreferenceController.java
+++ b/src/com/android/settings/dream/StartNowPreferenceController.java
@@ -17,20 +17,23 @@
 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.BasePreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.overlay.FeatureFactory;
-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;
 
 /**
  * Controller that used to enable screen saver
  */
-public class StartNowPreferenceController extends SettingsMainSwitchPreferenceController {
+public class StartNowPreferenceController extends BasePreferenceController {
 
     private final DreamBackend mBackend;
     private final MetricsFeatureProvider mMetricsFeatureProvider;
@@ -47,30 +50,23 @@
     }
 
     @Override
-    public void updateState(Preference preference) {
-        mSwitchPreference.setChecked(false);
-        mSwitchPreference.setEnabled(mBackend.getWhenToDreamSetting() != DreamBackend.NEVER);
-    }
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
 
-    @Override
-    public boolean isChecked() {
-        return false;
-    }
-
-    @Override
-    public boolean setChecked(boolean isChecked) {
-        if (isChecked) {
-            if (mSwitchPreference != null) {
-                mMetricsFeatureProvider.logClickedPreference(mSwitchPreference,
-                        mSwitchPreference.getExtras().getInt(DashboardFragment.CATEGORY));
-            }
+        final LayoutPreference pref = screen.findPreference(getPreferenceKey());
+        final Button startButton = pref.findViewById(R.id.dream_start_now_button);
+        startButton.setOnClickListener(v -> {
+            mMetricsFeatureProvider.logClickedPreference(pref,
+                    pref.getExtras().getInt(DashboardFragment.CATEGORY));
             mBackend.startDreaming();
-        }
-        return true;
+        });
     }
 
     @Override
-    public int getSliceHighlightMenuRes() {
-        return R.string.menu_key_display;
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        final Button startButton = ((LayoutPreference) preference)
+                .findViewById(R.id.dream_start_now_button);
+        startButton.setEnabled(mBackend.getWhenToDreamSetting() != DreamBackend.NEVER);
     }
 }
diff --git a/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java
index 6c8a8f6..98ba1ce 100644
--- a/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/dream/StartNowPreferenceControllerTest.java
@@ -16,17 +16,18 @@
 
 package com.android.settings.dream;
 
-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.widget.Button;
 
 import androidx.preference.PreferenceScreen;
 
+import com.android.settings.R;
 import com.android.settingslib.dream.DreamBackend;
-import com.android.settingslib.widget.MainSwitchPreference;
+import com.android.settingslib.widget.LayoutPreference;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -46,7 +47,9 @@
     @Mock
     private PreferenceScreen mScreen;
     @Mock
-    private MainSwitchPreference mPref;
+    private LayoutPreference mLayoutPref;
+    @Mock
+    private Button mButton;
     @Mock
     private DreamBackend mBackend;
 
@@ -56,36 +59,29 @@
 
         mContext = spy(RuntimeEnvironment.application);
         mController = new StartNowPreferenceController(mContext, "key");
-        mPref = mock(MainSwitchPreference.class);
-        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPref);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPref);
+        when(mLayoutPref.findViewById(R.id.dream_start_now_button)).thenReturn(mButton);
 
         ReflectionHelpers.setField(mController, "mBackend", mBackend);
     }
 
     @Test
-    public void displayPreference_shouldAddOnSwitchChangeListener() {
-        mController.displayPreference(mScreen);
-
-        verify(mPref).addOnSwitchChangeListener(mController);
-    }
-
-    @Test
-    public void updateState_neverDreaming_preferenceShouldDidabled() {
+    public void updateState_neverDreaming_buttonShouldDidabled() {
         when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER);
         mController.displayPreference(mScreen);
 
-        mController.updateState(mPref);
+        mController.updateState(mLayoutPref);
 
-        verify(mPref).setEnabled(false);
+        verify(mButton).setEnabled(false);
     }
 
     @Test
-    public void updateState_dreamIsAvailable_preferenceShouldEnabled() {
+    public void updateState_dreamIsAvailable_buttonShouldEnabled() {
         when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER);
         mController.displayPreference(mScreen);
 
-        mController.updateState(mPref);
+        mController.updateState(mLayoutPref);
 
-        verify(mPref).setEnabled(true);
+        verify(mButton).setEnabled(true);
     }
 }