diff options
| author | 2019-05-20 12:04:36 -0700 | |
|---|---|---|
| committer | 2019-05-20 15:28:50 -0700 | |
| commit | c682034a934d2ae32699f7d69024f79c63bb552b (patch) | |
| tree | 8072826d00377903c654588ca65b445bf0d55ff8 | |
| parent | 8281f9c42e0a147aa4d939934630593479061cdd (diff) | |
Add a check at the global level for notification bubbles being enabled
None of the checks in NoManService for 'flag bubble' encorporated the
global setting... this CL adds a check for that.
Test changes:
* Moves prefs set up into a helper function so it's clear whether
bubbles are enabled / disabled at different levels:
- global
- app
- channel
* Adds an additional test to make sure flag isn't applied when disabled
at app level
Test: atest NotificationManagerServiceTest
Bug: 132699005
Change-Id: I937b6984f4698cfe026d3e34f4808adfb969392d
2 files changed, 60 insertions, 146 deletions
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 4f859412f5f4..86368cafb353 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -4818,6 +4818,7 @@ public class NotificationManagerService extends SystemService { // Does the app want to bubble & is able to bubble boolean canBubble = notification.getBubbleMetadata() != null && mPreferencesHelper.areBubblesAllowed(pkg, userId) + && mPreferencesHelper.bubblesEnabled(r.sbn.getUser()) && r.getChannel().canBubble() && !mActivityManager.isLowRamDevice(); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index d2332bf86e95..73b5ff1e63a5 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -119,7 +119,6 @@ import android.provider.Settings; import android.service.notification.Adjustment; import android.service.notification.NotificationListenerService; import android.service.notification.NotificationStats; -import android.service.notification.NotifyingApp; import android.service.notification.StatusBarNotification; import android.service.notification.ZenPolicy; import android.test.suitebuilder.annotation.SmallTest; @@ -165,10 +164,8 @@ import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.function.Consumer; @SmallTest @@ -414,6 +411,19 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mTestableLooper.processAllMessages(); } + private void setUpPrefsForBubbles(boolean globalEnabled, boolean pkgEnabled, + boolean channelEnabled) { + mService.setPreferencesHelper(mPreferencesHelper); + when(mPreferencesHelper.bubblesEnabled(any())).thenReturn(globalEnabled); + when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(pkgEnabled); + when(mPreferencesHelper.getNotificationChannel( + anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( + mTestNotificationChannel); + when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( + mTestNotificationChannel.getImportance()); + mTestNotificationChannel.setAllowBubbles(channelEnabled); + } + private StatusBarNotification generateSbn(String pkg, int uid, long postTime, int userId) { Notification.Builder nb = new Notification.Builder(mContext, "a") .setContentTitle("foo") @@ -4226,13 +4236,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubble() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif with bubble metadata but not our other misc requirements NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -4254,15 +4258,33 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { } @Test + public void testFlagBubble_noFlag_appNotAllowed() throws RemoteException { + // Bubbles are allowed! + setUpPrefsForBubbles(true /* global */, false /* app */, true /* channel */); + + // Notif with bubble metadata but not our other misc requirements + NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, + null /* tvExtender */, true /* isBubble */); + + // Say we're foreground + when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( + IMPORTANCE_FOREGROUND); + + mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", + nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); + waitForIdle(); + + StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); + assertEquals(1, notifs.length); + assertEquals((notifs[0].getNotification().flags & FLAG_BUBBLE), 0); + assertFalse(mService.getNotificationRecord( + nr.sbn.getKey()).getNotification().isBubbleNotification()); + } + + @Test public void testFlagBubbleNotifs_flag_appForeground() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif with bubble metadata but not our other misc requirements NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -4284,13 +4306,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_appNotForeground() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif with bubble metadata but not our other misc requirements NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -4312,13 +4328,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_flag_previousForegroundFlag() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif with bubble metadata but not our other misc requirements NotificationRecord nr1 = generateNotificationRecord(mTestNotificationChannel, @@ -4358,13 +4368,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { public void testFlagBubbleNotifs_noFlag_previousForegroundFlag_afterRemoval() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif with bubble metadata but not our other misc requirements NotificationRecord nr1 = generateNotificationRecord(mTestNotificationChannel, @@ -4412,13 +4416,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_flag_messaging() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4464,13 +4462,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_flag_phonecall() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4505,13 +4497,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_phonecall_noForegroundService() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4544,13 +4530,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_phonecall_noPerson() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4580,13 +4560,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_phonecall_noCategory() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4620,13 +4594,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_messaging_appNotAllowed() throws RemoteException { // Bubbles are NOT allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(false); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(false /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4665,13 +4633,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_notBubble() throws RemoteException { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif WITHOUT bubble metadata NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); @@ -4688,17 +4650,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_messaging_channelNotAllowed() throws RemoteException { - // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); - - // But not on this channel! - mTestNotificationChannel.setAllowBubbles(false); + // Bubbles are allowed except on this channel + setUpPrefsForBubbles(true /* global */, true /* app */, false /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4736,14 +4689,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_phonecall_notAllowed() throws RemoteException { - // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(false); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + // Bubbles are not allowed! + setUpPrefsForBubbles(false /* global */, true /* app */, true /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4777,17 +4724,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testFlagBubbleNotifs_noFlag_phonecall_channelNotAllowed() throws RemoteException { - // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(false); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); - - // But not on this channel! - mTestNotificationChannel.setAllowBubbles(false); + // Bubbles are allowed, but not on channel. + setUpPrefsForBubbles(true /* global */, true /* app */, false /* channel */); // Give it bubble metadata Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); @@ -4992,13 +4930,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testNotificationBubbleChanged_false() throws Exception { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif with bubble metadata but not our other misc requirements NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -5030,13 +4962,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testNotificationBubbleChanged_true() throws Exception { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Plain notification that has bubble metadata NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -5067,13 +4993,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testNotificationBubbleChanged_true_notAllowed() throws Exception { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Notif that is not a bubble NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -5100,13 +5020,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testNotificationBubbles_disabled_lowRamDevice() throws Exception { // Bubbles are allowed! - mService.setPreferencesHelper(mPreferencesHelper); - when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(true); - when(mPreferencesHelper.getNotificationChannel( - anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - mTestNotificationChannel); - when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( - mTestNotificationChannel.getImportance()); + setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); // Plain notification that has bubble metadata NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, @@ -5131,6 +5045,5 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); assertEquals(1, notifsAfter.length); assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); - } } |