Merge "System gets its own dnd toggle" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-03-07 16:39:40 +00:00
committed by Android (Google) Code Review
9 changed files with 253 additions and 30 deletions

View File

@@ -7549,10 +7549,10 @@
<string name="zen_mode_alarms">Alarms</string>
<!-- [CHAR LIMIT=50] Zen mode settings: Media option -->
<string name="zen_mode_media_system_other">Media</string>
<string name="zen_mode_media">Media</string>
<!-- [CHAR LIMIT=120] Zen mode settings: Media secondary text explaining sounds include system feedback such as system tapping sounds, haptic feedback, etc. -->
<string name="zen_mode_media_system_other_secondary_text">Includes system feedback like touch and charging sounds</string>
<!-- [CHAR LIMIT=50] Zen mode settings: System option which includes sounds such as touch and charging sounds -->
<string name="zen_mode_system">Touch and charging sounds</string>
<!-- [CHAR LIMIT=50] Zen mode settings: Reminders option -->
<string name="zen_mode_reminders">Reminders</string>

View File

@@ -20,7 +20,7 @@
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="zen_mode_behavior_settings_page"
android:title="@string/zen_mode_behavior_settings_title"
settings:initialExpandedChildrenCount="7">
settings:initialExpandedChildrenCount="8">
<PreferenceCategory
android:title="@string/zen_mode_behavior_allow_title"
@@ -34,8 +34,12 @@
<!-- Media -->
<SwitchPreference
android:key="zen_mode_media"
android:title="@string/zen_mode_media_system_other"
android:summary="@string/zen_mode_media_system_other_secondary_text"/>
android:title="@string/zen_mode_media"/>
<!-- System -->
<SwitchPreference
android:key="zen_mode_system"
android:title="@string/zen_mode_system"/>
<!-- Reminders -->
<SwitchPreference

View File

@@ -40,7 +40,8 @@ public class ZenModeBehaviorSettings extends ZenModeSettingsBase implements Inde
Lifecycle lifecycle) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModeAlarmsPreferenceController(context, lifecycle));
controllers.add(new ZenModeMediaSystemOtherPreferenceController(context, lifecycle));
controllers.add(new ZenModeMediaPreferenceController(context, lifecycle));
controllers.add(new ZenModeSystemPreferenceController(context, lifecycle));
controllers.add(new ZenModeEventsPreferenceController(context, lifecycle));
controllers.add(new ZenModeRemindersPreferenceController(context, lifecycle));
controllers.add(new ZenModeMessagesPreferenceController(context, lifecycle));
@@ -85,7 +86,7 @@ public class ZenModeBehaviorSettings extends ZenModeSettingsBase implements Inde
public List<String> getNonIndexableKeys(Context context) {
final List<String> keys = super.getNonIndexableKeys(context);
keys.add(ZenModeAlarmsPreferenceController.KEY);
keys.add(ZenModeMediaSystemOtherPreferenceController.KEY);
keys.add(ZenModeMediaPreferenceController.KEY);
keys.add(ZenModeEventsPreferenceController.KEY);
keys.add(ZenModeRemindersPreferenceController.KEY);
keys.add(ZenModeMessagesPreferenceController.KEY);

View File

@@ -25,13 +25,13 @@ import android.util.Log;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeMediaSystemOtherPreferenceController extends AbstractZenModePreferenceController
public class ZenModeMediaPreferenceController extends AbstractZenModePreferenceController
implements Preference.OnPreferenceChangeListener {
protected static final String KEY = "zen_mode_media";
private final ZenModeBackend mBackend;
public ZenModeMediaSystemOtherPreferenceController(Context context, Lifecycle lifecycle) {
public ZenModeMediaPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle);
mBackend = ZenModeBackend.getInstance(context);
}
@@ -63,7 +63,7 @@ public class ZenModeMediaSystemOtherPreferenceController extends AbstractZenMode
default:
pref.setEnabled(true);
pref.setChecked(mBackend.isPriorityCategoryEnabled(
Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER));
Policy.PRIORITY_CATEGORY_MEDIA));
}
}
@@ -71,9 +71,9 @@ public class ZenModeMediaSystemOtherPreferenceController extends AbstractZenMode
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean allowMedia = (Boolean) newValue;
if (ZenModeSettingsBase.DEBUG) {
Log.d(TAG, "onPrefChange allowMediaSystemOther=" + allowMedia);
Log.d(TAG, "onPrefChange allowMedia=" + allowMedia);
}
mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER, allowMedia);
mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_MEDIA, allowMedia);
return true;
}
}

View File

