diff options
| author | 2024-03-05 19:22:08 +0000 | |
|---|---|---|
| committer | 2024-03-05 19:22:08 +0000 | |
| commit | a53d1af34538a95fdcad9c2967d68af5972e6bb4 (patch) | |
| tree | 1c90fd248d28b3b9c01bb65aa3b7fc6c9961c700 | |
| parent | 1cfa8a9ee615ca32fd77a96148357e178138e8f6 (diff) | |
| parent | 8b93b36074d8527db506dd59225a60eee9ec8506 (diff) | |
Merge "Call setFrameContentVelocity in ScrollView and HorizontalScrollView." into main
6 files changed, 420 insertions, 196 deletions
diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java index 55b2251ac196..0b99df323b09 100644 --- a/core/java/android/widget/HorizontalScrollView.java +++ b/core/java/android/widget/HorizontalScrollView.java @@ -16,6 +16,8 @@ package android.widget; +import static android.view.flags.Flags.viewVelocityApi; + import android.annotation.ColorInt; import android.annotation.NonNull; import android.compat.annotation.UnsupportedAppUsage; @@ -1488,6 +1490,11 @@ public class HorizontalScrollView extends FrameLayout { if (!awakenScrollBars()) { postInvalidateOnAnimation(); } + + // For variable refresh rate project to track the current velocity of this View + if (viewVelocityApi()) { + setFrameContentVelocity(Math.abs(mScroller.getCurrVelocity())); + } } } @@ -1810,6 +1817,11 @@ public class HorizontalScrollView extends FrameLayout { mScroller.fling(mScrollX, mScrollY, velocityX, 0, 0, maxScroll, 0, 0, width / 2, 0); + // For variable refresh rate project to track the current velocity of this View + if (viewVelocityApi()) { + setFrameContentVelocity(Math.abs(mScroller.getCurrVelocity())); + } + final boolean movingRight = velocityX > 0; View currentFocused = findFocus(); diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java index a1ebde76e98e..42c2d80ea322 100644 --- a/core/java/android/widget/ScrollView.java +++ b/core/java/android/widget/ScrollView.java @@ -16,6 +16,8 @@ package android.widget; +import static android.view.flags.Flags.viewVelocityApi; + import android.annotation.ColorInt; import android.annotation.NonNull; import android.compat.annotation.UnsupportedAppUsage; @@ -726,6 +728,12 @@ public class ScrollView extends FrameLayout { * isFinished() is correct. */ mScroller.computeScrollOffset(); + + // For variable refresh rate project to track the current velocity of this View + if (viewVelocityApi()) { + setFrameContentVelocity(Math.abs(mScroller.getCurrVelocity())); + } + mIsBeingDragged = !mScroller.isFinished() || !mEdgeGlowBottom.isFinished() || !mEdgeGlowTop.isFinished(); // Catch the edge effect if it is active. @@ -1573,6 +1581,11 @@ public class ScrollView extends FrameLayout { // Keep on drawing until the animation has finished. postInvalidateOnAnimation(); } + + // For variable refresh rate project to track the current velocity of this View + if (viewVelocityApi()) { + setFrameContentVelocity(Math.abs(mScroller.getCurrVelocity())); + } } else { if (mFlingStrictSpan != null) { mFlingStrictSpan.finish(); @@ -1884,6 +1897,10 @@ public class ScrollView extends FrameLayout { mScroller.fling(mScrollX, mScrollY, 0, velocityY, 0, 0, 0, Math.max(0, bottom - height), 0, height/2); + // For variable refresh rate project to track the current velocity of this View + if (viewVelocityApi()) { + setFrameContentVelocity(Math.abs(mScroller.getCurrVelocity())); + } if (mFlingStrictSpan == null) { mFlingStrictSpan = StrictMode.enterCriticalSpan("ScrollView-fling"); } diff --git a/core/tests/coretests/res/layout/activity_horizontal_scroll_view.xml b/core/tests/coretests/res/layout/activity_horizontal_scroll_view.xml index 866e1a95c3f5..502921263462 100644 --- a/core/tests/coretests/res/layout/activity_horizontal_scroll_view.xml +++ b/core/tests/coretests/res/layout/activity_horizontal_scroll_view.xml @@ -14,105 +14,150 @@ ~ limitations under the License --> -<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" - android:id="@+id/horizontal_scroll_view"> + android:orientation="vertical"> - <LinearLayout + <HorizontalScrollView android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal"> - - <View - android:background="#F00" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#880" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#0F0" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#088" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#00F" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#808" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#F00" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#880" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#0F0" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#088" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#00F" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#808" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#F00" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#880" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#0F0" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#088" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#00F" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#808" - android:layout_width="100dp" - android:layout_height="100dp" /> - - </LinearLayout> -</HorizontalScrollView> + android:layout_height="match_parent" + android:id="@+id/horizontal_scroll_view"> + + <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="horizontal"> + + <View + android:background="#F00" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#880" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#0F0" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#088" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#00F" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#808" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#F00" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#880" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#0F0" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#088" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#00F" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#808" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#F00" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#880" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#0F0" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#088" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#00F" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#808" + android:layout_width="100dp" + android:layout_height="100dp" /> + + </LinearLayout> + </HorizontalScrollView> + + <view + class="android.widget.HorizontalScrollViewFunctionalTest$MyHorizontalScrollView" + android:id="@+id/my_horizontal_scroll_view" + android:layout_width="90dp" + android:layout_height="90dp" + android:background="#FFF" + android:defaultFocusHighlightEnabled="false"> + <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="vertical"> + <View + android:background="#00F" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#0FF" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#0F0" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#FF0" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#F00" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#F0F" + android:layout_width="90dp" + android:layout_height="50dp"/> + </LinearLayout> + </view> +</LinearLayout>
\ No newline at end of file diff --git a/core/tests/coretests/res/layout/activity_scroll_view.xml b/core/tests/coretests/res/layout/activity_scroll_view.xml index 61fabf8ee437..db8cd026e71a 100644 --- a/core/tests/coretests/res/layout/activity_scroll_view.xml +++ b/core/tests/coretests/res/layout/activity_scroll_view.xml @@ -14,105 +14,150 @@ ~ limitations under the License --> -<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" - android:id="@+id/scroll_view"> + android:orientation="vertical"> - <LinearLayout + <ScrollView android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="vertical"> - - <View - android:background="#F00" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#880" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#0F0" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#088" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#00F" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#808" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#F00" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#880" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#0F0" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#088" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#00F" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#808" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#F00" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#880" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#0F0" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#088" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#00F" - android:layout_width="100dp" - android:layout_height="100dp" /> - - <View - android:background="#808" - android:layout_width="100dp" - android:layout_height="100dp" /> - - </LinearLayout> -</ScrollView> + android:layout_height="match_parent" + android:id="@+id/scroll_view"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + + <View + android:background="#F00" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#880" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#0F0" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#088" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#00F" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#808" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#F00" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#880" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#0F0" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#088" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#00F" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#808" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#F00" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#880" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#0F0" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#088" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#00F" + android:layout_width="100dp" + android:layout_height="100dp" /> + + <View + android:background="#808" + android:layout_width="100dp" + android:layout_height="100dp" /> + + </LinearLayout> + </ScrollView> + + <view + class="android.widget.ScrollViewFunctionalTest$MyScrollView" + android:id="@+id/my_scroll_view" + android:layout_width="90dp" + android:layout_height="90dp" + android:background="#FFF" + android:defaultFocusHighlightEnabled="false"> + <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="vertical"> + <View + android:background="#00F" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#0FF" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#0F0" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#FF0" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#F00" + android:layout_width="90dp" + android:layout_height="50dp"/> + <View + android:background="#F0F" + android:layout_width="90dp" + android:layout_height="50dp"/> + </LinearLayout> + </view> +</LinearLayout> diff --git a/core/tests/coretests/src/android/widget/HorizontalScrollViewFunctionalTest.java b/core/tests/coretests/src/android/widget/HorizontalScrollViewFunctionalTest.java index df212ebe1744..cd38bd68a26b 100644 --- a/core/tests/coretests/src/android/widget/HorizontalScrollViewFunctionalTest.java +++ b/core/tests/coretests/src/android/widget/HorizontalScrollViewFunctionalTest.java @@ -16,11 +16,17 @@ package android.widget; +import static android.view.flags.Flags.FLAG_VIEW_VELOCITY_API; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import android.content.Context; import android.platform.test.annotations.Presubmit; +import android.platform.test.annotations.RequiresFlagsEnabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.DeviceFlagsValueProvider; +import android.util.AttributeSet; import android.util.PollingCheck; import androidx.test.filters.MediumTest; @@ -43,14 +49,21 @@ import java.util.concurrent.TimeUnit; public class HorizontalScrollViewFunctionalTest { private HorizontalScrollViewActivity mActivity; private HorizontalScrollView mHorizontalScrollView; + private MyHorizontalScrollView mMyHorizontalScrollView; @Rule public ActivityTestRule<HorizontalScrollViewActivity> mActivityRule = new ActivityTestRule<>( HorizontalScrollViewActivity.class); + @Rule + public final CheckFlagsRule mCheckFlagsRule = + DeviceFlagsValueProvider.createCheckFlagsRule(); + @Before public void setUp() throws Exception { mActivity = mActivityRule.getActivity(); mHorizontalScrollView = mActivity.findViewById(R.id.horizontal_scroll_view); + mMyHorizontalScrollView = + (MyHorizontalScrollView) mActivity.findViewById(R.id.my_horizontal_scroll_view); } @Test @@ -79,6 +92,22 @@ public class HorizontalScrollViewFunctionalTest { assertEquals(maxScroll, mHorizontalScrollView.getScrollX()); } + @Test + @RequiresFlagsEnabled(FLAG_VIEW_VELOCITY_API) + public void testSetVelocity() throws Throwable { + mActivityRule.runOnUiThread(() -> { + mMyHorizontalScrollView.setFrameContentVelocity(0); + }); + // set setFrameContentVelocity shouldn't do anything. + assertEquals(mMyHorizontalScrollView.isSetVelocityCalled, false); + + mActivityRule.runOnUiThread(() -> { + mMyHorizontalScrollView.fling(100); + }); + // set setFrameContentVelocity should be called when fling. + assertEquals(mMyHorizontalScrollView.isSetVelocityCalled, true); + } + static class WatchedEdgeEffect extends EdgeEffect { public CountDownLatch onAbsorbLatch = new CountDownLatch(1); @@ -92,5 +121,29 @@ public class HorizontalScrollViewFunctionalTest { onAbsorbLatch.countDown(); } } + + public static class MyHorizontalScrollView extends ScrollView { + + public boolean isSetVelocityCalled; + + public MyHorizontalScrollView(Context context) { + super(context); + } + + public MyHorizontalScrollView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + public void setFrameContentVelocity(float pixelsPerSecond) { + if (pixelsPerSecond != 0) { + isSetVelocityCalled = true; + } + } + } } diff --git a/core/tests/coretests/src/android/widget/ScrollViewFunctionalTest.java b/core/tests/coretests/src/android/widget/ScrollViewFunctionalTest.java index 109c8080de94..a60b2a13e2eb 100644 --- a/core/tests/coretests/src/android/widget/ScrollViewFunctionalTest.java +++ b/core/tests/coretests/src/android/widget/ScrollViewFunctionalTest.java @@ -16,11 +16,17 @@ package android.widget; +import static android.view.flags.Flags.FLAG_VIEW_VELOCITY_API; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import android.content.Context; import android.platform.test.annotations.Presubmit; +import android.platform.test.annotations.RequiresFlagsEnabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.DeviceFlagsValueProvider; +import android.util.AttributeSet; import android.util.PollingCheck; import androidx.test.filters.MediumTest; @@ -43,14 +49,20 @@ import java.util.concurrent.TimeUnit; public class ScrollViewFunctionalTest { private ScrollViewActivity mActivity; private ScrollView mScrollView; + private MyScrollView mMyScrollView; @Rule public ActivityTestRule<ScrollViewActivity> mActivityRule = new ActivityTestRule<>( ScrollViewActivity.class); + @Rule + public final CheckFlagsRule mCheckFlagsRule = + DeviceFlagsValueProvider.createCheckFlagsRule(); + @Before public void setUp() throws Exception { mActivity = mActivityRule.getActivity(); mScrollView = mActivity.findViewById(R.id.scroll_view); + mMyScrollView = (MyScrollView) mActivity.findViewById(R.id.my_scroll_view); } @Test @@ -79,6 +91,22 @@ public class ScrollViewFunctionalTest { assertEquals(maxScroll, mScrollView.getScrollY()); } + @Test + @RequiresFlagsEnabled(FLAG_VIEW_VELOCITY_API) + public void testSetVelocity() throws Throwable { + mActivityRule.runOnUiThread(() -> { + mMyScrollView.setFrameContentVelocity(0); + }); + // set setFrameContentVelocity shouldn't do anything. + assertEquals(mMyScrollView.isSetVelocityCalled, false); + + mActivityRule.runOnUiThread(() -> { + mMyScrollView.fling(100); + }); + // set setFrameContentVelocity should be called when fling. + assertEquals(mMyScrollView.isSetVelocityCalled, true); + } + static class WatchedEdgeEffect extends EdgeEffect { public CountDownLatch onAbsorbLatch = new CountDownLatch(1); @@ -92,5 +120,29 @@ public class ScrollViewFunctionalTest { onAbsorbLatch.countDown(); } } + + public static class MyScrollView extends ScrollView { + + public boolean isSetVelocityCalled; + + public MyScrollView(Context context) { + super(context); + } + + public MyScrollView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public MyScrollView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + public void setFrameContentVelocity(float pixelsPerSecond) { + if (pixelsPerSecond != 0) { + isSetVelocityCalled = true; + } + } + } } |