Revert DND toggle back to a button

Test: manual, Robotests
Fixes: 188616179
Change-Id: I70b469ff6ef809056c9eceec7f87f91aed9a23ca
This commit is contained in:
Julia Reynolds
2021-06-01 10:18:03 -04:00
parent 55b577dff8
commit eb838f2eef
4 changed files with 143 additions and 69 deletions

View File

@@ -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>

View File

@@ -21,8 +21,12 @@
android:title="@string/zen_mode_settings_title"> android:title="@string/zen_mode_settings_title">
<!-- Turn on DND button --> <!-- Turn on DND button -->
<com.android.settingslib.widget.MainSwitchPreference <com.android.settingslib.widget.LayoutPreference
android:key="zen_mode_toggle" 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"/> settings:keywords="@string/keywords_zen_mode_settings"/>
<PreferenceCategory <PreferenceCategory

View File

@@ -16,34 +16,37 @@
package com.android.settings.notification.zen; package com.android.settings.notification.zen;
import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_FOCUSED;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Switch; import android.view.View;
import android.widget.Button;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.notification.SettingsEnableZenModeDialog; import com.android.settings.notification.SettingsEnableZenModeDialog;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.MainSwitchPreference; import com.android.settingslib.widget.LayoutPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController
implements PreferenceControllerMixin, OnMainSwitchChangeListener { implements PreferenceControllerMixin {
private static final String TAG = "EnableZenModeButton";
public static final String KEY = "zen_mode_toggle"; public static final String KEY = "zen_mode_toggle";
private static final String TAG = "EnableZenModeButton";
private final FragmentManager mFragment; private final FragmentManager mFragment;
// DND can also be toggled from QS. // DND can also be toggled from QS. If DND wasn't toggled by this preference, don't
private MainSwitchPreference mPreference; // reroute focus.
private boolean mRefocusButton = false;
private Button mZenButtonOn;
private Button mZenButtonOff;
public ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager public ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager
fragment) { fragment) {
@@ -61,27 +64,26 @@ public class ZenModeButtonPreferenceController extends AbstractZenModePreference
return KEY; return KEY;
} }
@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 @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
super.updateState(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); updatePreference(preference);
} }
@@ -90,29 +92,41 @@ public class ZenModeButtonPreferenceController extends AbstractZenModePreference
case Settings.Global.ZEN_MODE_ALARMS: case Settings.Global.ZEN_MODE_ALARMS:
case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
mPreference.updateStatus(true); mZenButtonOff.setVisibility(View.VISIBLE);
mPreference.setTitle(R.string.do_not_disturb_main_switch_title_on); mZenButtonOn.setVisibility(View.GONE);
if (mRefocusButton) {
mRefocusButton = false;
mZenButtonOff.sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
}
break; break;
case Settings.Global.ZEN_MODE_OFF: case Settings.Global.ZEN_MODE_OFF:
default: default:
mPreference.setTitle(R.string.do_not_disturb_main_switch_title_off); mZenButtonOff.setVisibility(View.GONE);
mPreference.updateStatus(false); updateZenButtonOnClickListener(preference);
mZenButtonOn.setVisibility(View.VISIBLE);
if (mRefocusButton) {
mRefocusButton = false;
mZenButtonOn.sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
}
} }
} }
private void updateZenModeState(Preference preference) { private void updateZenButtonOnClickListener(Preference preference) {
writeMetrics(preference, true); mZenButtonOn.setOnClickListener(v -> {
int zenDuration = getZenDuration(); mRefocusButton = true;
switch (zenDuration) { writeMetrics(preference, true);
case Settings.Secure.ZEN_DURATION_PROMPT: int zenDuration = getZenDuration();
new SettingsEnableZenModeDialog().show(mFragment, TAG); switch (zenDuration) {
break; case Settings.Secure.ZEN_DURATION_PROMPT:
case Settings.Secure.ZEN_DURATION_FOREVER: new SettingsEnableZenModeDialog().show(mFragment, TAG);
mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); break;
break; case Settings.Secure.ZEN_DURATION_FOREVER:
default: mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
mBackend.setZenModeForDuration(zenDuration); break;
} default:
mBackend.setZenModeForDuration(zenDuration);
}
});
} }
private void writeMetrics(Preference preference, boolean buttonOn) { private void writeMetrics(Preference preference, boolean buttonOn) {

View File

@@ -22,19 +22,23 @@ import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_OFF; 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.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; 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.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.MainSwitchPreference; import com.android.settingslib.widget.MainSwitchPreference;
@@ -58,10 +62,14 @@ public class ZenModeButtonPreferenceControllerTest {
@Mock @Mock
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
@Mock @Mock
private MainSwitchPreference mMockPref; private Preference mMockPref;
@Mock @Mock
private NotificationManager.Policy mPolicy; private NotificationManager.Policy mPolicy;
@Mock @Mock
private Button mZenButtonOn;
@Mock
private Button mZenButtonOff;
@Mock
private PreferenceScreen mPreferenceScreen; private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private Context mContext; private Context mContext;
@@ -78,6 +86,8 @@ public class ZenModeButtonPreferenceControllerTest {
mock(FragmentManager.class)); mock(FragmentManager.class));
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
ReflectionHelpers.setField(mController, "mBackend", mBackend); ReflectionHelpers.setField(mController, "mBackend", mBackend);
ReflectionHelpers.setField(mController, "mZenButtonOn", mZenButtonOn);
ReflectionHelpers.setField(mController, "mZenButtonOff", mZenButtonOff);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mMockPref); mMockPref);
@@ -87,56 +97,52 @@ public class ZenModeButtonPreferenceControllerTest {
@Test @Test
public void updateState_TotalSilence() { public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final MainSwitchPreference pref = new MainSwitchPreference(mContext); mController.updateState(mMockPref);
mController.updateState(pref); verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
assertThat(pref.isChecked()).isFalse();
} }
@Test @Test
public void updateState_AlarmsOnly() { public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final MainSwitchPreference pref = new MainSwitchPreference(mContext); mController.updateState(mMockPref);
mController.updateState(pref); verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
assertThat(pref.isChecked()).isFalse();
} }
@Test @Test
public void updateState_Priority() { public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
final MainSwitchPreference pref = new MainSwitchPreference(mContext); mController.updateState(mMockPref);
mController.updateState(pref); verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
assertThat(pref.isChecked()).isFalse();
} }
@Test @Test
public void updateState_ZenOff() { public void updateState_ZenOff() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
final MainSwitchPreference pref = new MainSwitchPreference(mContext); mController.updateState(mMockPref);
mController.updateState(pref); verify(mZenButtonOn).setVisibility(View.VISIBLE);
verify(mZenButtonOff).setVisibility(View.GONE);
assertThat(pref.isChecked()).isFalse();
} }
@Test @Test
public void updateState_otherUserChangedZen() { public void updateState_otherUserChangedZen() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
final MainSwitchPreference pref = new MainSwitchPreference(mContext); mController.updateState(mMockPref);
mController.updateState(pref); verify(mZenButtonOn).setVisibility(View.VISIBLE);
verify(mZenButtonOff).setVisibility(View.GONE);
assertThat(pref.isChecked()).isFalse();
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
final int GUEST_USER_ID = 10; final int GUEST_USER_ID = 10;
mController.mSettingObserver.onChange(false, mController.mSettingObserver.onChange(false,
Settings.Global.getUriFor(Settings.Global.ZEN_MODE), GUEST_USER_ID); Settings.Global.getUriFor(Settings.Global.ZEN_MODE), GUEST_USER_ID);
assertThat(pref.isChecked()).isFalse(); verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
} }
} }