From 4228dd1bf3e620deadddccc5180ba11bf6025ad2 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Tue, 11 Jan 2022 12:13:33 -0500 Subject: [PATCH] Delay sending notif block state broadcast Now that blocking notifications results in the app being killed, we need to delay before notifying the app to give the app time to respond to the block change Bug: 194833441 Test: NotificationManagerServiceTest/NotificationPermissionMigrationTest Change-Id: I5e9441135590fa271d30957a02bfe0da925d4c7a --- .../NotificationManagerService.java | 24 +++++++++++-------- .../NotificationManagerServiceTest.java | 4 ++++ .../NotificationPermissionMigrationTest.java | 4 ++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 3d533616513bc..86b385b4d8103 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2657,16 +2657,20 @@ public class NotificationManagerService extends SystemService { } private void sendAppBlockStateChangedBroadcast(String pkg, int uid, boolean blocked) { - try { - getContext().sendBroadcastAsUser( - new Intent(ACTION_APP_BLOCK_STATE_CHANGED) - .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, blocked) - .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); - } + // From Android T, revoking the notification permission will cause the app to be killed. + // delay this broadcast so it doesn't race with that process death + mHandler.postDelayed(() -> { + try { + getContext().sendBroadcastAsUser( + new Intent(ACTION_APP_BLOCK_STATE_CHANGED) + .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, blocked) + .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); + } + }, 500); } @Override 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 d83190353a87f..a192bf87cce42 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -2701,6 +2701,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { // should trigger a broadcast mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); + Thread.sleep(500); + waitForIdle(); ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); @@ -2728,6 +2730,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { // should trigger a broadcast mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); + Thread.sleep(500); + waitForIdle(); ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationPermissionMigrationTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationPermissionMigrationTest.java index 1362628bde5e2..f6400b65bc4dc 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationPermissionMigrationTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationPermissionMigrationTest.java @@ -601,6 +601,8 @@ public class NotificationPermissionMigrationTest extends UiServiceTestCase { when(mAppOpsManager.checkOpNoThrow(anyInt(), eq(mUid), eq(PKG))).thenReturn(MODE_IGNORED); mService.mAppOpsCallback.opChanged(0, mUid, PKG); + Thread.sleep(500); + waitForIdle(); ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); @@ -616,6 +618,8 @@ public class NotificationPermissionMigrationTest extends UiServiceTestCase { when(mAppOpsManager.checkOpNoThrow(anyInt(), eq(mUid), eq(PKG))).thenReturn(MODE_ALLOWED); mService.mAppOpsCallback.opChanged(0, mUid, PKG); + Thread.sleep(500); + waitForIdle(); ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null));