Media - Handle cleanup of pending notification entries

It is possible for notifications to be canceled prior to inflation,
which means there will not be a callback when the notification is
removed. Utitlize the NotifCollectionListener to get this event.

Fixes: 166315453
Test: manual, using RCNs to generate a race condition as they
create/cancel notifications immediately

Change-Id: I56d3ab9ef46d86c8623f9bac9152e8a2b8341c01
(cherry picked from commit a332522abb7c91f4d1501997532f3f5173096e80)
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java
index 739d30c..8cd82cc 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java
@@ -21,6 +21,7 @@
 import static com.android.systemui.statusbar.phone.StatusBar.SHOW_LOCKSCREEN_MEDIA_ARTWORK;
 
 import android.annotation.MainThread;
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.Notification;
 import android.content.Context;
@@ -57,6 +58,7 @@
 import com.android.systemui.statusbar.notification.NotificationEntryListener;
 import com.android.systemui.statusbar.notification.NotificationEntryManager;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
+import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
 import com.android.systemui.statusbar.phone.BiometricUnlockController;
 import com.android.systemui.statusbar.phone.KeyguardBypassController;
 import com.android.systemui.statusbar.phone.LockscreenWallpaper;
@@ -234,8 +236,17 @@
                     NotificationVisibility visibility,
                     boolean removedByUser,
                     int reason) {
-                onNotificationRemoved(entry.getKey());
-                mediaDataManager.onNotificationRemoved(entry.getKey());
+                removeEntry(entry);
+            }
+        });
+
+        // Pending entries are never inflated, and will never generate a call to onEntryRemoved().
+        // This can happen when notifications are added and canceled before inflation. Add this
+        // separate listener for cleanup, since media inflation occurs onPendingEntryAdded().
+        notificationEntryManager.addCollectionListener(new NotifCollectionListener() {
+            @Override
+            public void onEntryCleanUp(@NonNull NotificationEntry entry) {
+                removeEntry(entry);
             }
         });
 
@@ -248,6 +259,11 @@
                 mPropertiesChangedListener);
     }
 
+    private void removeEntry(NotificationEntry entry) {
+        onNotificationRemoved(entry.getKey());
+        mMediaDataManager.onNotificationRemoved(entry.getKey());
+    }
+
     /**
      * Check if a state should be considered actively playing
      * @param state a PlaybackState