Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 18 | |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 19 | import static com.android.app.animation.Interpolators.SCROLL; |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled; |
| 21 | import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType; |
Jagrut Desai | 311d519 | 2023-09-13 10:18:02 -0700 | [diff] [blame] | 22 | import static com.android.launcher3.testing.shared.TestProtocol.SCROLL_FINISHED_MESSAGE; |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 23 | import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR; |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 24 | import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY; |
| 25 | import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO; |
| 26 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 27 | import android.animation.LayoutTransition; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 28 | import android.annotation.SuppressLint; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 30 | import android.content.res.Configuration; |
| 31 | import android.content.res.Resources; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 32 | import android.content.res.TypedArray; |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 33 | import android.graphics.Canvas; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 34 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 36 | import android.provider.Settings; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 37 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 38 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 39 | import android.view.InputDevice; |
| 40 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 41 | import android.view.MotionEvent; |
| 42 | import android.view.VelocityTracker; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 45 | import android.view.ViewDebug; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 46 | import android.view.ViewGroup; |
| 47 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityEvent; |
| 49 | import android.view.accessibility.AccessibilityNodeInfo; |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 50 | import android.widget.OverScroller; |
Vadim Tryshev | e6bbefb | 2018-04-03 13:38:06 -0700 | [diff] [blame] | 51 | import android.widget.ScrollView; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 52 | |
Tony Wickham | 62117d7 | 2020-03-26 16:56:26 -0700 | [diff] [blame] | 53 | import androidx.annotation.Nullable; |
| 54 | |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 55 | import com.android.launcher3.compat.AccessibilityManagerCompat; |
| 56 | import com.android.launcher3.config.FeatureFlags; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 57 | import com.android.launcher3.pageindicators.PageIndicator; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 58 | import com.android.launcher3.touch.PagedOrientationHandler; |
| 59 | import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds; |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 60 | import com.android.launcher3.util.EdgeEffectCompat; |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 61 | import com.android.launcher3.util.IntSet; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 62 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 63 | import com.android.launcher3.views.ActivityContext; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 64 | |
| 65 | import java.util.ArrayList; |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 66 | import java.util.function.Consumer; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 67 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 68 | /** |
| 69 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 70 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 71 | */ |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 72 | public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 73 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 74 | private static final boolean DEBUG = false; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 75 | public static final boolean DEBUG_FAILED_QUICKSWITCH = false; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 76 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 77 | public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1; |
Sunny Goyal | a7a5bf3 | 2020-01-05 15:35:29 +0530 | [diff] [blame] | 78 | public static final int INVALID_PAGE = -1; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 79 | protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 80 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 81 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 82 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 83 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 84 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 85 | private static final float MAX_SCROLL_PROGRESS = 1.0f; |
| 86 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 87 | private boolean mFreeScroll = false; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 88 | |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 89 | private int mFlingThresholdVelocity; |
| 90 | private int mEasyFlingThresholdVelocity; |
| 91 | private int mMinFlingVelocity; |
| 92 | private int mMinSnapVelocity; |
| 93 | private int mPageSnapAnimationDuration; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 94 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 95 | protected boolean mFirstLayout = true; |
| 96 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 97 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 98 | protected int mCurrentPage; |
Alex Chau | ba61b98 | 2021-06-15 16:29:19 +0100 | [diff] [blame] | 99 | // Difference between current scroll position and mCurrentPage's page scroll. Used to maintain |
| 100 | // relative scroll position unchanged in updateCurrentPageScroll. Cleared when snapping to a |
| 101 | // page. |
| 102 | protected int mCurrentPageScrollDiff; |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 103 | // The current page the PagedView is scrolling over on it's way to the destination page. |
| 104 | protected int mCurrentScrollOverPage; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 105 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 106 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 107 | protected int mNextPage = INVALID_PAGE; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 108 | protected int mMaxScroll; |
| 109 | protected int mMinScroll; |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 110 | protected OverScroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 111 | private VelocityTracker mVelocityTracker; |
Sunny Goyal | e15e2a8 | 2017-12-15 13:05:42 -0800 | [diff] [blame] | 112 | protected int mPageSpacing = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 113 | |
| 114 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 115 | private float mDownMotionY; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 116 | private float mDownMotionPrimary; |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 117 | private int mLastMotion; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 118 | private float mTotalMotion; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 119 | // Used in special cases where the fling checks can be relaxed for an intentional gesture |
| 120 | private boolean mAllowEasyFling; |
Sunny Goyal | acd17df | 2020-04-03 15:58:45 -0700 | [diff] [blame] | 121 | protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 122 | |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 123 | private final ArrayList<Runnable> mOnPageScrollsInitializedCallbacks = new ArrayList<>(); |
| 124 | |
| 125 | // We should always check pageScrollsInitialized() is true when using mPageScrolls. |
| 126 | @Nullable protected int[] mPageScrolls = null; |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 127 | private boolean mIsBeingDragged; |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 128 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 129 | // The amount of movement to begin scrolling |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 130 | protected int mTouchSlop; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 131 | // The amount of movement to begin paging |
| 132 | protected int mPageSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 133 | private int mMaximumVelocity; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 134 | protected boolean mAllowOverScroll = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 135 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 136 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 137 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 138 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 139 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 140 | protected boolean mIsPageInTransition = false; |
Tony Wickham | 62117d7 | 2020-03-26 16:56:26 -0700 | [diff] [blame] | 141 | private Runnable mOnPageTransitionEndCallback; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 142 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 143 | // Page Indicator |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 144 | @Thunk int mPageIndicatorViewId; |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 145 | protected T mPageIndicator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 146 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 147 | protected final Rect mInsets = new Rect(); |
Tony Wickham | dfb5cc9 | 2018-02-26 16:41:57 -0800 | [diff] [blame] | 148 | protected boolean mIsRtl; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 149 | |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 150 | // Similar to the platform implementation of isLayoutValid(); |
| 151 | protected boolean mIsLayoutValid; |
| 152 | |
Vadim Tryshev | 528b9e0 | 2018-05-24 18:22:03 -0700 | [diff] [blame] | 153 | private int[] mTmpIntPair = new int[2]; |
| 154 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 155 | protected EdgeEffectCompat mEdgeGlowLeft; |
| 156 | protected EdgeEffectCompat mEdgeGlowRight; |
| 157 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 158 | public PagedView(Context context) { |
| 159 | this(context, null); |
| 160 | } |
| 161 | |
| 162 | public PagedView(Context context, AttributeSet attrs) { |
| 163 | this(context, attrs, 0); |
| 164 | } |
| 165 | |
| 166 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 167 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 168 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 169 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 170 | R.styleable.PagedView, defStyle, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 171 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 172 | a.recycle(); |
| 173 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 174 | setHapticFeedbackEnabled(false); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 175 | mIsRtl = Utilities.isRtl(getResources()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 176 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 177 | mScroller = new OverScroller(context, SCROLL); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 178 | mCurrentPage = 0; |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 179 | mCurrentScrollOverPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 180 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 181 | final ViewConfiguration configuration = ViewConfiguration.get(context); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 182 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 183 | mPageSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 184 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 185 | |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 186 | updateVelocityValues(); |
Sunny Goyal | a9f5546 | 2018-03-25 18:15:15 -0700 | [diff] [blame] | 187 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 188 | initEdgeEffect(); |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 189 | setDefaultFocusHighlightEnabled(false); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 190 | setWillNotDraw(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 193 | protected void initEdgeEffect() { |
| 194 | mEdgeGlowLeft = new EdgeEffectCompat(getContext()); |
| 195 | mEdgeGlowRight = new EdgeEffectCompat(getContext()); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 198 | public void initParentViews(View parent) { |
| 199 | if (mPageIndicatorViewId > -1) { |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 200 | mPageIndicator = parent.findViewById(mPageIndicatorViewId); |
Tony Wickham | bab101c | 2021-09-20 15:12:44 -0700 | [diff] [blame] | 201 | mPageIndicator.setMarkersCount(getChildCount() / getPanelCount()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 205 | public T getPageIndicator() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 206 | return mPageIndicator; |
| 207 | } |
| 208 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 209 | /** |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 210 | * Returns the index of the currently displayed page. When in free scroll mode, this is the page |
| 211 | * that the user was on before entering free scroll mode (e.g. the home screen page they |
Alex Chau | b794ea6 | 2021-02-03 18:00:30 +0000 | [diff] [blame] | 212 | * long-pressed on to enter the overview). Try using {@link #getDestinationPage()} |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 213 | * to get the page the user is currently scrolling over. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 214 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 215 | public int getCurrentPage() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 216 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 217 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 218 | |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Returns the index of page to be shown immediately afterwards. |
| 221 | */ |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 222 | public int getNextPage() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 223 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 224 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 225 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 226 | public int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 227 | return getChildCount(); |
| 228 | } |
| 229 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 230 | public View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 231 | return getChildAt(index); |
| 232 | } |
| 233 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 234 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 235 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 236 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 237 | * the previous tab page. |
| 238 | */ |
| 239 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 240 | // If the current page is invalid, just reset the scroll position to zero |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 241 | int newPosition = 0; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 242 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Alex Chau | ba61b98 | 2021-06-15 16:29:19 +0100 | [diff] [blame] | 243 | newPosition = getScrollForPage(mCurrentPage) + mCurrentPageScrollDiff; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 244 | } |
Alex Chau | 6499e83 | 2021-11-19 14:35:46 +0000 | [diff] [blame] | 245 | mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, newPosition); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 246 | mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0); |
Tony Wickham | d552132 | 2021-09-14 14:26:50 -0700 | [diff] [blame] | 247 | forceFinishScroller(); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Tony Wickham | f8d731a | 2021-05-26 14:58:13 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Immediately finishes any overscroll effect and jumps to the end of the scroller animation. |
| 252 | */ |
| 253 | public void abortScrollerAnimation() { |
| 254 | mEdgeGlowLeft.finish(); |
| 255 | mEdgeGlowRight.finish(); |
| 256 | abortScrollerAnimation(true); |
| 257 | } |
| 258 | |
Schneider Victor-tulias | a602a0a | 2022-09-06 13:34:00 -0700 | [diff] [blame] | 259 | protected void onScrollerAnimationAborted() { |
| 260 | // No-Op |
| 261 | } |
| 262 | |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 263 | private void abortScrollerAnimation(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 264 | mScroller.abortAnimation(); |
Schneider Victor-tulias | a602a0a | 2022-09-06 13:34:00 -0700 | [diff] [blame] | 265 | onScrollerAnimationAborted(); |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 266 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 267 | // updating current page on the pass. |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 268 | if (resetNextPage) { |
| 269 | mNextPage = INVALID_PAGE; |
Sunny Goyal | 857b1b9 | 2018-03-05 10:49:05 -0800 | [diff] [blame] | 270 | pageEndTransition(); |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 271 | } |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 272 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 273 | |
Tony Wickham | d552132 | 2021-09-14 14:26:50 -0700 | [diff] [blame] | 274 | /** |
| 275 | * Immediately finishes any in-progress scroll, maintaining the current position. Also sets |
| 276 | * mNextPage = INVALID_PAGE and calls pageEndTransition(). |
| 277 | */ |
| 278 | public void forceFinishScroller() { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 279 | mScroller.forceFinished(true); |
| 280 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 281 | // updating current page on the pass. |
Tony Wickham | d552132 | 2021-09-14 14:26:50 -0700 | [diff] [blame] | 282 | mNextPage = INVALID_PAGE; |
| 283 | pageEndTransition(); |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 286 | private int validateNewPage(int newPage) { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 287 | newPage = ensureWithinScrollBounds(newPage); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 288 | // Ensure that it is clamped by the actual set of children in all cases |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 289 | newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1); |
| 290 | |
| 291 | if (getPanelCount() > 1) { |
Alex Chau | 1e44846 | 2021-08-13 21:48:35 +0100 | [diff] [blame] | 292 | // Always return left most panel as new page |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 293 | newPage = getLeftmostVisiblePageForIndex(newPage); |
| 294 | } |
| 295 | return newPage; |
| 296 | } |
| 297 | |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 298 | /** |
| 299 | * In most cases where panelCount is 1, this method will just return the page index that was |
| 300 | * passed in. |
| 301 | * But for example when two panel home is enabled we might need the leftmost visible page index |
| 302 | * because that page is the current page. |
| 303 | */ |
| 304 | public int getLeftmostVisiblePageForIndex(int pageIndex) { |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 305 | int panelCount = getPanelCount(); |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 306 | return pageIndex - pageIndex % panelCount; |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns the number of pages that are shown at the same time. |
| 311 | */ |
| 312 | protected int getPanelCount() { |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | /** |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 317 | * Returns an IntSet with the indices of the currently visible pages |
| 318 | */ |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 319 | public IntSet getVisiblePageIndices() { |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 320 | return getPageIndices(mCurrentPage); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * In case the panelCount is 1 this just returns the same page index in an IntSet. |
| 325 | * But in cases where the panelCount > 1 this will return all the page indices that belong |
| 326 | * together, i.e. on the Workspace they are next to each other and shown at the same time. |
| 327 | */ |
| 328 | private IntSet getPageIndices(int pageIndex) { |
| 329 | // we want to make sure the pageIndex is the leftmost page |
| 330 | pageIndex = getLeftmostVisiblePageForIndex(pageIndex); |
| 331 | |
| 332 | IntSet pageIndices = new IntSet(); |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 333 | int panelCount = getPanelCount(); |
| 334 | int pageCount = getPageCount(); |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 335 | for (int page = pageIndex; page < pageIndex + panelCount && page < pageCount; page++) { |
| 336 | pageIndices.add(page); |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 337 | } |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 338 | return pageIndices; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Returns an IntSet with the indices of the neighbour pages that are in the focus direction. |
| 343 | */ |
| 344 | private IntSet getNeighbourPageIndices(int focus) { |
| 345 | int panelCount = getPanelCount(); |
| 346 | // getNextPage is more reliable than getCurrentPage |
| 347 | int currentPage = getNextPage(); |
| 348 | |
| 349 | int nextPage; |
| 350 | if (focus == View.FOCUS_LEFT) { |
| 351 | nextPage = currentPage - panelCount; |
| 352 | } else if (focus == View.FOCUS_RIGHT) { |
| 353 | nextPage = currentPage + panelCount; |
| 354 | } else { |
Andras Kloczl | b063294 | 2021-08-03 14:03:58 +0200 | [diff] [blame] | 355 | // no neighbours to other directions |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 356 | return new IntSet(); |
| 357 | } |
| 358 | nextPage = validateNewPage(nextPage); |
| 359 | if (nextPage == currentPage) { |
| 360 | // We reached the end of the pages |
| 361 | return new IntSet(); |
| 362 | } |
| 363 | |
Andras Kloczl | b063294 | 2021-08-03 14:03:58 +0200 | [diff] [blame] | 364 | return getPageIndices(nextPage); |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 368 | * Executes the callback against each visible page |
Andras Kloczl | 2bb5134 | 2021-03-20 01:13:45 +0100 | [diff] [blame] | 369 | */ |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 370 | public void forEachVisiblePage(Consumer<View> callback) { |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 371 | getVisiblePageIndices().forEach(pageIndex -> { |
| 372 | View page = getPageAt(pageIndex); |
Andras Kloczl | 2bb5134 | 2021-03-20 01:13:45 +0100 | [diff] [blame] | 373 | if (page != null) { |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 374 | callback.accept(page); |
Andras Kloczl | 2bb5134 | 2021-03-20 01:13:45 +0100 | [diff] [blame] | 375 | } |
Andras Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 376 | }); |
Andras Kloczl | 2bb5134 | 2021-03-20 01:13:45 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 380 | * Returns true if the view is on one of the current pages, false otherwise. |
| 381 | */ |
| 382 | public boolean isVisible(View child) { |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 383 | return isVisible(indexOfChild(child)); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Returns true if the page with the given index is currently visible, false otherwise. |
| 388 | */ |
| 389 | private boolean isVisible(int pageIndex) { |
| 390 | return getLeftmostVisiblePageForIndex(pageIndex) == mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 393 | /** |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 394 | * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX. |
| 395 | */ |
| 396 | private int ensureWithinScrollBounds(int page) { |
| 397 | int dir = !mIsRtl ? 1 : - 1; |
| 398 | int currScroll = getScrollForPage(page); |
| 399 | int prevScroll; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 400 | while (currScroll < mMinScroll) { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 401 | page += dir; |
| 402 | prevScroll = currScroll; |
| 403 | currScroll = getScrollForPage(page); |
| 404 | if (currScroll <= prevScroll) { |
| 405 | Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX"); |
| 406 | break; |
| 407 | } |
| 408 | } |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 409 | while (currScroll > mMaxScroll) { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 410 | page -= dir; |
| 411 | prevScroll = currScroll; |
| 412 | currScroll = getScrollForPage(page); |
| 413 | if (currScroll >= prevScroll) { |
| 414 | Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX"); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | return page; |
| 419 | } |
| 420 | |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 421 | public void setCurrentPage(int currentPage) { |
| 422 | setCurrentPage(currentPage, INVALID_PAGE); |
| 423 | } |
| 424 | |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 425 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 426 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 427 | */ |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 428 | public void setCurrentPage(int currentPage, int overridePrevPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 429 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 430 | abortScrollerAnimation(true); |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 431 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 432 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 433 | // the default |
| 434 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 435 | return; |
| 436 | } |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 437 | int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 438 | mCurrentPage = validateNewPage(currentPage); |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 439 | mCurrentScrollOverPage = mCurrentPage; |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 440 | updateCurrentPageScroll(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 441 | notifyPageSwitchListener(prevPage); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 442 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 445 | /** |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 446 | * Should be called whenever the page changes. In the case of a scroll, we wait until the page |
| 447 | * has settled. |
| 448 | */ |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 449 | protected void notifyPageSwitchListener(int prevPage) { |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 450 | updatePageIndicator(); |
| 451 | } |
| 452 | |
| 453 | private void updatePageIndicator() { |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 454 | if (mPageIndicator != null) { |
Tony Mak | 4f521af | 2018-03-15 16:37:34 +0000 | [diff] [blame] | 455 | mPageIndicator.setActiveMarker(getNextPage()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 456 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 457 | } |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 458 | protected void pageBeginTransition() { |
| 459 | if (!mIsPageInTransition) { |
| 460 | mIsPageInTransition = true; |
| 461 | onPageBeginTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 462 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 465 | protected void pageEndTransition() { |
Sunny Goyal | 05b59c0 | 2021-04-29 13:55:40 -0700 | [diff] [blame] | 466 | if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished() |
Sunny Goyal | b745a2a | 2021-04-30 13:38:22 -0700 | [diff] [blame] | 467 | && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) { |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 468 | mIsPageInTransition = false; |
| 469 | onPageEndTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 470 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Sunny Goyal | b745a2a | 2021-04-30 13:38:22 -0700 | [diff] [blame] | 473 | @Override |
| 474 | public void onVisibilityAggregated(boolean isVisible) { |
| 475 | pageEndTransition(); |
| 476 | super.onVisibilityAggregated(isVisible); |
| 477 | } |
| 478 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 479 | protected boolean isPageInTransition() { |
| 480 | return mIsPageInTransition; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Called when the page starts moving as part of the scroll. Subclasses can override this |
| 485 | * to provide custom behavior during animation. |
| 486 | */ |
| 487 | protected void onPageBeginTransition() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 490 | /** |
| 491 | * Called when the page ends moving as part of the scroll. Subclasses can override this |
| 492 | * to provide custom behavior during animation. |
| 493 | */ |
| 494 | protected void onPageEndTransition() { |
Alex Chau | ba61b98 | 2021-06-15 16:29:19 +0100 | [diff] [blame] | 495 | mCurrentPageScrollDiff = 0; |
Jagrut Desai | 311d519 | 2023-09-13 10:18:02 -0700 | [diff] [blame] | 496 | AccessibilityManagerCompat.sendTestProtocolEventToTest(getContext(), |
| 497 | SCROLL_FINISHED_MESSAGE); |
Tony Wickham | 619daaf | 2020-02-07 12:29:06 -0800 | [diff] [blame] | 498 | AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage), |
| 499 | AccessibilityEvent.TYPE_VIEW_FOCUSED, null); |
Tony Wickham | 62117d7 | 2020-03-26 16:56:26 -0700 | [diff] [blame] | 500 | if (mOnPageTransitionEndCallback != null) { |
| 501 | mOnPageTransitionEndCallback.run(); |
| 502 | mOnPageTransitionEndCallback = null; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Sets a callback to run once when the scrolling finishes. If there is currently |
| 508 | * no page in transition, then the callback is called immediately. |
| 509 | */ |
| 510 | public void setOnPageTransitionEndCallback(@Nullable Runnable callback) { |
| 511 | if (mIsPageInTransition || callback == null) { |
| 512 | mOnPageTransitionEndCallback = callback; |
| 513 | } else { |
| 514 | callback.run(); |
| 515 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 518 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 519 | public void scrollTo(int x, int y) { |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 520 | x = Utilities.boundToRange(x, |
| 521 | mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll); |
| 522 | y = Utilities.boundToRange(y, |
| 523 | mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 524 | super.scrollTo(x, y); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 527 | private void sendScrollAccessibilityEvent() { |
Sunny Goyal | d0030b0 | 2017-12-08 15:07:24 -0800 | [diff] [blame] | 528 | if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) { |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 529 | if (mCurrentPage != getNextPage()) { |
| 530 | AccessibilityEvent ev = |
| 531 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 532 | ev.setScrollable(true); |
| 533 | ev.setScrollX(getScrollX()); |
| 534 | ev.setScrollY(getScrollY()); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 535 | mOrientationHandler.setMaxScroll(ev, mMaxScroll); |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 536 | sendAccessibilityEventUnchecked(ev); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 537 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 541 | protected void announcePageForAccessibility() { |
| 542 | if (isAccessibilityEnabled(getContext())) { |
| 543 | // Notify the user when the page changes |
| 544 | announceForAccessibility(getCurrentPageDescription()); |
| 545 | } |
| 546 | } |
| 547 | |
Winson Chung | 8542541 | 2021-05-10 16:24:14 -0700 | [diff] [blame] | 548 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 549 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 550 | // Don't bother scrolling if the page does not need to be moved |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 551 | int oldPos = mOrientationHandler.getPrimaryScroll(this); |
| 552 | int newPos = mScroller.getCurrX(); |
| 553 | if (oldPos != newPos) { |
Alex Chau | 6499e83 | 2021-11-19 14:35:46 +0000 | [diff] [blame] | 554 | mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, mScroller.getCurrX()); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 555 | } |
Winson Chung | 8542541 | 2021-05-10 16:24:14 -0700 | [diff] [blame] | 556 | |
| 557 | if (mAllowOverScroll) { |
| 558 | if (newPos < mMinScroll && oldPos >= mMinScroll) { |
| 559 | mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity()); |
Schneider Victor-tulias | a602a0a | 2022-09-06 13:34:00 -0700 | [diff] [blame] | 560 | abortScrollerAnimation(false); |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 561 | onEdgeAbsorbingScroll(); |
Winson Chung | 8542541 | 2021-05-10 16:24:14 -0700 | [diff] [blame] | 562 | } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) { |
| 563 | mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity()); |
Schneider Victor-tulias | a602a0a | 2022-09-06 13:34:00 -0700 | [diff] [blame] | 564 | abortScrollerAnimation(false); |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 565 | onEdgeAbsorbingScroll(); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 566 | } |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 567 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 568 | |
Winson Chung | 8542541 | 2021-05-10 16:24:14 -0700 | [diff] [blame] | 569 | // If the scroller has scrolled to the final position and there is no edge effect, then |
| 570 | // finish the scroller to skip waiting for additional settling |
| 571 | int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(), |
| 572 | mScroller.getFinalY()); |
| 573 | if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) { |
Schneider Victor-tulias | a602a0a | 2022-09-06 13:34:00 -0700 | [diff] [blame] | 574 | abortScrollerAnimation(false); |
Winson Chung | 8542541 | 2021-05-10 16:24:14 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | invalidate(); |
| 578 | return true; |
| 579 | } else if (mNextPage != INVALID_PAGE) { |
| 580 | sendScrollAccessibilityEvent(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 581 | int prevPage = mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 582 | mCurrentPage = validateNewPage(mNextPage); |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 583 | mCurrentScrollOverPage = mCurrentPage; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 584 | mNextPage = INVALID_PAGE; |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 585 | notifyPageSwitchListener(prevPage); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 586 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 587 | // We don't want to trigger a page end moving unless the page has settled |
| 588 | // and the user has stopped scrolling |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 589 | if (!mIsBeingDragged) { |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 590 | pageEndTransition(); |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 591 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 592 | |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 593 | if (canAnnouncePageDescription()) { |
| 594 | announcePageForAccessibility(); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 595 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 596 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 597 | return false; |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public void computeScroll() { |
| 602 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 605 | public int getExpectedHeight() { |
| 606 | return getMeasuredHeight(); |
| 607 | } |
| 608 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 609 | public int getNormalChildHeight() { |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 610 | return getExpectedHeight() - getPaddingTop() - getPaddingBottom() |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 611 | - mInsets.top - mInsets.bottom; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 614 | public int getExpectedWidth() { |
| 615 | return getMeasuredWidth(); |
| 616 | } |
| 617 | |
Sunny Goyal | bc683e9 | 2017-12-06 10:25:07 -0800 | [diff] [blame] | 618 | public int getNormalChildWidth() { |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 619 | return getExpectedWidth() - getPaddingLeft() - getPaddingRight() |
Sunny Goyal | bc683e9 | 2017-12-06 10:25:07 -0800 | [diff] [blame] | 620 | - mInsets.left - mInsets.right; |
| 621 | } |
| 622 | |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 623 | private void updateVelocityValues() { |
| 624 | Resources res = getResources(); |
| 625 | mFlingThresholdVelocity = res.getDimensionPixelSize(R.dimen.fling_threshold_velocity); |
| 626 | mEasyFlingThresholdVelocity = |
| 627 | res.getDimensionPixelSize(R.dimen.easy_fling_threshold_velocity); |
| 628 | mMinFlingVelocity = res.getDimensionPixelSize(R.dimen.min_fling_velocity); |
| 629 | mMinSnapVelocity = res.getDimensionPixelSize(R.dimen.min_page_snap_velocity); |
| 630 | mPageSnapAnimationDuration = res.getInteger(R.integer.config_pageSnapAnimationDuration); |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | protected void onConfigurationChanged(Configuration newConfig) { |
| 635 | super.onConfigurationChanged(newConfig); |
| 636 | updateVelocityValues(); |
| 637 | } |
| 638 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 639 | @Override |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 640 | public void requestLayout() { |
| 641 | mIsLayoutValid = false; |
| 642 | super.requestLayout(); |
| 643 | } |
| 644 | |
| 645 | @Override |
| 646 | public void forceLayout() { |
| 647 | mIsLayoutValid = false; |
| 648 | super.forceLayout(); |
| 649 | } |
| 650 | |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 651 | private int getPageWidthSize(int widthSize) { |
Thales Lima | 7a6752d | 2021-09-23 14:31:00 +0100 | [diff] [blame] | 652 | // It's necessary to add the padding back because it is remove when measuring children, |
| 653 | // like when MeasureSpec.getSize in CellLayout. |
| 654 | return (widthSize - mInsets.left - mInsets.right - getPaddingLeft() - getPaddingRight()) |
| 655 | / getPanelCount() + getPaddingLeft() + getPaddingRight(); |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 658 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 659 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 660 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 661 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 662 | return; |
| 663 | } |
| 664 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 665 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 666 | // zoom out (and scale down), the view is still contained in the parent |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 667 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 668 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 669 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 670 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 671 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 672 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 673 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 674 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 677 | // Return early if we aren't given a proper dimension |
| 678 | if (widthSize <= 0 || heightSize <= 0) { |
| 679 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 680 | return; |
| 681 | } |
| 682 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 683 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 684 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 685 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 686 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 687 | int myWidthSpec = MeasureSpec.makeMeasureSpec( |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 688 | getPageWidthSize(widthSize), MeasureSpec.EXACTLY); |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 689 | int myHeightSpec = MeasureSpec.makeMeasureSpec( |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 690 | heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 691 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 692 | // measureChildren takes accounts for content padding, we only need to care about extra |
| 693 | // space due to insets. |
| 694 | measureChildren(myWidthSpec, myHeightSpec); |
| 695 | setMeasuredDimension(widthSize, heightSize); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 698 | /** Returns true iff this PagedView's scroll amounts are initialized to each page index. */ |
Schneider Victor-tulias | 7bb558a | 2022-12-15 05:42:53 -0800 | [diff] [blame] | 699 | protected boolean isPageScrollsInitialized() { |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 700 | return mPageScrolls != null && mPageScrolls.length == getChildCount(); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Queues the given callback to be run once {@code mPageScrolls} has been initialized. |
| 705 | */ |
| 706 | public void runOnPageScrollsInitialized(Runnable callback) { |
| 707 | mOnPageScrollsInitializedCallbacks.add(callback); |
Schneider Victor-tulias | 7bb558a | 2022-12-15 05:42:53 -0800 | [diff] [blame] | 708 | if (isPageScrollsInitialized()) { |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 709 | onPageScrollsInitialized(); |
| 710 | } |
| 711 | } |
| 712 | |
Schneider Victor-tulias | 7bb558a | 2022-12-15 05:42:53 -0800 | [diff] [blame] | 713 | protected void onPageScrollsInitialized() { |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 714 | for (Runnable callback : mOnPageScrollsInitializedCallbacks) { |
| 715 | callback.run(); |
| 716 | } |
| 717 | mOnPageScrollsInitializedCallbacks.clear(); |
| 718 | } |
| 719 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 720 | @SuppressLint("DrawAllocation") |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 721 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 722 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 723 | mIsLayoutValid = true; |
Vadim Tryshev | eb701c6 | 2018-05-14 16:00:15 -0700 | [diff] [blame] | 724 | final int childCount = getChildCount(); |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 725 | int[] pageScrolls = mPageScrolls; |
Vadim Tryshev | eb701c6 | 2018-05-14 16:00:15 -0700 | [diff] [blame] | 726 | boolean pageScrollChanged = false; |
Schneider Victor-tulias | 7bb558a | 2022-12-15 05:42:53 -0800 | [diff] [blame] | 727 | if (!isPageScrollsInitialized()) { |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 728 | pageScrolls = new int[childCount]; |
Vadim Tryshev | eb701c6 | 2018-05-14 16:00:15 -0700 | [diff] [blame] | 729 | pageScrollChanged = true; |
| 730 | } |
| 731 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 732 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 733 | |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 734 | pageScrollChanged |= getPageScrolls(pageScrolls, true, SIMPLE_SCROLL_LOGIC); |
| 735 | mPageScrolls = pageScrolls; |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 736 | |
Winson Chung | 359cb5d | 2022-05-26 23:38:48 +0000 | [diff] [blame] | 737 | if (childCount == 0) { |
| 738 | onPageScrollsInitialized(); |
| 739 | return; |
| 740 | } |
| 741 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 742 | final LayoutTransition transition = getLayoutTransition(); |
| 743 | // If the transition is running defer updating max scroll, as some empty pages could |
| 744 | // still be present, and a max scroll change could cause sudden jumps in scroll. |
| 745 | if (transition != null && transition.isRunning()) { |
| 746 | transition.addTransitionListener(new LayoutTransition.TransitionListener() { |
| 747 | |
| 748 | @Override |
| 749 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 750 | View view, int transitionType) { } |
| 751 | |
| 752 | @Override |
| 753 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 754 | View view, int transitionType) { |
| 755 | // Wait until all transitions are complete. |
| 756 | if (!transition.isRunning()) { |
| 757 | transition.removeTransitionListener(this); |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 758 | updateMinAndMaxScrollX(); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | }); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 762 | } else { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 763 | updateMinAndMaxScrollX(); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 766 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) { |
| 767 | updateCurrentPageScroll(); |
| 768 | mFirstLayout = false; |
| 769 | } |
| 770 | |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 771 | if (mScroller.isFinished() && pageScrollChanged) { |
Alex Chau | 0733ebd | 2022-10-07 15:11:09 +0100 | [diff] [blame] | 772 | // TODO(b/246283207): Remove logging once root cause of flake detected. |
Brian Isganitis | fdd044e | 2023-03-02 19:45:23 +0000 | [diff] [blame] | 773 | if (Utilities.isRunningInTestHarness() && !(this instanceof Workspace)) { |
Alex Chau | 0733ebd | 2022-10-07 15:11:09 +0100 | [diff] [blame] | 774 | Log.d("b/246283207", this.getClass().getSimpleName() + "#onLayout() -> " |
Pat Manning | 24b9554 | 2022-08-09 18:06:58 +0100 | [diff] [blame] | 775 | + "if(mScroller.isFinished() && pageScrollChanged) -> getNextPage(): " |
| 776 | + getNextPage() + ", getScrollForPage(getNextPage()): " |
| 777 | + getScrollForPage(getNextPage())); |
| 778 | } |
Sunny Goyal | c82c639 | 2018-06-06 15:39:13 -0700 | [diff] [blame] | 779 | setCurrentPage(getNextPage()); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 780 | } |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 781 | onPageScrollsInitialized(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 784 | /** |
| 785 | * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length |
| 786 | * of {@code outPageScrolls} should be same as the the childCount |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 787 | */ |
| 788 | protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren, |
| 789 | ComputePageScrollsLogic scrollLogic) { |
| 790 | final int childCount = getChildCount(); |
| 791 | |
| 792 | final int startIndex = mIsRtl ? childCount - 1 : 0; |
| 793 | final int endIndex = mIsRtl ? -1 : childCount; |
| 794 | final int delta = mIsRtl ? -1 : 1; |
| 795 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 796 | final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 797 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 798 | final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets); |
| 799 | final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 800 | boolean pageScrollChanged = false; |
Thales Lima | e445774 | 2021-07-20 16:46:38 +0100 | [diff] [blame] | 801 | int panelCount = getPanelCount(); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 802 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 803 | for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) { |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 804 | final View child = getPageAt(i); |
| 805 | if (scrollLogic.shouldIncludeView(child)) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 806 | ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart, |
| 807 | pageCenter, layoutChildren); |
| 808 | final int primaryDimension = bounds.primaryDimension; |
| 809 | final int childPrimaryEnd = bounds.childPrimaryEnd; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 810 | |
Alex Chau | b5c4d11 | 2021-03-29 17:37:53 +0100 | [diff] [blame] | 811 | // In case the pages are of different width, align the page to left edge for non-RTL |
| 812 | // or right edge for RTL. |
| 813 | final int pageScroll = |
| 814 | mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 815 | if (outPageScrolls[i] != pageScroll) { |
| 816 | pageScrollChanged = true; |
| 817 | outPageScrolls[i] = pageScroll; |
| 818 | } |
Alex Chau | 2e48760 | 2022-03-15 19:34:08 +0000 | [diff] [blame] | 819 | childStart += primaryDimension + getChildGap(i, i + delta); |
Thales Lima | e445774 | 2021-07-20 16:46:38 +0100 | [diff] [blame] | 820 | |
| 821 | // This makes sure that the space is added after the page, not after each panel |
Thales Lima | c2a6f6f | 2021-09-09 17:06:22 +0100 | [diff] [blame] | 822 | int lastPanel = mIsRtl ? 0 : panelCount - 1; |
| 823 | if (i % panelCount == lastPanel) { |
Thales Lima | e445774 | 2021-07-20 16:46:38 +0100 | [diff] [blame] | 824 | childStart += mPageSpacing; |
| 825 | } |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 828 | |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 829 | if (panelCount > 1) { |
| 830 | for (int i = 0; i < childCount; i++) { |
Alex Chau | 1e44846 | 2021-08-13 21:48:35 +0100 | [diff] [blame] | 831 | // In case we have multiple panels, always use left most panel's page scroll for all |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 832 | // panels on the screen. |
| 833 | int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)]; |
| 834 | if (outPageScrolls[i] != adjustedScroll) { |
| 835 | outPageScrolls[i] = adjustedScroll; |
| 836 | pageScrollChanged = true; |
| 837 | } |
| 838 | } |
| 839 | } |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 840 | return pageScrollChanged; |
| 841 | } |
| 842 | |
Alex Chau | 2e48760 | 2022-03-15 19:34:08 +0000 | [diff] [blame] | 843 | protected int getChildGap(int fromIndex, int toIndex) { |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 844 | return 0; |
| 845 | } |
| 846 | |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 847 | protected void updateMinAndMaxScrollX() { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 848 | mMinScroll = computeMinScroll(); |
| 849 | mMaxScroll = computeMaxScroll(); |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 852 | protected int computeMinScroll() { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 853 | return 0; |
Tony | 50876bf | 2018-09-19 23:09:14 -0400 | [diff] [blame] | 854 | } |
| 855 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 856 | protected int computeMaxScroll() { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 857 | int childCount = getChildCount(); |
| 858 | if (childCount > 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 859 | final int index = mIsRtl ? 0 : childCount - 1; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 860 | return getScrollForPage(index); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 861 | } else { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 862 | return 0; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 866 | public void setPageSpacing(int pageSpacing) { |
| 867 | mPageSpacing = pageSpacing; |
| 868 | requestLayout(); |
| 869 | } |
| 870 | |
Tony | 50876bf | 2018-09-19 23:09:14 -0400 | [diff] [blame] | 871 | public int getPageSpacing() { |
| 872 | return mPageSpacing; |
| 873 | } |
| 874 | |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 875 | private void dispatchPageCountChanged() { |
| 876 | if (mPageIndicator != null) { |
Tony Wickham | bab101c | 2021-09-20 15:12:44 -0700 | [diff] [blame] | 877 | mPageIndicator.setMarkersCount(getChildCount() / getPanelCount()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 878 | } |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 879 | // This ensures that when children are added, they get the correct transforms / alphas |
| 880 | // in accordance with any scroll effects. |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 881 | invalidate(); |
| 882 | } |
| 883 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 884 | @Override |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 885 | public void onViewAdded(View child) { |
Winson Chung | c7c5158 | 2018-02-23 17:58:36 -0800 | [diff] [blame] | 886 | super.onViewAdded(child); |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 887 | dispatchPageCountChanged(); |
| 888 | } |
| 889 | |
| 890 | @Override |
| 891 | public void onViewRemoved(View child) { |
Winson Chung | c7c5158 | 2018-02-23 17:58:36 -0800 | [diff] [blame] | 892 | super.onViewRemoved(child); |
Schneider Victor-tulias | bb44c03 | 2022-04-07 15:25:00 -0700 | [diff] [blame] | 893 | runOnPageScrollsInitialized(() -> { |
| 894 | mCurrentPage = validateNewPage(mCurrentPage); |
| 895 | mCurrentScrollOverPage = mCurrentPage; |
| 896 | }); |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 897 | dispatchPageCountChanged(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 900 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 901 | if (index < 0 || index > getChildCount() - 1) return 0; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 902 | View pageAtIndex = getPageAt(index); |
| 903 | return mOrientationHandler.getChildStart(pageAtIndex); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Alex Chau | 7944ee5 | 2021-01-08 19:07:25 +0000 | [diff] [blame] | 906 | protected int getChildVisibleSize(int index) { |
| 907 | View layout = getPageAt(index); |
| 908 | return mOrientationHandler.getMeasuredSize(layout); |
| 909 | } |
| 910 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 911 | @Override |
| 912 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 913 | int page = indexOfChild(child); |
| 914 | if (!isVisible(page) || !mScroller.isFinished()) { |
Vadim Tryshev | eeaa8ee | 2018-04-11 12:23:42 -0700 | [diff] [blame] | 915 | if (immediate) { |
| 916 | setCurrentPage(page); |
| 917 | } else { |
| 918 | snapToPage(page); |
| 919 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 920 | return true; |
| 921 | } |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | @Override |
| 926 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 927 | int focusablePage; |
| 928 | if (mNextPage != INVALID_PAGE) { |
| 929 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 930 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 931 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 932 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 933 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 934 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 935 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 936 | } |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | @Override |
| 941 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 942 | if (super.dispatchUnhandledMove(focused, direction)) { |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | if (mIsRtl) { |
| 947 | if (direction == View.FOCUS_LEFT) { |
| 948 | direction = View.FOCUS_RIGHT; |
| 949 | } else if (direction == View.FOCUS_RIGHT) { |
| 950 | direction = View.FOCUS_LEFT; |
| 951 | } |
| 952 | } |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 953 | |
| 954 | int currentPage = getNextPage(); |
| 955 | int closestNeighbourIndex = -1; |
| 956 | int closestNeighbourDistance = Integer.MAX_VALUE; |
| 957 | // Find the closest neighbour page |
| 958 | for (int neighbourPageIndex : getNeighbourPageIndices(direction)) { |
| 959 | int distance = Math.abs(neighbourPageIndex - currentPage); |
| 960 | if (closestNeighbourDistance > distance) { |
| 961 | closestNeighbourDistance = distance; |
| 962 | closestNeighbourIndex = neighbourPageIndex; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 965 | if (closestNeighbourIndex != -1) { |
| 966 | View page = getPageAt(closestNeighbourIndex); |
| 967 | snapToPage(closestNeighbourIndex); |
| 968 | page.requestFocus(direction); |
| 969 | return true; |
| 970 | } |
| 971 | |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 972 | return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Jon Miranda | 6a85817 | 2016-10-25 16:19:17 -0700 | [diff] [blame] | 977 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 978 | return; |
| 979 | } |
| 980 | |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 981 | // nextPage is more reliable when multiple control movements have been done in a short |
| 982 | // period of time |
| 983 | getPageIndices(getNextPage()) |
| 984 | .addAll(getNeighbourPageIndices(direction)) |
| 985 | .forEach(pageIndex -> |
| 986 | getPageAt(pageIndex).addFocusables(views, direction, focusableMode)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /** |
| 990 | * If one of our descendant views decides that it could be focused now, only |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 991 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 992 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 993 | * This happens when live folders requery, and if they're off page, they |
| 994 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 995 | */ |
| 996 | @Override |
| 997 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 998 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 999 | View v = focused; |
| 1000 | while (true) { |
| 1001 | if (v == current) { |
| 1002 | super.focusableViewAvailable(focused); |
| 1003 | return; |
| 1004 | } |
| 1005 | if (v == this) { |
| 1006 | return; |
| 1007 | } |
| 1008 | ViewParent parent = v.getParent(); |
| 1009 | if (parent instanceof View) { |
| 1010 | v = (View)v.getParent(); |
| 1011 | } else { |
| 1012 | return; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * {@inheritDoc} |
| 1019 | */ |
| 1020 | @Override |
| 1021 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 1022 | if (disallowIntercept) { |
| 1023 | // We need to make sure to cancel our long press if |
| 1024 | // a scrollable widget takes over touch events |
Andras Kloczl | 7a1ca0b | 2021-04-08 16:08:16 +0200 | [diff] [blame] | 1025 | cancelCurrentPageLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1026 | } |
| 1027 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1028 | } |
| 1029 | |
| 1030 | @Override |
| 1031 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 1032 | /* |
| 1033 | * This method JUST determines whether we want to intercept the motion. |
| 1034 | * If we return true, onTouchEvent will be called and we do the actual |
| 1035 | * scrolling there. |
| 1036 | */ |
| 1037 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1038 | // Skip touch handling if there are no pages to swipe |
Tony Wickham | b6841ac | 2019-07-22 12:39:59 -0700 | [diff] [blame] | 1039 | if (getChildCount() <= 0) return false; |
Sunny Goyal | c4bb373 | 2019-06-20 11:34:14 -0700 | [diff] [blame] | 1040 | |
| 1041 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1042 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * Shortcut the most recurring case: the user is in the dragging |
| 1045 | * state and he is moving his finger. We want to intercept this |
| 1046 | * motion. |
| 1047 | */ |
| 1048 | final int action = ev.getAction(); |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1049 | if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1050 | return true; |
| 1051 | } |
| 1052 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1053 | switch (action & MotionEvent.ACTION_MASK) { |
| 1054 | case MotionEvent.ACTION_MOVE: { |
| 1055 | /* |
| 1056 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1057 | * whether the user has moved far enough from their original down touch. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1058 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1059 | if (mActivePointerId != INVALID_POINTER) { |
| 1060 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1061 | } |
| 1062 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1063 | // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1064 | // i.e. fall through to the next case (don't break) |
| 1065 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1066 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1067 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | case MotionEvent.ACTION_DOWN: { |
| 1071 | final float x = ev.getX(); |
| 1072 | final float y = ev.getY(); |
| 1073 | // Remember location of down touch |
| 1074 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1075 | mDownMotionY = y; |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1076 | mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, 0); |
| 1077 | mLastMotion = (int) mDownMotionPrimary; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1078 | mTotalMotion = 0; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1079 | mAllowEasyFling = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1080 | mActivePointerId = ev.getPointerId(0); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1081 | updateIsBeingDraggedOnTouchDown(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1085 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1086 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1087 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1088 | break; |
| 1089 | |
| 1090 | case MotionEvent.ACTION_POINTER_UP: |
| 1091 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1092 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1093 | break; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * The only time we want to intercept motion events is if we are in the |
| 1098 | * drag mode. |
| 1099 | */ |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1100 | return mIsBeingDragged; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1103 | /** |
| 1104 | * If being flinged and user touches the screen, initiate drag; otherwise don't. |
| 1105 | */ |
Sunny Goyal | bb03056 | 2021-07-19 16:28:39 -0700 | [diff] [blame] | 1106 | protected void updateIsBeingDraggedOnTouchDown(MotionEvent ev) { |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1107 | // mScroller.isFinished should be false when being flinged. |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1108 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1109 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3); |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1110 | |
| 1111 | if (finishedScrolling) { |
| 1112 | mIsBeingDragged = false; |
| 1113 | if (!mScroller.isFinished() && !mFreeScroll) { |
| 1114 | setCurrentPage(getNextPage()); |
| 1115 | pageEndTransition(); |
| 1116 | } |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1117 | mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished(); |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1118 | } else { |
| 1119 | mIsBeingDragged = true; |
| 1120 | } |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1121 | |
| 1122 | // Catch the edge effect if it is active. |
| 1123 | float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY()) |
| 1124 | / mOrientationHandler.getSecondaryValue(getWidth(), getHeight()); |
| 1125 | if (!mEdgeGlowLeft.isFinished()) { |
| 1126 | mEdgeGlowLeft.onPullDistance(0f, 1f - displacement); |
| 1127 | } |
| 1128 | if (!mEdgeGlowRight.isFinished()) { |
| 1129 | mEdgeGlowRight.onPullDistance(0f, displacement); |
| 1130 | } |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1133 | public boolean isHandlingTouch() { |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1134 | return mIsBeingDragged; |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1137 | protected void determineScrollingStart(MotionEvent ev) { |
| 1138 | determineScrollingStart(ev, 1.0f); |
| 1139 | } |
| 1140 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * Determines if we should change the touch state to start scrolling after the |
| 1143 | * user moves their touch point too far. |
| 1144 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1145 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1146 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1147 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1148 | if (pointerIndex == -1) return; |
| 1149 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1150 | final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex); |
| 1151 | final int diff = (int) Math.abs(primaryDirection - mLastMotion); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1152 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1153 | boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1154 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1155 | if (moved) { |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1156 | // Scroll if the user moved far enough along the X axis |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1157 | mIsBeingDragged = true; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1158 | mTotalMotion += Math.abs(mLastMotion - primaryDirection); |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1159 | mLastMotion = (int) primaryDirection; |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1160 | pageBeginTransition(); |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 1161 | // Stop listening for things like pinches. |
| 1162 | requestDisallowInterceptTouchEvent(true); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | protected void cancelCurrentPageLongPress() { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1167 | // Try canceling the long press. It could also have been scheduled |
| 1168 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1169 | // everything |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 1170 | forEachVisiblePage(View::cancelLongPress); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1173 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1174 | final int halfScreenSize = getMeasuredWidth() / 2; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1175 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1176 | int panelCount = getPanelCount(); |
| 1177 | int pageCount = getChildCount(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1178 | |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1179 | int adjacentPage = page + panelCount; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1180 | if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) { |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1181 | adjacentPage = page - panelCount; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1184 | final int totalDistance; |
| 1185 | if (adjacentPage < 0 || adjacentPage > pageCount - 1) { |
| 1186 | totalDistance = (v.getMeasuredWidth() + mPageSpacing) * panelCount; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1189 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1190 | |
| 1191 | float scrollProgress = delta / (totalDistance * 1.0f); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1192 | scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS); |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1193 | scrollProgress = Math.max(scrollProgress, -MAX_SCROLL_PROGRESS); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1194 | return scrollProgress; |
| 1195 | } |
| 1196 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1197 | public int getScrollForPage(int index) { |
Schneider Victor-tulias | 7bb558a | 2022-12-15 05:42:53 -0800 | [diff] [blame] | 1198 | if (!isPageScrollsInitialized() || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1199 | return 0; |
| 1200 | } else { |
| 1201 | return mPageScrolls[index]; |
| 1202 | } |
| 1203 | } |
| 1204 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1205 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1206 | // position. This method returns the magnitude of this stray at any given time. |
| 1207 | public int getLayoutTransitionOffsetForPage(int index) { |
Schneider Victor-tulias | 7bb558a | 2022-12-15 05:42:53 -0800 | [diff] [blame] | 1208 | if (!isPageScrollsInitialized() || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1209 | return 0; |
| 1210 | } else { |
| 1211 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1212 | |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1213 | int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft(); |
| 1214 | int baselineX = mPageScrolls[index] + scrollOffset; |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1215 | return (int) (child.getX() - baselineX); |
| 1216 | } |
| 1217 | } |
| 1218 | |
Tony Wickham | 023f404 | 2019-01-29 10:52:31 -0800 | [diff] [blame] | 1219 | public void setEnableFreeScroll(boolean freeScroll) { |
Winson Chung | 0e44900 | 2019-04-26 11:09:58 -0700 | [diff] [blame] | 1220 | if (mFreeScroll == freeScroll) { |
| 1221 | return; |
| 1222 | } |
| 1223 | |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1224 | boolean wasFreeScroll = mFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1225 | mFreeScroll = freeScroll; |
| 1226 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1227 | if (mFreeScroll) { |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1228 | setCurrentPage(getNextPage()); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1229 | } else if (wasFreeScroll) { |
Tony Wickham | af33f2c | 2019-10-04 12:55:22 -0700 | [diff] [blame] | 1230 | if (getScrollForPage(getNextPage()) != getScrollX()) { |
| 1231 | snapToPage(getNextPage()); |
| 1232 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1233 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Sunny Goyal | 2f0ec85 | 2015-03-26 13:38:27 -0700 | [diff] [blame] | 1236 | protected void setEnableOverscroll(boolean enable) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1237 | mAllowOverScroll = enable; |
| 1238 | } |
| 1239 | |
Andras Kloczl | 07111f2 | 2022-03-30 17:25:54 +0100 | [diff] [blame] | 1240 | protected boolean isSignificantMove(float absoluteDelta, int pageOrientedSize) { |
| 1241 | return absoluteDelta > pageOrientedSize * SIGNIFICANT_MOVE_THRESHOLD; |
Alex Chau | 822acf4 | 2021-06-03 11:35:05 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1244 | @Override |
| 1245 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1246 | // Skip touch handling if there are no pages to swipe |
Tony Wickham | b6841ac | 2019-07-22 12:39:59 -0700 | [diff] [blame] | 1247 | if (getChildCount() <= 0) return false; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1248 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1249 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1250 | |
| 1251 | final int action = ev.getAction(); |
| 1252 | |
| 1253 | switch (action & MotionEvent.ACTION_MASK) { |
| 1254 | case MotionEvent.ACTION_DOWN: |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1255 | updateIsBeingDraggedOnTouchDown(ev); |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1256 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1257 | /* |
| 1258 | * If being flinged and user touches, stop the fling. isFinished |
| 1259 | * will be false if being flinged. |
| 1260 | */ |
| 1261 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1262 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | // Remember where the motion event started |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1266 | mDownMotionX = ev.getX(); |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1267 | mDownMotionY = ev.getY(); |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1268 | mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, 0); |
| 1269 | mLastMotion = (int) mDownMotionPrimary; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1270 | mTotalMotion = 0; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1271 | mAllowEasyFling = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1272 | mActivePointerId = ev.getPointerId(0); |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1273 | if (mIsBeingDragged) { |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1274 | pageBeginTransition(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1275 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1276 | break; |
| 1277 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1278 | case ACTION_MOVE_ALLOW_EASY_FLING: |
| 1279 | // Start scrolling immediately |
| 1280 | determineScrollingStart(ev); |
| 1281 | mAllowEasyFling = true; |
| 1282 | break; |
| 1283 | |
| 1284 | case MotionEvent.ACTION_MOVE: |
| 1285 | if (mIsBeingDragged) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1286 | // Scroll to follow the motion event |
| 1287 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1288 | |
| 1289 | if (pointerIndex == -1) return true; |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1290 | int oldScroll = mOrientationHandler.getPrimaryScroll(this); |
| 1291 | int dx = (int) ev.getX(pointerIndex); |
| 1292 | int dy = (int) ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1293 | |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1294 | int direction = mOrientationHandler.getPrimaryValue(dx, dy); |
| 1295 | int delta = mLastMotion - direction; |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1296 | |
| 1297 | int width = getWidth(); |
| 1298 | int height = getHeight(); |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1299 | float size = mOrientationHandler.getPrimaryValue(width, height); |
| 1300 | float displacement = (width == 0 || height == 0) ? 0 |
| 1301 | : (float) mOrientationHandler.getSecondaryValue(dx, dy) |
| 1302 | / mOrientationHandler.getSecondaryValue(width, height); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1303 | mTotalMotion += Math.abs(delta); |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1304 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1305 | if (mAllowOverScroll) { |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1306 | int consumed = 0; |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1307 | if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) { |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1308 | consumed = Math.round(size * |
| 1309 | mEdgeGlowRight.onPullDistance(delta / size, displacement)); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1310 | } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) { |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1311 | consumed = Math.round(-size * |
| 1312 | mEdgeGlowLeft.onPullDistance(-delta / size, 1 - displacement)); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1313 | } |
| 1314 | delta -= consumed; |
| 1315 | } |
Alex Chau | 822acf4 | 2021-06-03 11:35:05 +0100 | [diff] [blame] | 1316 | delta /= mOrientationHandler.getPrimaryScale(this); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1317 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1318 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1319 | // keep the remainder because we are actually testing if we've moved from the last |
| 1320 | // scrolled position (which is discrete). |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1321 | mLastMotion = direction; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1322 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1323 | if (delta != 0) { |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1324 | mOrientationHandler.setPrimary(this, VIEW_SCROLL_BY, delta); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (mAllowOverScroll) { |
| 1327 | final float pulledToX = oldScroll + delta; |
| 1328 | |
| 1329 | if (pulledToX < mMinScroll) { |
| 1330 | mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement); |
| 1331 | if (!mEdgeGlowRight.isFinished()) { |
| 1332 | mEdgeGlowRight.onRelease(); |
| 1333 | } |
| 1334 | } else if (pulledToX > mMaxScroll) { |
| 1335 | mEdgeGlowRight.onPullDistance(delta / size, displacement); |
| 1336 | if (!mEdgeGlowLeft.isFinished()) { |
| 1337 | mEdgeGlowLeft.onRelease(); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) { |
| 1342 | postInvalidateOnAnimation(); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1346 | } else { |
| 1347 | awakenScrollBars(); |
| 1348 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1349 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1350 | determineScrollingStart(ev); |
| 1351 | } |
| 1352 | break; |
| 1353 | |
| 1354 | case MotionEvent.ACTION_UP: |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1355 | if (mIsBeingDragged) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1356 | final int activePointerId = mActivePointerId; |
| 1357 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
Vinit Nayak | 93bf1fe | 2020-05-26 17:22:13 -0700 | [diff] [blame] | 1358 | if (pointerIndex == -1) return true; |
| 1359 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1360 | final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, |
| 1361 | pointerIndex); |
Sunny Goyal | 4bb8b06 | 2019-02-19 16:37:38 -0800 | [diff] [blame] | 1362 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1363 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1364 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1365 | int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker, |
Andras Kloczl | 07111f2 | 2022-03-30 17:25:54 +0100 | [diff] [blame] | 1366 | mActivePointerId); |
Alex Chau | 822acf4 | 2021-06-03 11:35:05 +0100 | [diff] [blame] | 1367 | float delta = primaryDirection - mDownMotionPrimary; |
Randy Pfohl | 583241e | 2023-03-02 03:26:56 +0000 | [diff] [blame] | 1368 | |
| 1369 | View current = getPageAt(mCurrentPage); |
| 1370 | if (current == null) { |
| 1371 | Log.e(TAG, "current page was null. this should not happen."); |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
| 1375 | int pageOrientedSize = (int) (mOrientationHandler.getMeasuredSize(current) |
Andras Kloczl | 07111f2 | 2022-03-30 17:25:54 +0100 | [diff] [blame] | 1376 | * mOrientationHandler.getPrimaryScale(this)); |
| 1377 | boolean isSignificantMove = isSignificantMove(Math.abs(delta), pageOrientedSize); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1378 | |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1379 | mTotalMotion += Math.abs(mLastMotion - primaryDirection); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1380 | boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop; |
| 1381 | boolean isFling = passedSlop && shouldFlingForVelocity(velocity); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1382 | boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0; |
| 1383 | boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1384 | if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) { |
| 1385 | Log.d("Quickswitch", "isFling=false vel=" + velocity |
| 1386 | + " threshold=" + mEasyFlingThresholdVelocity); |
| 1387 | } |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1388 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1389 | if (!mFreeScroll) { |
| 1390 | // In the case that the page is moved far to one direction and then is flung |
| 1391 | // in the opposite direction, we use a threshold to determine whether we should |
| 1392 | // just return to the starting page, or if we should skip one further. |
| 1393 | boolean returnToOriginalPage = false; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1394 | if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1395 | Math.signum(velocity) != Math.signum(delta) && isFling) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1396 | returnToOriginalPage = true; |
| 1397 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1398 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1399 | int finalPage; |
| 1400 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1401 | // test for a large move if a fling has been registered. That is, a large |
| 1402 | // move to the left and fling to the right will register as a fling to the right. |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1403 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1404 | if (((isSignificantMove && !isDeltaLeft && !isFling) || |
| 1405 | (isFling && !isVelocityLeft)) && mCurrentPage > 0) { |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 1406 | finalPage = returnToOriginalPage |
| 1407 | ? mCurrentPage : mCurrentPage - getPanelCount(); |
Schneider Victor-tulias | 50f6651 | 2022-12-14 02:53:38 -0800 | [diff] [blame] | 1408 | runOnPageScrollsInitialized( |
| 1409 | () -> snapToPageWithVelocity(finalPage, velocity)); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1410 | } else if (((isSignificantMove && isDeltaLeft && !isFling) || |
| 1411 | (isFling && isVelocityLeft)) && |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1412 | mCurrentPage < getChildCount() - 1) { |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 1413 | finalPage = returnToOriginalPage |
| 1414 | ? mCurrentPage : mCurrentPage + getPanelCount(); |
Schneider Victor-tulias | 50f6651 | 2022-12-14 02:53:38 -0800 | [diff] [blame] | 1415 | runOnPageScrollsInitialized( |
| 1416 | () -> snapToPageWithVelocity(finalPage, velocity)); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1417 | } else { |
Schneider Victor-tulias | 50f6651 | 2022-12-14 02:53:38 -0800 | [diff] [blame] | 1418 | runOnPageScrollsInitialized(this::snapToDestination); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1419 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1420 | } else { |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1421 | if (!mScroller.isFinished()) { |
| 1422 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1423 | } |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1424 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1425 | int initialScroll = mOrientationHandler.getPrimaryScroll(this); |
| 1426 | int maxScroll = mMaxScroll; |
| 1427 | int minScroll = mMinScroll; |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1428 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1429 | if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) || |
| 1430 | ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) { |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1431 | mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0); |
Alex Chau | b794ea6 | 2021-02-03 18:00:30 +0000 | [diff] [blame] | 1432 | mNextPage = getDestinationPage(); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1433 | } else { |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1434 | int velocity1 = -velocity; |
| 1435 | // Continue a scroll or fling in progress |
| 1436 | mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0, |
| 1437 | Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0); |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1438 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1439 | int finalPos = mScroller.getFinalX(); |
Alex Chau | b794ea6 | 2021-02-03 18:00:30 +0000 | [diff] [blame] | 1440 | mNextPage = getDestinationPage(finalPos); |
Schneider Victor-tulias | 50f6651 | 2022-12-14 02:53:38 -0800 | [diff] [blame] | 1441 | runOnPageScrollsInitialized(this::onNotSnappingToPageInFreeScroll); |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1442 | } |
| 1443 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1444 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1445 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1446 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1447 | mEdgeGlowLeft.onRelease(); |
| 1448 | mEdgeGlowRight.onRelease(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1449 | // End any intermediate reordering states |
| 1450 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1451 | break; |
| 1452 | |
| 1453 | case MotionEvent.ACTION_CANCEL: |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1454 | if (mIsBeingDragged) { |
Schneider Victor-tulias | 50f6651 | 2022-12-14 02:53:38 -0800 | [diff] [blame] | 1455 | runOnPageScrollsInitialized(this::snapToDestination); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1456 | } |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1457 | mEdgeGlowLeft.onRelease(); |
| 1458 | mEdgeGlowRight.onRelease(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1459 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1460 | break; |
| 1461 | |
| 1462 | case MotionEvent.ACTION_POINTER_UP: |
| 1463 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1464 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1465 | break; |
| 1466 | } |
| 1467 | |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1471 | protected void onNotSnappingToPageInFreeScroll() { } |
Alex Chau | b794ea6 | 2021-02-03 18:00:30 +0000 | [diff] [blame] | 1472 | |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 1473 | /** |
| 1474 | * Called when the view edges absorb part of the scroll. Subclasses can override this |
| 1475 | * to provide custom behavior during animation. |
| 1476 | */ |
| 1477 | protected void onEdgeAbsorbingScroll() { |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * Called when the current page closest to the center of the screen changes as part of the |
| 1482 | * scroll. Subclasses can override this to provide custom behavior during scroll. |
| 1483 | */ |
| 1484 | protected void onScrollOverPageChanged() { |
| 1485 | } |
| 1486 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1487 | protected boolean shouldFlingForVelocity(int velocity) { |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1488 | float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity; |
| 1489 | return Math.abs(velocity) > threshold; |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Alex Chau | 9d8eeab | 2022-04-05 17:13:56 +0100 | [diff] [blame] | 1492 | protected void resetTouchState() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1493 | releaseVelocityTracker(); |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1494 | mIsBeingDragged = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1495 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1498 | @Override |
| 1499 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1500 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1501 | switch (event.getAction()) { |
| 1502 | case MotionEvent.ACTION_SCROLL: { |
| 1503 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1504 | final float vscroll; |
| 1505 | final float hscroll; |
| 1506 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1507 | vscroll = 0; |
| 1508 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1509 | } else { |
| 1510 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1511 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1512 | } |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 1513 | if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) { |
| 1514 | return false; |
Samuel Fufa | 59cba19 | 2019-08-22 18:31:34 -0700 | [diff] [blame] | 1515 | } |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1516 | if (hscroll != 0 || vscroll != 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1517 | boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1518 | : (hscroll > 0 || vscroll > 0); |
| 1519 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1520 | scrollRight(); |
| 1521 | } else { |
| 1522 | scrollLeft(); |
| 1523 | } |
| 1524 | return true; |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | return super.onGenericMotionEvent(event); |
| 1530 | } |
| 1531 | |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 1532 | /** |
| 1533 | * Returns true if the paged view can scroll for the provided vertical and horizontal |
| 1534 | * scroll values |
| 1535 | */ |
| 1536 | protected boolean canScroll(float absVScroll, float absHScroll) { |
| 1537 | ActivityContext ac = ActivityContext.lookupContext(getContext()); |
| 1538 | return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null); |
Samuel Fufa | 59cba19 | 2019-08-22 18:31:34 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1541 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1542 | if (mVelocityTracker == null) { |
| 1543 | mVelocityTracker = VelocityTracker.obtain(); |
| 1544 | } |
| 1545 | mVelocityTracker.addMovement(ev); |
| 1546 | } |
| 1547 | |
| 1548 | private void releaseVelocityTracker() { |
| 1549 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1550 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1551 | mVelocityTracker.recycle(); |
| 1552 | mVelocityTracker = null; |
| 1553 | } |
| 1554 | } |
| 1555 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1556 | private void onSecondaryPointerUp(MotionEvent ev) { |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1557 | final int pointerIndex = ev.getActionIndex(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1558 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1559 | if (pointerId == mActivePointerId) { |
| 1560 | // This was our active pointer going up. Choose a new |
| 1561 | // active pointer and adjust accordingly. |
| 1562 | // TODO: Make this decision more intelligent. |
| 1563 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
Sunny Goyal | b7c0f3c | 2023-03-15 17:21:57 -0700 | [diff] [blame] | 1564 | mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, newPointerIndex); |
| 1565 | mLastMotion = (int) mDownMotionPrimary; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1566 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1567 | if (mVelocityTracker != null) { |
| 1568 | mVelocityTracker.clear(); |
| 1569 | } |
| 1570 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1573 | @Override |
| 1574 | public void requestChildFocus(View child, View focused) { |
| 1575 | super.requestChildFocus(child, focused); |
Schneider Victor-tulias | bdae57c | 2023-10-10 11:16:54 -0400 | [diff] [blame] | 1576 | if (!shouldHandleRequestChildFocus()) { |
| 1577 | return; |
| 1578 | } |
Andras Kloczl | 359d1ae | 2021-05-04 22:25:27 +0200 | [diff] [blame] | 1579 | // In case the device is controlled by a controller, mCurrentPage isn't updated properly |
| 1580 | // which results in incorrect navigation |
| 1581 | int nextPage = getNextPage(); |
| 1582 | if (nextPage != mCurrentPage) { |
| 1583 | setCurrentPage(nextPage); |
| 1584 | } |
| 1585 | |
Andras Kloczl | 7e91244 | 2021-05-06 12:50:28 +0200 | [diff] [blame] | 1586 | int page = indexOfChild(child); |
| 1587 | if (page >= 0 && !isVisible(page) && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1588 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1589 | } |
| 1590 | } |
| 1591 | |
Schneider Victor-tulias | bdae57c | 2023-10-10 11:16:54 -0400 | [diff] [blame] | 1592 | protected boolean shouldHandleRequestChildFocus() { |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
Alex Chau | b794ea6 | 2021-02-03 18:00:30 +0000 | [diff] [blame] | 1596 | public int getDestinationPage() { |
| 1597 | return getDestinationPage(mOrientationHandler.getPrimaryScroll(this)); |
| 1598 | } |
| 1599 | |
Alex Chau | dedbc8a | 2021-03-17 16:53:26 +0000 | [diff] [blame] | 1600 | protected int getDestinationPage(int primaryScroll) { |
| 1601 | return getPageNearestToCenterOfScreen(primaryScroll); |
Alex Chau | b794ea6 | 2021-02-03 18:00:30 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1604 | public int getPageNearestToCenterOfScreen() { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1605 | return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this)); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Alex Chau | dedbc8a | 2021-03-17 16:53:26 +0000 | [diff] [blame] | 1608 | private int getPageNearestToCenterOfScreen(int primaryScroll) { |
| 1609 | int screenCenter = getScreenCenter(primaryScroll); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1610 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1611 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1612 | final int childCount = getChildCount(); |
| 1613 | for (int i = 0; i < childCount; ++i) { |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1614 | int distanceFromScreenCenter = Math.abs( |
| 1615 | getDisplacementFromScreenCenter(i, screenCenter)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1616 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1617 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1618 | minDistanceFromScreenCenterIndex = i; |
| 1619 | } |
| 1620 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1621 | return minDistanceFromScreenCenterIndex; |
| 1622 | } |
| 1623 | |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1624 | private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) { |
Cole Faust | 50ec4af | 2022-10-15 21:33:28 -0700 | [diff] [blame] | 1625 | int childSize = getChildVisibleSize(childIndex); |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1626 | int halfChildSize = (childSize / 2); |
| 1627 | int childCenter = getChildOffset(childIndex) + halfChildSize; |
| 1628 | return childCenter - screenCenter; |
| 1629 | } |
| 1630 | |
| 1631 | protected int getDisplacementFromScreenCenter(int childIndex) { |
Alex Chau | dedbc8a | 2021-03-17 16:53:26 +0000 | [diff] [blame] | 1632 | int primaryScroll = mOrientationHandler.getPrimaryScroll(this); |
| 1633 | int screenCenter = getScreenCenter(primaryScroll); |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1634 | return getDisplacementFromScreenCenter(childIndex, screenCenter); |
| 1635 | } |
| 1636 | |
Andras Kloczl | 9732df1 | 2021-09-09 19:40:42 +0100 | [diff] [blame] | 1637 | protected int getScreenCenter(int primaryScroll) { |
Alex Chau | dedbc8a | 2021-03-17 16:53:26 +0000 | [diff] [blame] | 1638 | float primaryScale = mOrientationHandler.getPrimaryScale(this); |
| 1639 | float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY()); |
| 1640 | int pageOrientationSize = mOrientationHandler.getMeasuredSize(this); |
| 1641 | return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale |
| 1642 | + primaryPivot); |
| 1643 | } |
| 1644 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1645 | protected void snapToDestination() { |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 1646 | snapToPage(getDestinationPage(), mPageSnapAnimationDuration); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1649 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1650 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1651 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1652 | // of travel has on the overall snap duration. |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1653 | private float distanceInfluenceForSnapDuration(float f) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1654 | f -= 0.5f; // center the values about 0. |
| 1655 | f *= 0.3f * Math.PI / 2.0f; |
| 1656 | return (float) Math.sin(f); |
| 1657 | } |
| 1658 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1659 | protected boolean snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1660 | whichPage = validateNewPage(whichPage); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1661 | int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1662 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1663 | final int newLoc = getScrollForPage(whichPage); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1664 | int delta = newLoc - mOrientationHandler.getPrimaryScroll(this); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1665 | int duration = 0; |
| 1666 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1667 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1668 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1669 | // as opposed to an apparent physical response to flinging |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 1670 | return snapToPage(whichPage, mPageSnapAnimationDuration); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1674 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1675 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1676 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1677 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1678 | float distance = halfScreenSize + halfScreenSize * |
| 1679 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1680 | |
| 1681 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1682 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1683 | |
| 1684 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1685 | // user flings, so we scale the duration by a value near to the derivative of the scroll |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1686 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1687 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1688 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1689 | return snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1692 | public boolean snapToPage(int whichPage) { |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 1693 | return snapToPage(whichPage, mPageSnapAnimationDuration); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1696 | public boolean snapToPageImmediately(int whichPage) { |
Andras Kloczl | c2bd15f | 2022-03-30 18:06:13 +0100 | [diff] [blame] | 1697 | return snapToPage(whichPage, mPageSnapAnimationDuration, true); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1700 | public boolean snapToPage(int whichPage, int duration) { |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1701 | return snapToPage(whichPage, duration, false); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1704 | protected boolean snapToPage(int whichPage, int duration, boolean immediate) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1705 | whichPage = validateNewPage(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1706 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1707 | int newLoc = getScrollForPage(whichPage); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1708 | final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this); |
| 1709 | return snapToPage(whichPage, delta, duration, immediate); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1712 | protected boolean snapToPage(int whichPage, int delta, int duration) { |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1713 | return snapToPage(whichPage, delta, duration, false); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1714 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1715 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1716 | protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) { |
Sunny Goyal | 9bb8ffb | 2018-05-24 17:11:16 -0700 | [diff] [blame] | 1717 | if (mFirstLayout) { |
| 1718 | setCurrentPage(whichPage); |
| 1719 | return false; |
| 1720 | } |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 1721 | |
Brian Isganitis | fdd044e | 2023-03-02 19:45:23 +0000 | [diff] [blame] | 1722 | if (FeatureFlags.IS_STUDIO_BUILD && !Utilities.isRunningInTestHarness()) { |
Tony Wickham | cb3f870 | 2018-08-27 17:36:03 -0700 | [diff] [blame] | 1723 | duration *= Settings.Global.getFloat(getContext().getContentResolver(), |
| 1724 | Settings.Global.WINDOW_ANIMATION_SCALE, 1); |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1727 | whichPage = validateNewPage(whichPage); |
| 1728 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1729 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1730 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1731 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1732 | if (immediate) { |
| 1733 | duration = 0; |
| 1734 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1735 | duration = Math.abs(delta); |
| 1736 | } |
| 1737 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1738 | if (duration != 0) { |
| 1739 | pageBeginTransition(); |
| 1740 | } |
| 1741 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1742 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1743 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1744 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1745 | |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1746 | mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration); |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 1747 | updatePageIndicator(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1748 | |
| 1749 | // Trigger a compute() to finish switching pages if necessary |
| 1750 | if (immediate) { |
| 1751 | computeScroll(); |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 1752 | pageEndTransition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1755 | invalidate(); |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1756 | return Math.abs(delta) > 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1759 | public boolean scrollLeft() { |
| 1760 | if (getNextPage() > 0) { |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1761 | snapToPage(getNextPage() - getPanelCount()); |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1762 | return true; |
| 1763 | } |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1764 | return mAllowOverScroll; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1767 | public boolean scrollRight() { |
| 1768 | if (getNextPage() < getChildCount() - 1) { |
Andras Kloczl | 77b3eb0 | 2021-07-15 11:15:24 +0200 | [diff] [blame] | 1769 | snapToPage(getNextPage() + getPanelCount()); |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1770 | return true; |
| 1771 | } |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1772 | return mAllowOverScroll; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Vadim Tryshev | e6bbefb | 2018-04-03 13:38:06 -0700 | [diff] [blame] | 1775 | @Override |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 1776 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
Lais Andrade | f6e31b1 | 2021-10-27 12:04:28 +0100 | [diff] [blame] | 1777 | if (mScroller.isFinished()) { |
| 1778 | // This was not caused by the scroller, skip it. |
| 1779 | return; |
| 1780 | } |
Lais Andrade | cc5c884 | 2021-06-04 18:20:30 +0100 | [diff] [blame] | 1781 | int newDestinationPage = getDestinationPage(); |
| 1782 | if (newDestinationPage >= 0 && newDestinationPage != mCurrentScrollOverPage) { |
| 1783 | mCurrentScrollOverPage = newDestinationPage; |
| 1784 | onScrollOverPageChanged(); |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | @Override |
Vadim Tryshev | e6bbefb | 2018-04-03 13:38:06 -0700 | [diff] [blame] | 1789 | public CharSequence getAccessibilityClassName() { |
| 1790 | // Some accessibility services have special logic for ScrollView. Since we provide same |
| 1791 | // accessibility info as ScrollView, inform the service to handle use the same way. |
| 1792 | return ScrollView.class.getName(); |
| 1793 | } |
| 1794 | |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1795 | protected boolean isPageOrderFlipped() { |
| 1796 | return false; |
| 1797 | } |
| 1798 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1799 | /* Accessibility */ |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 1800 | @SuppressWarnings("deprecation") |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1801 | @Override |
| 1802 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1803 | super.onInitializeAccessibilityNodeInfo(info); |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1804 | final boolean pagesFlipped = isPageOrderFlipped(); |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1805 | info.setScrollable(getPageCount() > 0); |
| 1806 | int primaryScroll = mOrientationHandler.getPrimaryScroll(this); |
| 1807 | if (getCurrentPage() < getPageCount() - getPanelCount() |
| 1808 | || (getCurrentPage() == getPageCount() - getPanelCount() |
| 1809 | && primaryScroll != getScrollForPage(getPageCount() - getPanelCount()))) { |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1810 | info.addAction(pagesFlipped ? |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1811 | AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD |
| 1812 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD); |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1813 | info.addAction(mIsRtl ? |
| 1814 | AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT |
| 1815 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1816 | } |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1817 | if (getCurrentPage() > 0 |
| 1818 | || (getCurrentPage() == 0 && primaryScroll != getScrollForPage(0))) { |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1819 | info.addAction(pagesFlipped ? |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1820 | AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD |
| 1821 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD); |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1822 | info.addAction(mIsRtl ? |
| 1823 | AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT |
| 1824 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1825 | } |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 1826 | // Accessibility-wise, PagedView doesn't support long click, so disabling it. |
| 1827 | // Besides disabling the accessibility long-click, this also prevents this view from getting |
| 1828 | // accessibility focus. |
| 1829 | info.setLongClickable(false); |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 1830 | info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 1834 | public void sendAccessibilityEvent(int eventType) { |
| 1835 | // Don't let the view send real scroll events. |
| 1836 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 1837 | super.sendAccessibilityEvent(eventType); |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1842 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1843 | super.onInitializeAccessibilityEvent(event); |
Pinyao Ting | af6daa2 | 2019-09-18 22:18:03 +0000 | [diff] [blame] | 1844 | event.setScrollable(mAllowOverScroll || getPageCount() > 1); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1847 | @Override |
| 1848 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 1849 | if (super.performAccessibilityAction(action, arguments)) { |
| 1850 | return true; |
| 1851 | } |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1852 | final boolean pagesFlipped = isPageOrderFlipped(); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1853 | switch (action) { |
| 1854 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1855 | if (pagesFlipped ? scrollLeft() : scrollRight()) { |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1856 | return true; |
| 1857 | } |
| 1858 | } break; |
| 1859 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1860 | if (pagesFlipped ? scrollRight() : scrollLeft()) { |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1861 | return true; |
| 1862 | } |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1863 | } break; |
| 1864 | case android.R.id.accessibilityActionPageRight: { |
| 1865 | if (!mIsRtl) { |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1866 | return scrollRight(); |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1867 | } else { |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1868 | return scrollLeft(); |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1869 | } |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1870 | } |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1871 | case android.R.id.accessibilityActionPageLeft: { |
| 1872 | if (!mIsRtl) { |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1873 | return scrollLeft(); |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1874 | } else { |
Alex Chau | 9ead9ca | 2021-11-26 16:22:49 +0000 | [diff] [blame] | 1875 | return scrollRight(); |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1876 | } |
| 1877 | } |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1878 | } |
| 1879 | return false; |
| 1880 | } |
| 1881 | |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 1882 | protected boolean canAnnouncePageDescription() { |
| 1883 | return true; |
| 1884 | } |
| 1885 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1886 | protected String getCurrentPageDescription() { |
Sunny Goyal | f4f89ef | 2015-09-02 15:06:12 -0700 | [diff] [blame] | 1887 | return getContext().getString(R.string.default_scroll_format, |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1888 | getNextPage() + 1, getChildCount()); |
| 1889 | } |
| 1890 | |
Tony Mak | 2e56440 | 2018-02-27 17:19:34 +0000 | [diff] [blame] | 1891 | protected float getDownMotionX() { |
| 1892 | return mDownMotionX; |
| 1893 | } |
| 1894 | |
| 1895 | protected float getDownMotionY() { |
| 1896 | return mDownMotionY; |
| 1897 | } |
| 1898 | |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 1899 | protected interface ComputePageScrollsLogic { |
| 1900 | |
| 1901 | boolean shouldIncludeView(View view); |
| 1902 | } |
Vadim Tryshev | 528b9e0 | 2018-05-24 18:22:03 -0700 | [diff] [blame] | 1903 | |
| 1904 | public int[] getVisibleChildrenRange() { |
| 1905 | float visibleLeft = 0; |
| 1906 | float visibleRight = visibleLeft + getMeasuredWidth(); |
| 1907 | float scaleX = getScaleX(); |
| 1908 | if (scaleX < 1 && scaleX > 0) { |
| 1909 | float mid = getMeasuredWidth() / 2; |
| 1910 | visibleLeft = mid - ((mid - visibleLeft) / scaleX); |
| 1911 | visibleRight = mid + ((visibleRight - mid) / scaleX); |
| 1912 | } |
| 1913 | |
| 1914 | int leftChild = -1; |
| 1915 | int rightChild = -1; |
| 1916 | final int childCount = getChildCount(); |
| 1917 | for (int i = 0; i < childCount; i++) { |
| 1918 | final View child = getPageAt(i); |
| 1919 | |
| 1920 | float left = child.getLeft() + child.getTranslationX() - getScrollX(); |
| 1921 | if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) { |
| 1922 | if (leftChild == -1) { |
| 1923 | leftChild = i; |
| 1924 | } |
| 1925 | rightChild = i; |
| 1926 | } |
| 1927 | } |
| 1928 | mTmpIntPair[0] = leftChild; |
| 1929 | mTmpIntPair[1] = rightChild; |
| 1930 | return mTmpIntPair; |
| 1931 | } |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1932 | |
| 1933 | @Override |
| 1934 | public void draw(Canvas canvas) { |
| 1935 | super.draw(canvas); |
| 1936 | drawEdgeEffect(canvas); |
Sunny Goyal | 05b59c0 | 2021-04-29 13:55:40 -0700 | [diff] [blame] | 1937 | pageEndTransition(); |
Sunny Goyal | c39db73 | 2021-04-03 01:20:21 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | protected void drawEdgeEffect(Canvas canvas) { |
| 1941 | if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) { |
| 1942 | final int width = getWidth(); |
| 1943 | final int height = getHeight(); |
| 1944 | if (!mEdgeGlowLeft.isFinished()) { |
| 1945 | final int restoreCount = canvas.save(); |
| 1946 | canvas.rotate(-90); |
| 1947 | canvas.translate(-height, Math.min(mMinScroll, getScrollX())); |
| 1948 | mEdgeGlowLeft.setSize(height, width); |
| 1949 | if (mEdgeGlowLeft.draw(canvas)) { |
| 1950 | postInvalidateOnAnimation(); |
| 1951 | } |
| 1952 | canvas.restoreToCount(restoreCount); |
| 1953 | } |
| 1954 | if (!mEdgeGlowRight.isFinished()) { |
| 1955 | final int restoreCount = canvas.save(); |
| 1956 | canvas.rotate(90, width, 0); |
| 1957 | canvas.translate(width, -(Math.max(mMaxScroll, getScrollX()))); |
| 1958 | |
| 1959 | mEdgeGlowRight.setSize(height, width); |
| 1960 | if (mEdgeGlowRight.draw(canvas)) { |
| 1961 | postInvalidateOnAnimation(); |
| 1962 | } |
| 1963 | canvas.restoreToCount(restoreCount); |
| 1964 | } |
| 1965 | } |
| 1966 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 1967 | } |