Fix GroupAlertTransferHelper logic on update.

Previously, all transferred alerts were removed when the inflation was
aborted. This could cancel transfers when the child was simply updating.
Now, this only occurs on remove. On update, the previous heads up/
ambient inflation flag is carried over and the helper determines if
the content should actually heads up after inflation.

Bug: 111809944
Test: runtest systemui, manual
Change-Id: Ic7c5414849e2bfa1978bb1c57ed314c4997b8ea2
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelper.java
index d38d30e..c74514e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelper.java
@@ -85,6 +85,29 @@
         Dependency.get(StatusBarStateController.class).addListener(this);
     }
 
+    /**
+     * Whether or not a notification has transferred its alert state to the notification and
+     * the notification should alert after inflating.
+     *
+     * @param entry notification to check
+     * @return true if the entry was transferred to and should inflate + alert
+     */
+    public boolean isAlertTransferPending(@NonNull Entry entry) {
+        PendingAlertInfo alertInfo = mPendingAlerts.get(entry.key);
+        return alertInfo != null && alertInfo.isStillValid();
+    }
+
+    /**
+     * Removes any alerts pending on this entry. Note that this will not stop any inflation tasks
+     * started by a transfer, so this should only be used as clean-up for when inflation is stopped
+     * and the pending alert no longer needs to happen.
+     *
+     * @param key notification key that may have info that needs to be cleaned up
+     */
+    public void cleanUpPendingAlertInfo(@NonNull String key) {
+        mPendingAlerts.remove(key);
+    }
+
     public void setHeadsUpManager(HeadsUpManager headsUpManager) {
         mHeadsUpManager = headsUpManager;
     }
@@ -181,20 +204,6 @@
     }
 
     /**
-     * Called when the entry's reinflation has been aborted.
-     *
-     * @param entry entry whose inflation has been aborted
-     */
-    public void onInflationAborted(@NonNull Entry entry) {
-        GroupAlertEntry groupAlertEntry = mGroupAlertEntries.get(
-                mGroupManager.getGroupKey(entry.notification));
-        if (groupAlertEntry == null) {
-            return;
-        }
-        mPendingAlerts.remove(entry.key);
-    }
-
-    /**
      * Called when a new notification has been posted but is not inflated yet. We use this to see
      * as early as we can if we need to abort a transfer.
      *
@@ -382,8 +391,7 @@
             @NonNull AlertingNotificationManager alertManager) {
         @InflationFlag int contentFlag = alertManager.getContentFlag();
         if (!entry.row.isInflationFlagSet(contentFlag)) {
-            // Take in the current alert manager in case it changes.
-            mPendingAlerts.put(entry.key, new PendingAlertInfo(alertManager));
+            mPendingAlerts.put(entry.key, new PendingAlertInfo(entry, alertManager));
             entry.row.updateInflationFlag(contentFlag, true /* shouldInflate */);
             entry.row.inflateViews();
             return;
@@ -409,7 +417,18 @@
      * inflation completes.
      */
     private class PendingAlertInfo {
+        /**
+         * The alert manager when the transfer is initiated.
+         */
         final AlertingNotificationManager mAlertManager;
+
+        /**
+         * The original notification when the transfer is initiated. This is used to determine if
+         * the transfer is still valid if the notification is updated.
+         */
+        final StatusBarNotification mOriginalNotification;
+        final Entry mEntry;
+
         /**
          * The notification is still pending inflation but we've decided that we no longer need
          * the content view (e.g. suppression might have changed and we decided we need to transfer
@@ -419,7 +438,9 @@
          */
         boolean mAbortOnInflation;
 
-        PendingAlertInfo(AlertingNotificationManager alertManager) {
+        PendingAlertInfo(Entry entry, AlertingNotificationManager alertManager) {
+            mOriginalNotification = entry.notification;
+            mEntry = entry;
             mAlertManager = alertManager;
         }
 
@@ -437,6 +458,15 @@
                 // Alert manager has changed
                 return false;
             }
+            if (mEntry.notification.getGroupKey() != mOriginalNotification.getGroupKey()) {
+                // Groups have changed
+                return false;
+            }
+            if (mEntry.notification.getNotification().isGroupSummary()
+                    != mOriginalNotification.getNotification().isGroupSummary()) {
+                // Notification has changed from group summary to not or vice versa
+                return false;
+            }
             return true;
         }
     }