From d640a97ce823fe5b69cdad2385a666f82226ed51 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Fri, 2 Mar 2018 15:51:14 -0500 Subject: [PATCH] Show new visual effects options in dnd settings Bug: 74075050 Test: make -j20 RunSettingsRoboTests Change-Id: Ia629007d1c80b657aaf756d0f6d2edfd89a7f6b4 --- AndroidManifest.xml | 13 ++ res/values/strings.xml | 32 +++- res/xml/zen_mode_behavior_settings.xml | 94 +++++------ res/xml/zen_mode_block_settings.xml | 61 +++++++ res/xml/zen_mode_settings.xml | 8 +- src/com/android/settings/Settings.java | 1 + .../settings/notification/ZenModeBackend.java | 8 +- .../notification/ZenModeBehaviorSettings.java | 11 -- ...odeBlockedEffectsPreferenceController.java | 51 ++++++ .../ZenModeBlockedEffectsSettings.java | 123 ++++++++++++++ .../ZenModeScreenOffPreferenceController.java | 66 -------- .../ZenModeScreenOnPreferenceController.java | 65 -------- .../notification/ZenModeSettings.java | 13 ++ .../ZenModeVisEffectPreferenceController.java | 91 +++++++++++ .../search/SearchIndexableResourcesImpl.java | 2 + ...ockedEffectsPreferenceControllerTest.java} | 50 ++---- ...ModeScreenOffPreferenceControllerTest.java | 96 ----------- ...ModeVisEffectPreferenceControllerTest.java | 152 ++++++++++++++++++ 18 files changed, 601 insertions(+), 336 deletions(-) create mode 100644 res/xml/zen_mode_block_settings.xml create mode 100644 src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceController.java create mode 100644 src/com/android/settings/notification/ZenModeBlockedEffectsSettings.java delete mode 100644 src/com/android/settings/notification/ZenModeScreenOffPreferenceController.java delete mode 100644 src/com/android/settings/notification/ZenModeScreenOnPreferenceController.java create mode 100644 src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java rename tests/robotests/src/com/android/settings/notification/{ZenModeScreenOnPreferenceControllerTest.java => ZenModeBlockedEffectsPreferenceControllerTest.java} (57%) delete mode 100644 tests/robotests/src/com/android/settings/notification/ZenModeScreenOffPreferenceControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index feb1b05f32a..d0ed63884d4 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -738,6 +738,19 @@ android:value="true" /> + + + + + Turn on Do Not Disturb - - Behavior + + Exceptions Allow sounds and vibrations from @@ -6972,6 +6972,34 @@ Allow visual signals + + What to block + + When notifications arrive + + Mute sound and vibration + + Don\'t turn on screen + + Don\'t blink light + + Don\'t pop notifications on screen + + Hide status bar icons + + Hide notification dots + + Hide from ambient display + + Hide from notification list + + + Sound and vibration + + Sound, vibration, and some visual signs of notifications + + Sound, vibration, and visual signs of notifications + Add diff --git a/res/xml/zen_mode_behavior_settings.xml b/res/xml/zen_mode_behavior_settings.xml index 084b78d5c51..8426d9154c2 100644 --- a/res/xml/zen_mode_behavior_settings.xml +++ b/res/xml/zen_mode_behavior_settings.xml @@ -19,71 +19,51 @@ xmlns:android="http://schemas.android.com/apk/res/android" 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="8"> + android:title="@string/zen_mode_behavior_settings_title" > - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - + + diff --git a/res/xml/zen_mode_block_settings.xml b/res/xml/zen_mode_block_settings.xml new file mode 100644 index 00000000000..ce33e978811 --- /dev/null +++ b/res/xml/zen_mode_block_settings.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/xml/zen_mode_settings.xml b/res/xml/zen_mode_settings.xml index ca8ad10f227..1f863c20b13 100644 --- a/res/xml/zen_mode_settings.xml +++ b/res/xml/zen_mode_settings.xml @@ -19,7 +19,13 @@ android:key="zen_mode_settings" android:title="@string/zen_mode_settings_title"> - + + + + getNonIndexableKeys(Context context) { final List keys = super.getNonIndexableKeys(context); - keys.add(ZenModeAlarmsPreferenceController.KEY); - keys.add(ZenModeMediaPreferenceController.KEY); - keys.add(ZenModeEventsPreferenceController.KEY); - keys.add(ZenModeRemindersPreferenceController.KEY); - keys.add(ZenModeMessagesPreferenceController.KEY); - keys.add(ZenModeCallsPreferenceController.KEY); - keys.add(ZenModeRepeatCallersPreferenceController.KEY); - keys.add(ZenModeScreenOnPreferenceController.KEY); - keys.add(ZenModeScreenOffPreferenceController.KEY); return keys; } diff --git a/src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceController.java b/src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceController.java new file mode 100644 index 00000000000..9500320967a --- /dev/null +++ b/src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceController.java @@ -0,0 +1,51 @@ +/* + * 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.content.Context; +import android.support.v7.preference.Preference; + +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.core.lifecycle.Lifecycle; + +public class ZenModeBlockedEffectsPreferenceController extends + AbstractZenModePreferenceController implements PreferenceControllerMixin { + + protected static final String KEY = "zen_mode_block_effects_settings"; + private final ZenModeSettings.SummaryBuilder mSummaryBuilder; + + public ZenModeBlockedEffectsPreferenceController(Context context, Lifecycle lifecycle) { + super(context, KEY, lifecycle); + mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context); + } + + @Override + public String getPreferenceKey() { + return KEY; + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public CharSequence getSummary() { + return mSummaryBuilder.getBlockedEffectsSummary(getPolicy()); + } + +} diff --git a/src/com/android/settings/notification/ZenModeBlockedEffectsSettings.java b/src/com/android/settings/notification/ZenModeBlockedEffectsSettings.java new file mode 100644 index 00000000000..0cac9cb103d --- /dev/null +++ b/src/com/android/settings/notification/ZenModeBlockedEffectsSettings.java @@ -0,0 +1,123 @@ +/* + * 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.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; + +import android.content.Context; +import android.provider.SearchIndexableResource; +import android.support.v7.preference.CheckBoxPreference; + +import com.android.internal.logging.nano.MetricsProto.MetricsEvent; +import com.android.settings.R; +import com.android.settings.search.BaseSearchIndexProvider; +import com.android.settings.search.Indexable; +import com.android.settingslib.core.AbstractPreferenceController; +import com.android.settingslib.core.lifecycle.Lifecycle; + +import java.util.ArrayList; +import java.util.List; + +public class ZenModeBlockedEffectsSettings extends ZenModeSettingsBase implements Indexable { + + @Override + public void onResume() { + super.onResume(); + CheckBoxPreference soundPreference = + (CheckBoxPreference) getPreferenceScreen().findPreference("zen_effect_sound"); + if (soundPreference != null) { + soundPreference.setChecked(true); + } + } + + @Override + protected List createPreferenceControllers(Context context) { + return buildPreferenceControllers(context, getLifecycle()); + } + + private static List buildPreferenceControllers(Context context, + Lifecycle lifecycle) { + List controllers = new ArrayList<>(); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_intent", SUPPRESSED_EFFECT_FULL_SCREEN_INTENT, + MetricsEvent.ACTION_ZEN_BLOCK_FULL_SCREEN_INTENTS, null)); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_light", SUPPRESSED_EFFECT_LIGHTS, + MetricsEvent.ACTION_ZEN_BLOCK_LIGHT, null)); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_peek", SUPPRESSED_EFFECT_PEEK, + MetricsEvent.ACTION_ZEN_BLOCK_PEEK, null)); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_status", SUPPRESSED_EFFECT_STATUS_BAR, + MetricsEvent.ACTION_ZEN_BLOCK_STATUS, + new int[] {SUPPRESSED_EFFECT_NOTIFICATION_LIST})); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_badge", SUPPRESSED_EFFECT_BADGE, + MetricsEvent.ACTION_ZEN_BLOCK_BADGE, null)); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_ambient", SUPPRESSED_EFFECT_AMBIENT, + MetricsEvent.ACTION_ZEN_BLOCK_AMBIENT, null)); + controllers.add(new ZenModeVisEffectPreferenceController(context, lifecycle, + "zen_effect_list", SUPPRESSED_EFFECT_NOTIFICATION_LIST, + MetricsEvent.ACTION_ZEN_BLOCK_NOTIFICATION_LIST, null)); + return controllers; + } + + @Override + protected int getPreferenceScreenResId() { + return R.xml.zen_mode_block_settings; + } + + @Override + public int getMetricsCategory() { + return MetricsEvent.ZEN_WHAT_TO_BLOCK; + } + + /** + * For Search. + */ + public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = + new BaseSearchIndexProvider() { + @Override + public List getXmlResourcesToIndex(Context context, + boolean enabled) { + final ArrayList result = new ArrayList<>(); + + final SearchIndexableResource sir = new SearchIndexableResource(context); + sir.xmlResId = R.xml.zen_mode_block_settings; + result.add(sir); + return result; + } + + @Override + public List getNonIndexableKeys(Context context) { + final List keys = super.getNonIndexableKeys(context); + return keys; + } + + @Override + public List createPreferenceControllers(Context context) { + return buildPreferenceControllers(context, null); + } + }; +} diff --git a/src/com/android/settings/notification/ZenModeScreenOffPreferenceController.java b/src/com/android/settings/notification/ZenModeScreenOffPreferenceController.java deleted file mode 100644 index 81c9b0d3f72..00000000000 --- a/src/com/android/settings/notification/ZenModeScreenOffPreferenceController.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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. - */ - -package com.android.settings.notification; - -import android.app.NotificationManager.Policy; -import android.content.Context; -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 ZenModeScreenOffPreferenceController extends - AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener { - - protected static final String KEY = "zen_mode_screen_off"; - - public ZenModeScreenOffPreferenceController(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; - pref.setChecked(mBackend.isEffectAllowed(Policy.SUPPRESSED_EFFECT_SCREEN_OFF)); - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - final boolean bypass = (Boolean) newValue; - if (ZenModeSettingsBase.DEBUG) { - Log.d(TAG, "onPrefChange allowWhenScreenOff=" + bypass); - } - mMetricsFeatureProvider.action(mContext, - MetricsProto.MetricsEvent.ACTION_ZEN_ALLOW_WHEN_SCREEN_OFF, bypass); - mBackend.saveVisualEffectsPolicy(Policy.SUPPRESSED_EFFECT_SCREEN_OFF, bypass); - return true; - } -} diff --git a/src/com/android/settings/notification/ZenModeScreenOnPreferenceController.java b/src/com/android/settings/notification/ZenModeScreenOnPreferenceController.java deleted file mode 100644 index bab4dd1abce..00000000000 --- a/src/com/android/settings/notification/ZenModeScreenOnPreferenceController.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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. - */ - -package com.android.settings.notification; - -import android.app.NotificationManager.Policy; -import android.content.Context; -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 ZenModeScreenOnPreferenceController extends - AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener { - - protected static final String KEY = "zen_mode_screen_on"; - - public ZenModeScreenOnPreferenceController(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; - pref.setChecked(mBackend.isEffectAllowed(Policy.SUPPRESSED_EFFECT_SCREEN_ON)); - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - final boolean bypass = (Boolean) newValue; - if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allowWhenScreenOn=" - + bypass); - mMetricsFeatureProvider.action(mContext, - MetricsProto.MetricsEvent.ACTION_ZEN_ALLOW_WHEN_SCREEN_ON, bypass); - mBackend.saveVisualEffectsPolicy(Policy.SUPPRESSED_EFFECT_SCREEN_ON, bypass); - return true; - } -} diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java index 4e8793befc8..fdb5cc6dbc5 100644 --- a/src/com/android/settings/notification/ZenModeSettings.java +++ b/src/com/android/settings/notification/ZenModeSettings.java @@ -63,6 +63,7 @@ public class ZenModeSettings extends ZenModeSettingsBase { Lifecycle lifecycle, FragmentManager fragmentManager) { List controllers = new ArrayList<>(); controllers.add(new ZenModeBehaviorPreferenceController(context, lifecycle)); + controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle)); controllers.add(new ZenModeAutomationPreferenceController(context)); controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager)); controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle)); @@ -137,6 +138,18 @@ public class ZenModeSettings extends ZenModeSettingsBase { } } + String getBlockedEffectsSummary(Policy policy) { + if (policy.suppressedVisualEffects == 0) { + return mContext.getResources().getString( + R.string.zen_mode_block_effect_summary_sound); + } else if (Policy.areAllVisualEffectsSuppressed(policy.suppressedVisualEffects)) { + return mContext.getResources().getString( + R.string.zen_mode_block_effect_summary_all); + } + return mContext.getResources().getString( + R.string.zen_mode_block_effect_summary_some); + } + String getAutomaticRulesSummary() { final int count = getEnabledAutomaticRulesCount(); return count == 0 ? mContext.getString(R.string.zen_mode_settings_summary_off) diff --git a/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java b/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java new file mode 100644 index 00000000000..89b953d6a5b --- /dev/null +++ b/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java @@ -0,0 +1,91 @@ +/* + * 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.content.Context; +import android.support.v7.preference.CheckBoxPreference; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.core.lifecycle.Lifecycle; + +public class ZenModeVisEffectPreferenceController + extends AbstractZenModePreferenceController + implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { + + protected final String mKey; + protected final int mEffect; + protected final int mMetricsCategory; + // if any of these effects are suppressed, this effect must be too + protected final int[] mParentSuppressedEffects; + private PreferenceScreen mScreen; + + public ZenModeVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key, + int visualEffect, int metricsCategory, int[] parentSuppressedEffects) { + super(context, key, lifecycle); + mKey = key; + mEffect = visualEffect; + mMetricsCategory = metricsCategory; + mParentSuppressedEffects = parentSuppressedEffects; + } + + @Override + public String getPreferenceKey() { + return mKey; + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + mScreen = screen; + super.displayPreference(screen); + } + + @Override + public void updateState(Preference preference) { + super.updateState(preference); + boolean suppressed = mBackend.isVisualEffectSuppressed(mEffect); + boolean parentSuppressed = false; + if (mParentSuppressedEffects != null) { + for (int parentEffect : mParentSuppressedEffects) { + parentSuppressed |= mBackend.isVisualEffectSuppressed(parentEffect); + } + } + if (parentSuppressed) { + ((CheckBoxPreference) preference).setChecked(parentSuppressed); + onPreferenceChange(preference, parentSuppressed); + preference.setEnabled(false); + } else { + preference.setEnabled(true); + ((CheckBoxPreference) preference).setChecked(suppressed); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final boolean suppressEffect = (Boolean) newValue; + + mMetricsFeatureProvider.action(mContext, mMetricsCategory, suppressEffect); + mBackend.saveVisualEffectsPolicy(mEffect, suppressEffect); + return true; + } +} diff --git a/src/com/android/settings/search/SearchIndexableResourcesImpl.java b/src/com/android/settings/search/SearchIndexableResourcesImpl.java index 1798d34e303..61f5fecd18f 100644 --- a/src/com/android/settings/search/SearchIndexableResourcesImpl.java +++ b/src/com/android/settings/search/SearchIndexableResourcesImpl.java @@ -74,6 +74,7 @@ import com.android.settings.notification.ConfigureNotificationSettings; import com.android.settings.notification.SoundSettings; import com.android.settings.notification.ZenModeAutomationSettings; import com.android.settings.notification.ZenModeBehaviorSettings; +import com.android.settings.notification.ZenModeBlockedEffectsSettings; import com.android.settings.notification.ZenModeSettings; import com.android.settings.print.PrintSettingsFragment; import com.android.settings.security.EncryptionAndCredential; @@ -175,6 +176,7 @@ public class SearchIndexableResourcesImpl implements SearchIndexableResources { addIndex(UsbDetailsFragment.class); addIndex(WifiDisplaySettings.class); addIndex(ZenModeBehaviorSettings.class); + addIndex(ZenModeBlockedEffectsSettings.class); addIndex(ZenModeAutomationSettings.class); addIndex(NightDisplaySettings.class); addIndex(SmartBatterySettings.class); diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeScreenOnPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceControllerTest.java similarity index 57% rename from tests/robotests/src/com/android/settings/notification/ZenModeScreenOnPreferenceControllerTest.java rename to tests/robotests/src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceControllerTest.java index 2fd13805a67..4bf05efd47e 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeScreenOnPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeBlockedEffectsPreferenceControllerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * 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. @@ -16,13 +16,13 @@ package com.android.settings.notification; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; 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.Context; -import android.support.v14.preference.SwitchPreference; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settingslib.core.lifecycle.Lifecycle; @@ -37,22 +37,17 @@ import org.robolectric.shadows.ShadowApplication; import org.robolectric.util.ReflectionHelpers; @RunWith(SettingsRobolectricTestRunner.class) -public class ZenModeScreenOnPreferenceControllerTest { +public final class ZenModeBlockedEffectsPreferenceControllerTest { - private static final boolean MOCK_PRIORITY_SCREEN_ON_SETTING = false; - - private ZenModeScreenOnPreferenceController mController; - - @Mock - private ZenModeBackend mBackend; + private ZenModeBlockedEffectsPreferenceController mController; @Mock private NotificationManager mNotificationManager; @Mock - private SwitchPreference mockPref; - @Mock private NotificationManager.Policy mPolicy; private Context mContext; + @Mock + private ZenModeBackend mBackend; @Before public void setup() { @@ -63,35 +58,18 @@ public class ZenModeScreenOnPreferenceControllerTest { mContext = RuntimeEnvironment.application; when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); - mController = new ZenModeScreenOnPreferenceController(mContext, mock(Lifecycle.class)); + mController = new ZenModeBlockedEffectsPreferenceController( + mContext, mock(Lifecycle.class)); ReflectionHelpers.setField(mController, "mBackend", mBackend); } @Test - public void updateState() { - final SwitchPreference mockPref = mock(SwitchPreference.class); - when(mBackend.isEffectAllowed(NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON)) - .thenReturn(MOCK_PRIORITY_SCREEN_ON_SETTING); - mController.updateState(mockPref); - - verify(mockPref).setChecked(MOCK_PRIORITY_SCREEN_ON_SETTING); + public void testIsAvailable() { + assertTrue(mController.isAvailable()); } @Test - public void onPreferenceChanged_EnableScreenOn() { - boolean allow = true; - mController.onPreferenceChange(mockPref, allow); - - verify(mBackend) - .saveVisualEffectsPolicy(NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON, allow); + public void testHasSummary() { + assertNotNull(mController.getSummary()); } - - @Test - public void onPreferenceChanged_DisableScreenOn() { - boolean allow = false; - mController.onPreferenceChange(mockPref, allow); - - verify(mBackend) - .saveVisualEffectsPolicy(NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON, allow); - } -} \ No newline at end of file +} diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeScreenOffPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeScreenOffPreferenceControllerTest.java deleted file mode 100644 index 6dd3c178417..00000000000 --- a/tests/robotests/src/com/android/settings/notification/ZenModeScreenOffPreferenceControllerTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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. - */ - -package com.android.settings.notification; - -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.Context; -import android.support.v14.preference.SwitchPreference; - -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.shadows.ShadowApplication; -import org.robolectric.util.ReflectionHelpers; - -@RunWith(SettingsRobolectricTestRunner.class) -public class ZenModeScreenOffPreferenceControllerTest { - - private static final boolean MOCK_PRIORITY_SCREEN_OFF_SETTING = false; - - private ZenModeScreenOffPreferenceController mController; - - @Mock - private ZenModeBackend mBackend; - @Mock - private NotificationManager mNotificationManager; - @Mock - private SwitchPreference mockPref; - @Mock - private NotificationManager.Policy mPolicy; - - private Context mContext; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - ShadowApplication shadowApplication = ShadowApplication.getInstance(); - shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); - - mContext = shadowApplication.getApplicationContext(); - when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); - - mController = new ZenModeScreenOffPreferenceController(mContext, mock(Lifecycle.class)); - ReflectionHelpers.setField(mController, "mBackend", mBackend); - } - - @Test - public void updateState() { - final SwitchPreference mockPref = mock(SwitchPreference.class); - when(mBackend.isEffectAllowed(NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) - .thenReturn(MOCK_PRIORITY_SCREEN_OFF_SETTING); - mController.updateState(mockPref); - - verify(mockPref).setChecked(MOCK_PRIORITY_SCREEN_OFF_SETTING); - } - - @Test - public void onPreferenceChanged_EnableScreenOff() { - boolean allow = true; - mController.onPreferenceChange(mockPref, allow); - - verify(mBackend).saveVisualEffectsPolicy( - NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF, allow); - } - - @Test - public void onPreferenceChanged_DisableScreenOff() { - boolean allow = false; - mController.onPreferenceChange(mockPref, allow); - - verify(mBackend).saveVisualEffectsPolicy( - NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF, allow); - } -} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java new file mode 100644 index 00000000000..a424491ab06 --- /dev/null +++ b/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java @@ -0,0 +1,152 @@ +/* + * 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.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; +import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.NotificationManager; +import android.content.Context; +import android.support.v7.preference.CheckBoxPreference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.testutils.FakeFeatureFactory; +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.shadows.ShadowApplication; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(SettingsRobolectricTestRunner.class) +public class ZenModeVisEffectPreferenceControllerTest { + private ZenModeVisEffectPreferenceController mController; + + @Mock + private ZenModeBackend mBackend; + @Mock + private CheckBoxPreference mockPref; + private Context mContext; + private FakeFeatureFactory mFeatureFactory; + @Mock + private PreferenceScreen mScreen; + @Mock NotificationManager mNotificationManager; + + private static final String PREF_KEY = "main_pref"; + private static final int PREF_METRICS = 1; + private static final int PARENT_EFFECT1 = SUPPRESSED_EFFECT_BADGE; + private static final int PARENT_EFFECT2 = SUPPRESSED_EFFECT_NOTIFICATION_LIST; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + ShadowApplication shadowApplication = ShadowApplication.getInstance(); + mContext = shadowApplication.getApplicationContext(); + mFeatureFactory = FakeFeatureFactory.setupForTest(); + shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); + when(mNotificationManager.getNotificationPolicy()).thenReturn( + mock(NotificationManager.Policy.class)); + mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class), + PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, null); + ReflectionHelpers.setField(mController, "mBackend", mBackend); + + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref); + mController.displayPreference(mScreen); + } + + @Test + public void updateState_notChecked() { + when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false); + mController.updateState(mockPref); + + verify(mockPref).setChecked(false); + verify(mockPref).setEnabled(true); + } + + @Test + public void updateState_checked() { + when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(true); + mController.updateState(mockPref); + + verify(mockPref).setChecked(true); + verify(mockPref).setEnabled(true); + } + + @Test + public void updateState_checkedFalse_parentChecked() { + mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class), + PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, + new int[] {PARENT_EFFECT1, PARENT_EFFECT2}); + ReflectionHelpers.setField(mController, "mBackend", mBackend); + when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false); + when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT1)).thenReturn(false); + when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT2)).thenReturn(true); + mController.updateState(mockPref); + + verify(mockPref).setChecked(true); + verify(mockPref).setEnabled(false); + verify(mBackend, times(1)).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true); + } + + @Test + public void updateState_checkedFalse_parentNotChecked() { + mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class), + PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, + new int[] {PARENT_EFFECT1, PARENT_EFFECT2}); + ReflectionHelpers.setField(mController, "mBackend", mBackend); + when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false); + when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT1)).thenReturn(false); + when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT2)).thenReturn(false); + mController.updateState(mockPref); + + verify(mockPref).setChecked(false); + verify(mockPref).setEnabled(true); + verify(mBackend, never()).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true); + } + + @Test + public void onPreferenceChanged_checkedFalse() { + mController.onPreferenceChange(mockPref, false); + + verify(mBackend).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, false); + verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class), + eq(PREF_METRICS), + eq(false)); + } + + @Test + public void onPreferenceChanged_checkedTrue() { + mController.onPreferenceChange(mockPref, true); + verify(mBackend).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true); + verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class), + eq(PREF_METRICS), + eq(true)); + } +}