Skip to content

Commit 2e4cb8c

Browse files
Julia Reynoldsandroid-build-team Robot
authored andcommitted
Restrict alarm broadcast
To android only Test: NotificationManagerTest, NotificationManagerServiceTest Bug: 175614289 Change-Id: I4f8e56729d90f8f5288d08881129b1c45d5790e7 Merged-In: I4f8e56729d90f8f5288d08881129b1c45d5790e7 (cherry picked from commit abf15b731af248f512b7cc31ea99019436841af4) (cherry picked from commit 4056976f1e7556873696e0a23ad612659669ebad)
1 parent dd70c8f commit 2e4cb8c

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

services/core/java/com/android/server/notification/NotificationManagerService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6864,6 +6864,7 @@ void scheduleTimeoutLocked(NotificationRecord record) {
68646864
final PendingIntent pi = PendingIntent.getBroadcast(getContext(),
68656865
REQUEST_CODE_TIMEOUT,
68666866
new Intent(ACTION_NOTIFICATION_TIMEOUT)
6867+
.setPackage(PackageManagerService.PLATFORM_PACKAGE_NAME)
68676868
.setData(new Uri.Builder().scheme(SCHEME_TIMEOUT)
68686869
.appendPath(record.getKey()).build())
68696870
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)

services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
import android.app.ActivityManager;
8686
import android.app.ActivityManagerInternal;
87+
import android.app.AlarmManager;
8788
import android.app.AppOpsManager;
8889
import android.app.AutomaticZenRule;
8990
import android.app.IActivityManager;
@@ -171,6 +172,7 @@
171172
import com.android.server.lights.LogicalLight;
172173
import com.android.server.notification.NotificationManagerService.NotificationAssistants;
173174
import com.android.server.notification.NotificationManagerService.NotificationListeners;
175+
import com.android.server.pm.PackageManagerService;
174176
import com.android.server.statusbar.StatusBarManagerInternal;
175177
import com.android.server.uri.UriGrantsManagerInternal;
176178
import com.android.server.wm.ActivityTaskManagerInternal;
@@ -282,6 +284,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
282284
NotificationHistoryManager mHistoryManager;
283285
@Mock
284286
StatsManager mStatsManager;
287+
@Mock
288+
AlarmManager mAlarmManager;
285289
NotificationRecordLoggerFake mNotificationRecordLogger = new NotificationRecordLoggerFake();
286290
private InstanceIdSequence mNotificationInstanceIdSequence = new InstanceIdSequenceFake(
287291
1 << 30);
@@ -423,6 +427,8 @@ public void setUp() throws Exception {
423427
LocalServices.addService(DeviceIdleInternal.class, deviceIdleInternal);
424428
LocalServices.removeServiceForTest(ActivityManagerInternal.class);
425429
LocalServices.addService(ActivityManagerInternal.class, activityManagerInternal);
430+
mContext.addMockSystemService(Context.ALARM_SERVICE, mAlarmManager);
431+
426432

427433
doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any());
428434

@@ -831,6 +837,26 @@ private NotificationRecord addGroupWithBubblesAndValidateAdded(boolean summaryAu
831837
return nrSummary;
832838
}
833839

840+
@Test
841+
public void testLimitTimeOutBroadcast() {
842+
NotificationChannel channel = new NotificationChannel("id", "name",
843+
NotificationManager.IMPORTANCE_HIGH);
844+
Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
845+
.setContentTitle("foo")
846+
.setSmallIcon(android.R.drawable.sym_def_app_icon)
847+
.setTimeoutAfter(1);
848+
849+
StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 8, "tag", mUid, 0,
850+
nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0);
851+
NotificationRecord r = new NotificationRecord(mContext, sbn, channel);
852+
853+
mService.scheduleTimeoutLocked(r);
854+
ArgumentCaptor<PendingIntent> captor = ArgumentCaptor.forClass(PendingIntent.class);
855+
verify(mAlarmManager).setExactAndAllowWhileIdle(anyInt(), anyLong(), captor.capture());
856+
assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME,
857+
captor.getValue().getIntent().getPackage());
858+
}
859+
834860
@Test
835861
public void testDefaultAssistant_overrideDefault() {
836862
final int userId = 0;

0 commit comments

Comments
 (0)