Sharesheet - Modify header scrolling behavior

To align with UX, scroll the listview before the header when
collapsing the sharesheet to its minified state, on both flings and
drags. Fix an issue with the expansion area, whereby state is only
updated if view positions are actually updated.

Bug: 129979914
Test: Visual inspection
Change-Id: I2e32a1a3288a21bcd3ce07819f77760f10d5d929
diff --git a/core/java/com/android/internal/app/ChooserActivity.java b/core/java/com/android/internal/app/ChooserActivity.java
index ca2bc7f..26e47fd 100644
--- a/core/java/com/android/internal/app/ChooserActivity.java
+++ b/core/java/com/android/internal/app/ChooserActivity.java
@@ -3042,10 +3042,9 @@
             int yDiff = (int) ((oldy - y) * DIRECT_SHARE_EXPANSION_RATE);
 
             int prevHeight = mDirectShareCurrHeight;
-            mDirectShareCurrHeight = Math.min(mDirectShareCurrHeight + yDiff,
-                    mDirectShareMaxHeight);
-            mDirectShareCurrHeight = Math.max(mDirectShareCurrHeight, mDirectShareMinHeight);
-            yDiff = mDirectShareCurrHeight - prevHeight;
+            int newHeight = Math.min(prevHeight + yDiff, mDirectShareMaxHeight);
+            newHeight = Math.max(newHeight, mDirectShareMinHeight);
+            yDiff = newHeight - prevHeight;
 
             if (view == null || view.getChildCount() == 0 || yDiff == 0) {
                 return;
@@ -3062,7 +3061,7 @@
                     if (child.getTag() != null && child.getTag() instanceof DirectShareViewHolder) {
                         int widthSpec = MeasureSpec.makeMeasureSpec(child.getWidth(),
                                 MeasureSpec.EXACTLY);
-                        int heightSpec = MeasureSpec.makeMeasureSpec(mDirectShareCurrHeight,
+                        int heightSpec = MeasureSpec.makeMeasureSpec(newHeight,
                                 MeasureSpec.EXACTLY);
                         child.measure(widthSpec, heightSpec);
                         child.getLayoutParams().height = child.getMeasuredHeight();
@@ -3073,6 +3072,10 @@
                     }
                 }
             }
+
+            if (foundExpansion) {
+                mDirectShareCurrHeight = newHeight;
+            }
         }
     }
 
diff --git a/core/java/com/android/internal/widget/ResolverDrawerLayout.java b/core/java/com/android/internal/widget/ResolverDrawerLayout.java
index 66bbe22..6b0d85e 100644
--- a/core/java/com/android/internal/widget/ResolverDrawerLayout.java
+++ b/core/java/com/android/internal/widget/ResolverDrawerLayout.java
@@ -114,6 +114,8 @@
 
     private final Rect mTempRect = new Rect();
 
+    private AbsListView mNestedScrollingChild;
+
     private final ViewTreeObserver.OnTouchModeChangeListener mTouchModeChangeListener =
             new ViewTreeObserver.OnTouchModeChangeListener() {
                 @Override
@@ -317,6 +319,13 @@
         return mIsDragging || mOpenOnClick;
     }
 
+    private boolean isNestedChildScrolled() {
+        return mNestedScrollingChild != null
+                && mNestedScrollingChild.getChildCount() > 0
+                && (mNestedScrollingChild.getFirstVisiblePosition() > 0
+                        || mNestedScrollingChild.getChildAt(0).getTop() < 0);
+    }
+
     @Override
     public boolean onTouchEvent(MotionEvent ev) {
         final int action = ev.getActionMasked();
@@ -359,7 +368,11 @@
                 }
                 if (mIsDragging) {
                     final float dy = y - mLastTouchY;
-                    performDrag(dy);
+                    if (dy > 0 && isNestedChildScrolled()) {
+                        mNestedScrollingChild.smoothScrollBy((int) -dy, 0);
+                    } else {
+                        performDrag(dy);
+                    }
                 }
                 mLastTouchY = y;
             }
@@ -411,6 +424,9 @@
                             smoothScrollTo(mCollapsibleHeight + mUncollapsibleHeight, yvel);
                             mDismissOnScrollerFinished = true;
                         } else {
+                            if (isNestedChildScrolled()) {
+                                mNestedScrollingChild.smoothScrollToPosition(0);
+                            }
                             smoothScrollTo(yvel < 0 ? 0 : mCollapsibleHeight, yvel);
                         }
                     }
@@ -680,7 +696,13 @@
 
     @Override
     public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
-        return (nestedScrollAxes & View.SCROLL_AXIS_VERTICAL) != 0;
+        if ((nestedScrollAxes & View.SCROLL_AXIS_VERTICAL) != 0) {
+            if (child instanceof AbsListView) {
+                mNestedScrollingChild = (AbsListView) child;
+            }
+            return true;
+        }
+        return false;
     }
 
     @Override