Skip to content
Snippets Groups Projects
Commit c11765ac authored by Julia Reynolds's avatar Julia Reynolds Committed by Automerger Merge Worker
Browse files

Merge "notification: fix Alarm & PendingIntent leak" into main am: 11a67a20

parents 7d16c9c4 11a67a20
No related branches found
No related tags found
No related merge requests found
......@@ -8721,24 +8721,41 @@ public class NotificationManagerService extends SystemService {
}
}
 
private PendingIntent getNotificationTimeoutPendingIntent(NotificationRecord record,
int flags) {
flags |= PendingIntent.FLAG_IMMUTABLE;
return 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)
.putExtra(EXTRA_KEY, record.getKey()),
flags);
}
@VisibleForTesting
@GuardedBy("mNotificationLock")
void scheduleTimeoutLocked(NotificationRecord record) {
if (record.getNotification().getTimeoutAfter() > 0) {
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)
.putExtra(EXTRA_KEY, record.getKey()),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
final PendingIntent pi = getNotificationTimeoutPendingIntent(
record, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi);
}
}
 
@VisibleForTesting
@GuardedBy("mNotificationLock")
void cancelScheduledTimeoutLocked(NotificationRecord record) {
final PendingIntent pi = getNotificationTimeoutPendingIntent(
record, PendingIntent.FLAG_CANCEL_CURRENT);
if (pi != null) {
mAlarmManager.cancel(pi);
}
}
@VisibleForTesting
@GuardedBy("mNotificationLock")
/**
......@@ -9743,21 +9760,7 @@ public class NotificationManagerService extends SystemService {
int rank, int count, boolean wasPosted, String listenerName,
@ElapsedRealtimeLong long cancellationElapsedTimeMs) {
final String canceledKey = r.getKey();
// Get pending intent used to create alarm, use FLAG_NO_CREATE if PendingIntent
// does not already exist, then null will be returned.
final PendingIntent pi = PendingIntent.getBroadcast(getContext(),
REQUEST_CODE_TIMEOUT,
new Intent(ACTION_NOTIFICATION_TIMEOUT)
.setData(new Uri.Builder().scheme(SCHEME_TIMEOUT)
.appendPath(r.getKey()).build())
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND),
PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE);
// Cancel alarm corresponding to pi.
if (pi != null) {
mAlarmManager.cancel(pi);
}
cancelScheduledTimeoutLocked(r);
 
// Record caller.
recordCallerLocked(r);
......
......@@ -1214,6 +1214,11 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
verify(mAlarmManager).setExactAndAllowWhileIdle(anyInt(), anyLong(), captor.capture());
assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME,
captor.getValue().getIntent().getPackage());
mService.cancelScheduledTimeoutLocked(r);
verify(mAlarmManager).cancel(captor.capture());
assertEquals(PackageManagerService.PLATFORM_PACKAGE_NAME,
captor.getValue().getIntent().getPackage());
}
 
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment