Revert DND toggle back to a button

Test: manual, Robotests
Fixes: 188616179
Change-Id: I70b469ff6ef809056c9eceec7f87f91aed9a23ca
diff --git a/res/layout/zen_mode_settings_button.xml b/res/layout/zen_mode_settings_button.xml
new file mode 100644
index 0000000..005e0fa
--- /dev/null
+++ b/res/layout/zen_mode_settings_button.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2017 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:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:orientation="vertical">
+
+    <TextView
+        android:text="@string/zen_mode_settings_summary"
+        android:textAppearance="?android:attr/textAppearanceSmall"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/zen_mode_settings_button_margin_vertical"/>
+
+    <Button
+        android:id="@+id/zen_mode_settings_turn_on_button"
+        style="@style/ActionPrimaryButton"
+        android:layout_marginTop="@dimen/zen_mode_settings_button_margin_vertical"
+        android:layout_marginBottom="@dimen/zen_mode_settings_button_margin_vertical"
+        android:text="@string/zen_mode_button_turn_on"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+
+    <Button
+        android:id="@+id/zen_mode_settings_turn_off_button"
+        style="@style/ActionPrimaryButton"
+        android:layout_marginTop="@dimen/zen_mode_settings_button_margin_vertical"
+        android:layout_marginBottom="@dimen/zen_mode_settings_button_margin_vertical"
+        android:text="@string/zen_mode_button_turn_off"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/res/xml/zen_mode_settings.xml b/res/xml/zen_mode_settings.xml
index 886790a..f5589de 100644
--- a/res/xml/zen_mode_settings.xml
+++ b/res/xml/zen_mode_settings.xml
@@ -21,8 +21,12 @@
     android:title="@string/zen_mode_settings_title">
 
     <!-- Turn on DND button -->
-    <com.android.settingslib.widget.MainSwitchPreference
+    <com.android.settingslib.widget.LayoutPreference
         android:key="zen_mode_toggle"
+        android:title="@string/zen_mode_settings_title"
+        android:selectable="false"
+        android:layout="@layout/zen_mode_settings_button"
+        settings:allowDividerBelow="true"
         settings:keywords="@string/keywords_zen_mode_settings"/>
 
     <PreferenceCategory
diff --git a/src/com/android/settings/notification/zen/ZenModeButtonPreferenceController.java b/src/com/android/settings/notification/zen/ZenModeButtonPreferenceController.java
index 8bfcd13..dc338bd 100644
--- a/src/com/android/settings/notification/zen/ZenModeButtonPreferenceController.java
+++ b/src/com/android/settings/notification/zen/ZenModeButtonPreferenceController.java
@@ -16,34 +16,37 @@
 
 package com.android.settings.notification.zen;
 
+import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_FOCUSED;
+
 import android.app.settings.SettingsEnums;
 import android.content.Context;
 import android.provider.Settings;
-import android.widget.Switch;
+import android.view.View;
+import android.widget.Button;
 
 import androidx.fragment.app.FragmentManager;
 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.notification.SettingsEnableZenModeDialog;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-import com.android.settingslib.widget.MainSwitchPreference;
-import com.android.settingslib.widget.OnMainSwitchChangeListener;
+import com.android.settingslib.widget.LayoutPreference;
 
 public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController
