Add GroupedTaskView to recents view only if needed

Fixes: 197712963
Test: Got into the state of the exception once, but
didn't seem to repro after change.

Change-Id: Ifebf198ba0d1d0cca83935a700711fb22a8a276a
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index 5974677..629d59c 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -1278,7 +1278,8 @@
             while (getTaskViewCount() > requiredTaskViewCount) {
                 removeView(getChildAt(getChildCount() - 1));
             }
-            while (requiredGroupTaskViews > 0) {
+            int groupedTaskViewCount = getGroupedTaskViewCount();
+            while (requiredGroupTaskViews > groupedTaskViewCount) {
                 // Add to front of list
                 addView(getTaskViewFromPool(true), 0);
                 requiredGroupTaskViews--;
@@ -1405,6 +1406,16 @@
         return taskViewCount;
     }
 
+    public int getGroupedTaskViewCount() {
+        int groupViewCount = 0;
+        for (int i = 0; i < getChildCount(); i++) {
+            if (getChildAt(i) instanceof GroupedTaskView) {
+                groupViewCount++;
+            }
+        }
+        return groupViewCount;
+    }
+
     protected void onTaskStackUpdated() {
         // Lazily update the empty message only when the task stack is reapplied
         updateEmptyMessage();