blob: b084eb1b9bf381cb75708e7030de8013075547df [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
Sunny Goyal8b37c572020-03-31 12:12:14 -070019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
22import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
23import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
24import 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;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070031import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080032import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070034import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070035import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
48import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070050import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
Tony Wickham62117d72020-03-26 16:56:26 -070052import androidx.annotation.Nullable;
53
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.anim.Interpolators;
55import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
58import com.android.launcher3.touch.OverScroll;
59import com.android.launcher3.touch.PagedOrientationHandler;
60import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Jon Miranda9e198662020-03-11 14:42:02 -070061import com.android.launcher3.util.OverScroller;
62import 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;
66
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080071public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070074 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070075
Winson Chung4941f652020-06-18 15:11:53 -070076 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053077 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070078 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Vadim Tryshevfedca432015-08-19 17:55:02 -070080 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Sunny Goyalb72d8b22017-07-14 00:02:27 -070082 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070083 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070084
Adam Cohenb64cb5a2011-02-15 13:53:42 -080085 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080086 // The page is moved more than halfway, automatically move to the next page on touch up.
87 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080088
Sunny Goyal8e2133b2015-05-06 13:39:07 -070089 private static final float MAX_SCROLL_PROGRESS = 1.0f;
90
Adam Cohen265b9a62011-12-07 14:37:18 -080091 // The following constants need to be scaled based on density. The scaled versions will be
92 // assigned to the corresponding member variables below.
93 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070094 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080095 private static final int MIN_SNAP_VELOCITY = 1500;
96 private static final int MIN_FLING_VELOCITY = 250;
97
Adam Cohenf358a4b2013-07-23 16:47:31 -070098 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070099
Winson Chung4941f652020-06-18 15:11:53 -0700100 protected final int mFlingThresholdVelocity;
101 protected final int mEasyFlingThresholdVelocity;
102 protected final int mMinFlingVelocity;
103 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700104
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected boolean mFirstLayout = true;
106
Sunny Goyal4ffec482016-02-09 11:28:52 -0800107 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700108 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700109
Sunny Goyal4ffec482016-02-09 11:28:52 -0800110 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700111 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800112 protected int mMaxScroll;
113 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700114 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800115 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800117 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
119 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700120 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800121 private float mDownMotionPrimary;
122 private float mLastMotion;
123 private float mLastMotionRemainder;
124 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700125 // Used in special cases where the fling checks can be relaxed for an intentional gesture
126 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700127 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700128
Vadim Trysheve628c472018-05-16 14:28:18 -0700129 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700130 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800131
Winson Chung4941f652020-06-18 15:11:53 -0700132 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700134 // The amount of movement to begin paging
135 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700143 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700144 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Vinit Nayaka406f722019-12-19 11:50:55 -0800146 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800147
Sunny Goyal061380a2016-02-29 15:15:03 -0800148 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700149
Vinit Nayaka406f722019-12-19 11:50:55 -0800150 protected int mUnboundedScroll;
151
Winson Chungd2be3812013-07-16 11:11:32 -0700152 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800154 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700155
John Spurlock77e1f472013-09-11 10:09:51 -0400156 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800157 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400158
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800159 // Similar to the platform implementation of isLayoutValid();
160 protected boolean mIsLayoutValid;
161
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700162 private int[] mTmpIntPair = new int[2];
163
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 public PagedView(Context context) {
165 this(context, null);
166 }
167
168 public PagedView(Context context, AttributeSet attrs) {
169 this(context, attrs, 0);
170 }
171
172 public PagedView(Context context, AttributeSet attrs, int defStyle) {
173 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700174
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 TypedArray a = context.obtainStyledAttributes(attrs,
176 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700177 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700178 a.recycle();
179
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700181 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Vinit Nayaka406f722019-12-19 11:50:55 -0800183 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700184 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700185 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Vinit Nayaka406f722019-12-19 11:50:55 -0800187 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700188 mTouchSlop = configuration.getScaledTouchSlop();
189 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800191
Sunny Goyalcf25b522015-07-09 00:01:18 -0700192 float density = getResources().getDisplayMetrics().density;
193 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700194 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700195 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
196 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700197
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700198 setDefaultFocusHighlightEnabled(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 }
200
Adam Cohenf9618852013-11-08 06:45:03 -0800201 protected void setDefaultInterpolator(Interpolator interpolator) {
202 mDefaultInterpolator = interpolator;
203 mScroller.setInterpolator(mDefaultInterpolator);
204 }
205
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700206 public void initParentViews(View parent) {
207 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800208 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700209 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700210 }
211 }
212
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800213 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700214 return mPageIndicator;
215 }
216
Adam Cohen674531f2013-12-13 15:07:14 -0800217 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700218 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
219 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000220 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700221 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700223 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700224 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700226
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700227 /**
228 * Returns the index of page to be shown immediately afterwards.
229 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700230 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700231 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
232 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700233
Adam Cohenf9c184a2016-01-15 16:47:43 -0800234 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 return getChildCount();
236 }
237
Sunny Goyal83a8f042015-05-19 12:52:12 -0700238 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 return getChildAt(index);
240 }
241
Adam Cohenae4f1552011-10-20 00:15:42 -0700242 protected int indexToPage(int index) {
243 return index;
244 }
245
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800247 * Updates the scroll of the current page immediately to its final scroll position. We use this
248 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
249 * the previous tab page.
250 */
251 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700252 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800253 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700256 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800257 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
258 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700259 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800260 }
261
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700262 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700263 mScroller.abortAnimation();
264 // We need to clean up the next page here to avoid computeScrollHelper from
265 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700266 if (resetNextPage) {
267 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800268 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700269 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700270 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271
Tony Wickham8f7ead32016-04-07 18:46:44 -0700272 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273 mScroller.forceFinished(true);
274 // We need to clean up the next page here to avoid computeScrollHelper from
275 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700276 if (resetNextPage) {
277 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800278 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700279 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700280 }
281
Adam Cohen6f127a62014-06-12 14:54:41 -0700282 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700283 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700284 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100285 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
286
287 if (getPanelCount() > 1) {
288 // Always return left panel as new page
289 newPage = getLeftmostVisiblePageForIndex(newPage);
290 }
291 return newPage;
292 }
293
294 private int getLeftmostVisiblePageForIndex(int pageIndex) {
295 int panelCount = getPanelCount();
296 return (pageIndex / panelCount) * panelCount;
297 }
298
299 /**
300 * Returns the number of pages that are shown at the same time.
301 */
302 protected int getPanelCount() {
303 return 1;
304 }
305
306 /**
307 * Returns true if the view is on one of the current pages, false otherwise.
308 */
309 public boolean isVisible(View child) {
310 return getLeftmostVisiblePageForIndex(indexOfChild(child)) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700311 }
312
Chet Haasebc2f0822012-10-26 17:59:53 -0700313 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700314 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
315 */
316 private int ensureWithinScrollBounds(int page) {
317 int dir = !mIsRtl ? 1 : - 1;
318 int currScroll = getScrollForPage(page);
319 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800320 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700321 page += dir;
322 prevScroll = currScroll;
323 currScroll = getScrollForPage(page);
324 if (currScroll <= prevScroll) {
325 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
326 break;
327 }
328 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800329 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700330 page -= dir;
331 prevScroll = currScroll;
332 currScroll = getScrollForPage(page);
333 if (currScroll >= prevScroll) {
334 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
335 break;
336 }
337 }
338 return page;
339 }
340
Tony Wickham03f27012019-04-25 14:22:54 -0700341 public void setCurrentPage(int currentPage) {
342 setCurrentPage(currentPage, INVALID_PAGE);
343 }
344
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700345 /**
Winson Chung86f77532010-08-24 11:08:22 -0700346 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 */
Tony Wickham03f27012019-04-25 14:22:54 -0700348 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800349 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700350 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800351 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800352 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
353 // the default
354 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800355 return;
356 }
Tony Wickham03f27012019-04-25 14:22:54 -0700357 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700358 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700359 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700360 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800361 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 }
363
Winson Chung8c87cd82013-07-23 16:20:10 -0700364 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800365 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
366 * has settled.
367 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700368 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800369 updatePageIndicator();
370 }
371
372 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700373 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000374 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700375 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700377 protected void pageBeginTransition() {
378 if (!mIsPageInTransition) {
379 mIsPageInTransition = true;
380 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700381 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700382 }
383
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700384 protected void pageEndTransition() {
385 if (mIsPageInTransition) {
386 mIsPageInTransition = false;
387 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700388 }
Michael Jurka0142d492010-08-25 17:46:15 -0700389 }
390
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700391 protected boolean isPageInTransition() {
392 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700393 }
394
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700395 /**
396 * Called when the page starts moving as part of the scroll. Subclasses can override this
397 * to provide custom behavior during animation.
398 */
399 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700400 }
401
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700402 /**
403 * Called when the page ends moving as part of the scroll. Subclasses can override this
404 * to provide custom behavior during animation.
405 */
406 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700407 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700408 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800409 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
410 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700411 if (mOnPageTransitionEndCallback != null) {
412 mOnPageTransitionEndCallback.run();
413 mOnPageTransitionEndCallback = null;
414 }
415 }
416
417 /**
418 * Sets a callback to run once when the scrolling finishes. If there is currently
419 * no page in transition, then the callback is called immediately.
420 */
421 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
422 if (mIsPageInTransition || callback == null) {
423 mOnPageTransitionEndCallback = callback;
424 } else {
425 callback.run();
426 }
Michael Jurka0142d492010-08-25 17:46:15 -0700427 }
428
Vinit Nayaka406f722019-12-19 11:50:55 -0800429 protected int getUnboundedScroll() {
430 return mUnboundedScroll;
431 }
432
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800434 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800435 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800436 }
437
438 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700439 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800440 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
441 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
442 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700443
Vinit Nayaka406f722019-12-19 11:50:55 -0800444 boolean isBeforeFirstPage = mIsRtl ?
445 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
446 boolean isAfterLastPage = mIsRtl ?
447 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
448 if (!isBeforeFirstPage && !isAfterLastPage) {
449 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800450 }
451
Vinit Nayaka406f722019-12-19 11:50:55 -0800452 if (isBeforeFirstPage) {
453 mOrientationHandler.delegateScrollTo(this,
454 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800455 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700456 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800457 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800458 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800459 } else if (isAfterLastPage) {
460 mOrientationHandler.delegateScrollTo(this,
461 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800462 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700463 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800464 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800465 }
466 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700467 if (mWasInOverscroll) {
468 overScroll(0);
469 mWasInOverscroll = false;
470 }
Adam Cohen68d73932010-11-15 10:50:58 -0800471 super.scrollTo(x, y);
472 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800473 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700474
Vinit Nayaka406f722019-12-19 11:50:55 -0800475 /**
476 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
477 */
478 public void superScrollTo(int x, int y) {
479 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700480 }
481
Adam Cohen53805212013-10-01 10:39:23 -0700482 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800483 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700484 if (mCurrentPage != getNextPage()) {
485 AccessibilityEvent ev =
486 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700487 ev.setScrollable(true);
488 ev.setScrollX(getScrollX());
489 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800490 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700491 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700492 }
Adam Cohen53805212013-10-01 10:39:23 -0700493 }
494 }
495
Michael Jurka0142d492010-08-25 17:46:15 -0700496 // we moved this functionality to a helper function so SmoothPagedView can reuse it
497 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800498 return computeScrollHelper(true);
499 }
500
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700501 protected void announcePageForAccessibility() {
502 if (isAccessibilityEnabled(getContext())) {
503 // Notify the user when the page changes
504 announceForAccessibility(getCurrentPageDescription());
505 }
506 }
507
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800508 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700509 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700510 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800511 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
512 if (mUnboundedScroll != mScroller.getCurrPos()
513 || currentScroll != mScroller.getCurrPos()) {
514 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700515 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800516 if (shouldInvalidate) {
517 invalidate();
518 }
Michael Jurka0142d492010-08-25 17:46:15 -0700519 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800520 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700521 sendScrollAccessibilityEvent();
522
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700523 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700524 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700525 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700526 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700527
Adam Cohen73aa9752010-11-24 16:26:58 -0800528 // We don't want to trigger a page end moving unless the page has settled
529 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700530 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700531 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800532 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700533
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700534 if (canAnnouncePageDescription()) {
535 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700536 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 }
Michael Jurka0142d492010-08-25 17:46:15 -0700538 return false;
539 }
540
541 @Override
542 public void computeScroll() {
543 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 }
545
Sunny Goyalce8809a2018-01-18 14:02:47 -0800546 public int getExpectedHeight() {
547 return getMeasuredHeight();
548 }
549
Adam Cohen410f3cd2013-09-22 12:09:32 -0700550 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800551 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800552 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700553 }
554
Sunny Goyalce8809a2018-01-18 14:02:47 -0800555 public int getExpectedWidth() {
556 return getMeasuredWidth();
557 }
558
Sunny Goyalbc683e92017-12-06 10:25:07 -0800559 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800560 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800561 - mInsets.left - mInsets.right;
562 }
563
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800565 public void requestLayout() {
566 mIsLayoutValid = false;
567 super.requestLayout();
568 }
569
570 @Override
571 public void forceLayout() {
572 mIsLayoutValid = false;
573 super.forceLayout();
574 }
575
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100576 private int getPageWidthSize(int widthSize) {
577 return (widthSize - mInsets.left - mInsets.right) / getPanelCount();
578 }
579
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800580 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700581 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700582 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700583 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
584 return;
585 }
586
Adam Cohen7d30a372013-07-01 17:03:59 -0700587 // We measure the dimensions of the PagedView to be larger than the pages so that when we
588 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700589 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
590 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
591 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700592 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700593
Adam Cohen7d30a372013-07-01 17:03:59 -0700594 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
595 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
596 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
598
Winson Chung8aad6102012-05-11 16:27:49 -0700599 // Return early if we aren't given a proper dimension
600 if (widthSize <= 0 || heightSize <= 0) {
601 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
602 return;
603 }
604
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700606 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700607 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700608
Sunny Goyalac00cba2017-11-13 15:58:01 -0800609 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100610 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800611 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800612 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700613
Sunny Goyalac00cba2017-11-13 15:58:01 -0800614 // measureChildren takes accounts for content padding, we only need to care about extra
615 // space due to insets.
616 measureChildren(myWidthSpec, myHeightSpec);
617 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800618 }
619
Sunny Goyalcf25b522015-07-09 00:01:18 -0700620 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700622 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800623 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700624 final int childCount = getChildCount();
625 boolean pageScrollChanged = false;
626 if (mPageScrolls == null || childCount != mPageScrolls.length) {
627 mPageScrolls = new int[childCount];
628 pageScrollChanged = true;
629 }
630
631 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700632 return;
633 }
634
Winson Chung785d2eb2011-04-14 16:08:02 -0700635 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700636
Vinit Nayaka406f722019-12-19 11:50:55 -0800637 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
638 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700639 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700641
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700642 final LayoutTransition transition = getLayoutTransition();
643 // If the transition is running defer updating max scroll, as some empty pages could
644 // still be present, and a max scroll change could cause sudden jumps in scroll.
645 if (transition != null && transition.isRunning()) {
646 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
647
648 @Override
649 public void startTransition(LayoutTransition transition, ViewGroup container,
650 View view, int transitionType) { }
651
652 @Override
653 public void endTransition(LayoutTransition transition, ViewGroup container,
654 View view, int transitionType) {
655 // Wait until all transitions are complete.
656 if (!transition.isRunning()) {
657 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700658 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700659 }
660 }
661 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700662 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700663 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700664 }
665
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700666 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
667 updateCurrentPageScroll();
668 mFirstLayout = false;
669 }
670
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800671 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700672 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700673 }
Winson Chunge3193b92010-09-10 11:44:42 -0700674 }
675
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700676 /**
677 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
678 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700679 */
680 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
681 ComputePageScrollsLogic scrollLogic) {
682 final int childCount = getChildCount();
683
684 final int startIndex = mIsRtl ? childCount - 1 : 0;
685 final int endIndex = mIsRtl ? -1 : childCount;
686 final int delta = mIsRtl ? -1 : 1;
687
Vinit Nayaka406f722019-12-19 11:50:55 -0800688 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700689
Vinit Nayaka406f722019-12-19 11:50:55 -0800690 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
691 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700692 boolean pageScrollChanged = false;
693
Vinit Nayaka406f722019-12-19 11:50:55 -0800694 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700695 final View child = getPageAt(i);
696 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800697 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
698 pageCenter, layoutChildren);
699 final int primaryDimension = bounds.primaryDimension;
700 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700701
Sunny Goyalc82c6392018-06-06 15:39:13 -0700702 // In case the pages are of different width, align the page to left or right edge
703 // based on the orientation.
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100704 // In case we have multiple panels on the screen, scrollOffsetEnd is the scroll
705 // needed for the whole visible area, so we have to divide it by panelCount.
Sunny Goyalc82c6392018-06-06 15:39:13 -0700706 final int pageScroll = mIsRtl
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100707 ? (childStart - scrollOffsetStart)
708 : Math.max(0, childPrimaryEnd - scrollOffsetEnd / getPanelCount());
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700709 if (outPageScrolls[i] != pageScroll) {
710 pageScrollChanged = true;
711 outPageScrolls[i] = pageScroll;
712 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800713 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700714 }
715 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100716
717 int panelCount = getPanelCount();
718 if (panelCount > 1) {
719 for (int i = 0; i < childCount; i++) {
720 // In case we have multiple panels, always use left panel's page scroll for all
721 // panels on the screen.
722 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
723 if (outPageScrolls[i] != adjustedScroll) {
724 outPageScrolls[i] = adjustedScroll;
725 pageScrollChanged = true;
726 }
727 }
728 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700729 return pageScrollChanged;
730 }
731
Sunny Goyala1fbd842015-05-20 13:40:27 -0700732 protected int getChildGap() {
733 return 0;
734 }
735
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700736 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800737 mMinScroll = computeMinScroll();
738 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700739 }
740
Vinit Nayaka406f722019-12-19 11:50:55 -0800741 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700742 return 0;
Tony50876bf2018-09-19 23:09:14 -0400743 }
744
Vinit Nayaka406f722019-12-19 11:50:55 -0800745 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700746 int childCount = getChildCount();
747 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700748 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700749 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700750 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700751 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700752 }
753 }
754
Adam Cohen3b185e22013-10-29 14:45:58 -0700755 public void setPageSpacing(int pageSpacing) {
756 mPageSpacing = pageSpacing;
757 requestLayout();
758 }
759
Tony50876bf2018-09-19 23:09:14 -0400760 public int getPageSpacing() {
761 return mPageSpacing;
762 }
763
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800764 private void dispatchPageCountChanged() {
765 if (mPageIndicator != null) {
766 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700767 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700768 // This ensures that when children are added, they get the correct transforms / alphas
769 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700770 invalidate();
771 }
772
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800774 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800775 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800776 dispatchPageCountChanged();
777 }
778
779 @Override
780 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800781 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700782 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800783 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700784 }
785
Adam Cohen73894962011-10-31 13:17:17 -0700786 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700787 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800788 View pageAtIndex = getPageAt(index);
789 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700790 }
791
Alex Chau7944ee52021-01-08 19:07:25 +0000792 protected int getChildVisibleSize(int index) {
793 View layout = getPageAt(index);
794 return mOrientationHandler.getMeasuredSize(layout);
795 }
796
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 @Override
798 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700799 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700800 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700801 if (immediate) {
802 setCurrentPage(page);
803 } else {
804 snapToPage(page);
805 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 return true;
807 }
808 return false;
809 }
810
811 @Override
812 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700813 int focusablePage;
814 if (mNextPage != INVALID_PAGE) {
815 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700817 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 }
Winson Chung86f77532010-08-24 11:08:22 -0700819 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700821 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 }
823 return false;
824 }
825
826 @Override
827 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800828 if (super.dispatchUnhandledMove(focused, direction)) {
829 return true;
830 }
831
832 if (mIsRtl) {
833 if (direction == View.FOCUS_LEFT) {
834 direction = View.FOCUS_RIGHT;
835 } else if (direction == View.FOCUS_RIGHT) {
836 direction = View.FOCUS_LEFT;
837 }
838 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (getCurrentPage() > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100841 int nextPage = validateNewPage(getCurrentPage() - 1);
842 snapToPage(nextPage);
843 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 return true;
845 }
846 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700847 if (getCurrentPage() < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100848 int nextPage = validateNewPage(getCurrentPage() + 1);
849 snapToPage(nextPage);
850 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 return true;
852 }
853 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800854 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 }
856
857 @Override
858 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700859 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
860 return;
861 }
862
Adam Cohen0ffac432013-07-10 11:19:26 -0700863 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700864 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700865 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 }
867 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700868 if (mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100869 int nextPage = validateNewPage(mCurrentPage - 1);
870 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100872 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700873 if (mCurrentPage < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100874 int nextPage = validateNewPage(mCurrentPage + 1);
875 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 }
877 }
878 }
879
880 /**
881 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700882 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 *
Winson Chung86f77532010-08-24 11:08:22 -0700884 * This happens when live folders requery, and if they're off page, they
885 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 */
887 @Override
888 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700889 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 View v = focused;
891 while (true) {
892 if (v == current) {
893 super.focusableViewAvailable(focused);
894 return;
895 }
896 if (v == this) {
897 return;
898 }
899 ViewParent parent = v.getParent();
900 if (parent instanceof View) {
901 v = (View)v.getParent();
902 } else {
903 return;
904 }
905 }
906 }
907
908 /**
909 * {@inheritDoc}
910 */
911 @Override
912 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
913 if (disallowIntercept) {
914 // We need to make sure to cancel our long press if
915 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700916 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700917 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 }
919 super.requestDisallowInterceptTouchEvent(disallowIntercept);
920 }
921
922 @Override
923 public boolean onInterceptTouchEvent(MotionEvent ev) {
924 /*
925 * This method JUST determines whether we want to intercept the motion.
926 * If we return true, onTouchEvent will be called and we do the actual
927 * scrolling there.
928 */
929
Winson Chung45e1d6e2010-11-09 17:19:49 -0800930 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700931 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700932
933 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800934
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 /*
936 * Shortcut the most recurring case: the user is in the dragging
937 * state and he is moving his finger. We want to intercept this
938 * motion.
939 */
940 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700941 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 return true;
943 }
944
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 switch (action & MotionEvent.ACTION_MASK) {
946 case MotionEvent.ACTION_MOVE: {
947 /*
948 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800949 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700951 if (mActivePointerId != INVALID_POINTER) {
952 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700953 }
954 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800955 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700956 // i.e. fall through to the next case (don't break)
957 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
958 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700959 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 }
961
962 case MotionEvent.ACTION_DOWN: {
963 final float x = ev.getX();
964 final float y = ev.getY();
965 // Remember location of down touch
966 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700967 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700968 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800969 mLastMotionRemainder = 0;
970 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700971 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700973
Tony Wickhama0aee212019-09-18 09:24:03 -0700974 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700975
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 break;
977 }
978
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800980 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700981 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 break;
983
984 case MotionEvent.ACTION_POINTER_UP:
985 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800986 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 break;
988 }
989
990 /*
991 * The only time we want to intercept motion events is if we are in the
992 * drag mode.
993 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700994 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 }
996
Tony Wickhama0aee212019-09-18 09:24:03 -0700997 /**
998 * If being flinged and user touches the screen, initiate drag; otherwise don't.
999 */
1000 private void updateIsBeingDraggedOnTouchDown() {
1001 // mScroller.isFinished should be false when being flinged.
1002 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -07001003 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001004
1005 if (finishedScrolling) {
1006 mIsBeingDragged = false;
1007 if (!mScroller.isFinished() && !mFreeScroll) {
1008 setCurrentPage(getNextPage());
1009 pageEndTransition();
1010 }
1011 } else {
1012 mIsBeingDragged = true;
1013 }
1014 }
1015
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001016 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001017 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001018 }
1019
Adam Cohenf8d28232011-02-01 21:47:00 -08001020 protected void determineScrollingStart(MotionEvent ev) {
1021 determineScrollingStart(ev, 1.0f);
1022 }
1023
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 /*
1025 * Determines if we should change the touch state to start scrolling after the
1026 * user moves their touch point too far.
1027 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001028 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001029 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001031 if (pointerIndex == -1) return;
1032
Vinit Nayaka406f722019-12-19 11:50:55 -08001033 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1034 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001035 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001036 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001037
Vinit Nayaka406f722019-12-19 11:50:55 -08001038 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001039 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001040 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001041 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1042 mLastMotion = primaryDirection;
1043 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001044 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001045 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001046 // Stop listening for things like pinches.
1047 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001048 }
1049 }
1050
1051 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001052 // Try canceling the long press. It could also have been scheduled
1053 // by a distant descendant, so use the mAllowLongPress flag to block
1054 // everything
1055 final View currentPage = getPageAt(mCurrentPage);
1056 if (currentPage != null) {
1057 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 }
1059 }
1060
Adam Cohenb5ba0972011-09-07 18:02:31 -07001061 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001062 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001063
Adam Cohenedb40762013-07-18 16:45:45 -07001064 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001065 int count = getChildCount();
1066
1067 final int totalDistance;
1068
1069 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001070 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001071 adjacentPage = page - 1;
1072 }
1073
1074 if (adjacentPage < 0 || adjacentPage > count - 1) {
1075 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1076 } else {
1077 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1078 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001079
1080 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001081 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1082 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001083 return scrollProgress;
1084 }
1085
Adam Cohenedb40762013-07-18 16:45:45 -07001086 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001087 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001088 return 0;
1089 } else {
1090 return mPageScrolls[index];
1091 }
1092 }
1093
Adam Cohen564a2e72013-10-09 14:47:32 -07001094 // While layout transitions are occurring, a child's position may stray from its baseline
1095 // position. This method returns the magnitude of this stray at any given time.
1096 public int getLayoutTransitionOffsetForPage(int index) {
1097 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1098 return 0;
1099 } else {
1100 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001101
Sunny Goyal228153d2018-01-04 15:35:22 -08001102 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1103 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001104 return (int) (child.getX() - baselineX);
1105 }
1106 }
1107
Jon Miranda71cb80c2019-01-24 21:25:13 -08001108 @Override
1109 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001110 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001111 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001112 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001113 super.dispatchDraw(canvas);
1114
1115 canvas.restoreToCount(saveCount);
1116 } else {
1117 super.dispatchDraw(canvas);
1118 }
1119 }
1120
Jon Miranda57dab6f2020-04-10 13:38:14 -07001121 /**
1122 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1123 */
1124 private int getSpringOverScroll(int amount) {
1125 if (mScroller.isSpringing()) {
1126 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001127 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001128 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1129 } else {
1130 return 0;
1131 }
1132 }
1133
Sunny Goyalb7b01352018-08-09 12:31:20 -07001134 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001135 if (amount == 0) {
1136 return;
1137 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001138
Vinit Nayaka406f722019-12-19 11:50:55 -08001139 int size = mOrientationHandler.getMeasuredSize(this);
1140 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001141 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001142 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001143 invalidate();
1144 return;
1145 }
1146
Vinit Nayaka406f722019-12-19 11:50:55 -08001147 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1148 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1149 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001150 invalidate();
1151 }
1152
Sunny Goyalb7b01352018-08-09 12:31:20 -07001153 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001154 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001155 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001156 invalidate();
1157 return;
1158 }
1159
Sunny Goyalb7b01352018-08-09 12:31:20 -07001160 if (amount == 0) return;
1161
1162 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001163 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1164 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001165 } else {
1166 dampedOverScroll(amount);
1167 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001168 }
1169
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001170
Tony Wickham023f4042019-01-29 10:52:31 -08001171 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001172 if (mFreeScroll == freeScroll) {
1173 return;
1174 }
1175
Tony Wickham8f7ead32016-04-07 18:46:44 -07001176 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001177 mFreeScroll = freeScroll;
1178
Adam Cohenf9618852013-11-08 06:45:03 -08001179 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001180 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001181 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001182 if (getScrollForPage(getNextPage()) != getScrollX()) {
1183 snapToPage(getNextPage());
1184 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001185 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001186 }
1187
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001188 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001189 mAllowOverScroll = enable;
1190 }
1191
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 @Override
1193 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001194 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001195 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001196
Michael Jurkab8f06722010-10-10 15:58:46 -07001197 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001198
1199 final int action = ev.getAction();
1200
1201 switch (action & MotionEvent.ACTION_MASK) {
1202 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001203 updateIsBeingDraggedOnTouchDown();
1204
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 /*
1206 * If being flinged and user touches, stop the fling. isFinished
1207 * will be false if being flinged.
1208 */
1209 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001210 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 }
1212
1213 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001214 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001215 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001216 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1217 mLastMotionRemainder = 0;
1218 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001219 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001221 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001222 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001223 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001224 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 break;
1226
Winson Chung4941f652020-06-18 15:11:53 -07001227 case ACTION_MOVE_ALLOW_EASY_FLING:
1228 // Start scrolling immediately
1229 determineScrollingStart(ev);
1230 mAllowEasyFling = true;
1231 break;
1232
1233 case MotionEvent.ACTION_MOVE:
1234 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 // Scroll to follow the motion event
1236 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001237
1238 if (pointerIndex == -1) return true;
1239
Vinit Nayaka406f722019-12-19 11:50:55 -08001240 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1241 float delta = mLastMotion + mLastMotionRemainder - direction;
1242 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001243
Winson Chungc0844aa2011-02-02 15:25:58 -08001244 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1245 // keep the remainder because we are actually testing if we've moved from the last
1246 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001247 if (Math.abs(delta) >= 1.0f) {
1248 mLastMotion = direction;
1249 mLastMotionRemainder = delta - (int) delta;
1250
1251 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 } else {
1253 awakenScrollBars();
1254 }
Adam Cohen564976a2010-10-13 18:52:07 -07001255 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 determineScrollingStart(ev);
1257 }
1258 break;
1259
1260 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001261 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 final int activePointerId = mActivePointerId;
1263 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001264 if (pointerIndex == -1) return true;
1265
Vinit Nayaka406f722019-12-19 11:50:55 -08001266 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1267 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001268 final VelocityTracker velocityTracker = mVelocityTracker;
1269 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001270
Vinit Nayaka406f722019-12-19 11:50:55 -08001271 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1272 mActivePointerId);
1273 int delta = (int) (primaryDirection - mDownMotionPrimary);
1274 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1275
1276 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1277 SIGNIFICANT_MOVE_THRESHOLD;
1278
1279 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001280 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1281 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001282 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1283 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001284 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1285 Log.d("Quickswitch", "isFling=false vel=" + velocity
1286 + " threshold=" + mEasyFlingThresholdVelocity);
1287 }
Adam Cohen00481b32011-11-18 12:03:48 -08001288
Adam Cohenf358a4b2013-07-23 16:47:31 -07001289 if (!mFreeScroll) {
1290 // In the case that the page is moved far to one direction and then is flung
1291 // in the opposite direction, we use a threshold to determine whether we should
1292 // just return to the starting page, or if we should skip one further.
1293 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001294 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1295 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001296 returnToOriginalPage = true;
1297 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001298
Adam Cohenf358a4b2013-07-23 16:47:31 -07001299 int finalPage;
1300 // We give flings precedence over large moves, which is why we short-circuit our
1301 // test for a large move if a fling has been registered. That is, a large
1302 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001303
Vinit Nayaka406f722019-12-19 11:50:55 -08001304 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1305 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001306 finalPage = returnToOriginalPage
1307 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001308 snapToPageWithVelocity(finalPage, velocity);
1309 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1310 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001311 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001312 finalPage = returnToOriginalPage
1313 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001314 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001315 } else {
1316 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001317 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001319 if (!mScroller.isFinished()) {
1320 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001321 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001322
Vinit Nayaka406f722019-12-19 11:50:55 -08001323 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1324 int maxScroll = mMaxScroll;
1325 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001326
Vinit Nayaka406f722019-12-19 11:50:55 -08001327 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1328 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1329 mScroller.springBack(initialScroll, minScroll, maxScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001330 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001331 } else {
1332 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001333 mScroller.fling(initialScroll, -velocity,
1334 minScroll, maxScroll,
1335 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001336
Vinit Nayaka406f722019-12-19 11:50:55 -08001337 int finalPos = mScroller.getFinalPos();
Alex Chaub794ea62021-02-03 18:00:30 +00001338 mNextPage = getDestinationPage(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001339
1340 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1341 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001342 if (snapToPageInFreeScroll() && finalPos > minScroll
1343 && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001344 // If scrolling ends in the half of the added space that is closer to
1345 // the end, settle to the end. Otherwise snap to the nearest page.
1346 // If flinging past one of the ends, don't change the velocity as it
1347 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001348 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1349 ? minScroll
1350 : finalPos > (lastPageScroll + maxScroll) / 2
1351 ? maxScroll
1352 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001353
Vinit Nayaka406f722019-12-19 11:50:55 -08001354 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001355 // Ensure the scroll/snap doesn't happen too fast;
1356 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001357 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001358 if (extraScrollDuration > 0) {
1359 mScroller.extendDuration(extraScrollDuration);
1360 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001361 }
1362 }
1363 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001365 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001367
Adam Cohen7d30a372013-07-01 17:03:59 -07001368 // End any intermediate reordering states
1369 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 break;
1371
1372 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001373 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001374 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001375 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001376 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001377 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 break;
1379
1380 case MotionEvent.ACTION_POINTER_UP:
1381 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001382 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 break;
1384 }
1385
1386 return true;
1387 }
1388
Alex Chaub794ea62021-02-03 18:00:30 +00001389 protected boolean snapToPageInFreeScroll() {
1390 return true;
1391 }
1392
Vinit Nayaka406f722019-12-19 11:50:55 -08001393 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001394 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1395 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001396 }
1397
Adam Cohen7d30a372013-07-01 17:03:59 -07001398 private void resetTouchState() {
1399 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001400 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001401 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001402 }
1403
Adam Cohenc2d6e892014-10-16 09:49:24 -07001404 /**
1405 * Triggered by scrolling via touch
1406 */
1407 protected void onScrollInteractionBegin() {
1408 }
1409
1410 protected void onScrollInteractionEnd() {
1411 }
1412
Winson Chung185d7162011-02-28 13:47:29 -08001413 @Override
1414 public boolean onGenericMotionEvent(MotionEvent event) {
1415 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1416 switch (event.getAction()) {
1417 case MotionEvent.ACTION_SCROLL: {
1418 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1419 final float vscroll;
1420 final float hscroll;
1421 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1422 vscroll = 0;
1423 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1424 } else {
1425 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1426 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1427 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001428 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1429 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001430 }
Winson Chung185d7162011-02-28 13:47:29 -08001431 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001432 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001433 : (hscroll > 0 || vscroll > 0);
1434 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001435 scrollRight();
1436 } else {
1437 scrollLeft();
1438 }
1439 return true;
1440 }
1441 }
1442 }
1443 }
1444 return super.onGenericMotionEvent(event);
1445 }
1446
Sunny Goyal8b37c572020-03-31 12:12:14 -07001447 /**
1448 * Returns true if the paged view can scroll for the provided vertical and horizontal
1449 * scroll values
1450 */
1451 protected boolean canScroll(float absVScroll, float absHScroll) {
1452 ActivityContext ac = ActivityContext.lookupContext(getContext());
1453 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001454 }
1455
Michael Jurkab8f06722010-10-10 15:58:46 -07001456 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1457 if (mVelocityTracker == null) {
1458 mVelocityTracker = VelocityTracker.obtain();
1459 }
1460 mVelocityTracker.addMovement(ev);
1461 }
1462
1463 private void releaseVelocityTracker() {
1464 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001465 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001466 mVelocityTracker.recycle();
1467 mVelocityTracker = null;
1468 }
1469 }
1470
Winson Chung321e9ee2010-08-09 13:37:56 -07001471 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001472 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 final int pointerId = ev.getPointerId(pointerIndex);
1474 if (pointerId == mActivePointerId) {
1475 // This was our active pointer going up. Choose a new
1476 // active pointer and adjust accordingly.
1477 // TODO: Make this decision more intelligent.
1478 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001479 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1480 newPointerIndex);
1481 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 mActivePointerId = ev.getPointerId(newPointerIndex);
1483 if (mVelocityTracker != null) {
1484 mVelocityTracker.clear();
1485 }
1486 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001487 }
1488
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 @Override
1490 public void requestChildFocus(View child, View focused) {
1491 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001492 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001493 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001494 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 }
1496 }
1497
Alex Chaub794ea62021-02-03 18:00:30 +00001498 public int getDestinationPage() {
1499 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1500 }
1501
1502 protected int getDestinationPage(int scaledScroll) {
1503 return getPageNearestToCenterOfScreen(scaledScroll);
1504 }
1505
Sunny Goyal228153d2018-01-04 15:35:22 -08001506 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001507 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001508 }
1509
Vinit Nayaka406f722019-12-19 11:50:55 -08001510 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1511 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1512 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001513 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001514 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 final int childCount = getChildCount();
1516 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001517 int distanceFromScreenCenter = Math.abs(
1518 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001519 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1520 minDistanceFromScreenCenter = distanceFromScreenCenter;
1521 minDistanceFromScreenCenterIndex = i;
1522 }
1523 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001524 return minDistanceFromScreenCenterIndex;
1525 }
1526
Tony Wickham7383d4e2020-07-07 19:25:25 -07001527 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chau7944ee52021-01-08 19:07:25 +00001528 int childSize = getChildVisibleSize(childIndex);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001529 int halfChildSize = (childSize / 2);
1530 int childCenter = getChildOffset(childIndex) + halfChildSize;
1531 return childCenter - screenCenter;
1532 }
1533
1534 protected int getDisplacementFromScreenCenter(int childIndex) {
1535 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1536 int screenCenter = mOrientationHandler.getPrimaryScroll(this) + (pageOrientationSize / 2);
1537 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1538 }
1539
Adam Cohend19d3ca2010-09-15 14:43:42 -07001540 protected void snapToDestination() {
Alex Chaub794ea62021-02-03 18:00:30 +00001541 snapToPage(getDestinationPage(), getPageSnapDuration());
Adam Cohen8d769d62017-06-23 17:27:38 -07001542 }
1543
1544 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001545 int scroll = mOrientationHandler.getPrimaryScroll(this);
1546 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001547 }
1548
1549 protected int getPageSnapDuration() {
1550 if (isInOverScroll()) {
1551 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1552 }
1553 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001554 }
1555
Adam Cohene0f66b52010-11-23 15:06:07 -08001556 // We want the duration of the page snap animation to be influenced by the distance that
1557 // the screen has to travel, however, we don't want this duration to be effected in a
1558 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1559 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001560 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001561 f -= 0.5f; // center the values about 0.
1562 f *= 0.3f * Math.PI / 2.0f;
1563 return (float) Math.sin(f);
1564 }
1565
Winson Chung05a31ed2018-02-08 17:08:43 -08001566 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001567 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001568 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001569
Vinit Nayaka406f722019-12-19 11:50:55 -08001570 final int newLoc = getScrollForPage(whichPage);
1571 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001572 int duration = 0;
1573
Sunny Goyal4d113a52015-05-27 10:05:28 -07001574 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001575 // If the velocity is low enough, then treat this more as an automatic page advance
1576 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001577 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001578 }
1579
1580 // Here we compute a "distance" that will be used in the computation of the overall
1581 // snap duration. This is a function of the actual distance that needs to be traveled;
1582 // we keep this value close to half screen size in order to reduce the variance in snap
1583 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001584 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001585 float distance = halfScreenSize + halfScreenSize *
1586 distanceInfluenceForSnapDuration(distanceRatio);
1587
1588 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001589 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001590
1591 // we want the page's snap velocity to approximately match the velocity at which the
1592 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001593 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1594 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001595
Jon Miranda9e198662020-03-11 14:42:02 -07001596 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001597 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001598 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001599 } else {
1600 return snapToPage(whichPage, delta, duration);
1601 }
Michael Jurka0142d492010-08-25 17:46:15 -07001602 }
1603
Winson Chung05a31ed2018-02-08 17:08:43 -08001604 public boolean snapToPage(int whichPage) {
1605 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001606 }
1607
Winson Chung05a31ed2018-02-08 17:08:43 -08001608 public boolean snapToPageImmediately(int whichPage) {
1609 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001610 }
1611
Winson Chung05a31ed2018-02-08 17:08:43 -08001612 public boolean snapToPage(int whichPage, int duration) {
1613 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001614 }
1615
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001616 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001617 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001618 }
1619
Winson Chung05a31ed2018-02-08 17:08:43 -08001620 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001621 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001622 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001623
Vinit Nayaka406f722019-12-19 11:50:55 -08001624 int newLoc = getScrollForPage(whichPage);
1625 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001626 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001627 }
1628
Winson Chung05a31ed2018-02-08 17:08:43 -08001629 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001630 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001631 }
Michael Jurka0142d492010-08-25 17:46:15 -07001632
Winson Chung05a31ed2018-02-08 17:08:43 -08001633 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001634 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001635 if (mFirstLayout) {
1636 setCurrentPage(whichPage);
1637 return false;
1638 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001639
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001640 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001641 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1642 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001643 }
1644
Adam Cohen6f127a62014-06-12 14:54:41 -07001645 whichPage = validateNewPage(whichPage);
1646
Adam Cohen7d30a372013-07-01 17:03:59 -07001647 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001648
Winson Chung321e9ee2010-08-09 13:37:56 -07001649 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001650 if (immediate) {
1651 duration = 0;
1652 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001653 duration = Math.abs(delta);
1654 }
1655
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001656 if (duration != 0) {
1657 pageBeginTransition();
1658 }
1659
Adam Cohenf358a4b2013-07-23 16:47:31 -07001660 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001661 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001662 }
Adam Cohenf9618852013-11-08 06:45:03 -08001663
1664 if (interpolator != null) {
1665 mScroller.setInterpolator(interpolator);
1666 } else {
1667 mScroller.setInterpolator(mDefaultInterpolator);
1668 }
1669
Jon Miranda71cb80c2019-01-24 21:25:13 -08001670 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001671 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001672 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001673 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001674 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001675
Adam Cohen674531f2013-12-13 15:07:14 -08001676 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001677
1678 // Trigger a compute() to finish switching pages if necessary
1679 if (immediate) {
1680 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001681 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001682 }
1683
Winson Chung321e9ee2010-08-09 13:37:56 -07001684 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001685 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001686 }
1687
Vadim Tryshev98913d02018-05-18 18:41:34 -07001688 public boolean scrollLeft() {
1689 if (getNextPage() > 0) {
1690 snapToPage(getNextPage() - 1);
1691 return true;
1692 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001693 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001694 }
1695
Vadim Tryshev98913d02018-05-18 18:41:34 -07001696 public boolean scrollRight() {
1697 if (getNextPage() < getChildCount() - 1) {
1698 snapToPage(getNextPage() + 1);
1699 return true;
1700 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001701 return onOverscroll(getMeasuredWidth());
1702 }
1703
1704 protected boolean onOverscroll(int amount) {
1705 if (!mAllowOverScroll) return false;
1706 onScrollInteractionBegin();
1707 overScroll(amount);
1708 onScrollInteractionEnd();
1709 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001710 }
1711
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001712 @Override
1713 public CharSequence getAccessibilityClassName() {
1714 // Some accessibility services have special logic for ScrollView. Since we provide same
1715 // accessibility info as ScrollView, inform the service to handle use the same way.
1716 return ScrollView.class.getName();
1717 }
1718
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001719 protected boolean isPageOrderFlipped() {
1720 return false;
1721 }
1722
Winson Chung6a0f57d2011-06-29 20:10:49 -07001723 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001724 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001725 @Override
1726 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1727 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001728 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001729 int offset = (mAllowOverScroll ? 0 : 1);
1730 info.setScrollable(getPageCount() > offset);
1731 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001732 info.addAction(pagesFlipped ?
1733 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1734 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1735 info.addAction(mIsRtl ?
1736 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1737 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001738 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001739 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001740 info.addAction(pagesFlipped ?
1741 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1742 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1743 info.addAction(mIsRtl ?
1744 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1745 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001746 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001747 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1748 // Besides disabling the accessibility long-click, this also prevents this view from getting
1749 // accessibility focus.
1750 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001751 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001752 }
1753
1754 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001755 public void sendAccessibilityEvent(int eventType) {
1756 // Don't let the view send real scroll events.
1757 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1758 super.sendAccessibilityEvent(eventType);
1759 }
1760 }
1761
1762 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001763 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1764 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001765 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001766 }
1767
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001768 @Override
1769 public boolean performAccessibilityAction(int action, Bundle arguments) {
1770 if (super.performAccessibilityAction(action, arguments)) {
1771 return true;
1772 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001773 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001774 switch (action) {
1775 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001776 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001777 return true;
1778 }
1779 } break;
1780 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001781 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001782 return true;
1783 }
yingleiw02cc8482019-08-02 16:14:27 -07001784 } break;
1785 case android.R.id.accessibilityActionPageRight: {
1786 if (!mIsRtl) {
1787 return scrollRight();
1788 } else {
1789 return scrollLeft();
1790 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001791 }
yingleiw02cc8482019-08-02 16:14:27 -07001792 case android.R.id.accessibilityActionPageLeft: {
1793 if (!mIsRtl) {
1794 return scrollLeft();
1795 } else {
1796 return scrollRight();
1797 }
1798 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001799 }
1800 return false;
1801 }
1802
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001803 protected boolean canAnnouncePageDescription() {
1804 return true;
1805 }
1806
Adam Cohen0ffac432013-07-10 11:19:26 -07001807 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001808 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001809 getNextPage() + 1, getChildCount());
1810 }
1811
Tony Mak2e564402018-02-27 17:19:34 +00001812 protected float getDownMotionX() {
1813 return mDownMotionX;
1814 }
1815
1816 protected float getDownMotionY() {
1817 return mDownMotionY;
1818 }
1819
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001820 protected interface ComputePageScrollsLogic {
1821
1822 boolean shouldIncludeView(View view);
1823 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001824
1825 public int[] getVisibleChildrenRange() {
1826 float visibleLeft = 0;
1827 float visibleRight = visibleLeft + getMeasuredWidth();
1828 float scaleX = getScaleX();
1829 if (scaleX < 1 && scaleX > 0) {
1830 float mid = getMeasuredWidth() / 2;
1831 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1832 visibleRight = mid + ((visibleRight - mid) / scaleX);
1833 }
1834
1835 int leftChild = -1;
1836 int rightChild = -1;
1837 final int childCount = getChildCount();
1838 for (int i = 0; i < childCount; i++) {
1839 final View child = getPageAt(i);
1840
1841 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1842 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1843 if (leftChild == -1) {
1844 leftChild = i;
1845 }
1846 rightChild = i;
1847 }
1848 }
1849 mTmpIntPair[0] = leftChild;
1850 mTmpIntPair[1] = rightChild;
1851 return mTmpIntPair;
1852 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001853}