diff --git a/res/values/strings.xml b/res/values/strings.xml index e5c7cec78d0..45d6e9cf6ac 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7549,10 +7549,10 @@ Alarms - Media + Media - - Includes system feedback like touch and charging sounds + + Touch and charging sounds Reminders diff --git a/res/xml/zen_mode_behavior_settings.xml b/res/xml/zen_mode_behavior_settings.xml index 8d2c28c7d7d..084b78d5c51 100644 --- a/res/xml/zen_mode_behavior_settings.xml +++ b/res/xml/zen_mode_behavior_settings.xml @@ -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"> + android:title="@string/zen_mode_media"/> + + + 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 getNonIndexableKeys(Context context) { final List 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); diff --git a/src/com/android/settings/notification/ZenModeMediaSystemOtherPreferenceController.java b/src/com/android/settings/notification/ZenModeMediaPreferenceController.java similarity index 84% rename from src/com/android/settings/notification/ZenModeMediaSystemOtherPreferenceController.java rename to src/com/android/settings/notification/ZenModeMediaPreferenceController.java index 8afe881e434..12cedf26d24 100644 --- a/src/com/android/settings/notification/ZenModeMediaSystemOtherPreferenceController.java +++ b/src/com/android/settings/notification/ZenModeMediaPreferenceController.java @@ -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; } } diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java index 272bb416588..4e8793befc8 100644 --- a/src/com/android/settings/notification/ZenModeSettings.java +++ b/src/com/android/settings/notification/ZenModeSettings.java @@ -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) { diff --git a/src/com/android/settings/notification/ZenModeSystemPreferenceController.java b/src/com/android/settings/notification/ZenModeSystemPreferenceController.java new file mode 100644 index 00000000000..364c829bb35 --- /dev/null +++ b/src/com/android/settings/notification/ZenModeSystemPreferenceController.java @@ -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; + } +} diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeMediaPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeMediaPreferenceControllerTest.java index db0a2e900bc..db828a296b7 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeMediaPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeMediaPreferenceControllerTest.java @@ -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); } } \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java index 66b11a69cfa..5c81efc9468 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java @@ -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); diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeSystemPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeSystemPreferenceControllerTest.java new file mode 100644 index 00000000000..46dfc6986c3 --- /dev/null +++ b/tests/robotests/src/com/android/settings/notification/ZenModeSystemPreferenceControllerTest.java @@ -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); + } +} \ No newline at end of file