Fix overview grid scroll problem

- This can be reproduced ocassionally without any obvious pattern, when this happen the page scroll and min/max scroll is messed up
- When this happen, onLayout happens before the first setGridProgress call from animations
- The fix is to request a relayout at the end of state transition

Bug: 174464863
Test: manual
Change-Id: I12683c49d7ed72349a4b9cb3b4d3871741e3e22e
diff --git a/quickstep/src/com/android/quickstep/views/ClearAllButton.java b/quickstep/src/com/android/quickstep/views/ClearAllButton.java
index 9af4d30..e7101cc 100644
--- a/quickstep/src/com/android/quickstep/views/ClearAllButton.java
+++ b/quickstep/src/com/android/quickstep/views/ClearAllButton.java
@@ -130,16 +130,16 @@
         applyPrimaryTranslation();
     }
 
-    public float getScrollAdjustment() {
+    public float getScrollAdjustment(boolean gridEnabled) {
         float scrollAdjustment = 0;
-        if (mGridProgress > 0) {
+        if (gridEnabled) {
             scrollAdjustment += mGridTranslationPrimary;
         }
         return scrollAdjustment;
     }
 
-    public float getOffsetAdjustment() {
-        return getScrollAdjustment();
+    public float getOffsetAdjustment(boolean gridEnabled) {
+        return getScrollAdjustment(gridEnabled);
     }
 
     /**
diff --git a/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java b/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java
index d99f707..c62f3e2 100644
--- a/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java
@@ -160,6 +160,8 @@
             reset();
         }
         setOverlayEnabled(finalState == OVERVIEW || finalState == OVERVIEW_MODAL_TASK);
+        setOverviewGridEnabled(finalState.displayOverviewTasksAsGrid(mActivity));
+        setOverviewFullscreenEnabled(finalState.getOverviewFullscreenProgress() == 1);
         setFreezeViewVisibility(false);
     }
 
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index f20ca82..bdd0a36 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -299,6 +299,8 @@
     protected boolean mDisallowScrollToClearAll;
     private boolean mOverlayEnabled;
     protected boolean mFreezeViewVisibility;
+    private boolean mOverviewGridEnabled;
+    private boolean mOverviewFullscreenEnabled;
 
     private float mAdjacentPageOffset = 0;
     private float mTaskViewsSecondaryTranslation = 0;
@@ -676,8 +678,10 @@
     }
 
     private boolean isTaskViewWithinBounds(TaskView tv, int start, int end) {
-        int taskStart = mOrientationHandler.getChildStart(tv) + (int) tv.getOffsetAdjustment();
-        int taskSize = (int) (mOrientationHandler.getMeasuredSize(tv) * tv.getSizeAdjustment());
+        int taskStart = mOrientationHandler.getChildStart(tv) + (int) tv.getOffsetAdjustment(
+                mOverviewFullscreenEnabled, mOverviewGridEnabled);
+        int taskSize = (int) (mOrientationHandler.getMeasuredSize(tv) * tv.getSizeAdjustment(
+                mOverviewFullscreenEnabled, mOverviewGridEnabled));
         int taskEnd = taskStart + taskSize;
         return (taskStart >= start && taskStart <= end) || (taskEnd >= start
                 && taskEnd <= end);
@@ -2664,9 +2668,10 @@
             View child = getChildAt(i);
             float scrollDiff = 0;
             if (child instanceof TaskView) {
-                scrollDiff = ((TaskView) child).getScrollAdjustment();
+                scrollDiff = ((TaskView) child).getScrollAdjustment(mOverviewFullscreenEnabled,
+                        mOverviewGridEnabled);
             } else if (child instanceof ClearAllButton) {
-                scrollDiff = ((ClearAllButton) child).getScrollAdjustment();
+                scrollDiff = ((ClearAllButton) child).getScrollAdjustment(mOverviewGridEnabled);
             }
 
             if (scrollDiff != 0) {
@@ -2682,9 +2687,10 @@
         int childOffset = super.getChildOffset(index);
         View child = getChildAt(index);
         if (child instanceof TaskView) {
-            childOffset += ((TaskView) child).getOffsetAdjustment();
+            childOffset += ((TaskView) child).getOffsetAdjustment(mOverviewFullscreenEnabled,
+                    mOverviewGridEnabled);
         } else if (child instanceof ClearAllButton) {
-            childOffset += ((ClearAllButton) child).getOffsetAdjustment();
+            childOffset += ((ClearAllButton) child).getOffsetAdjustment(mOverviewGridEnabled);
         }
         return childOffset;
     }
@@ -2695,7 +2701,8 @@
         if (taskView == null) {
             return super.getChildVisibleSize(index);
         }
-        return (int) (super.getChildVisibleSize(index) * taskView.getSizeAdjustment());
+        return (int) (super.getChildVisibleSize(index) * taskView.getSizeAdjustment(
+                mOverviewFullscreenEnabled, mOverviewGridEnabled));
     }
 
     @Override
@@ -2810,6 +2817,23 @@
         }
     }
 
+    public void setOverviewGridEnabled(boolean overviewGridEnabled) {
+        if (mOverviewGridEnabled != overviewGridEnabled) {
+            mOverviewGridEnabled = overviewGridEnabled;
+            // Request layout to ensure scroll position is recalculated with updated mGridProgress.
+            requestLayout();
+        }
+    }
+
+    public void setOverviewFullscreenEnabled(boolean overviewFullscreenEnabled) {
+        if (mOverviewFullscreenEnabled != overviewFullscreenEnabled) {
+            mOverviewFullscreenEnabled = overviewFullscreenEnabled;
+            // Request layout to ensure scroll position is recalculated with updated
+            // mFullscreenProgress.
+            requestLayout();
+        }
+    }
+
     /**
      * Switch the current running task view to static snapshot mode,
      * capturing the snapshot at the same time.
diff --git a/quickstep/src/com/android/quickstep/views/TaskView.java b/quickstep/src/com/android/quickstep/views/TaskView.java
index 0cf7261..88545c6 100644
--- a/quickstep/src/com/android/quickstep/views/TaskView.java
+++ b/quickstep/src/com/android/quickstep/views/TaskView.java
@@ -938,31 +938,31 @@
         mNonRtlVisibleOffset = nonRtlVisibleOffset;
     }
 
-    public float getScrollAdjustment() {
+    public float getScrollAdjustment(boolean fullscreenEnabled, boolean gridEnabled) {
         float scrollAdjustment = 0;
-        if (mFullscreenProgress > 0) {
+        if (fullscreenEnabled) {
             scrollAdjustment += mFullscreenTranslationX + mAccumulatedFullscreenTranslationX;
         }
-        if (mGridProgress > 0) {
+        if (gridEnabled) {
             scrollAdjustment += mGridTranslationX;
         }
         return scrollAdjustment;
     }
 
-    public float getOffsetAdjustment() {
-        float offsetAdjustment = getScrollAdjustment();
-        if (mGridProgress > 0) {
+    public float getOffsetAdjustment(boolean fullscreenEnabled, boolean gridEnabled) {
+        float offsetAdjustment = getScrollAdjustment(fullscreenEnabled, gridEnabled);
+        if (gridEnabled) {
             offsetAdjustment += mGridOffsetTranslationX + mNonRtlVisibleOffset;
         }
         return offsetAdjustment;
     }
 
-    public float getSizeAdjustment() {
+    public float getSizeAdjustment(boolean fullscreenEnabled, boolean gridEnabled) {
         float sizeAdjustment = 1;
-        if (mFullscreenProgress > 0) {
+        if (fullscreenEnabled) {
             sizeAdjustment *= mFullscreenScale;
         }
-        if (mGridProgress > 0) {
+        if (gridEnabled) {
             sizeAdjustment *= mGridScale;
         }
         return sizeAdjustment;