-        implements PreferenceControllerMixin, OnMainSwitchChangeListener {
-
-    private static final String TAG = "EnableZenModeButton";
+        implements PreferenceControllerMixin {
 
     public static final String KEY = "zen_mode_toggle";
 
+    private static final String TAG = "EnableZenModeButton";
     private final FragmentManager mFragment;
 
-    // DND can also be toggled from QS.
-    private MainSwitchPreference mPreference;
+    // DND can also be toggled from QS. If DND wasn't toggled by this preference, don't
+    // reroute focus.
+    private boolean mRefocusButton = false;
+    private Button mZenButtonOn;
+    private Button mZenButtonOff;
 
     public ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager
             fragment) {
@@ -62,26 +65,25 @@
     }
 
     @Override
-    public void displayPreference(PreferenceScreen screen) {
-        super.displayPreference(screen);
-        mPreference = (MainSwitchPreference) screen.findPreference(getPreferenceKey());
-        mPreference.addOnSwitchChangeListener(this);
-    }
-
-    @Override
-    public void onSwitchChanged(Switch switchView, boolean isChecked) {
-        if (isChecked) {
-            updateZenModeState(mPreference);
-        } else {
-            writeMetrics(mPreference, false);
-            mBackend.setZenMode(Settings.Global.ZEN_MODE_OFF);
-        }
-    }
-
-    @Override
     public void updateState(Preference preference) {
         super.updateState(preference);
 
+        if (null == mZenButtonOn) {
+            mZenButtonOn = ((LayoutPreference) preference)
+                    .findViewById(R.id.zen_mode_settings_turn_on_button);
+            updateZenButtonOnClickListener(preference);
+        }
+
+        if (null == mZenButtonOff) {
+            mZenButtonOff = ((LayoutPreference) preference)
+                    .findViewById(R.id.zen_mode_settings_turn_off_button);
+            mZenButtonOff.setOnClickListener(v -> {
+                mRefocusButton = true;
+                writeMetrics(preference, false);
+                mBackend.setZenMode(Settings.Global.ZEN_MODE_OFF);
+            });
+        }
+
         updatePreference(preference);
     }
 
@@ -90,29 +92,41 @@
             case Settings.Global.ZEN_MODE_ALARMS:
             case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
-                mPreference.updateStatus(true);
-                mPreference.setTitle(R.string.do_not_disturb_main_switch_title_on);
+                mZenButtonOff.setVisibility(View.VISIBLE);
+                mZenButtonOn.setVisibility(View.GONE);
+                if (mRefocusButton) {
+                    mRefocusButton = false;
+                    mZenButtonOff.sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
+                }
                 break;
             case Settings.Global.ZEN_MODE_OFF:
             default:
-                mPreference.setTitle(R.string.do_not_disturb_main_switch_title_off);
-                mPreference.updateStatus(false);
+                mZenButtonOff.setVisibility(View.GONE);
+                updateZenButtonOnClickListener(preference);
+                mZenButtonOn.setVisibility(View.VISIBLE);
+                if (mRefocusButton) {
+                    mRefocusButton = false;
+                    mZenButtonOn.sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
+                }
         }
     }
 
-    private void updateZenModeState(Preference preference) {
-        writeMetrics(preference, true);
-        int zenDuration = getZenDuration();
-        switch (zenDuration) {
-            case Settings.Secure.ZEN_DURATION_PROMPT:
-                new SettingsEnableZenModeDialog().show(mFragment, TAG);
-                break;
-            case Settings.Secure.ZEN_DURATION_FOREVER:
-                mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
-                break;
-            default:
-                mBackend.setZenModeForDuration(zenDuration);
-        }
+    private void updateZenButtonOnClickListener(Preference preference) {
+        mZenButtonOn.setOnClickListener(v -> {
+            mRefocusButton = true;
+            writeMetrics(preference, true);
+            int zenDuration = getZenDuration();
+            switch (zenDuration) {
+                case Settings.Secure.ZEN_DURATION_PROMPT:
+                    new SettingsEnableZenModeDialog().show(mFragment, TAG);
+                    break;
+                case Settings.Secure.ZEN_DURATION_FOREVER:
+                    mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
+                    break;
+                default:
+                    mBackend.setZenModeForDuration(zenDuration);
+            }
+        });
     }
 
     private void writeMetrics(Preference preference, boolean buttonOn) {
@@ -121,4 +135,4 @@
         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ZEN_TOGGLE_DND_BUTTON,
                 buttonOn);
     }
-}
+}
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/notification/zen/ZenModeButtonPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/zen/ZenModeButtonPreferenceControllerTest.java
index 9daef1a..257a7a1 100644
--- a/tests/robotests/src/com/android/settings/notification/zen/ZenModeButtonPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/zen/ZenModeButtonPreferenceControllerTest.java
@@ -22,19 +22,23 @@
 import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
 import static android.provider.Settings.Global.ZEN_MODE_OFF;
 
