Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11a67a20 authored by Julia Reynolds's avatar Julia Reynolds Committed by Gerrit Code Review
Browse files

Merge "notification: fix Alarm & PendingIntent leak" into main

parents d529e4c7 1f7f5d2b
Loading
Loading
Loading
Loading
+27 −24
Original line number Diff line number Diff line
@@ -8237,11 +8237,10 @@ public class NotificationManagerService extends SystemService {
        }
    }
    @VisibleForTesting
    @GuardedBy("mNotificationLock")
    void scheduleTimeoutLocked(NotificationRecord record) {
        if (record.getNotification().getTimeoutAfter() > 0) {
            final PendingIntent pi = PendingIntent.getBroadcast(getContext(),
    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)
@@ -8249,12 +8248,30 @@ 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_IMMUTABLE);
                flags);
    }
    @VisibleForTesting
    @GuardedBy("mNotificationLock")
    void scheduleTimeoutLocked(NotificationRecord record) {
        if (record.getNotification().getTimeoutAfter() > 0) {
            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")
    /**
@@ -9249,21 +9266,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);
+5 −0
Original line number Diff line number Diff line
@@ -1104,6 +1104,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