blob: 1fede5644276efac1ae9944d04f586a0309366d1 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Kateryna Ivanova71203732023-05-24 15:09:00 +000019import static com.android.app.animation.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070020import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
21import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Jagrut Desai311d5192023-09-13 10:18:02 -070022import static com.android.launcher3.testing.shared.TestProtocol.SCROLL_FINISHED_MESSAGE;
Sunny Goyal8b37c572020-03-31 12:12:14 -070023import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070024import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
25import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
26
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070027import android.animation.LayoutTransition;
Sunny Goyalcf25b522015-07-09 00:01:18 -070028import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.content.Context;
Andras Kloczlc2bd15f2022-03-30 18:06:13 +010030import android.content.res.Configuration;
31import android.content.res.Resources;
Adam Cohen9c4949e2010-10-05 12:27:22 -070032import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080033import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070035import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070036import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070037import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
49import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalc39db732021-04-03 01:20:21 -070050import android.widget.OverScroller;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070051import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
Tony Wickham62117d72020-03-26 16:56:26 -070053import androidx.annotation.Nullable;
54
Jon Miranda9e198662020-03-11 14:42:02 -070055import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070058import com.android.launcher3.touch.PagedOrientationHandler;
59import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070060import com.android.launcher3.util.EdgeEffectCompat;
Andras Kloczl55edfe52021-05-14 12:21:30 +020061import com.android.launcher3.util.IntSet;
Jon Miranda9e198662020-03-11 14:42:02 -070062import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070063import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070064
65import java.util.ArrayList;
Sunny Goyal59969372021-05-06 12:11:44 -070066import java.util.function.Consumer;
Jon Miranda9e198662020-03-11 14:42:02 -070067
Winson Chung321e9ee2010-08-09 13:37:56 -070068/**
69 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070070 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070071 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080072public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070073 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070074 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070075 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070076
Winson Chung4941f652020-06-18 15:11:53 -070077 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053078 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070079 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Adam Cohenb64cb5a2011-02-15 13:53:42 -080081 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080082 // 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 Cohen68d73932010-11-15 10:50:58 -080084
Sunny Goyal8e2133b2015-05-06 13:39:07 -070085 private static final float MAX_SCROLL_PROGRESS = 1.0f;
86
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070088
Andras Kloczlc2bd15f2022-03-30 18:06:13 +010089 private int mFlingThresholdVelocity;
90 private int mEasyFlingThresholdVelocity;
91 private int mMinFlingVelocity;
92 private int mMinSnapVelocity;
93 private int mPageSnapAnimationDuration;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected boolean mFirstLayout = true;
96
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mCurrentPage;
Alex Chauba61b982021-06-15 16:29:19 +010099 // 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 Andradecc5c8842021-06-04 18:20:30 +0100103 // The current page the PagedView is scrolling over on it's way to the destination page.
104 protected int mCurrentScrollOverPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700105
Sunny Goyal4ffec482016-02-09 11:28:52 -0800106 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800108 protected int mMaxScroll;
109 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700110 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800112 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
114 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700115 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800116 private float mDownMotionPrimary;
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -0700117 private int mLastMotion;
Vinit Nayaka406f722019-12-19 11:50:55 -0800118 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700119 // Used in special cases where the fling checks can be relaxed for an intentional gesture
120 private boolean mAllowEasyFling;
Jordan Silva715e25a2024-01-30 18:02:27 +0000121 private PagedOrientationHandler mOrientationHandler =
122 PagedOrientationHandler.DEFAULT;
Adam Cohenedb40762013-07-18 16:45:45 -0700123
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700124 private final ArrayList<Runnable> mOnPageScrollsInitializedCallbacks = new ArrayList<>();
125
126 // We should always check pageScrollsInitialized() is true when using mPageScrolls.
127 @Nullable protected int[] mPageScrolls = null;
Sunny Goyalaad33592018-08-07 17:20:35 -0700128 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800129
Winson Chung4941f652020-06-18 15:11:53 -0700130 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800131 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700132 // The amount of movement to begin paging
133 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800135 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Michael Jurka5f1c5092010-09-03 14:15:02 -0700137 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700141 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700142 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700143
Winson Chungd2be3812013-07-16 11:11:32 -0700144 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800146 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700147
John Spurlock77e1f472013-09-11 10:09:51 -0400148 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800149 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400150
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800151 // Similar to the platform implementation of isLayoutValid();
152 protected boolean mIsLayoutValid;
153
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700154 private int[] mTmpIntPair = new int[2];
155
Sunny Goyalc39db732021-04-03 01:20:21 -0700156 protected EdgeEffectCompat mEdgeGlowLeft;
157 protected EdgeEffectCompat mEdgeGlowRight;
158
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 public PagedView(Context context) {
160 this(context, null);
161 }
162
163 public PagedView(Context context, AttributeSet attrs) {
164 this(context, attrs, 0);
165 }
166
167 public PagedView(Context context, AttributeSet attrs, int defStyle) {
168 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700169
Adam Cohen9c4949e2010-10-05 12:27:22 -0700170 TypedArray a = context.obtainStyledAttributes(attrs,
171 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700172 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700173 a.recycle();
174
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700176 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700177
Sunny Goyalc39db732021-04-03 01:20:21 -0700178 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700179 mCurrentPage = 0;
Lais Andradecc5c8842021-06-04 18:20:30 +0100180 mCurrentScrollOverPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700181
Vinit Nayaka406f722019-12-19 11:50:55 -0800182 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700183 mTouchSlop = configuration.getScaledTouchSlop();
184 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700185 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800186
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100187 updateVelocityValues();
Sunny Goyala9f55462018-03-25 18:15:15 -0700188
Sunny Goyalc39db732021-04-03 01:20:21 -0700189 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700190 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700191 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 }
193
Sunny Goyalc39db732021-04-03 01:20:21 -0700194 protected void initEdgeEffect() {
195 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
196 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800197 }
198
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700199 public void initParentViews(View parent) {
200 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800201 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Tony Wickhambab101c2021-09-20 15:12:44 -0700202 mPageIndicator.setMarkersCount(getChildCount() / getPanelCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700203 }
204 }
205
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800206 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700207 return mPageIndicator;
208 }
209
Adam Cohen674531f2013-12-13 15:07:14 -0800210 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700211 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
212 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000213 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700214 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700216 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700217 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700219
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700220 /**
221 * Returns the index of page to be shown immediately afterwards.
222 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700223 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700224 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
225 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700226
Adam Cohenf9c184a2016-01-15 16:47:43 -0800227 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 return getChildCount();
229 }
230
Sunny Goyal83a8f042015-05-19 12:52:12 -0700231 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 return getChildAt(index);
233 }
234
Jordan Silva715e25a2024-01-30 18:02:27 +0000235 protected PagedOrientationHandler getPagedOrientationHandler() {
236 return mOrientationHandler;
237 }
238
239 protected void setOrientationHandler(PagedOrientationHandler orientationHandler) {
240 this.mOrientationHandler = orientationHandler;
241 }
242
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800244 * Updates the scroll of the current page immediately to its final scroll position. We use this
245 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
246 * the previous tab page.
247 */
248 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700249 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800250 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700251 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Alex Chauba61b982021-06-15 16:29:19 +0100252 newPosition = getScrollForPage(mCurrentPage) + mCurrentPageScrollDiff;
Adam Cohen0ffac432013-07-10 11:19:26 -0700253 }
Alex Chau6499e832021-11-19 14:35:46 +0000254 mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700255 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Tony Wickhamd5521322021-09-14 14:26:50 -0700256 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800257 }
258
Tony Wickhamf8d731a2021-05-26 14:58:13 -0700259 /**
260 * Immediately finishes any overscroll effect and jumps to the end of the scroller animation.
261 */
262 public void abortScrollerAnimation() {
263 mEdgeGlowLeft.finish();
264 mEdgeGlowRight.finish();
265 abortScrollerAnimation(true);
266 }
267
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700268 protected void onScrollerAnimationAborted() {
269 // No-Op
270 }
271
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700272 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273 mScroller.abortAnimation();
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700274 onScrollerAnimationAborted();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700275 // We need to clean up the next page here to avoid computeScrollHelper from
276 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700277 if (resetNextPage) {
278 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800279 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700280 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700281 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700282
Tony Wickhamd5521322021-09-14 14:26:50 -0700283 /**
284 * Immediately finishes any in-progress scroll, maintaining the current position. Also sets
285 * mNextPage = INVALID_PAGE and calls pageEndTransition().
286 */
287 public void forceFinishScroller() {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700288 mScroller.forceFinished(true);
289 // We need to clean up the next page here to avoid computeScrollHelper from
290 // updating current page on the pass.
Tony Wickhamd5521322021-09-14 14:26:50 -0700291 mNextPage = INVALID_PAGE;
292 pageEndTransition();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700293 }
294
Adam Cohen6f127a62014-06-12 14:54:41 -0700295 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700296 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700297 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100298 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
299
300 if (getPanelCount() > 1) {
Alex Chau1e448462021-08-13 21:48:35 +0100301 // Always return left most panel as new page
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100302 newPage = getLeftmostVisiblePageForIndex(newPage);
303 }
304 return newPage;
305 }
306
Andras Kloczl55edfe52021-05-14 12:21:30 +0200307 /**
308 * In most cases where panelCount is 1, this method will just return the page index that was
309 * passed in.
310 * But for example when two panel home is enabled we might need the leftmost visible page index
311 * because that page is the current page.
312 */
313 public int getLeftmostVisiblePageForIndex(int pageIndex) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100314 int panelCount = getPanelCount();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200315 return pageIndex - pageIndex % panelCount;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100316 }
317
318 /**
319 * Returns the number of pages that are shown at the same time.
320 */
321 protected int getPanelCount() {
322 return 1;
323 }
324
325 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +0200326 * Returns an IntSet with the indices of the currently visible pages
327 */
Andras Kloczl55edfe52021-05-14 12:21:30 +0200328 public IntSet getVisiblePageIndices() {
Andras Kloczl7e912442021-05-06 12:50:28 +0200329 return getPageIndices(mCurrentPage);
330 }
331
332 /**
333 * In case the panelCount is 1 this just returns the same page index in an IntSet.
334 * But in cases where the panelCount > 1 this will return all the page indices that belong
335 * together, i.e. on the Workspace they are next to each other and shown at the same time.
336 */
337 private IntSet getPageIndices(int pageIndex) {
338 // we want to make sure the pageIndex is the leftmost page
339 pageIndex = getLeftmostVisiblePageForIndex(pageIndex);
340
341 IntSet pageIndices = new IntSet();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200342 int panelCount = getPanelCount();
343 int pageCount = getPageCount();
Andras Kloczl7e912442021-05-06 12:50:28 +0200344 for (int page = pageIndex; page < pageIndex + panelCount && page < pageCount; page++) {
345 pageIndices.add(page);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200346 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200347 return pageIndices;
348 }
349
350 /**
351 * Returns an IntSet with the indices of the neighbour pages that are in the focus direction.
352 */
353 private IntSet getNeighbourPageIndices(int focus) {
354 int panelCount = getPanelCount();
355 // getNextPage is more reliable than getCurrentPage
356 int currentPage = getNextPage();
357
358 int nextPage;
359 if (focus == View.FOCUS_LEFT) {
360 nextPage = currentPage - panelCount;
361 } else if (focus == View.FOCUS_RIGHT) {
362 nextPage = currentPage + panelCount;
363 } else {
Andras Kloczlb0632942021-08-03 14:03:58 +0200364 // no neighbours to other directions
Andras Kloczl7e912442021-05-06 12:50:28 +0200365 return new IntSet();
366 }
367 nextPage = validateNewPage(nextPage);
368 if (nextPage == currentPage) {
369 // We reached the end of the pages
370 return new IntSet();
371 }
372
Andras Kloczlb0632942021-08-03 14:03:58 +0200373 return getPageIndices(nextPage);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200374 }
375
376 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700377 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100378 */
Sunny Goyal59969372021-05-06 12:11:44 -0700379 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200380 getVisiblePageIndices().forEach(pageIndex -> {
381 View page = getPageAt(pageIndex);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100382 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700383 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100384 }
Andras Kloczl55edfe52021-05-14 12:21:30 +0200385 });
Andras Kloczl2bb51342021-03-20 01:13:45 +0100386 }
387
388 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100389 * Returns true if the view is on one of the current pages, false otherwise.
390 */
391 public boolean isVisible(View child) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200392 return isVisible(indexOfChild(child));
393 }
394
395 /**
396 * Returns true if the page with the given index is currently visible, false otherwise.
397 */
398 private boolean isVisible(int pageIndex) {
399 return getLeftmostVisiblePageForIndex(pageIndex) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700400 }
401
Chet Haasebc2f0822012-10-26 17:59:53 -0700402 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700403 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
404 */
405 private int ensureWithinScrollBounds(int page) {
406 int dir = !mIsRtl ? 1 : - 1;
407 int currScroll = getScrollForPage(page);
408 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800409 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700410 page += dir;
411 prevScroll = currScroll;
412 currScroll = getScrollForPage(page);
413 if (currScroll <= prevScroll) {
414 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
415 break;
416 }
417 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800418 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700419 page -= dir;
420 prevScroll = currScroll;
421 currScroll = getScrollForPage(page);
422 if (currScroll >= prevScroll) {
423 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
424 break;
425 }
426 }
427 return page;
428 }
429
Tony Wickham03f27012019-04-25 14:22:54 -0700430 public void setCurrentPage(int currentPage) {
431 setCurrentPage(currentPage, INVALID_PAGE);
432 }
433
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700434 /**
Winson Chung86f77532010-08-24 11:08:22 -0700435 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 */
Tony Wickham03f27012019-04-25 14:22:54 -0700437 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800438 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700439 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800440 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800441 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
442 // the default
443 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800444 return;
445 }
Tony Wickham03f27012019-04-25 14:22:54 -0700446 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700447 mCurrentPage = validateNewPage(currentPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100448 mCurrentScrollOverPage = mCurrentPage;
Winson Chung181c3dc2013-07-17 15:36:20 -0700449 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700450 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800451 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 }
453
Winson Chung8c87cd82013-07-23 16:20:10 -0700454 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800455 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
456 * has settled.
457 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700458 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800459 updatePageIndicator();
460 }
461
462 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700463 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000464 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700465 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700467 protected void pageBeginTransition() {
468 if (!mIsPageInTransition) {
469 mIsPageInTransition = true;
470 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700471 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700472 }
473
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700474 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700475 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700476 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700477 mIsPageInTransition = false;
478 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700479 }
Michael Jurka0142d492010-08-25 17:46:15 -0700480 }
481
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700482 @Override
483 public void onVisibilityAggregated(boolean isVisible) {
484 pageEndTransition();
485 super.onVisibilityAggregated(isVisible);
486 }
487
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700488 protected boolean isPageInTransition() {
489 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700490 }
491
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700492 /**
493 * Called when the page starts moving as part of the scroll. Subclasses can override this
494 * to provide custom behavior during animation.
495 */
496 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700497 }
498
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700499 /**
500 * Called when the page ends moving as part of the scroll. Subclasses can override this
501 * to provide custom behavior during animation.
502 */
503 protected void onPageEndTransition() {
Alex Chauba61b982021-06-15 16:29:19 +0100504 mCurrentPageScrollDiff = 0;
Jagrut Desai311d5192023-09-13 10:18:02 -0700505 AccessibilityManagerCompat.sendTestProtocolEventToTest(getContext(),
506 SCROLL_FINISHED_MESSAGE);
Tony Wickham619daaf2020-02-07 12:29:06 -0800507 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
508 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700509 if (mOnPageTransitionEndCallback != null) {
510 mOnPageTransitionEndCallback.run();
511 mOnPageTransitionEndCallback = null;
512 }
513 }
514
515 /**
516 * Sets a callback to run once when the scrolling finishes. If there is currently
517 * no page in transition, then the callback is called immediately.
518 */
519 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
520 if (mIsPageInTransition || callback == null) {
521 mOnPageTransitionEndCallback = callback;
522 } else {
523 callback.run();
524 }
Michael Jurka0142d492010-08-25 17:46:15 -0700525 }
526
Adam Cohen68d73932010-11-15 10:50:58 -0800527 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700528 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700529 x = Utilities.boundToRange(x,
530 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
531 y = Utilities.boundToRange(y,
532 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800533 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700534 }
535
Adam Cohen53805212013-10-01 10:39:23 -0700536 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800537 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700538 if (mCurrentPage != getNextPage()) {
539 AccessibilityEvent ev =
540 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700541 ev.setScrollable(true);
542 ev.setScrollX(getScrollX());
543 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800544 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700545 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700546 }
Adam Cohen53805212013-10-01 10:39:23 -0700547 }
548 }
549
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700550 protected void announcePageForAccessibility() {
551 if (isAccessibilityEnabled(getContext())) {
552 // Notify the user when the page changes
553 announceForAccessibility(getCurrentPageDescription());
554 }
555 }
556
Winson Chung85425412021-05-10 16:24:14 -0700557 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700559 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700560 int oldPos = mOrientationHandler.getPrimaryScroll(this);
561 int newPos = mScroller.getCurrX();
562 if (oldPos != newPos) {
Alex Chau6499e832021-11-19 14:35:46 +0000563 mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700564 }
Winson Chung85425412021-05-10 16:24:14 -0700565
566 if (mAllowOverScroll) {
567 if (newPos < mMinScroll && oldPos >= mMinScroll) {
568 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700569 abortScrollerAnimation(false);
Lais Andradecc5c8842021-06-04 18:20:30 +0100570 onEdgeAbsorbingScroll();
Winson Chung85425412021-05-10 16:24:14 -0700571 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
572 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700573 abortScrollerAnimation(false);
Lais Andradecc5c8842021-06-04 18:20:30 +0100574 onEdgeAbsorbingScroll();
Sunny Goyalc39db732021-04-03 01:20:21 -0700575 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800576 }
Adam Cohen53805212013-10-01 10:39:23 -0700577
Winson Chung85425412021-05-10 16:24:14 -0700578 // If the scroller has scrolled to the final position and there is no edge effect, then
579 // finish the scroller to skip waiting for additional settling
580 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
581 mScroller.getFinalY());
582 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700583 abortScrollerAnimation(false);
Winson Chung85425412021-05-10 16:24:14 -0700584 }
585
586 invalidate();
587 return true;
588 } else if (mNextPage != INVALID_PAGE) {
589 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700590 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700591 mCurrentPage = validateNewPage(mNextPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100592 mCurrentScrollOverPage = mCurrentPage;
Winson Chung86f77532010-08-24 11:08:22 -0700593 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700594 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700595
Adam Cohen73aa9752010-11-24 16:26:58 -0800596 // We don't want to trigger a page end moving unless the page has settled
597 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700598 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700599 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800600 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700601
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700602 if (canAnnouncePageDescription()) {
603 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700604 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 }
Michael Jurka0142d492010-08-25 17:46:15 -0700606 return false;
607 }
608
609 @Override
610 public void computeScroll() {
611 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700612 }
613
Sunny Goyalce8809a2018-01-18 14:02:47 -0800614 public int getExpectedHeight() {
615 return getMeasuredHeight();
616 }
617
Adam Cohen410f3cd2013-09-22 12:09:32 -0700618 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800619 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800620 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700621 }
622
Sunny Goyalce8809a2018-01-18 14:02:47 -0800623 public int getExpectedWidth() {
624 return getMeasuredWidth();
625 }
626
Sunny Goyalbc683e92017-12-06 10:25:07 -0800627 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800628 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800629 - mInsets.left - mInsets.right;
630 }
631
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100632 private void updateVelocityValues() {
633 Resources res = getResources();
634 mFlingThresholdVelocity = res.getDimensionPixelSize(R.dimen.fling_threshold_velocity);
635 mEasyFlingThresholdVelocity =
636 res.getDimensionPixelSize(R.dimen.easy_fling_threshold_velocity);
637 mMinFlingVelocity = res.getDimensionPixelSize(R.dimen.min_fling_velocity);
638 mMinSnapVelocity = res.getDimensionPixelSize(R.dimen.min_page_snap_velocity);
639 mPageSnapAnimationDuration = res.getInteger(R.integer.config_pageSnapAnimationDuration);
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -0500640 onVelocityValuesUpdated();
641 }
642
643 protected void onVelocityValuesUpdated() {
644 // Overridden in RecentsView
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100645 }
646
647 @Override
648 protected void onConfigurationChanged(Configuration newConfig) {
649 super.onConfigurationChanged(newConfig);
650 updateVelocityValues();
651 }
652
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800654 public void requestLayout() {
655 mIsLayoutValid = false;
656 super.requestLayout();
657 }
658
659 @Override
660 public void forceLayout() {
661 mIsLayoutValid = false;
662 super.forceLayout();
663 }
664
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100665 private int getPageWidthSize(int widthSize) {
Thales Lima7a6752d2021-09-23 14:31:00 +0100666 // It's necessary to add the padding back because it is remove when measuring children,
667 // like when MeasureSpec.getSize in CellLayout.
668 return (widthSize - mInsets.left - mInsets.right - getPaddingLeft() - getPaddingRight())
669 / getPanelCount() + getPaddingLeft() + getPaddingRight();
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100670 }
671
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800672 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700674 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700675 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
676 return;
677 }
678
Adam Cohen7d30a372013-07-01 17:03:59 -0700679 // We measure the dimensions of the PagedView to be larger than the pages so that when we
680 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700681 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
682 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
683 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700684 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700685
Adam Cohen7d30a372013-07-01 17:03:59 -0700686 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
687 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
688 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 }
690
Winson Chung8aad6102012-05-11 16:27:49 -0700691 // Return early if we aren't given a proper dimension
692 if (widthSize <= 0 || heightSize <= 0) {
693 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
694 return;
695 }
696
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700698 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700699 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700700
Sunny Goyalac00cba2017-11-13 15:58:01 -0800701 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100702 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800703 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800704 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700705
Sunny Goyalac00cba2017-11-13 15:58:01 -0800706 // measureChildren takes accounts for content padding, we only need to care about extra
707 // space due to insets.
708 measureChildren(myWidthSpec, myHeightSpec);
709 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800710 }
711
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700712 /** Returns true iff this PagedView's scroll amounts are initialized to each page index. */
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800713 protected boolean isPageScrollsInitialized() {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700714 return mPageScrolls != null && mPageScrolls.length == getChildCount();
715 }
716
717 /**
718 * Queues the given callback to be run once {@code mPageScrolls} has been initialized.
719 */
720 public void runOnPageScrollsInitialized(Runnable callback) {
721 mOnPageScrollsInitializedCallbacks.add(callback);
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800722 if (isPageScrollsInitialized()) {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700723 onPageScrollsInitialized();
724 }
725 }
726
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800727 protected void onPageScrollsInitialized() {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700728 for (Runnable callback : mOnPageScrollsInitializedCallbacks) {
729 callback.run();
730 }
731 mOnPageScrollsInitializedCallbacks.clear();
732 }
733
Sunny Goyalcf25b522015-07-09 00:01:18 -0700734 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700736 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800737 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700738 final int childCount = getChildCount();
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700739 int[] pageScrolls = mPageScrolls;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700740 boolean pageScrollChanged = false;
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800741 if (!isPageScrollsInitialized()) {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700742 pageScrolls = new int[childCount];
Vadim Trysheveb701c62018-05-14 16:00:15 -0700743 pageScrollChanged = true;
744 }
745
Winson Chung785d2eb2011-04-14 16:08:02 -0700746 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700747
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700748 pageScrollChanged |= getPageScrolls(pageScrolls, true, SIMPLE_SCROLL_LOGIC);
749 mPageScrolls = pageScrolls;
Winson Chungc3665fa2011-09-14 17:56:27 -0700750
Winson Chung359cb5d2022-05-26 23:38:48 +0000751 if (childCount == 0) {
752 onPageScrollsInitialized();
753 return;
754 }
755
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700756 final LayoutTransition transition = getLayoutTransition();
757 // If the transition is running defer updating max scroll, as some empty pages could
758 // still be present, and a max scroll change could cause sudden jumps in scroll.
759 if (transition != null && transition.isRunning()) {
760 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
761
762 @Override
763 public void startTransition(LayoutTransition transition, ViewGroup container,
764 View view, int transitionType) { }
765
766 @Override
767 public void endTransition(LayoutTransition transition, ViewGroup container,
768 View view, int transitionType) {
769 // Wait until all transitions are complete.
770 if (!transition.isRunning()) {
771 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700772 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700773 }
774 }
775 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700776 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700777 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700778 }
779
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700780 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
781 updateCurrentPageScroll();
782 mFirstLayout = false;
783 }
784
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800785 if (mScroller.isFinished() && pageScrollChanged) {
Alex Chau0733ebd2022-10-07 15:11:09 +0100786 // TODO(b/246283207): Remove logging once root cause of flake detected.
Brian Isganitisfdd044e2023-03-02 19:45:23 +0000787 if (Utilities.isRunningInTestHarness() && !(this instanceof Workspace)) {
Alex Chau0733ebd2022-10-07 15:11:09 +0100788 Log.d("b/246283207", this.getClass().getSimpleName() + "#onLayout() -> "
Pat Manning24b95542022-08-09 18:06:58 +0100789 + "if(mScroller.isFinished() && pageScrollChanged) -> getNextPage(): "
790 + getNextPage() + ", getScrollForPage(getNextPage()): "
791 + getScrollForPage(getNextPage()));
792 }
Sunny Goyalc82c6392018-06-06 15:39:13 -0700793 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700794 }
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700795 onPageScrollsInitialized();
Winson Chunge3193b92010-09-10 11:44:42 -0700796 }
797
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700798 /**
799 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
800 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700801 */
802 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
803 ComputePageScrollsLogic scrollLogic) {
804 final int childCount = getChildCount();
805
806 final int startIndex = mIsRtl ? childCount - 1 : 0;
807 final int endIndex = mIsRtl ? -1 : childCount;
808 final int delta = mIsRtl ? -1 : 1;
809
Vinit Nayaka406f722019-12-19 11:50:55 -0800810 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700811
Vinit Nayaka406f722019-12-19 11:50:55 -0800812 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
813 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700814 boolean pageScrollChanged = false;
Thales Limae4457742021-07-20 16:46:38 +0100815 int panelCount = getPanelCount();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700816
Vinit Nayaka406f722019-12-19 11:50:55 -0800817 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700818 final View child = getPageAt(i);
819 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800820 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
821 pageCenter, layoutChildren);
822 final int primaryDimension = bounds.primaryDimension;
823 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700824
Alex Chaub5c4d112021-03-29 17:37:53 +0100825 // In case the pages are of different width, align the page to left edge for non-RTL
826 // or right edge for RTL.
827 final int pageScroll =
828 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700829 if (outPageScrolls[i] != pageScroll) {
830 pageScrollChanged = true;
831 outPageScrolls[i] = pageScroll;
832 }
Alex Chau2e487602022-03-15 19:34:08 +0000833 childStart += primaryDimension + getChildGap(i, i + delta);
Thales Limae4457742021-07-20 16:46:38 +0100834
835 // This makes sure that the space is added after the page, not after each panel
Thales Limac2a6f6f2021-09-09 17:06:22 +0100836 int lastPanel = mIsRtl ? 0 : panelCount - 1;
837 if (i % panelCount == lastPanel) {
Thales Limae4457742021-07-20 16:46:38 +0100838 childStart += mPageSpacing;
839 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700840 }
841 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100842
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100843 if (panelCount > 1) {
844 for (int i = 0; i < childCount; i++) {
Alex Chau1e448462021-08-13 21:48:35 +0100845 // In case we have multiple panels, always use left most panel's page scroll for all
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100846 // panels on the screen.
847 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
848 if (outPageScrolls[i] != adjustedScroll) {
849 outPageScrolls[i] = adjustedScroll;
850 pageScrollChanged = true;
851 }
852 }
853 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700854 return pageScrollChanged;
855 }
856
Alex Chau2e487602022-03-15 19:34:08 +0000857 protected int getChildGap(int fromIndex, int toIndex) {
Sunny Goyala1fbd842015-05-20 13:40:27 -0700858 return 0;
859 }
860
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700861 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800862 mMinScroll = computeMinScroll();
863 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700864 }
865
Vinit Nayaka406f722019-12-19 11:50:55 -0800866 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700867 return 0;
Tony50876bf2018-09-19 23:09:14 -0400868 }
869
Vinit Nayaka406f722019-12-19 11:50:55 -0800870 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700871 int childCount = getChildCount();
872 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700873 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700874 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700875 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700876 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700877 }
878 }
879
Adam Cohen3b185e22013-10-29 14:45:58 -0700880 public void setPageSpacing(int pageSpacing) {
881 mPageSpacing = pageSpacing;
882 requestLayout();
883 }
884
Tony50876bf2018-09-19 23:09:14 -0400885 public int getPageSpacing() {
886 return mPageSpacing;
887 }
888
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800889 private void dispatchPageCountChanged() {
890 if (mPageIndicator != null) {
Tony Wickhambab101c2021-09-20 15:12:44 -0700891 mPageIndicator.setMarkersCount(getChildCount() / getPanelCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700892 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700893 // This ensures that when children are added, they get the correct transforms / alphas
894 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700895 invalidate();
896 }
897
Michael Jurka8b805b12012-04-18 14:23:14 -0700898 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800899 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800900 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800901 dispatchPageCountChanged();
902 }
903
904 @Override
905 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800906 super.onViewRemoved(child);
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700907 runOnPageScrollsInitialized(() -> {
908 mCurrentPage = validateNewPage(mCurrentPage);
909 mCurrentScrollOverPage = mCurrentPage;
910 });
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800911 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700912 }
913
Adam Cohen73894962011-10-31 13:17:17 -0700914 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700915 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800916 View pageAtIndex = getPageAt(index);
917 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700918 }
919
Alex Chau7944ee52021-01-08 19:07:25 +0000920 protected int getChildVisibleSize(int index) {
921 View layout = getPageAt(index);
922 return mOrientationHandler.getMeasuredSize(layout);
923 }
924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 @Override
926 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200927 int page = indexOfChild(child);
928 if (!isVisible(page) || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700929 if (immediate) {
930 setCurrentPage(page);
931 } else {
932 snapToPage(page);
933 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 return true;
935 }
936 return false;
937 }
938
939 @Override
940 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700941 int focusablePage;
942 if (mNextPage != INVALID_PAGE) {
943 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700945 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 }
Winson Chung86f77532010-08-24 11:08:22 -0700947 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700949 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 }
951 return false;
952 }
953
954 @Override
955 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800956 if (super.dispatchUnhandledMove(focused, direction)) {
957 return true;
958 }
959
960 if (mIsRtl) {
961 if (direction == View.FOCUS_LEFT) {
962 direction = View.FOCUS_RIGHT;
963 } else if (direction == View.FOCUS_RIGHT) {
964 direction = View.FOCUS_LEFT;
965 }
966 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200967
968 int currentPage = getNextPage();
969 int closestNeighbourIndex = -1;
970 int closestNeighbourDistance = Integer.MAX_VALUE;
971 // Find the closest neighbour page
972 for (int neighbourPageIndex : getNeighbourPageIndices(direction)) {
973 int distance = Math.abs(neighbourPageIndex - currentPage);
974 if (closestNeighbourDistance > distance) {
975 closestNeighbourDistance = distance;
976 closestNeighbourIndex = neighbourPageIndex;
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 }
978 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200979 if (closestNeighbourIndex != -1) {
980 View page = getPageAt(closestNeighbourIndex);
981 snapToPage(closestNeighbourIndex);
982 page.requestFocus(direction);
983 return true;
984 }
985
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800986 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 }
988
989 @Override
990 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700991 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
992 return;
993 }
994
Andras Kloczl7e912442021-05-06 12:50:28 +0200995 // nextPage is more reliable when multiple control movements have been done in a short
996 // period of time
997 getPageIndices(getNextPage())
998 .addAll(getNeighbourPageIndices(direction))
999 .forEach(pageIndex ->
1000 getPageAt(pageIndex).addFocusables(views, direction, focusableMode));
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 }
1002
1003 /**
1004 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001005 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 *
Winson Chung86f77532010-08-24 11:08:22 -07001007 * This happens when live folders requery, and if they're off page, they
1008 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 */
1010 @Override
1011 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001012 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 View v = focused;
1014 while (true) {
1015 if (v == current) {
1016 super.focusableViewAvailable(focused);
1017 return;
1018 }
1019 if (v == this) {
1020 return;
1021 }
1022 ViewParent parent = v.getParent();
1023 if (parent instanceof View) {
1024 v = (View)v.getParent();
1025 } else {
1026 return;
1027 }
1028 }
1029 }
1030
1031 /**
1032 * {@inheritDoc}
1033 */
1034 @Override
1035 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1036 if (disallowIntercept) {
1037 // We need to make sure to cancel our long press if
1038 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +02001039 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 }
1041 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1042 }
1043
1044 @Override
1045 public boolean onInterceptTouchEvent(MotionEvent ev) {
1046 /*
1047 * This method JUST determines whether we want to intercept the motion.
1048 * If we return true, onTouchEvent will be called and we do the actual
1049 * scrolling there.
1050 */
1051
Winson Chung45e1d6e2010-11-09 17:19:49 -08001052 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001053 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -07001054
1055 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001056
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 /*
1058 * Shortcut the most recurring case: the user is in the dragging
1059 * state and he is moving his finger. We want to intercept this
1060 * motion.
1061 */
1062 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -07001063 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 return true;
1065 }
1066
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 switch (action & MotionEvent.ACTION_MASK) {
1068 case MotionEvent.ACTION_MOVE: {
1069 /*
1070 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -08001071 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001073 if (mActivePointerId != INVALID_POINTER) {
1074 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001075 }
1076 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -08001077 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -07001078 // i.e. fall through to the next case (don't break)
1079 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1080 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001081 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 }
1083
1084 case MotionEvent.ACTION_DOWN: {
1085 final float x = ev.getX();
1086 final float y = ev.getY();
1087 // Remember location of down touch
1088 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001089 mDownMotionY = y;
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001090 mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, 0);
1091 mLastMotion = (int) mDownMotionPrimary;
Vinit Nayaka406f722019-12-19 11:50:55 -08001092 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001093 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -07001095 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 break;
1097 }
1098
Winson Chung321e9ee2010-08-09 13:37:56 -07001099 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001100 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001101 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 break;
1103
1104 case MotionEvent.ACTION_POINTER_UP:
1105 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001106 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 break;
1108 }
1109
1110 /*
1111 * The only time we want to intercept motion events is if we are in the
1112 * drag mode.
1113 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001114 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 }
1116
Tony Wickhama0aee212019-09-18 09:24:03 -07001117 /**
1118 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1119 */
Sunny Goyalbb030562021-07-19 16:28:39 -07001120 protected void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -07001121 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -07001122 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -07001123 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001124
1125 if (finishedScrolling) {
1126 mIsBeingDragged = false;
1127 if (!mScroller.isFinished() && !mFreeScroll) {
1128 setCurrentPage(getNextPage());
1129 pageEndTransition();
1130 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001131 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -07001132 } else {
1133 mIsBeingDragged = true;
1134 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001135
1136 // Catch the edge effect if it is active.
1137 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
1138 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
1139 if (!mEdgeGlowLeft.isFinished()) {
1140 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
1141 }
1142 if (!mEdgeGlowRight.isFinished()) {
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001143 mEdgeGlowRight.onPullDistance(0f, displacement, ev);
Sunny Goyalc39db732021-04-03 01:20:21 -07001144 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001145 }
1146
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001147 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001148 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001149 }
1150
Adam Cohenf8d28232011-02-01 21:47:00 -08001151 protected void determineScrollingStart(MotionEvent ev) {
1152 determineScrollingStart(ev, 1.0f);
1153 }
1154
Winson Chung321e9ee2010-08-09 13:37:56 -07001155 /*
1156 * Determines if we should change the touch state to start scrolling after the
1157 * user moves their touch point too far.
1158 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001159 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001160 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001162 if (pointerIndex == -1) return;
1163
Vinit Nayaka406f722019-12-19 11:50:55 -08001164 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1165 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001166 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001167 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001168
Vinit Nayaka406f722019-12-19 11:50:55 -08001169 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001170 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001171 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001172 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001173 mLastMotion = (int) primaryDirection;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001174 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001175 // Stop listening for things like pinches.
1176 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001177 }
1178 }
1179
1180 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001181 // Try canceling the long press. It could also have been scheduled
1182 // by a distant descendant, so use the mAllowLongPress flag to block
1183 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001184 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 }
1186
Adam Cohenb5ba0972011-09-07 18:02:31 -07001187 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001188 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenedb40762013-07-18 16:45:45 -07001189 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001190 int panelCount = getPanelCount();
1191 int pageCount = getChildCount();
Adam Cohen3b185e22013-10-29 14:45:58 -07001192
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001193 int adjacentPage = page + panelCount;
Sunny Goyal70660032015-05-14 00:07:08 -07001194 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001195 adjacentPage = page - panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001196 }
1197
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001198 final int totalDistance;
1199 if (adjacentPage < 0 || adjacentPage > pageCount - 1) {
1200 totalDistance = (v.getMeasuredWidth() + mPageSpacing) * panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001201 } else {
1202 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1203 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001204
1205 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001206 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001207 scrollProgress = Math.max(scrollProgress, -MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001208 return scrollProgress;
1209 }
1210
Adam Cohenedb40762013-07-18 16:45:45 -07001211 public int getScrollForPage(int index) {
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -08001212 if (!isPageScrollsInitialized() || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001213 return 0;
1214 } else {
1215 return mPageScrolls[index];
1216 }
1217 }
1218
Adam Cohen564a2e72013-10-09 14:47:32 -07001219 // While layout transitions are occurring, a child's position may stray from its baseline
1220 // position. This method returns the magnitude of this stray at any given time.
1221 public int getLayoutTransitionOffsetForPage(int index) {
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -08001222 if (!isPageScrollsInitialized() || index >= mPageScrolls.length || index < 0) {
Adam Cohen564a2e72013-10-09 14:47:32 -07001223 return 0;
1224 } else {
1225 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001226
Sunny Goyal228153d2018-01-04 15:35:22 -08001227 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1228 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001229 return (int) (child.getX() - baselineX);
1230 }
1231 }
1232
Tony Wickham023f4042019-01-29 10:52:31 -08001233 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001234 if (mFreeScroll == freeScroll) {
1235 return;
1236 }
1237
Tony Wickham8f7ead32016-04-07 18:46:44 -07001238 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001239 mFreeScroll = freeScroll;
1240
Adam Cohenf9618852013-11-08 06:45:03 -08001241 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001242 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001243 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001244 if (getScrollForPage(getNextPage()) != getScrollX()) {
1245 snapToPage(getNextPage());
1246 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001247 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001248 }
1249
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001250 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 mAllowOverScroll = enable;
1252 }
1253
Andras Kloczl07111f22022-03-30 17:25:54 +01001254 protected boolean isSignificantMove(float absoluteDelta, int pageOrientedSize) {
1255 return absoluteDelta > pageOrientedSize * SIGNIFICANT_MOVE_THRESHOLD;
Alex Chau822acf42021-06-03 11:35:05 +01001256 }
1257
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 @Override
1259 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001260 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001261 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001262
Michael Jurkab8f06722010-10-10 15:58:46 -07001263 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001264
1265 final int action = ev.getAction();
1266
1267 switch (action & MotionEvent.ACTION_MASK) {
1268 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001269 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001270
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 /*
1272 * If being flinged and user touches, stop the fling. isFinished
1273 * will be false if being flinged.
1274 */
1275 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001276 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 }
1278
1279 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001280 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001281 mDownMotionY = ev.getY();
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001282 mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, 0);
1283 mLastMotion = (int) mDownMotionPrimary;
Vinit Nayaka406f722019-12-19 11:50:55 -08001284 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001285 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001287 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001288 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001289 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 break;
1291
Winson Chung4941f652020-06-18 15:11:53 -07001292 case ACTION_MOVE_ALLOW_EASY_FLING:
1293 // Start scrolling immediately
1294 determineScrollingStart(ev);
1295 mAllowEasyFling = true;
1296 break;
1297
1298 case MotionEvent.ACTION_MOVE:
1299 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 // Scroll to follow the motion event
1301 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001302
1303 if (pointerIndex == -1) return true;
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001304 int oldScroll = mOrientationHandler.getPrimaryScroll(this);
1305 int dx = (int) ev.getX(pointerIndex);
1306 int dy = (int) ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001307
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001308 int direction = mOrientationHandler.getPrimaryValue(dx, dy);
1309 int delta = mLastMotion - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001310
1311 int width = getWidth();
1312 int height = getHeight();
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001313 float size = mOrientationHandler.getPrimaryValue(width, height);
1314 float displacement = (width == 0 || height == 0) ? 0
1315 : (float) mOrientationHandler.getSecondaryValue(dx, dy)
1316 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001317 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001318
Sunny Goyalc39db732021-04-03 01:20:21 -07001319 if (mAllowOverScroll) {
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001320 int consumed = 0;
Sunny Goyalc39db732021-04-03 01:20:21 -07001321 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001322 consumed = Math.round(size *
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001323 mEdgeGlowRight.onPullDistance(delta / size, displacement, ev));
Sunny Goyalc39db732021-04-03 01:20:21 -07001324 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001325 consumed = Math.round(-size *
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001326 mEdgeGlowLeft.onPullDistance(-delta / size, 1 - displacement, ev));
Sunny Goyalc39db732021-04-03 01:20:21 -07001327 }
1328 delta -= consumed;
1329 }
Alex Chau822acf42021-06-03 11:35:05 +01001330 delta /= mOrientationHandler.getPrimaryScale(this);
Sunny Goyalc39db732021-04-03 01:20:21 -07001331
Winson Chungc0844aa2011-02-02 15:25:58 -08001332 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1333 // keep the remainder because we are actually testing if we've moved from the last
1334 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001335 mLastMotion = direction;
Vinit Nayaka406f722019-12-19 11:50:55 -08001336
Sunny Goyalc39db732021-04-03 01:20:21 -07001337 if (delta != 0) {
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001338 mOrientationHandler.setPrimary(this, VIEW_SCROLL_BY, delta);
Sunny Goyalc39db732021-04-03 01:20:21 -07001339
1340 if (mAllowOverScroll) {
1341 final float pulledToX = oldScroll + delta;
1342
1343 if (pulledToX < mMinScroll) {
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001344 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement, ev);
Sunny Goyalc39db732021-04-03 01:20:21 -07001345 if (!mEdgeGlowRight.isFinished()) {
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001346 mEdgeGlowRight.onRelease(ev);
Sunny Goyalc39db732021-04-03 01:20:21 -07001347 }
1348 } else if (pulledToX > mMaxScroll) {
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001349 mEdgeGlowRight.onPullDistance(delta / size, displacement, ev);
Sunny Goyalc39db732021-04-03 01:20:21 -07001350 if (!mEdgeGlowLeft.isFinished()) {
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001351 mEdgeGlowLeft.onRelease(ev);
Sunny Goyalc39db732021-04-03 01:20:21 -07001352 }
1353 }
1354
1355 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1356 postInvalidateOnAnimation();
1357 }
1358 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 } else {
1360 awakenScrollBars();
1361 }
Adam Cohen564976a2010-10-13 18:52:07 -07001362 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 determineScrollingStart(ev);
1364 }
1365 break;
1366
1367 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001368 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 final int activePointerId = mActivePointerId;
1370 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001371 if (pointerIndex == -1) return true;
1372
Vinit Nayaka406f722019-12-19 11:50:55 -08001373 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1374 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001375 final VelocityTracker velocityTracker = mVelocityTracker;
1376 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001377
Vinit Nayaka406f722019-12-19 11:50:55 -08001378 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
Andras Kloczl07111f22022-03-30 17:25:54 +01001379 mActivePointerId);
Alex Chau822acf42021-06-03 11:35:05 +01001380 float delta = primaryDirection - mDownMotionPrimary;
Randy Pfohl583241e2023-03-02 03:26:56 +00001381
1382 View current = getPageAt(mCurrentPage);
1383 if (current == null) {
1384 Log.e(TAG, "current page was null. this should not happen.");
1385 return true;
1386 }
1387
1388 int pageOrientedSize = (int) (mOrientationHandler.getMeasuredSize(current)
Andras Kloczl07111f22022-03-30 17:25:54 +01001389 * mOrientationHandler.getPrimaryScale(this));
1390 boolean isSignificantMove = isSignificantMove(Math.abs(delta), pageOrientedSize);
Vinit Nayaka406f722019-12-19 11:50:55 -08001391
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001392 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001393 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1394 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001395 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1396 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001397 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1398 Log.d("Quickswitch", "isFling=false vel=" + velocity
1399 + " threshold=" + mEasyFlingThresholdVelocity);
1400 }
Adam Cohen00481b32011-11-18 12:03:48 -08001401
Adam Cohenf358a4b2013-07-23 16:47:31 -07001402 if (!mFreeScroll) {
1403 // In the case that the page is moved far to one direction and then is flung
1404 // in the opposite direction, we use a threshold to determine whether we should
1405 // just return to the starting page, or if we should skip one further.
1406 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001407 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1408 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001409 returnToOriginalPage = true;
1410 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001411
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 int finalPage;
1413 // We give flings precedence over large moves, which is why we short-circuit our
1414 // test for a large move if a fling has been registered. That is, a large
1415 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001416
Vinit Nayaka406f722019-12-19 11:50:55 -08001417 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1418 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001419 finalPage = returnToOriginalPage
1420 ? mCurrentPage : mCurrentPage - getPanelCount();
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001421 runOnPageScrollsInitialized(
1422 () -> snapToPageWithVelocity(finalPage, velocity));
Vinit Nayaka406f722019-12-19 11:50:55 -08001423 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1424 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001426 finalPage = returnToOriginalPage
1427 ? mCurrentPage : mCurrentPage + getPanelCount();
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001428 runOnPageScrollsInitialized(
1429 () -> snapToPageWithVelocity(finalPage, velocity));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430 } else {
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001431 runOnPageScrollsInitialized(this::snapToDestination);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001432 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001434 if (!mScroller.isFinished()) {
1435 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001436 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001437
Vinit Nayaka406f722019-12-19 11:50:55 -08001438 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1439 int maxScroll = mMaxScroll;
1440 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001441
Vinit Nayaka406f722019-12-19 11:50:55 -08001442 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1443 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001444 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001445 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001446 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001447 int velocity1 = -velocity;
1448 // Continue a scroll or fling in progress
1449 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1450 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001451
Sunny Goyalc39db732021-04-03 01:20:21 -07001452 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001453 mNextPage = getDestinationPage(finalPos);
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001454 runOnPageScrollsInitialized(this::onNotSnappingToPageInFreeScroll);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001455 }
1456 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 }
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001458 mEdgeGlowLeft.onFlingVelocity(velocity);
1459 mEdgeGlowRight.onFlingVelocity(velocity);
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 }
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001461 mEdgeGlowLeft.onRelease(ev);
1462 mEdgeGlowRight.onRelease(ev);
Adam Cohen7d30a372013-07-01 17:03:59 -07001463 // End any intermediate reordering states
1464 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 break;
1466
1467 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001468 if (mIsBeingDragged) {
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001469 runOnPageScrollsInitialized(this::snapToDestination);
Michael Jurkab8f06722010-10-10 15:58:46 -07001470 }
Sunny Goyaldc7cdd82024-02-13 14:56:36 -08001471 mEdgeGlowLeft.onRelease(ev);
1472 mEdgeGlowRight.onRelease(ev);
Adam Cohen7d30a372013-07-01 17:03:59 -07001473 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 break;
1475
1476 case MotionEvent.ACTION_POINTER_UP:
1477 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001478 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001479 break;
1480 }
1481
1482 return true;
1483 }
1484
Sunny Goyalc39db732021-04-03 01:20:21 -07001485 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001486
Lais Andradecc5c8842021-06-04 18:20:30 +01001487 /**
1488 * Called when the view edges absorb part of the scroll. Subclasses can override this
1489 * to provide custom behavior during animation.
1490 */
1491 protected void onEdgeAbsorbingScroll() {
1492 }
1493
1494 /**
1495 * Called when the current page closest to the center of the screen changes as part of the
1496 * scroll. Subclasses can override this to provide custom behavior during scroll.
1497 */
1498 protected void onScrollOverPageChanged() {
1499 }
1500
Vinit Nayaka406f722019-12-19 11:50:55 -08001501 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001502 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1503 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001504 }
1505
Alex Chau9d8eeab2022-04-05 17:13:56 +01001506 protected void resetTouchState() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001507 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001508 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001509 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001510 }
1511
Winson Chung185d7162011-02-28 13:47:29 -08001512 @Override
1513 public boolean onGenericMotionEvent(MotionEvent event) {
1514 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1515 switch (event.getAction()) {
1516 case MotionEvent.ACTION_SCROLL: {
1517 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1518 final float vscroll;
1519 final float hscroll;
1520 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1521 vscroll = 0;
1522 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1523 } else {
1524 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1525 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1526 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001527 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1528 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001529 }
Winson Chung185d7162011-02-28 13:47:29 -08001530 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001531 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001532 : (hscroll > 0 || vscroll > 0);
1533 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001534 scrollRight();
1535 } else {
1536 scrollLeft();
1537 }
1538 return true;
1539 }
1540 }
1541 }
1542 }
1543 return super.onGenericMotionEvent(event);
1544 }
1545
Sunny Goyal8b37c572020-03-31 12:12:14 -07001546 /**
1547 * Returns true if the paged view can scroll for the provided vertical and horizontal
1548 * scroll values
1549 */
1550 protected boolean canScroll(float absVScroll, float absHScroll) {
1551 ActivityContext ac = ActivityContext.lookupContext(getContext());
1552 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001553 }
1554
Michael Jurkab8f06722010-10-10 15:58:46 -07001555 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1556 if (mVelocityTracker == null) {
1557 mVelocityTracker = VelocityTracker.obtain();
1558 }
1559 mVelocityTracker.addMovement(ev);
1560 }
1561
1562 private void releaseVelocityTracker() {
1563 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001564 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001565 mVelocityTracker.recycle();
1566 mVelocityTracker = null;
1567 }
1568 }
1569
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001571 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001572 final int pointerId = ev.getPointerId(pointerIndex);
1573 if (pointerId == mActivePointerId) {
1574 // This was our active pointer going up. Choose a new
1575 // active pointer and adjust accordingly.
1576 // TODO: Make this decision more intelligent.
1577 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Sunny Goyalb7c0f3c2023-03-15 17:21:57 -07001578 mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, newPointerIndex);
1579 mLastMotion = (int) mDownMotionPrimary;
Winson Chung321e9ee2010-08-09 13:37:56 -07001580 mActivePointerId = ev.getPointerId(newPointerIndex);
1581 if (mVelocityTracker != null) {
1582 mVelocityTracker.clear();
1583 }
1584 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001585 }
1586
Winson Chung321e9ee2010-08-09 13:37:56 -07001587 @Override
1588 public void requestChildFocus(View child, View focused) {
1589 super.requestChildFocus(child, focused);
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001590 if (!shouldHandleRequestChildFocus(child)) {
Schneider Victor-tuliasbdae57c2023-10-10 11:16:54 -04001591 return;
1592 }
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001593 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1594 // which results in incorrect navigation
1595 int nextPage = getNextPage();
1596 if (nextPage != mCurrentPage) {
1597 setCurrentPage(nextPage);
1598 }
1599
Andras Kloczl7e912442021-05-06 12:50:28 +02001600 int page = indexOfChild(child);
1601 if (page >= 0 && !isVisible(page) && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001602 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001603 }
1604 }
1605
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001606 protected boolean shouldHandleRequestChildFocus(View child) {
Schneider Victor-tuliasbdae57c2023-10-10 11:16:54 -04001607 return true;
1608 }
1609
Alex Chaub794ea62021-02-03 18:00:30 +00001610 public int getDestinationPage() {
1611 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1612 }
1613
Alex Chaudedbc8a2021-03-17 16:53:26 +00001614 protected int getDestinationPage(int primaryScroll) {
1615 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001616 }
1617
Sunny Goyal228153d2018-01-04 15:35:22 -08001618 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001619 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001620 }
1621
Alex Chaudedbc8a2021-03-17 16:53:26 +00001622 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1623 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001624 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001625 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001626 final int childCount = getChildCount();
1627 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001628 int distanceFromScreenCenter = Math.abs(
1629 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001630 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1631 minDistanceFromScreenCenter = distanceFromScreenCenter;
1632 minDistanceFromScreenCenterIndex = i;
1633 }
1634 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001635 return minDistanceFromScreenCenterIndex;
1636 }
1637
Tony Wickham7383d4e2020-07-07 19:25:25 -07001638 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Cole Faust50ec4af2022-10-15 21:33:28 -07001639 int childSize = getChildVisibleSize(childIndex);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001640 int halfChildSize = (childSize / 2);
1641 int childCenter = getChildOffset(childIndex) + halfChildSize;
1642 return childCenter - screenCenter;
1643 }
1644
1645 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001646 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1647 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001648 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1649 }
1650
Andras Kloczl9732df12021-09-09 19:40:42 +01001651 protected int getScreenCenter(int primaryScroll) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001652 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1653 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1654 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1655 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1656 + primaryPivot);
1657 }
1658
Adam Cohend19d3ca2010-09-15 14:43:42 -07001659 protected void snapToDestination() {
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001660 snapToPage(getDestinationPage(), getSnapAnimationDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07001661 }
1662
Adam Cohene0f66b52010-11-23 15:06:07 -08001663 // We want the duration of the page snap animation to be influenced by the distance that
1664 // the screen has to travel, however, we don't want this duration to be effected in a
1665 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1666 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001667 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001668 f -= 0.5f; // center the values about 0.
1669 f *= 0.3f * Math.PI / 2.0f;
1670 return (float) Math.sin(f);
1671 }
1672
Winson Chung05a31ed2018-02-08 17:08:43 -08001673 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001674 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001675 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001676
Vinit Nayaka406f722019-12-19 11:50:55 -08001677 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001678 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001679 int duration = 0;
1680
Sunny Goyal4d113a52015-05-27 10:05:28 -07001681 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001682 // If the velocity is low enough, then treat this more as an automatic page advance
1683 // as opposed to an apparent physical response to flinging
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001684 return snapToPage(whichPage, getSnapAnimationDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08001685 }
1686
1687 // Here we compute a "distance" that will be used in the computation of the overall
1688 // snap duration. This is a function of the actual distance that needs to be traveled;
1689 // we keep this value close to half screen size in order to reduce the variance in snap
1690 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001691 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001692 float distance = halfScreenSize + halfScreenSize *
1693 distanceInfluenceForSnapDuration(distanceRatio);
1694
1695 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001696 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001697
1698 // we want the page's snap velocity to approximately match the velocity at which the
1699 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001700 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1701 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001702
Sunny Goyalc39db732021-04-03 01:20:21 -07001703 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001704 }
1705
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001706 protected int getSnapAnimationDuration() {
1707 return mPageSnapAnimationDuration;
1708 }
1709
Winson Chung05a31ed2018-02-08 17:08:43 -08001710 public boolean snapToPage(int whichPage) {
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001711 return snapToPage(whichPage, getSnapAnimationDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07001712 }
1713
Winson Chung05a31ed2018-02-08 17:08:43 -08001714 public boolean snapToPageImmediately(int whichPage) {
Schneider Victor-tulias1ca66992024-01-10 14:04:12 -05001715 return snapToPage(whichPage, getSnapAnimationDuration(), true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001716 }
1717
Winson Chung05a31ed2018-02-08 17:08:43 -08001718 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001719 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001720 }
1721
Sunny Goyalc39db732021-04-03 01:20:21 -07001722 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001723 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001724
Vinit Nayaka406f722019-12-19 11:50:55 -08001725 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001726 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1727 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001728 }
1729
Winson Chung05a31ed2018-02-08 17:08:43 -08001730 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001731 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001732 }
Michael Jurka0142d492010-08-25 17:46:15 -07001733
Sunny Goyalc39db732021-04-03 01:20:21 -07001734 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001735 if (mFirstLayout) {
1736 setCurrentPage(whichPage);
1737 return false;
1738 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001739
Brian Isganitisfdd044e2023-03-02 19:45:23 +00001740 if (FeatureFlags.IS_STUDIO_BUILD && !Utilities.isRunningInTestHarness()) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001741 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1742 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001743 }
1744
Adam Cohen6f127a62014-06-12 14:54:41 -07001745 whichPage = validateNewPage(whichPage);
1746
Adam Cohen7d30a372013-07-01 17:03:59 -07001747 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001748
Winson Chung321e9ee2010-08-09 13:37:56 -07001749 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001750 if (immediate) {
1751 duration = 0;
1752 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001753 duration = Math.abs(delta);
1754 }
1755
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001756 if (duration != 0) {
1757 pageBeginTransition();
1758 }
1759
Adam Cohenf358a4b2013-07-23 16:47:31 -07001760 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001761 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001762 }
Adam Cohenf9618852013-11-08 06:45:03 -08001763
Sunny Goyalc39db732021-04-03 01:20:21 -07001764 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001765 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001766
1767 // Trigger a compute() to finish switching pages if necessary
1768 if (immediate) {
1769 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001770 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001771 }
1772
Winson Chung321e9ee2010-08-09 13:37:56 -07001773 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001774 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001775 }
1776
Vadim Tryshev98913d02018-05-18 18:41:34 -07001777 public boolean scrollLeft() {
1778 if (getNextPage() > 0) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001779 snapToPage(getNextPage() - getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001780 return true;
1781 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001782 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001783 }
1784
Vadim Tryshev98913d02018-05-18 18:41:34 -07001785 public boolean scrollRight() {
1786 if (getNextPage() < getChildCount() - 1) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001787 snapToPage(getNextPage() + getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001788 return true;
1789 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001790 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001791 }
1792
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001793 @Override
Lais Andradecc5c8842021-06-04 18:20:30 +01001794 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Lais Andradef6e31b12021-10-27 12:04:28 +01001795 if (mScroller.isFinished()) {
1796 // This was not caused by the scroller, skip it.
1797 return;
1798 }
Lais Andradecc5c8842021-06-04 18:20:30 +01001799 int newDestinationPage = getDestinationPage();
1800 if (newDestinationPage >= 0 && newDestinationPage != mCurrentScrollOverPage) {
1801 mCurrentScrollOverPage = newDestinationPage;
1802 onScrollOverPageChanged();
1803 }
1804 }
1805
1806 @Override
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001807 public CharSequence getAccessibilityClassName() {
1808 // Some accessibility services have special logic for ScrollView. Since we provide same
1809 // accessibility info as ScrollView, inform the service to handle use the same way.
1810 return ScrollView.class.getName();
1811 }
1812
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001813 protected boolean isPageOrderFlipped() {
1814 return false;
1815 }
1816
Winson Chung6a0f57d2011-06-29 20:10:49 -07001817 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001818 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001819 @Override
1820 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1821 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001822 final boolean pagesFlipped = isPageOrderFlipped();
Alex Chau9ead9ca2021-11-26 16:22:49 +00001823 info.setScrollable(getPageCount() > 0);
1824 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1825 if (getCurrentPage() < getPageCount() - getPanelCount()
1826 || (getCurrentPage() == getPageCount() - getPanelCount()
1827 && primaryScroll != getScrollForPage(getPageCount() - getPanelCount()))) {
yingleiw02cc8482019-08-02 16:14:27 -07001828 info.addAction(pagesFlipped ?
Alex Chau9ead9ca2021-11-26 16:22:49 +00001829 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1830 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
yingleiw02cc8482019-08-02 16:14:27 -07001831 info.addAction(mIsRtl ?
1832 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1833 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001834 }
Alex Chau9ead9ca2021-11-26 16:22:49 +00001835 if (getCurrentPage() > 0
1836 || (getCurrentPage() == 0 && primaryScroll != getScrollForPage(0))) {
yingleiw02cc8482019-08-02 16:14:27 -07001837 info.addAction(pagesFlipped ?
Alex Chau9ead9ca2021-11-26 16:22:49 +00001838 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1839 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
yingleiw02cc8482019-08-02 16:14:27 -07001840 info.addAction(mIsRtl ?
1841 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1842 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001843 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001844 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1845 // Besides disabling the accessibility long-click, this also prevents this view from getting
1846 // accessibility focus.
1847 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001848 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001849 }
1850
1851 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001852 public void sendAccessibilityEvent(int eventType) {
1853 // Don't let the view send real scroll events.
1854 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1855 super.sendAccessibilityEvent(eventType);
1856 }
1857 }
1858
1859 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001860 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1861 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001862 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001863 }
1864
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001865 @Override
1866 public boolean performAccessibilityAction(int action, Bundle arguments) {
1867 if (super.performAccessibilityAction(action, arguments)) {
1868 return true;
1869 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001870 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001871 switch (action) {
1872 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001873 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001874 return true;
1875 }
1876 } break;
1877 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001878 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001879 return true;
1880 }
yingleiw02cc8482019-08-02 16:14:27 -07001881 } break;
1882 case android.R.id.accessibilityActionPageRight: {
1883 if (!mIsRtl) {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001884 return scrollRight();
yingleiw02cc8482019-08-02 16:14:27 -07001885 } else {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001886 return scrollLeft();
yingleiw02cc8482019-08-02 16:14:27 -07001887 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001888 }
yingleiw02cc8482019-08-02 16:14:27 -07001889 case android.R.id.accessibilityActionPageLeft: {
1890 if (!mIsRtl) {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001891 return scrollLeft();
yingleiw02cc8482019-08-02 16:14:27 -07001892 } else {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001893 return scrollRight();
yingleiw02cc8482019-08-02 16:14:27 -07001894 }
1895 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001896 }
1897 return false;
1898 }
1899
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001900 protected boolean canAnnouncePageDescription() {
1901 return true;
1902 }
1903
Adam Cohen0ffac432013-07-10 11:19:26 -07001904 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001905 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001906 getNextPage() + 1, getChildCount());
1907 }
1908
Tony Mak2e564402018-02-27 17:19:34 +00001909 protected float getDownMotionX() {
1910 return mDownMotionX;
1911 }
1912
1913 protected float getDownMotionY() {
1914 return mDownMotionY;
1915 }
1916
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001917 protected interface ComputePageScrollsLogic {
1918
1919 boolean shouldIncludeView(View view);
1920 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001921
1922 public int[] getVisibleChildrenRange() {
1923 float visibleLeft = 0;
1924 float visibleRight = visibleLeft + getMeasuredWidth();
1925 float scaleX = getScaleX();
1926 if (scaleX < 1 && scaleX > 0) {
1927 float mid = getMeasuredWidth() / 2;
1928 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1929 visibleRight = mid + ((visibleRight - mid) / scaleX);
1930 }
1931
1932 int leftChild = -1;
1933 int rightChild = -1;
1934 final int childCount = getChildCount();
1935 for (int i = 0; i < childCount; i++) {
1936 final View child = getPageAt(i);
1937
1938 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1939 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1940 if (leftChild == -1) {
1941 leftChild = i;
1942 }
1943 rightChild = i;
1944 }
1945 }
1946 mTmpIntPair[0] = leftChild;
1947 mTmpIntPair[1] = rightChild;
1948 return mTmpIntPair;
1949 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001950
1951 @Override
1952 public void draw(Canvas canvas) {
1953 super.draw(canvas);
1954 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001955 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001956 }
1957
1958 protected void drawEdgeEffect(Canvas canvas) {
1959 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1960 final int width = getWidth();
1961 final int height = getHeight();
1962 if (!mEdgeGlowLeft.isFinished()) {
1963 final int restoreCount = canvas.save();
1964 canvas.rotate(-90);
1965 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1966 mEdgeGlowLeft.setSize(height, width);
1967 if (mEdgeGlowLeft.draw(canvas)) {
1968 postInvalidateOnAnimation();
1969 }
1970 canvas.restoreToCount(restoreCount);
1971 }
1972 if (!mEdgeGlowRight.isFinished()) {
1973 final int restoreCount = canvas.save();
1974 canvas.rotate(90, width, 0);
1975 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1976
1977 mEdgeGlowRight.setSize(height, width);
1978 if (mEdgeGlowRight.draw(canvas)) {
1979 postInvalidateOnAnimation();
1980 }
1981 canvas.restoreToCount(restoreCount);
1982 }
1983 }
1984 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001985}