Tweaking layout to make two-task stacks to closer match design.

- In the small-stack cases, set the initial state to top-align the
  back-most task.  In addition to offsetting the position, ensure that
  the dim for the back-most task is offset by this new position.
- Fixing issue with wrong return value when update insets.
- Fixing issue where tasks disappear after rotating while scrolling the
  stack, due to the scrollering setting the stack scroll to an invalid
  scroll position in the new layout in the new orientation.

Bug: 27859654
Change-Id: Ia4aaa3fe445032493c94a8dee7f6fea33bee36ec
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java
index 77b7338..e3fe1ab 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java
@@ -404,7 +404,7 @@
      * Sets the system insets.
      */
     public boolean setSystemInsets(Rect systemInsets) {
-        boolean changed = mSystemInsets.equals(systemInsets);
+        boolean changed = !mSystemInsets.equals(systemInsets);
         mSystemInsets.set(systemInsets);
         return changed;
     }
@@ -583,11 +583,25 @@
         if (getInitialFocusState() == STATE_UNFOCUSED && mNumStackTasks > 1) {
             if (ignoreScrollToFront || (!launchState.launchedWithAltTab && !scrollToFront)) {
                 // Set the initial scroll to the predefined state (which differs from the stack)
-                float [] initialNormX = new float[] {
-                        getNormalizedXFromUnfocusedY(mSystemInsets.bottom + mInitialBottomOffset,
-                                FROM_BOTTOM),
-                        getNormalizedXFromUnfocusedY(mInitialTopOffset, FROM_TOP)
-                };
+                float [] initialNormX = null;
+                float minBottomTaskNormX = getNormalizedXFromUnfocusedY(mSystemInsets.bottom +
+                        mInitialBottomOffset, FROM_BOTTOM);
+                float maxBottomTaskNormX = getNormalizedXFromUnfocusedY(mFocusedTopPeekHeight +
+                        mTaskRect.height() - mMinMargin, FROM_TOP);
+                if (mNumStackTasks <= 2) {
+                    // For small stacks, position the tasks so that they are top aligned to under
+                    // the action button, but ensure that it is at least a certain offset from the
+                    // bottom of the stack
+                    initialNormX = new float[] {
+                            Math.min(maxBottomTaskNormX, minBottomTaskNormX),
+                            getNormalizedXFromUnfocusedY(mFocusedTopPeekHeight, FROM_TOP)
+                    };
+                } else {
+                    initialNormX = new float[] {
+                            minBottomTaskNormX,
+                            getNormalizedXFromUnfocusedY(mInitialTopOffset, FROM_TOP)
+                    };
+                }
 
                 mUnfocusedRange.offset(0f);
                 List<Task> tasks = stack.getStackTasks();
@@ -881,14 +895,7 @@
             TaskViewTransform frontTransform, boolean ignoreSingleTaskCase, boolean forceUpdate) {
         SystemServicesProxy ssp = Recents.getSystemServices();
 
-        // Compute the focused and unfocused offset
-        float boundedStackScroll = Utilities.clamp(stackScroll, mMinScrollP, mMaxScrollP);
-        mUnfocusedRange.offset(boundedStackScroll);
-        mFocusedRange.offset(boundedStackScroll);
-        float boundedScrollUnfocusedRangeX = mUnfocusedRange.getNormalizedX(taskProgress);
-        float boundedScrollFocusedRangeX = mFocusedRange.getNormalizedX(taskProgress);
-        float boundedScrollUnfocusedNonOverrideRangeX =
-                mUnfocusedRange.getNormalizedX(nonOverrideTaskProgress);
+        // Ensure that the task is in range
         mUnfocusedRange.offset(stackScroll);
         mFocusedRange.offset(stackScroll);
         boolean unfocusedVisible = mUnfocusedRange.isInRange(taskProgress);
@@ -900,9 +907,30 @@
             return;
         }
 
+        // Map the absolute task progress to the normalized x at the stack scroll.  We use this to
+        // calculate positions along the curve.
+        mUnfocusedRange.offset(stackScroll);
+        mFocusedRange.offset(stackScroll);
         float unfocusedRangeX = mUnfocusedRange.getNormalizedX(taskProgress);
         float focusedRangeX = mFocusedRange.getNormalizedX(taskProgress);
 
+        // Map the absolute task progress to the normalized x at the bounded stack scroll.  We use
+        // this to calculate bounded properties, like translationZ and outline alpha.
+        float boundedStackScroll = Utilities.clamp(stackScroll, mMinScrollP, mMaxScrollP);
+        mUnfocusedRange.offset(boundedStackScroll);
+        mFocusedRange.offset(boundedStackScroll);
+        float boundedScrollUnfocusedRangeX = mUnfocusedRange.getNormalizedX(taskProgress);
+        float boundedScrollUnfocusedNonOverrideRangeX =
+                mUnfocusedRange.getNormalizedX(nonOverrideTaskProgress);
+
+        // Map the absolute task progress to the normalized x at the upper bounded stack scroll.
+        // We use this to calculate the dim, which is bounded only on one end.
+        float lowerBoundedStackScroll = Utilities.clamp(stackScroll, -Float.MAX_VALUE, mMaxScrollP);
+        mUnfocusedRange.offset(lowerBoundedStackScroll);
+        mFocusedRange.offset(lowerBoundedStackScroll);
+        float lowerBoundedUnfocusedRangeX = mUnfocusedRange.getNormalizedX(taskProgress);
+        float lowerBoundedFocusedRangeX = mFocusedRange.getNormalizedX(taskProgress);
+
         int x = (mStackRect.width() - mTaskRect.width()) / 2;
         int y;
         float z;
@@ -917,7 +945,8 @@
             y = centerYOffset + getYForDeltaP(tmpP, 0);
             z = mMaxTranslationZ;
             dimAlpha = 0f;
-            viewOutlineAlpha = (OUTLINE_ALPHA_MIN_VALUE + OUTLINE_ALPHA_MAX_VALUE) / 2f;
+            viewOutlineAlpha = OUTLINE_ALPHA_MIN_VALUE +
+                    (OUTLINE_ALPHA_MAX_VALUE - OUTLINE_ALPHA_MIN_VALUE) / 2f;
 
         } else {
             // Otherwise, update the task to the stack layout
@@ -926,9 +955,22 @@
             int focusedY = (int) ((1f - mFocusedCurveInterpolator.getInterpolation(
                     focusedRangeX)) * mStackRect.height());
             float unfocusedDim = mUnfocusedDimCurveInterpolator.getInterpolation(
-                    boundedScrollUnfocusedRangeX);
+                    lowerBoundedUnfocusedRangeX);
             float focusedDim = mFocusedDimCurveInterpolator.getInterpolation(
-                    boundedScrollFocusedRangeX);
+                    lowerBoundedFocusedRangeX);
+
+            // Special case, because we override the initial task positions differently for small
+            // stacks, we clamp the dim to 0 in the initial position, and then only modulate the
+            // dim when the task is scrolled back towards the top of the screen
+            if (mNumStackTasks <= 2 && nonOverrideTaskProgress == 0f) {
+                if (boundedScrollUnfocusedRangeX >= 0.5f) {
+                    unfocusedDim = 0f;
+                } else {
+                    float offset = mUnfocusedDimCurveInterpolator.getInterpolation(0.5f);
+                    unfocusedDim -= offset;
+                    unfocusedDim *= MAX_DIM / (MAX_DIM - offset);
+                }
+            }
 
             y = (mStackRect.top - mTaskRect.top) +
                     (int) Utilities.mapRange(focusState, unfocusedY, focusedY);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
index 0d9f77c..231360e 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
@@ -1996,6 +1996,10 @@
         if (event.fromDeviceOrientationChange) {
             mDisplayOrientation = Utilities.getAppConfiguration(mContext).orientation;
             mDisplayRect = Recents.getSystemServices().getDisplayRect();
+
+            // Always stop the scroller, otherwise, we may continue setting the stack scroll to the
+            // wrong bounds in the new layout
+            mStackScroller.stopScroller();
         }
         reloadOnConfigurationChange();