diff --git a/api/current.txt b/api/current.txt index b117d3e1624fd..fa896eb5cc171 100644 --- a/api/current.txt +++ b/api/current.txt @@ -5693,6 +5693,7 @@ package android.app { method public final void setInterruptionFilter(int); method public void setNotificationPolicy(android.app.NotificationManager.Policy); method public boolean updateAutomaticZenRule(java.lang.String, android.app.AutomaticZenRule); + field public static final java.lang.String ACTION_APP_BLOCK_STATE_CHANGED = "android.app.action.APP_BLOCK_STATE_CHANGED"; field public static final java.lang.String ACTION_INTERRUPTION_FILTER_CHANGED = "android.app.action.INTERRUPTION_FILTER_CHANGED"; field public static final java.lang.String ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED = "android.app.action.NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED"; field public static final java.lang.String ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED = "android.app.action.NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED"; diff --git a/core/java/android/app/NotificationManager.java b/core/java/android/app/NotificationManager.java index 45bed5d5711ca..49c03ab9cfac8 100644 --- a/core/java/android/app/NotificationManager.java +++ b/core/java/android/app/NotificationManager.java @@ -92,6 +92,18 @@ public class NotificationManager { private static String TAG = "NotificationManager"; private static boolean localLOGV = false; + /** + * Intent that is broadcast when an application is blocked or unblocked. + * + * This broadcast is only sent to the app whose block state has changed. + * + * Input: nothing + * Output: nothing + */ + @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) + public static final String ACTION_APP_BLOCK_STATE_CHANGED = + "android.app.action.APP_BLOCK_STATE_CHANGED"; + /** * Intent that is broadcast when a {@link NotificationChannel} is blocked * (when {@link NotificationChannel#getImportance()} is {@link #IMPORTANCE_NONE}) or unblocked diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 081c92cebb98e..d0956415b3c37 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -514,6 +514,7 @@ + diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index efe54faa0f79d..4c9da8949cc95 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -16,6 +16,7 @@ package com.android.server.notification; +import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; import static android.app.NotificationManager.IMPORTANCE_LOW; @@ -1881,6 +1882,18 @@ public class NotificationManagerService extends SystemService { cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); } + + try { + getContext().sendBroadcastAsUser( + new Intent(ACTION_APP_BLOCK_STATE_CHANGED) + .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, !enabled) + .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) + .setPackage(pkg), + UserHandle.of(UserHandle.getUserId(uid)), null); + } catch (SecurityException e) { + Slog.w(TAG, "Can't notify app about app block change", e); + } + savePolicyFile(); } 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 e0bebee2475e1..9ae6f00f44caf 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -1178,6 +1178,34 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean()); } + @Test + public void testUpdateAppNotifyCreatorBlock() throws Exception { + mService.setRankingHelper(mRankingHelper); + + mBinderService.setNotificationsEnabledForPackage(PKG, 0, false); + ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); + verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); + + assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, + captor.getValue().getAction()); + assertEquals(PKG, captor.getValue().getPackage()); + assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); + } + + @Test + public void testUpdateAppNotifyCreatorUnblock() throws Exception { + mService.setRankingHelper(mRankingHelper); + + mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); + ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); + verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); + + assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, + captor.getValue().getAction()); + assertEquals(PKG, captor.getValue().getPackage()); + assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); + } + @Test public void testUpdateChannelNotifyCreatorBlock() throws Exception { mService.setRankingHelper(mRankingHelper);