From 4056976f1e7556873696e0a23ad612659669ebad Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Mon, 5 Apr 2021 16:58:17 -0400 Subject: Restrict alarm broadcast To android only Test: NotificationManagerTest, NotificationManagerServiceTest Bug: 175614289 Change-Id: I4f8e56729d90f8f5288d08881129b1c45d5790e7 Merged-In: I4f8e56729d90f8f5288d08881129b1c45d5790e7 (cherry picked from commit abf15b731af248f512b7cc31ea99019436841af4) --- .../notification/NotificationManagerService.java | 1 + .../NotificationManagerServiceTest.java | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index e94507bccea0..496dcb23140b 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -6835,6 +6835,7 @@ public class NotificationManagerService extends SystemService { final PendingIntent pi = PendingIntent.getBroadcast(getContext(), REQUEST_CODE_TIMEOUT, new Intent(ACTION_NOTIFICATION_TIMEOUT) + .setPackage(PackageManagerService.PLATFORM_PACKAGE_NAME) .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) .appendPath(record.getKey()).build()) .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) 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 a55d28ee6065..2b43c7a7c145 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -84,6 +84,7 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.ActivityManagerInternal; +import android.app.AlarmManager; import android.app.AppOpsManager; import android.app.AutomaticZenRule; import android.app.IActivityManager; @@ -171,6 +172,7 @@ import com.android.server.lights.LightsManager; import com.android.server.lights.LogicalLight; import com.android.server.notification.NotificationManagerService.NotificationAssistants; import com.android.server.notification.NotificationManagerService.NotificationListeners; +import com.android.server.pm.PackageManagerService; import com.android.server.statusbar.StatusBarManagerInternal; import com.android.server.uri.UriGrantsManagerInternal; import com.android.server.wm.ActivityTaskManagerInternal; @@ -282,6 +284,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { NotificationHistoryManager mHistoryManager; @Mock StatsManager mStatsManager; + @Mock + AlarmManager mAlarmManager; NotificationRecordLoggerFake mNotificationRecordLogger = new NotificationRecordLoggerFake(); private InstanceIdSequence mNotificationInstanceIdSequence = new InstanceIdSequenceFake( 1 << 30); @@ -423,6 +427,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { LocalServices.addService(DeviceIdleInternal.class, deviceIdleInternal); LocalServices.removeServiceForTest(ActivityManagerInternal.class); LocalServices.addService(ActivityManagerInternal.class, activityManagerInternal); + mContext.addMockSystemService(Context.ALARM_SERVICE, mAlarmManager); + doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any()); @@ -830,6 +836,26 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { return nrSummary; } + @Test + public void testLimitTimeOutBroadcast() { + NotificationChannel channel = new NotificationChannel("id", "name", + NotificationManager.IMPORTANCE_HIGH); + Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) + .setContentTitle("foo") + .setSmallIcon(android.R.drawable.sym_def_app_icon) + .setTimeoutAfter(1); + + StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 8, "tag", mUid, 0, + nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); + NotificationRecord r = new NotificationRecord(mContext, sbn, channel); + + mService.scheduleTimeoutLocked(r); + ArgumentCaptor captor = ArgumentCaptor.forClass(PendingIntent.class); + verify(mAlarmManager).setExactAndAllowWhileIdle(anyInt(), anyLong(), captor.capture()); + assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME, + captor.getValue().getIntent().getPackage()); + } + @Test public void testDefaultAssistant_overrideDefault() { final int userId = 0; -- cgit v1.2.3-59-g8ed1b