Fixed a bug where notifications could become orphaned

When a child got set not be a heads up after it was already
removed (When for example it was clicked on shortly after
it got posted) then it got orphaned leading to bad bugs
like the next notification not being visible. This
could happen often with WhatsApp who set the head up flags
on the summary instead of the children.

Change-Id: I52c8f5d9bc50080e4ece530255236d36f998372e
Fixes: 28565942
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
index 9778a3a..48fec7d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
@@ -24,9 +24,12 @@
 import com.android.systemui.statusbar.StatusBarState;
 import com.android.systemui.statusbar.policy.HeadsUpManager;
 
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.Objects;
 
 /**
@@ -69,6 +72,7 @@
 
     public void onEntryRemoved(NotificationData.Entry removed) {
         onEntryRemovedInternal(removed, removed.notification);
+        mIsolatedEntries.remove(removed.key);
     }
 
     /**
@@ -425,6 +429,19 @@
         mHeadsUpManager = headsUpManager;
     }
 
+    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("GroupManager state:");
+        pw.println("  number of groups: " +  mGroupMap.size());
+        for (Map.Entry<String, NotificationGroup>  entry : mGroupMap.entrySet()) {
+            pw.println("\n    key: " + entry.getKey()); pw.println(entry.getValue());
+        }
+        pw.println("\n    isolated entries: " +  mIsolatedEntries.size());
+        for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
+            pw.print("      "); pw.print(entry.getKey());
+            pw.print(", "); pw.println(entry.getValue());
+        }
+    }
+
     public static class NotificationGroup {
         public final HashSet<NotificationData.Entry> children = new HashSet<>();
         public NotificationData.Entry summary;
@@ -433,6 +450,16 @@
          * Is this notification group suppressed, i.e its summary is hidden
          */
         public boolean suppressed;
+
+        @Override
+        public String toString() {
+            String result = "    summary:\n      " + summary.notification;
+            result += "\n    children size: " + children.size();
+            for (NotificationData.Entry child : children) {
+                result += "\n      " + child.notification;
+            }
+            return result;
+        }
     }
 
     public interface OnGroupChangeListener {