diff --git a/res/values/strings.xml b/res/values/strings.xml index 852e81d94f1..3af7f058900 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6168,7 +6168,7 @@ Power on sounds - Do not disturb + Do not disturb preferences Priority only allows diff --git a/src/com/android/settings/notification/ZenModePreferenceController.java b/src/com/android/settings/notification/ZenModePreferenceController.java index 61bbe9aa3d3..1427d5a4412 100644 --- a/src/com/android/settings/notification/ZenModePreferenceController.java +++ b/src/com/android/settings/notification/ZenModePreferenceController.java @@ -16,14 +16,22 @@ package com.android.settings.notification; +import android.app.NotificationManager; import android.content.Context; +import android.support.v7.preference.Preference; +import com.android.settings.R; public class ZenModePreferenceController extends AdjustVolumeRestrictedPreferenceController { private static final String KEY_ZEN_MODE = "zen_mode"; + private String mSummaryPrefix; + + private ZenModeSettings.SummaryBuilder mSummaryBuilder; public ZenModePreferenceController(Context context) { super(context); + mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context); + mSummaryPrefix = context.getString(R.string.zen_mode_priority_settings_title) + " "; } @Override @@ -36,4 +44,12 @@ public class ZenModePreferenceController extends AdjustVolumeRestrictedPreferenc return true; } + @Override + public void updateState(Preference preference) { + super.updateState(preference); + if (preference.isEnabled()) { + preference.setSummary(mSummaryPrefix + mSummaryBuilder.getPrioritySettingSummary( + NotificationManager.from(mContext).getNotificationPolicy())); + } + } } diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java index 1d5f09ebe4d..6266af99bf5 100644 --- a/src/com/android/settings/notification/ZenModeSettings.java +++ b/src/com/android/settings/notification/ZenModeSettings.java @@ -34,6 +34,7 @@ public class ZenModeSettings extends ZenModeSettingsBase { private Preference mPrioritySettings; private Preference mVisualSettings; private Policy mPolicy; + private SummaryBuilder mSummaryBuilder; @Override public void onCreate(Bundle savedInstanceState) { @@ -45,6 +46,7 @@ public class ZenModeSettings extends ZenModeSettingsBase { mPrioritySettings = root.findPreference(KEY_PRIORITY_SETTINGS); mVisualSettings = root.findPreference(KEY_VISUAL_SETTINGS); mPolicy = NotificationManager.from(mContext).getNotificationPolicy(); + mSummaryBuilder = new SummaryBuilder(getContext()); } @Override @@ -77,62 +79,80 @@ public class ZenModeSettings extends ZenModeSettingsBase { } private void updatePrioritySettingsSummary() { - String s = getResources().getString(R.string.zen_mode_alarms); - s = append(s, isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_REMINDERS), - R.string.zen_mode_reminders); - s = append(s, isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_EVENTS), - R.string.zen_mode_events); - if (isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_MESSAGES)) { - if (mPolicy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) { - s = append(s, true, R.string.zen_mode_all_messages); - } else { - s = append(s, true, R.string.zen_mode_selected_messages); - } - } - if (isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_CALLS)) { - if (mPolicy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) { - s = append(s, true, R.string.zen_mode_all_callers); - } else { - s = append(s, true, R.string.zen_mode_selected_callers); - } - } else if (isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)) { - s = append(s, true, R.string.zen_mode_repeat_callers); - } - mPrioritySettings.setSummary(s); + mPrioritySettings.setSummary(mSummaryBuilder.getPrioritySettingSummary(mPolicy)); } private void updateVisualSettingsSummary() { - String s = getString(R.string.zen_mode_all_visual_interruptions); - if (isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_ON) - && isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) { - s = getString(R.string.zen_mode_no_visual_interruptions); - } else if (isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_ON)) { - s = getString(R.string.zen_mode_screen_on_visual_interruptions); - } else if (isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) { - s = getString(R.string.zen_mode_screen_off_visual_interruptions); - } - mVisualSettings.setSummary(s); - } - - private boolean isEffectSuppressed(int effect) { - return (mPolicy.suppressedVisualEffects & effect) != 0; - } - - private boolean isCategoryEnabled(Policy policy, int categoryType) { - return (policy.priorityCategories & categoryType) != 0; - } - - @VisibleForTesting - String append(String s, boolean condition, int resId) { - if (condition) { - final Context context = getContext(); - return context.getString(R.string.join_many_items_middle, s, context.getString(resId)); - } - return s; + mVisualSettings.setSummary(mSummaryBuilder.getVisualSettingSummary(mPolicy)); } @Override protected int getHelpResource() { return R.string.help_uri_interruptions; } + + public static class SummaryBuilder { + + private Context mContext; + + public SummaryBuilder(Context context) { + mContext = context; + } + + String getPrioritySettingSummary(Policy policy) { + String s = mContext.getString(R.string.zen_mode_alarms); + s = append(s, isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_REMINDERS), + R.string.zen_mode_reminders); + s = append(s, isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_EVENTS), + R.string.zen_mode_events); + if (isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_MESSAGES)) { + if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) { + s = append(s, true, R.string.zen_mode_all_messages); + } else { + s = append(s, true, R.string.zen_mode_selected_messages); + } + } + if (isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_CALLS)) { + if (policy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) { + s = append(s, true, R.string.zen_mode_all_callers); + } else { + s = append(s, true, R.string.zen_mode_selected_callers); + } + } else if (isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)) { + s = append(s, true, R.string.zen_mode_repeat_callers); + } + return s; + } + + String getVisualSettingSummary(Policy policy) { + String s = mContext.getString(R.string.zen_mode_all_visual_interruptions); + if (isEffectSuppressed(policy, Policy.SUPPRESSED_EFFECT_SCREEN_ON) + && isEffectSuppressed(policy, Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) { + s = mContext.getString(R.string.zen_mode_no_visual_interruptions); + } else if (isEffectSuppressed(policy, Policy.SUPPRESSED_EFFECT_SCREEN_ON)) { + s = mContext.getString(R.string.zen_mode_screen_on_visual_interruptions); + } else if (isEffectSuppressed(policy, Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) { + s = mContext.getString(R.string.zen_mode_screen_off_visual_interruptions); + } + return s; + } + + @VisibleForTesting + String append(String s, boolean condition, int resId) { + if (condition) { + return mContext.getString( + R.string.join_many_items_middle, s, mContext.getString(resId)); + } + return s; + } + + private boolean isCategoryEnabled(Policy policy, int categoryType) { + return (policy.priorityCategories & categoryType) != 0; + } + + private boolean isEffectSuppressed(Policy policy, int effect) { + return (policy.suppressedVisualEffects & effect) != 0; + } + + } } diff --git a/tests/robotests/src/com/android/settings/notification/ZenModePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModePreferenceControllerTest.java index 155e6ea010e..a31fb5ec12c 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModePreferenceControllerTest.java @@ -16,7 +16,10 @@ package com.android.settings.notification; +import android.app.NotificationManager; +import android.app.NotificationManager.Policy; import android.content.Context; +import android.support.v7.preference.Preference; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -27,8 +30,13 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @@ -36,6 +44,12 @@ public class ZenModePreferenceControllerTest { @Mock private Context mContext; + @Mock + private Preference mPreference; + @Mock + private NotificationManager mNotificationManager; + @Mock + private Policy mPolicy; private ZenModePreferenceController mController; @@ -43,6 +57,9 @@ public class ZenModePreferenceControllerTest { public void setUp() { MockitoAnnotations.initMocks(this); mController = new ZenModePreferenceController(mContext); + when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)) + .thenReturn(mNotificationManager); + when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); } @Test @@ -50,4 +67,22 @@ public class ZenModePreferenceControllerTest { assertThat(mController.isAvailable()).isTrue(); } + @Test + public void updateState_preferenceEnabled_shouldSetSummary() { + when(mPreference.isEnabled()).thenReturn(true); + + mController.updateState(mPreference); + + verify(mPreference).setSummary(anyString()); + } + + @Test + public void updateState_preferenceDisabled_shouldNotSetSummary() { + when(mPreference.isEnabled()).thenReturn(false); + + mController.updateState(mPreference); + + verify(mPreference, never()).setSummary(anyString()); + } + } diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java index 40cfed54ead..7a3742d3266 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeSettingsTest.java @@ -36,21 +36,20 @@ import static org.mockito.Mockito.spy; @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class ZenModeSettingsTest { - private ZenModeSettings mSettings; + private ZenModeSettings.SummaryBuilder mBuilder; private Context mContext; @Before public void setUp() { mContext = RuntimeEnvironment.application.getApplicationContext(); - mSettings = spy(ZenModeSettings.class); - doReturn(mContext).when(mSettings).getContext(); + mBuilder = new ZenModeSettings.SummaryBuilder(mContext); } @Test public void testAppend_conditionFalse_shouldNotAppend() { String original = "test"; - final String result = mSettings.append(original, false, R.string.zen_mode_alarms); + final String result = mBuilder.append(original, false, R.string.zen_mode_alarms); assertThat(result).isEqualTo(original); } @@ -60,7 +59,7 @@ public class ZenModeSettingsTest { String original = "test"; String alarm = mContext.getString(R.string.zen_mode_alarms); - final String result = mSettings.append(original, true, R.string.zen_mode_alarms); + final String result = mBuilder.append(original, true, R.string.zen_mode_alarms); assertThat(result).contains(alarm); }