From bf6d9580730639b6d0d3070ee57c0f7286e69923 Mon Sep 17 00:00:00 2001 From: Yining Liu Date: Thu, 16 May 2024 05:03:29 +0000 Subject: [PATCH 1/3] Update Settings Page for Avalanche Suppression Update the Settings Page for the Avalanche visual and auditory Suppression. Bug: 330606963 Test: PoliteNotificationPreferenceControllerTest Flag: com.android.server.notification.polite_notifications Change-Id: I60b64a0c00cafb22ac1db74285002862affb82d2 --- res/values/strings.xml | 6 +- res/xml/polite_notifications_settings.xml | 45 ++++++++---- ...oliteNotifWorkProfileToggleController.java | 73 +++++++++++++++++-- ...otificationGlobalPreferenceController.java | 63 ++++++++++++++++ 4 files changed, 166 insertions(+), 21 deletions(-) create mode 100644 src/com/android/settings/notification/PoliteNotificationGlobalPreferenceController.java diff --git a/res/values/strings.xml b/res/values/strings.xml index e62bf14bf24..0cf6ab11def 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -8680,7 +8680,9 @@ More settings are available inside this app - Notification cooldown + Cooldown + Use Cooldown + When you get many notifications within a short time span, your device will minimize sound interruptions and pop-ups on the screen. Calls, alarms, and priority conversations will still alert as notmal, and all delivered notifications are easy to find in the Shade. Apply cooldown to all notifications Gradually lower the notification volume when you get many successive notifications from the same app Apply cooldown to conversations @@ -8690,7 +8692,7 @@ Vibrate when unlocked Only vibrate when screen is unlocked Apply to work profiles - Apply the notification cooldown settings from your personal profile to your work profile + Apply to work profile apps diff --git a/res/xml/polite_notifications_settings.xml b/res/xml/polite_notifications_settings.xml index 36f64f29d84..a854a714e81 100644 --- a/res/xml/polite_notifications_settings.xml +++ b/res/xml/polite_notifications_settings.xml @@ -17,22 +17,41 @@ - + + + + + + + - + + + + + + + + + + + + + + + + + + + Date: Thu, 23 May 2024 00:22:51 +0000 Subject: [PATCH 2/3] Add tests for updating Settings Page for Avalanche Suppression Add unit tests for the updating of the Settings Page for the Avalanche visual and auditory Suppression. Bug: 330606963 Test: PoliteNotificationGlobalPreferenceControllerTest, PoliteNotifWorkProfileToggleControllerTest Flag: com.android.server.notification.polite_notifications Change-Id: I8a84f688eb0b3981fbb0fdb699755b4601583702 --- ...eNotifWorkProfileToggleControllerTest.java | 75 ++++++++++- ...icationGlobalPreferenceControllerTest.java | 120 ++++++++++++++++++ 2 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceControllerTest.java diff --git a/tests/robotests/src/com/android/settings/notification/PoliteNotifWorkProfileToggleControllerTest.java b/tests/robotests/src/com/android/settings/notification/PoliteNotifWorkProfileToggleControllerTest.java index 5cda1eaab0a..739fd649d31 100644 --- a/tests/robotests/src/com/android/settings/notification/PoliteNotifWorkProfileToggleControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/PoliteNotifWorkProfileToggleControllerTest.java @@ -18,7 +18,9 @@ package com.android.settings.notification; import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; import static com.android.settings.accessibility.AccessibilityUtil.State.ON; + import static com.google.common.truth.Truth.assertThat; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -27,6 +29,8 @@ import android.os.UserHandle; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; +import androidx.preference.Preference; + import com.android.server.notification.Flags; import com.android.settings.core.BasePreferenceController; import com.android.settings.testutils.shadow.ShadowSystemSettings; @@ -50,6 +54,7 @@ public class PoliteNotifWorkProfileToggleControllerTest { private Context mContext; PoliteNotifWorkProfileToggleController mController; + private Preference mPreference; @Mock private AudioHelper mAudioHelper; @@ -58,12 +63,16 @@ public class PoliteNotifWorkProfileToggleControllerTest { MockitoAnnotations.initMocks(this); mContext = RuntimeEnvironment.application; when(mAudioHelper.getManagedProfileId(any())).thenReturn(UserHandle.MIN_SECONDARY_USER_ID); + setCoolDownEnabled(true); + assertThat(isCoolDownEnabled()).isTrue(); mController = new PoliteNotifWorkProfileToggleController(mContext, PREFERENCE_KEY, mAudioHelper); + mPreference = new Preference(RuntimeEnvironment.application); + mPreference.setKey(mController.getPreferenceKey()); } @Test - public void isAvailable_flagEnabled_workProfileExists_shouldReturnTrue() { + public void isAvailable_flagEnabled_coolDownEnabled_workProfileExists_shouldReturnTrue() { // TODO: b/291907312 - remove feature flags mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); assertThat(mController.isAvailable()).isTrue(); @@ -71,6 +80,17 @@ public class PoliteNotifWorkProfileToggleControllerTest { BasePreferenceController.AVAILABLE); } + @Test + public void isAvailable_flagEnabled_coolDownDisabled_workProfileExists_shouldReturnFalse() { + // TODO: b/291907312 - remove feature flags + mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); + setCoolDownEnabled(false); + assertThat(isCoolDownEnabled()).isFalse(); + assertThat(mController.isAvailable()).isFalse(); + assertThat(mController.getAvailabilityStatus()).isEqualTo( + BasePreferenceController.CONDITIONALLY_UNAVAILABLE); + } + @Test public void isAvailable_flagEnabled_workProfileMissing_shouldReturnFalse() { // TODO: b/291907312 - remove feature flags @@ -84,7 +104,7 @@ public class PoliteNotifWorkProfileToggleControllerTest { } @Test - public void isAvailable_flagDisabled_shouldReturnFalse() { + public void isAvailable_coolDownEnabled_flagDisabled_shouldReturnFalse() { // TODO: b/291907312 - remove feature flags mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); assertThat(mController.isAvailable()).isFalse(); @@ -94,7 +114,7 @@ public class PoliteNotifWorkProfileToggleControllerTest { @Test @Config(shadows = ShadowSystemSettings.class) - public void isChecked_enabledForWorkProfile_shouldReturnTrue() { + public void isChecked_coolDownEnabled_enabledForWorkProfile_shouldReturnTrue() { Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON, UserHandle.MIN_SECONDARY_USER_ID); @@ -103,7 +123,18 @@ public class PoliteNotifWorkProfileToggleControllerTest { @Test @Config(shadows = ShadowSystemSettings.class) - public void isChecked_disabledForWorkProfile_shouldReturnFalse() { + public void isChecked_coolDownDisabled_enabledForWorkProfile_shouldReturnFalse() { + setCoolDownEnabled(false); + assertThat(isCoolDownEnabled()).isFalse(); + Settings.System.putIntForUser(mContext.getContentResolver(), + Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON, + UserHandle.MIN_SECONDARY_USER_ID); + assertThat(mController.isChecked()).isFalse(); + } + + @Test + @Config(shadows = ShadowSystemSettings.class) + public void isChecked_coolDownEnabled_disabledForWorkProfile_shouldReturnFalse() { Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF, UserHandle.MIN_SECONDARY_USER_ID); @@ -112,7 +143,7 @@ public class PoliteNotifWorkProfileToggleControllerTest { @Test @Config(shadows = ShadowSystemSettings.class) - public void setChecked_setTrue_shouldEnablePoliteNotifForWorkProfile() { + public void setChecked_coolDownEnabled_setTrue_shouldEnablePoliteNotifForWorkProfile() { Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF, UserHandle.MIN_SECONDARY_USER_ID); @@ -124,7 +155,7 @@ public class PoliteNotifWorkProfileToggleControllerTest { @Test @Config(shadows = ShadowSystemSettings.class) - public void setChecked_setFalse_shouldDisablePoliteNotifForWorkProfile() { + public void setChecked_coolDownEnabled_setFalse_shouldDisablePoliteNotifForWorkProfile() { Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON, UserHandle.MIN_SECONDARY_USER_ID); @@ -133,4 +164,36 @@ public class PoliteNotifWorkProfileToggleControllerTest { Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON, UserHandle.MIN_SECONDARY_USER_ID)).isEqualTo(OFF); } + + @Test + public void isVisible_coolDownSetToBeDisabled_shouldReturnFalse() { + assertThat(mPreference.isVisible()).isTrue(); + setCoolDownEnabled(false); + assertThat(isCoolDownEnabled()).isFalse(); + mController.updateState(mPreference); + assertThat(mPreference.isVisible()).isFalse(); + } + @Test + public void isVisible_coolDownSetToBeEnabled_shouldReturnTrue() { + setCoolDownEnabled(false); + assertThat(isCoolDownEnabled()).isFalse(); + mController.updateState(mPreference); + assertThat(mPreference.isVisible()).isFalse(); + + setCoolDownEnabled(true); + assertThat(isCoolDownEnabled()).isTrue(); + mController.updateState(mPreference); + assertThat(mPreference.isVisible()).isTrue(); + } + + + private void setCoolDownEnabled(boolean enabled) { + Settings.System.putInt(mContext.getContentResolver(), + Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF)); + } + + private boolean isCoolDownEnabled() { + return Settings.System.getInt(mContext.getContentResolver(), + Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == ON; + } } diff --git a/tests/robotests/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceControllerTest.java new file mode 100644 index 00000000000..8d04c0802ed --- /dev/null +++ b/tests/robotests/src/com/android/settings/notification/PoliteNotificationGlobalPreferenceControllerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2024 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF; +import static com.android.settings.accessibility.AccessibilityUtil.State.ON; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.platform.test.flag.junit.SetFlagsRule; +import android.provider.Settings; + +import com.android.server.notification.Flags; +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.shadow.ShadowSystemSettings; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +@RunWith(RobolectricTestRunner.class) +public class PoliteNotificationGlobalPreferenceControllerTest { + @Rule + public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); + + private static final String PREFERENCE_KEY = "preference_key"; + + private Context mContext; + PoliteNotificationGlobalPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + setCoolDownEnabled(true); + assertThat(isCoolDownEnabled()).isTrue(); + // TODO: b/291907312 - remove feature flags + mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); + mController = new PoliteNotificationGlobalPreferenceController(mContext, PREFERENCE_KEY); + } + + @Test + public void isAvailable_flagEnabled_shouldReturnTrue() { + assertThat(mController.isAvailable()).isTrue(); + assertThat(mController.getAvailabilityStatus()).isEqualTo( + BasePreferenceController.AVAILABLE); + } + + @Test + public void isAvailable_flagDisabled_shouldReturnFalse() { + // TODO: b/291907312 - remove feature flags + mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); + assertThat(mController.isAvailable()).isFalse(); + assertThat(mController.getAvailabilityStatus()).isEqualTo( + BasePreferenceController.CONDITIONALLY_UNAVAILABLE); + } + + @Test + @Config(shadows = ShadowSystemSettings.class) + public void isChecked_coolDownEnabled_shouldReturnTrue() { + assertThat(mController.isChecked()).isTrue(); + } + + @Test + @Config(shadows = ShadowSystemSettings.class) + public void isChecked_coolDownDisabled_shouldReturnFalse() { + setCoolDownEnabled(false); + assertThat(isCoolDownEnabled()).isFalse(); + assertThat(mController.isChecked()).isFalse(); + } + + @Test + @Config(shadows = ShadowSystemSettings.class) + public void setChecked_setTrue_shouldEnableCoolDown() { + setCoolDownEnabled(false); + assertThat(isCoolDownEnabled()).isFalse(); + + mController.setChecked(true); + assertThat(isCoolDownEnabled()).isTrue(); + } + + @Test + @Config(shadows = ShadowSystemSettings.class) + public void setChecked_setFalse_shouldDisableCoolDown() { + assertThat(isCoolDownEnabled()).isTrue(); + + mController.setChecked(false); + assertThat(isCoolDownEnabled()).isFalse(); + } + + private void setCoolDownEnabled(boolean enabled) { + Settings.System.putInt(mContext.getContentResolver(), + Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF)); + } + + private boolean isCoolDownEnabled() { + return Settings.System.getInt(mContext.getContentResolver(), + Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == ON; + } +} From 3d0640985be717205e7145c46019b3b19b3a84e7 Mon Sep 17 00:00:00 2001 From: Yining Liu Date: Thu, 23 May 2024 01:16:12 +0000 Subject: [PATCH 3/3] Clean un-used code for notification cool down Clean the un-used code for notification cool down. Bug: 330606963 Test: PoliteNotificationGlobalPreferenceControllerTest Flag: com.android.server.notification.polite_notifications Change-Id: I081a35c8be8b9996fabb04b238cb426f0da2de58 --- res/values/arrays.xml | 14 -- res/values/strings.xml | 8 - res/xml/polite_notifications_settings.xml | 16 -- ...eNotifVibrateUnlockedToggleController.java | 66 ------- .../PoliteNotificationFilterController.java | 114 ------------ ...ifVibrateUnlockedToggleControllerTest.java | 126 ------------- ...oliteNotificationFilterControllerTest.java | 165 ------------------ 7 files changed, 509 deletions(-) delete mode 100644 src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleController.java delete mode 100644 src/com/android/settings/notification/PoliteNotificationFilterController.java delete mode 100644 tests/robotests/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleControllerTest.java delete mode 100644 tests/robotests/src/com/android/settings/notification/PoliteNotificationFilterControllerTest.java diff --git a/res/values/arrays.xml b/res/values/arrays.xml index cd3134b87eb..98d93a0cb4e 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -1511,20 +1511,6 @@ fr - - - @string/notification_polite_all_apps - @string/notification_polite_conversations - @string/notification_polite_disabled - - - - - 0 - 1 - 2 - - @string/private_space_auto_lock_every_time diff --git a/res/values/strings.xml b/res/values/strings.xml index 0cf6ab11def..0666d430b8f 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -8683,14 +8683,6 @@ Cooldown Use Cooldown When you get many notifications within a short time span, your device will minimize sound interruptions and pop-ups on the screen. Calls, alarms, and priority conversations will still alert as notmal, and all delivered notifications are easy to find in the Shade. - Apply cooldown to all notifications - Gradually lower the notification volume when you get many successive notifications from the same app - Apply cooldown to conversations - Gradually lower the notification volume when you get many messages from the same chat within a short period of time - Don\'t use notification cooldown - Never lower notification volume, regardless of the amount of successive notifications from the same app - Vibrate when unlocked - Only vibrate when screen is unlocked Apply to work profiles Apply to work profile apps diff --git a/res/xml/polite_notifications_settings.xml b/res/xml/polite_notifications_settings.xml index a854a714e81..3baf88f6085 100644 --- a/res/xml/polite_notifications_settings.xml +++ b/res/xml/polite_notifications_settings.xml @@ -17,24 +17,8 @@ - - - - - - - - - - - - - - - diff --git a/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleController.java b/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleController.java deleted file mode 100644 index dfdd64976b1..00000000000 --- a/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleController.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2023 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; - -import android.content.Context; -import android.os.Vibrator; -import android.provider.Settings; - -import com.android.server.notification.Flags; -import com.android.settings.R; -import com.android.settings.core.TogglePreferenceController; - -/** - * Controls the toggle that determines whether notifications - * should only vibrate (no sound) when the device is unlocked. - */ -public class PoliteNotifVibrateUnlockedToggleController extends TogglePreferenceController { - - public PoliteNotifVibrateUnlockedToggleController(Context context, String preferenceKey) { - super(context, preferenceKey); - } - - @Override - public int getAvailabilityStatus() { - // TODO: b/291897570 - remove this when the feature flag is removed! - if (!Flags.politeNotifications() || !Flags.vibrateWhileUnlocked()) { - return CONDITIONALLY_UNAVAILABLE; - } - return mContext.getSystemService(Vibrator.class).hasVibrator() ? AVAILABLE - : UNSUPPORTED_ON_DEVICE; - } - - @Override - public boolean isChecked() { - return Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, OFF) != OFF; - } - - @Override - public boolean setChecked(boolean isChecked) { - return Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, (isChecked ? ON : OFF)); - } - - @Override - public int getSliceHighlightMenuRes() { - return R.string.menu_key_accessibility; - } -} diff --git a/src/com/android/settings/notification/PoliteNotificationFilterController.java b/src/com/android/settings/notification/PoliteNotificationFilterController.java deleted file mode 100644 index 8093f55acd5..00000000000 --- a/src/com/android/settings/notification/PoliteNotificationFilterController.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2023 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; - -import android.content.Context; -import android.provider.Settings; -import android.util.Log; - -import androidx.preference.ListPreference; -import androidx.preference.Preference; - -import com.android.server.notification.Flags; -import com.android.settings.R; -import com.android.settings.core.BasePreferenceController; -import com.android.settings.core.PreferenceControllerMixin; - - -/** - * Controls whether polite notifications are enabled and apply to all apps or just to conversations. - */ -public class PoliteNotificationFilterController extends BasePreferenceController - implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { - static final String TAG = "PoliteNotificationFilterController"; - - private static final int POLITE_NOTIFICATIONS_ALL = 0; - private static final int POLITE_NOTIFICATIONS_CONVERSATIONS = 1; - private static final int POLITE_NOTIFICATIONS_DISABLED = 2; - - public PoliteNotificationFilterController(Context context, String key) { - super(context, key); - } - - @Override - public int getAvailabilityStatus() { - // TODO: b/291897570 - remove this when the feature flag is removed! - return Flags.politeNotifications() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; - } - - @Override - public void updateState(Preference preference) { - super.updateState(preference); - final ListPreference pref = (ListPreference) preference; - - if (isPoliteNotifDisabled()) { - pref.setValue(Integer.toString(POLITE_NOTIFICATIONS_DISABLED)); - } else if (shouldApplyForAllApps()) { - pref.setValue(Integer.toString(POLITE_NOTIFICATIONS_ALL)); - } else { - pref.setValue(Integer.toString(POLITE_NOTIFICATIONS_CONVERSATIONS)); - } - } - - @Override - public CharSequence getSummary() { - if (isPoliteNotifDisabled()) { - return mContext.getString(R.string.notification_polite_disabled_summary); - } - if (shouldApplyForAllApps()) { - return mContext.getString(R.string.notification_polite_all_apps_summary); - } else { - return mContext.getString(R.string.notification_polite_conversations_summary); - } - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - final int prefValue = Integer.parseInt((String) newValue); - if (prefValue == POLITE_NOTIFICATIONS_ALL) { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, ON); - } else if (prefValue == POLITE_NOTIFICATIONS_CONVERSATIONS) { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, OFF); - } else if (prefValue == POLITE_NOTIFICATIONS_DISABLED) { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF); - } else { - Log.e(TAG, "Unexpected preference value: " + prefValue); - } - refreshSummary(preference); - return true; - } - - private boolean isPoliteNotifDisabled() { - return Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == OFF; - } - - private boolean shouldApplyForAllApps() { - return Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, ON) != OFF; - } -} diff --git a/tests/robotests/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleControllerTest.java b/tests/robotests/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleControllerTest.java deleted file mode 100644 index 4d546ec2b40..00000000000 --- a/tests/robotests/src/com/android/settings/notification/PoliteNotifVibrateUnlockedToggleControllerTest.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2023 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -import android.content.Context; -import android.os.Vibrator; -import android.platform.test.flag.junit.SetFlagsRule; -import android.provider.Settings; - -import com.android.server.notification.Flags; -import com.android.settings.core.BasePreferenceController; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class PoliteNotifVibrateUnlockedToggleControllerTest { - - @Rule - public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); - - private static final String PREFERENCE_KEY = "preference_key"; - - private PoliteNotifVibrateUnlockedToggleController mController; - private Context mContext; - @Mock - private Vibrator mVibrator; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mContext = spy(RuntimeEnvironment.application); - mController = new PoliteNotifVibrateUnlockedToggleController(mContext, PREFERENCE_KEY); - when(mContext.getSystemService(Vibrator.class)).thenReturn(mVibrator); - } - - @Test - public void isAvailable_flagEnabled_vibrationSupported_shouldReturnTrue() { - // TODO: b/291907312 - remove feature flags - mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); - mSetFlagsRule.enableFlags(Flags.FLAG_VIBRATE_WHILE_UNLOCKED); - when(mVibrator.hasVibrator()).thenReturn(true); - assertThat(mController.isAvailable()).isTrue(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.AVAILABLE); - } - - @Test - public void isAvailable_flagEnabled_vibrationNotSupported_shouldReturnFalse() { - // TODO: b/291907312 - remove feature flags - mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); - mSetFlagsRule.enableFlags(Flags.FLAG_VIBRATE_WHILE_UNLOCKED); - when(mVibrator.hasVibrator()).thenReturn(false); - assertThat(mController.isAvailable()).isFalse(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.UNSUPPORTED_ON_DEVICE); - } - - @Test - public void isAvailable_flagDisabled_shouldReturnFalse() { - // TODO: b/291907312 - remove feature flags - mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); - mSetFlagsRule.enableFlags(Flags.FLAG_VIBRATE_WHILE_UNLOCKED); - assertThat(mController.isAvailable()).isFalse(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.CONDITIONALLY_UNAVAILABLE); - } - - @Test - public void isChecked_vibrateEnabled_shouldReturnTrue() { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, ON); - assertThat(mController.isChecked()).isTrue(); - } - - @Test - public void isChecked_vibrateDisabled_shouldReturnFalse() { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, OFF); - assertThat(mController.isChecked()).isFalse(); - } - - @Test - public void setChecked_setTrue_shouldEnableVibrateSetting() { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, OFF); - mController.setChecked(true); - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, OFF)).isEqualTo(ON); - } - - @Test - public void setChecked_setFalse_shouldDisableVibrateSetting() { - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, ON); - mController.setChecked(false); - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_VIBRATE_UNLOCKED, ON)).isEqualTo(OFF); - } -} diff --git a/tests/robotests/src/com/android/settings/notification/PoliteNotificationFilterControllerTest.java b/tests/robotests/src/com/android/settings/notification/PoliteNotificationFilterControllerTest.java deleted file mode 100644 index 646e927c02f..00000000000 --- a/tests/robotests/src/com/android/settings/notification/PoliteNotificationFilterControllerTest.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2023 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.content.Context; -import android.platform.test.flag.junit.SetFlagsRule; -import android.provider.Settings; - -import androidx.preference.ListPreference; -import androidx.preference.Preference; -import androidx.preference.PreferenceScreen; - -import com.android.server.notification.Flags; -import com.android.settings.R; -import com.android.settings.core.BasePreferenceController; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class PoliteNotificationFilterControllerTest { - @Rule - public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); - - private static final String PREFERENCE_KEY = "preference_key"; - private static final int POLITE_NOTIFICATIONS_ALL = 0; - private static final int POLITE_NOTIFICATIONS_CONVERSATIONS = 1; - private static final int POLITE_NOTIFICATIONS_DISABLED = 2; - - @Mock - private PreferenceScreen mScreen; - - private PoliteNotificationFilterController mController; - private Preference mPreference; - private Context mContext; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mContext = spy(RuntimeEnvironment.application); - - mController = new PoliteNotificationFilterController(mContext, PREFERENCE_KEY); - mPreference = new Preference(mContext); - mPreference.setKey(mController.getPreferenceKey()); - when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); - } - - @Test - public void isAvailable_flagEnabled_shouldReturnTrue() { - // TODO: b/291907312 - remove feature flags - mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); - assertThat(mController.isAvailable()).isTrue(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.AVAILABLE); - } - - @Test - public void isAvailable_flagDisabled_shouldReturnFalse() { - // TODO: b/291907312 - remove feature flags - mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS); - assertThat(mController.isAvailable()).isFalse(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.CONDITIONALLY_UNAVAILABLE); - } - - @Test - public void updateState_politeNotificationDisabled() { - final ListPreference preference = mock(ListPreference.class); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF); - mController.updateState(preference); - - verify(preference).setValue(Integer.toString(POLITE_NOTIFICATIONS_DISABLED)); - assertThat(mController.getSummary().toString()).isEqualTo( - mContext.getString(R.string.notification_polite_disabled_summary)); - } - - @Test - public void updateState_politeNotificationEnabled_applyAllApps() { - final ListPreference preference = mock(ListPreference.class); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, ON); - mController.updateState(preference); - - verify(preference).setValue(Integer.toString(POLITE_NOTIFICATIONS_ALL)); - assertThat(mController.getSummary().toString()).isEqualTo( - mContext.getString(R.string.notification_polite_all_apps_summary)); - } - - @Test - public void updateState_politeNotificationEnabled_applyOnlyConversations() { - final ListPreference preference = mock(ListPreference.class); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON); - Settings.System.putInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, OFF); - mController.updateState(preference); - - verify(preference).setValue(Integer.toString(POLITE_NOTIFICATIONS_CONVERSATIONS)); - assertThat(mController.getSummary().toString()).isEqualTo( - mContext.getString(R.string.notification_polite_conversations_summary)); - } - - @Test - public void onPreferenceChanged_firstItemSelected_shouldEnableForAll() { - mController.displayPreference(mScreen); - mController.onPreferenceChange(mPreference, "0"); - - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF)).isEqualTo(ON); - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, OFF)).isEqualTo(ON); - } - - @Test - public void onPreferenceChanged_secondItemSelected_shouldEnableForConversationsOnly() { - mController.displayPreference(mScreen); - mController.onPreferenceChange(mPreference, "1"); - - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF)).isEqualTo(ON); - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ALL, OFF)).isEqualTo(OFF); - } - - @Test - public void onPreferenceChanged_thirdItemSelected_shouldDisable() { - mController.displayPreference(mScreen); - mController.onPreferenceChange(mPreference, "2"); - - assertThat(Settings.System.getInt(mContext.getContentResolver(), - Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF)).isEqualTo(OFF); - } - -}