-import static com.google.common.truth.Truth.assertThat;
-
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import android.app.NotificationManager;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.provider.Settings;
+import android.view.View;
+import android.widget.Button;
 
 import androidx.fragment.app.FragmentManager;
+import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
 
+import com.android.settings.notification.zen.ZenModeBackend;
+import com.android.settings.notification.zen.ZenModeButtonPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
 import com.android.settingslib.widget.MainSwitchPreference;
 
@@ -58,10 +62,14 @@
     @Mock
     private NotificationManager mNotificationManager;
     @Mock
-    private MainSwitchPreference mMockPref;
+    private Preference mMockPref;
     @Mock
     private NotificationManager.Policy mPolicy;
     @Mock
+    private Button mZenButtonOn;
+    @Mock
+    private Button mZenButtonOff;
+    @Mock
     private PreferenceScreen mPreferenceScreen;
     private ContentResolver mContentResolver;
     private Context mContext;
@@ -78,6 +86,8 @@
                 mock(FragmentManager.class));
         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
         ReflectionHelpers.setField(mController, "mBackend", mBackend);
+        ReflectionHelpers.setField(mController, "mZenButtonOn", mZenButtonOn);
+        ReflectionHelpers.setField(mController, "mZenButtonOff", mZenButtonOff);
 
         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                 mMockPref);
@@ -87,56 +97,52 @@
     @Test
     public void updateState_TotalSilence() {
         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
-        final MainSwitchPreference pref = new MainSwitchPreference(mContext);
+        mController.updateState(mMockPref);
 
-        mController.updateState(pref);
-
-        assertThat(pref.isChecked()).isFalse();
+        verify(mZenButtonOn).setVisibility(View.GONE);
+        verify(mZenButtonOff).setVisibility(View.VISIBLE);
     }
 
     @Test
     public void updateState_AlarmsOnly() {
         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
-        final MainSwitchPreference pref = new MainSwitchPreference(mContext);
+        mController.updateState(mMockPref);
 
-        mController.updateState(pref);
-
-        assertThat(pref.isChecked()).isFalse();
+        verify(mZenButtonOn).setVisibility(View.GONE);
+        verify(mZenButtonOff).setVisibility(View.VISIBLE);
     }
 
     @Test
     public void updateState_Priority() {
         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
-        final MainSwitchPreference pref = new MainSwitchPreference(mContext);
+        mController.updateState(mMockPref);
 
-        mController.updateState(pref);
-
-        assertThat(pref.isChecked()).isFalse();
+        verify(mZenButtonOn).setVisibility(View.GONE);
+        verify(mZenButtonOff).setVisibility(View.VISIBLE);
     }
 
     @Test
     public void updateState_ZenOff() {
         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
-        final MainSwitchPreference pref = new MainSwitchPreference(mContext);
+        mController.updateState(mMockPref);
 
-        mController.updateState(pref);
-
-        assertThat(pref.isChecked()).isFalse();
+        verify(mZenButtonOn).setVisibility(View.VISIBLE);
+        verify(mZenButtonOff).setVisibility(View.GONE);
     }
 
     @Test
     public void updateState_otherUserChangedZen() {
         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
-        final MainSwitchPreference pref = new MainSwitchPreference(mContext);
-        mController.updateState(pref);
-
-        assertThat(pref.isChecked()).isFalse();
+        mController.updateState(mMockPref);
+        verify(mZenButtonOn).setVisibility(View.VISIBLE);
+        verify(mZenButtonOff).setVisibility(View.GONE);
 
         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
         final int GUEST_USER_ID = 10;
         mController.mSettingObserver.onChange(false,
                 Settings.Global.getUriFor(Settings.Global.ZEN_MODE), GUEST_USER_ID);
 
-        assertThat(pref.isChecked()).isFalse();
+        verify(mZenButtonOn).setVisibility(View.GONE);
+        verify(mZenButtonOff).setVisibility(View.VISIBLE);
     }
 }