@@ -77,9 +77,11 @@ public class ZenModeSettings extends ZenModeSettingsBase {
mContext = context;
}
// these should match NotificationManager.Policy#ALL_PRIORITY_CATEGORIES
private static final int[] ALL_PRIORITY_CATEGORIES = {
Policy.PRIORITY_CATEGORY_ALARMS,
Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER,
Policy.PRIORITY_CATEGORY_MEDIA,
Policy.PRIORITY_CATEGORY_SYSTEM,
Policy.PRIORITY_CATEGORY_REMINDERS,
Policy.PRIORITY_CATEGORY_EVENTS,
Policy.PRIORITY_CATEGORY_MESSAGES,
@@ -104,10 +106,10 @@ public class ZenModeSettings extends ZenModeSettingsBase {
return mContext.getString(R.string.zen_mode_behavior_total_silence);
}
// only alarms and media/system can bypass dnd
// only alarms and media can bypass dnd
if (numCategories == 2 &&
isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_ALARMS) &&
isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER)) {
isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_MEDIA)) {
return mContext.getString(R.string.zen_mode_behavior_alarms_only);
}
@@ -164,9 +166,12 @@ public class ZenModeSettings extends ZenModeSettingsBase {
if (isCategoryEnabled(policy, category)) {
if (category == Policy.PRIORITY_CATEGORY_ALARMS) {
enabledCategories.add(mContext.getString(R.string.zen_mode_alarms));
} else if (category == Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER) {
} else if (category == Policy.PRIORITY_CATEGORY_MEDIA) {
enabledCategories.add(mContext.getString(
R.string.zen_mode_media_system_other));
R.string.zen_mode_media));
} else if (category == Policy.PRIORITY_CATEGORY_SYSTEM) {
enabledCategories.add(mContext.getString(
R.string.zen_mode_system));
} else if (category == Policy.PRIORITY_CATEGORY_REMINDERS) {
enabledCategories.add(mContext.getString(R.string.zen_mode_reminders));
} else if (category == Policy.PRIORITY_CATEGORY_EVENTS) {

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2018 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.notification;
import android.app.NotificationManager.Policy;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.util.Log;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeSystemPreferenceController extends
AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener {
protected static final String KEY = "zen_mode_system";
public ZenModeSystemPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle);
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
SwitchPreference pref = (SwitchPreference) preference;
switch (getZenMode()) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
pref.setEnabled(false);
pref.setChecked(false);
break;
case Settings.Global.ZEN_MODE_ALARMS:
pref.setEnabled(false);
pref.setChecked(false);
break;
default:
pref.setEnabled(true);
pref.setChecked(mBackend.isPriorityCategoryEnabled(
Policy.PRIORITY_CATEGORY_SYSTEM));
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean allowSystem = (Boolean) newValue;
if (ZenModeSettingsBase.DEBUG) {
Log.d(TAG, "onPrefChange allowSystem=" + allowSystem);
}
mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ZEN_ALLOW_SYSTEM,
allowSystem);
mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_SYSTEM, allowSystem);
return true;
}
}

View File

@@ -46,10 +46,7 @@ import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
public class ZenModeMediaPreferenceControllerTest {
private static final boolean MEDIA_SETTINGS = true;
private ZenModeMediaSystemOtherPreferenceController mController;
private ZenModeMediaPreferenceController mController;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@@ -75,7 +72,7 @@ public class ZenModeMediaPreferenceControllerTest {
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeMediaSystemOtherPreferenceController(mContext,
mController = new ZenModeMediaPreferenceController(mContext,
mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
@@ -111,13 +108,13 @@ public class ZenModeMediaPreferenceControllerTest {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER)).
thenReturn(MEDIA_SETTINGS);
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA)).
thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(MEDIA_SETTINGS);
verify(mockPref).setChecked(true);
}
@Test
@@ -126,7 +123,7 @@ public class ZenModeMediaPreferenceControllerTest {
mController.onPreferenceChange(mockPref, allow);
verify(mBackend).saveSoundPolicy(
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER, allow);
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
}
@Test
@@ -135,6 +132,6 @@ public class ZenModeMediaPreferenceControllerTest {
mController.onPreferenceChange(mockPref, allow);
verify(mBackend).saveSoundPolicy(
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER, allow);
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
}
}

View File

@@ -52,7 +52,7 @@ public class ZenModeSettingsTest {
NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS
| NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS
| NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS
| NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER,
| NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA,
0, 0);
final String result = mBuilder.getBehaviorSettingSummary(policy,
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
@@ -75,7 +75,7 @@ public class ZenModeSettingsTest {
public void testGetBehaviorSettingSummary_alarmsAndMedia() {
NotificationManager.Policy policy = new NotificationManager.Policy(
NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS
| NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER,
| NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA,
0, 0);
final String result = mBuilder.getBehaviorSettingSummary(policy,
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2018 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.notification;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static junit.framework.Assert.assertEquals;
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.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
public class ZenModeSystemPreferenceControllerTest {
private ZenModeSystemPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private Context mContext;
private ContentResolver mContentResolver;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = shadowApplication.getApplicationContext();
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeSystemPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM)).thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(true);
}
@Test
public void onPreferenceChanged_EnableSystem() {
mController.onPreferenceChange(mockPref, true);
verify(mBackend).saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM,
true);
}
@Test
public void onPreferenceChanged_DisableSystem() {
mController.onPreferenceChange(mockPref, false);
verify(mBackend).saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM,
false);
}
}