From ebe03fcb4b287e107faea69f653a8761a92e5af1 Mon Sep 17 00:00:00 2001 From: Henrik Baard Date: Fri, 30 Apr 2021 19:19:58 +0200 Subject: Cancel NotificationTimeout alarms when canceling notification If alarms with long timeouts are created, there is a risk of a crash in system_server because of alarm creation limit is reached: java.lang.IllegalStateException: Maximum limit of concurrent alarms 500 reached for uid: 1000, callingPackage: android To avoid this cancel alarms if Notifications are canceled. Suggested-by: Snild Dolkow Bug: 186456631 Change-Id: I63343b9450fc817ec6a2f46329de346320463c46 --- .../server/notification/NotificationManagerService.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index c01a1151ee67..29c3dd91c6a3 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -6850,7 +6850,7 @@ public class NotificationManagerService extends SystemService { .appendPath(record.getKey()).build()) .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) .putExtra(EXTRA_KEY, record.getKey()), - PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, mSystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); @@ -7713,6 +7713,21 @@ public class NotificationManagerService extends SystemService { int rank, int count, boolean wasPosted, String listenerName) { 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); + } + // Record caller. recordCallerLocked(r); -- cgit v1.2.3-59-g8ed1b