blob: 7d7739eb1279f571fcd7d4c27e14d530170f5d02 [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 Goyal3e2ff8a2015-04-15 16:31:22 -070019import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070020import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070021import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080024import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070026import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070027import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070029import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080030import android.view.InputDevice;
31import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.MotionEvent;
Vinit Nayaka406f722019-12-19 11:50:55 -080033import android.view.Surface;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080037import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.ViewGroup;
39import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070043import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070044
Vinit Nayaka406f722019-12-19 11:50:55 -080045import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
46import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
47import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
48import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
49import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
50import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
51import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
52
Tony Wickham62117d72020-03-26 16:56:26 -070053import androidx.annotation.Nullable;
54
Jon Miranda9e198662020-03-11 14:42:02 -070055import com.android.launcher3.anim.Interpolators;
56import com.android.launcher3.compat.AccessibilityManagerCompat;
57import com.android.launcher3.config.FeatureFlags;
58import com.android.launcher3.model.PagedViewOrientedState;
59import com.android.launcher3.pageindicators.PageIndicator;
60import com.android.launcher3.touch.OverScroll;
61import com.android.launcher3.touch.PagedOrientationHandler;
62import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
63import com.android.launcher3.touch.PortraitPagedViewHandler;
64import com.android.launcher3.util.OverScroller;
65import com.android.launcher3.util.Thunk;
66
67import java.util.ArrayList;
68
Winson Chung321e9ee2010-08-09 13:37:56 -070069/**
70 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070071 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070072 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080073public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070074 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070075 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070076
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;
94 private static final int MIN_SNAP_VELOCITY = 1500;
95 private static final int MIN_FLING_VELOCITY = 250;
96
Adam Cohenf358a4b2013-07-23 16:47:31 -070097 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070098
Adam Cohen265b9a62011-12-07 14:37:18 -080099 protected int mFlingThresholdVelocity;
100 protected int mMinFlingVelocity;
101 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700102
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected boolean mFirstLayout = true;
104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700107
Sunny Goyal4ffec482016-02-09 11:28:52 -0800108 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700109 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800110 protected int mMaxScroll;
111 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700112 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800113 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800115 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
117 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700118 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800119 private float mDownMotionPrimary;
120 private float mLastMotion;
121 private float mLastMotionRemainder;
122 private float mTotalMotion;
123 protected PagedOrientationHandler mOrientationHandler = new PortraitPagedViewHandler();
124 protected final PagedViewOrientedState mOrientationState = new PagedViewOrientedState();
Adam Cohenedb40762013-07-18 16:45:45 -0700125
Vadim Trysheve628c472018-05-16 14:28:18 -0700126 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700127 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800128
Michael Jurka7426c422010-11-11 15:23:47 -0800129 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800131 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka5f1c5092010-09-03 14:15:02 -0700133 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka5f1c5092010-09-03 14:15:02 -0700135 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700137 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700138 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700139
Vinit Nayaka406f722019-12-19 11:50:55 -0800140 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800141
Sunny Goyal061380a2016-02-29 15:15:03 -0800142 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700143
Vinit Nayaka406f722019-12-19 11:50:55 -0800144 protected int mUnboundedScroll;
145
146 protected int mLayoutRotation = Surface.ROTATION_0;
147 protected int mDisplayRotation = Surface.ROTATION_0;
Adam Cohen8d769d62017-06-23 17:27:38 -0700148
Winson Chungd2be3812013-07-16 11:11:32 -0700149 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700150 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800151 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700152
John Spurlock77e1f472013-09-11 10:09:51 -0400153 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800154 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400155
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800156 // Similar to the platform implementation of isLayoutValid();
157 protected boolean mIsLayoutValid;
158
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700159 private int[] mTmpIntPair = new int[2];
160
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 public PagedView(Context context) {
162 this(context, null);
163 }
164
165 public PagedView(Context context, AttributeSet attrs) {
166 this(context, attrs, 0);
167 }
168
169 public PagedView(Context context, AttributeSet attrs, int defStyle) {
170 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700171
Adam Cohen9c4949e2010-10-05 12:27:22 -0700172 TypedArray a = context.obtainStyledAttributes(attrs,
173 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700174 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 a.recycle();
176
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700178 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700179 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 }
181
182 /**
183 * Initializes various states for this workspace.
184 */
Michael Jurka0142d492010-08-25 17:46:15 -0700185 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800186 Context context = getContext();
187 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700188 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700189 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700190
Vinit Nayaka406f722019-12-19 11:50:55 -0800191 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700192 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800194
Sunny Goyalcf25b522015-07-09 00:01:18 -0700195 float density = getResources().getDisplayMetrics().density;
196 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
197 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
198 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700199
200 if (Utilities.ATLEAST_OREO) {
201 setDefaultFocusHighlightEnabled(false);
202 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 }
204
Adam Cohenf9618852013-11-08 06:45:03 -0800205 protected void setDefaultInterpolator(Interpolator interpolator) {
206 mDefaultInterpolator = interpolator;
207 mScroller.setInterpolator(mDefaultInterpolator);
208 }
209
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700210 public void initParentViews(View parent) {
211 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800212 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700213 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700214 }
215 }
216
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800217 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700218 return mPageIndicator;
219 }
220
Adam Cohen674531f2013-12-13 15:07:14 -0800221 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700222 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
223 * that the user was on before entering free scroll mode (e.g. the home screen page they
224 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
225 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700227 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700228 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700230
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700231 /**
232 * Returns the index of page to be shown immediately afterwards.
233 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700234 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700235 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
236 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700237
Adam Cohenf9c184a2016-01-15 16:47:43 -0800238 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 return getChildCount();
240 }
241
Sunny Goyal83a8f042015-05-19 12:52:12 -0700242 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 return getChildAt(index);
244 }
245
Adam Cohenae4f1552011-10-20 00:15:42 -0700246 protected int indexToPage(int index) {
247 return index;
248 }
249
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800251 * Updates the scroll of the current page immediately to its final scroll position. We use this
252 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
253 * the previous tab page.
254 */
255 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700256 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800257 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700258 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800259 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700260 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800261 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
262 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700263 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800264 }
265
Sreyase3e7e632020-01-29 10:42:10 -0800266 /**
267 * Returns left offset of a page. This is the gap between pages and prevents overlap.
268 */
269 public int scrollOffsetLeft() {
270 return mInsets.left + getPaddingLeft();
271 }
272
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700273 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700274 mScroller.abortAnimation();
275 // We need to clean up the next page here to avoid computeScrollHelper from
276 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700277 if (resetNextPage) {
278 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800279 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700280 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700281 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700282
Tony Wickham8f7ead32016-04-07 18:46:44 -0700283 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700284 mScroller.forceFinished(true);
285 // We need to clean up the next page here to avoid computeScrollHelper from
286 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700287 if (resetNextPage) {
288 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800289 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700290 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700291 }
292
Adam Cohen6f127a62014-06-12 14:54:41 -0700293 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700294 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700295 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700296 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700297 }
298
Chet Haasebc2f0822012-10-26 17:59:53 -0700299 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700300 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
301 */
302 private int ensureWithinScrollBounds(int page) {
303 int dir = !mIsRtl ? 1 : - 1;
304 int currScroll = getScrollForPage(page);
305 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800306 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700307 page += dir;
308 prevScroll = currScroll;
309 currScroll = getScrollForPage(page);
310 if (currScroll <= prevScroll) {
311 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
312 break;
313 }
314 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800315 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700316 page -= dir;
317 prevScroll = currScroll;
318 currScroll = getScrollForPage(page);
319 if (currScroll >= prevScroll) {
320 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
321 break;
322 }
323 }
324 return page;
325 }
326
Tony Wickham03f27012019-04-25 14:22:54 -0700327 public void setCurrentPage(int currentPage) {
328 setCurrentPage(currentPage, INVALID_PAGE);
329 }
330
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700331 /**
Winson Chung86f77532010-08-24 11:08:22 -0700332 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 */
Tony Wickham03f27012019-04-25 14:22:54 -0700334 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800335 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700336 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800337 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800338 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
339 // the default
340 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800341 return;
342 }
Tony Wickham03f27012019-04-25 14:22:54 -0700343 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700344 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700345 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700346 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800347 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 }
349
Winson Chung8c87cd82013-07-23 16:20:10 -0700350 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800351 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
352 * has settled.
353 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700354 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800355 updatePageIndicator();
356 }
357
358 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700359 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000360 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700361 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700363 protected void pageBeginTransition() {
364 if (!mIsPageInTransition) {
365 mIsPageInTransition = true;
366 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700367 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700368 }
369
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700370 protected void pageEndTransition() {
371 if (mIsPageInTransition) {
372 mIsPageInTransition = false;
373 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700374 }
Michael Jurka0142d492010-08-25 17:46:15 -0700375 }
376
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700377 protected boolean isPageInTransition() {
378 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700379 }
380
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700381 /**
382 * Called when the page starts moving as part of the scroll. Subclasses can override this
383 * to provide custom behavior during animation.
384 */
385 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700386 }
387
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700388 /**
389 * Called when the page ends moving as part of the scroll. Subclasses can override this
390 * to provide custom behavior during animation.
391 */
392 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700393 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700394 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800395 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
396 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700397 if (mOnPageTransitionEndCallback != null) {
398 mOnPageTransitionEndCallback.run();
399 mOnPageTransitionEndCallback = null;
400 }
401 }
402
403 /**
404 * Sets a callback to run once when the scrolling finishes. If there is currently
405 * no page in transition, then the callback is called immediately.
406 */
407 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
408 if (mIsPageInTransition || callback == null) {
409 mOnPageTransitionEndCallback = callback;
410 } else {
411 callback.run();
412 }
Michael Jurka0142d492010-08-25 17:46:15 -0700413 }
414
Vinit Nayaka406f722019-12-19 11:50:55 -0800415 protected int getUnboundedScroll() {
416 return mUnboundedScroll;
417 }
418
419 protected void updateLayoutRotation(int touchRotation) {
420 setLayoutRotation(touchRotation, mDisplayRotation);
421 }
422
423 /** @param touchRotation Must be one of {@link android.view.Surface.ROTATION_0/90/180/270} */
424 public void setLayoutRotation(int touchRotation, int displayRotation) {
425 if (mLayoutRotation == touchRotation && mDisplayRotation == displayRotation) {
426 return;
427 }
428
429 mOrientationState.update(touchRotation, displayRotation);
430 mOrientationHandler = mOrientationState.getOrientationHandler();
431 mLayoutRotation = touchRotation;
432 mDisplayRotation = displayRotation;
433 requestLayout();
434 }
435
436 public PagedViewOrientedState getPagedViewOrientedState() {
437 return mOrientationState;
438 }
439
440 public PagedOrientationHandler getPagedOrientationHandler() {
441 return getPagedViewOrientedState().getOrientationHandler();
442 }
443
444 public void disableMultipleLayoutRotations(boolean disable) {
445 mOrientationState.disableMultipleOrientations(disable);
446 mOrientationHandler = mOrientationState.getOrientationHandler();
447 requestLayout();
Sunny Goyalc86df472016-02-25 09:19:38 -0800448 }
449
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800451 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800452 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800453 }
454
455 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700456 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800457 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
458 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
459 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700460
Vinit Nayaka406f722019-12-19 11:50:55 -0800461 boolean isBeforeFirstPage = mIsRtl ?
462 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
463 boolean isAfterLastPage = mIsRtl ?
464 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
465 if (!isBeforeFirstPage && !isAfterLastPage) {
466 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800467 }
468
Vinit Nayaka406f722019-12-19 11:50:55 -0800469 if (isBeforeFirstPage) {
470 mOrientationHandler.delegateScrollTo(this,
471 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800472 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700473 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800474 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800475 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800476 } else if (isAfterLastPage) {
477 mOrientationHandler.delegateScrollTo(this,
478 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800479 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700480 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800481 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800482 }
483 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700484 if (mWasInOverscroll) {
485 overScroll(0);
486 mWasInOverscroll = false;
487 }
Adam Cohen68d73932010-11-15 10:50:58 -0800488 super.scrollTo(x, y);
489 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800490 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700491
Vinit Nayaka406f722019-12-19 11:50:55 -0800492 /**
493 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
494 */
495 public void superScrollTo(int x, int y) {
496 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700497 }
498
Adam Cohen53805212013-10-01 10:39:23 -0700499 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800500 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700501 if (mCurrentPage != getNextPage()) {
502 AccessibilityEvent ev =
503 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700504 ev.setScrollable(true);
505 ev.setScrollX(getScrollX());
506 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800507 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700508 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700509 }
Adam Cohen53805212013-10-01 10:39:23 -0700510 }
511 }
512
Michael Jurka0142d492010-08-25 17:46:15 -0700513 // we moved this functionality to a helper function so SmoothPagedView can reuse it
514 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800515 return computeScrollHelper(true);
516 }
517
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700518 protected void announcePageForAccessibility() {
519 if (isAccessibilityEnabled(getContext())) {
520 // Notify the user when the page changes
521 announceForAccessibility(getCurrentPageDescription());
522 }
523 }
524
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800525 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700526 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700527 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800528 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
529 if (mUnboundedScroll != mScroller.getCurrPos()
530 || currentScroll != mScroller.getCurrPos()) {
531 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700532 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800533 if (shouldInvalidate) {
534 invalidate();
535 }
Michael Jurka0142d492010-08-25 17:46:15 -0700536 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800537 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700538 sendScrollAccessibilityEvent();
539
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700540 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700541 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700542 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700543 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700544
Adam Cohen73aa9752010-11-24 16:26:58 -0800545 // We don't want to trigger a page end moving unless the page has settled
546 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700547 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700548 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800549 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700550
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700551 if (canAnnouncePageDescription()) {
552 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700553 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 }
Michael Jurka0142d492010-08-25 17:46:15 -0700555 return false;
556 }
557
558 @Override
559 public void computeScroll() {
560 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700561 }
562
Sunny Goyalce8809a2018-01-18 14:02:47 -0800563 public int getExpectedHeight() {
564 return getMeasuredHeight();
565 }
566
Adam Cohen410f3cd2013-09-22 12:09:32 -0700567 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800568 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800569 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700570 }
571
Sunny Goyalce8809a2018-01-18 14:02:47 -0800572 public int getExpectedWidth() {
573 return getMeasuredWidth();
574 }
575
Sunny Goyalbc683e92017-12-06 10:25:07 -0800576 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800577 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800578 - mInsets.left - mInsets.right;
579 }
580
Winson Chung321e9ee2010-08-09 13:37:56 -0700581 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800582 public void requestLayout() {
583 mIsLayoutValid = false;
584 super.requestLayout();
585 }
586
587 @Override
588 public void forceLayout() {
589 mIsLayoutValid = false;
590 super.forceLayout();
591 }
592
593 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700595 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700596 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
597 return;
598 }
599
Adam Cohen7d30a372013-07-01 17:03:59 -0700600 // We measure the dimensions of the PagedView to be larger than the pages so that when we
601 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700602 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
603 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
604 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700605 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700606
Adam Cohen7d30a372013-07-01 17:03:59 -0700607 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
608 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
609 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
611
Winson Chung8aad6102012-05-11 16:27:49 -0700612 // Return early if we aren't given a proper dimension
613 if (widthSize <= 0 || heightSize <= 0) {
614 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
615 return;
616 }
617
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700619 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700620 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700621
Sunny Goyalac00cba2017-11-13 15:58:01 -0800622 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800623 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800624 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800625 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700626
Sunny Goyalac00cba2017-11-13 15:58:01 -0800627 // measureChildren takes accounts for content padding, we only need to care about extra
628 // space due to insets.
629 measureChildren(myWidthSpec, myHeightSpec);
630 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800631 }
632
Sunny Goyalcf25b522015-07-09 00:01:18 -0700633 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700635 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800636 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700637 final int childCount = getChildCount();
638 boolean pageScrollChanged = false;
639 if (mPageScrolls == null || childCount != mPageScrolls.length) {
640 mPageScrolls = new int[childCount];
641 pageScrollChanged = true;
642 }
643
644 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700645 return;
646 }
647
Winson Chung785d2eb2011-04-14 16:08:02 -0700648 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700649
Vinit Nayaka406f722019-12-19 11:50:55 -0800650 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
651 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700652 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700654
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700655 final LayoutTransition transition = getLayoutTransition();
656 // If the transition is running defer updating max scroll, as some empty pages could
657 // still be present, and a max scroll change could cause sudden jumps in scroll.
658 if (transition != null && transition.isRunning()) {
659 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
660
661 @Override
662 public void startTransition(LayoutTransition transition, ViewGroup container,
663 View view, int transitionType) { }
664
665 @Override
666 public void endTransition(LayoutTransition transition, ViewGroup container,
667 View view, int transitionType) {
668 // Wait until all transitions are complete.
669 if (!transition.isRunning()) {
670 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700671 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700672 }
673 }
674 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700675 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700676 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700677 }
678
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700679 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
680 updateCurrentPageScroll();
681 mFirstLayout = false;
682 }
683
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800684 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700685 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700686 }
Winson Chunge3193b92010-09-10 11:44:42 -0700687 }
688
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700689 /**
690 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
691 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700692 */
693 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
694 ComputePageScrollsLogic scrollLogic) {
695 final int childCount = getChildCount();
696
697 final int startIndex = mIsRtl ? childCount - 1 : 0;
698 final int endIndex = mIsRtl ? -1 : childCount;
699 final int delta = mIsRtl ? -1 : 1;
700
Vinit Nayaka406f722019-12-19 11:50:55 -0800701 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700702
Vinit Nayaka406f722019-12-19 11:50:55 -0800703 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
704 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700705 boolean pageScrollChanged = false;
706
Vinit Nayaka406f722019-12-19 11:50:55 -0800707 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700708 final View child = getPageAt(i);
709 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800710 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
711 pageCenter, layoutChildren);
712 final int primaryDimension = bounds.primaryDimension;
713 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700714
Sunny Goyalc82c6392018-06-06 15:39:13 -0700715 // In case the pages are of different width, align the page to left or right edge
716 // based on the orientation.
717 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800718 ? (childStart - scrollOffsetStart)
719 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700720 if (outPageScrolls[i] != pageScroll) {
721 pageScrollChanged = true;
722 outPageScrolls[i] = pageScroll;
723 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800724 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700725 }
726 }
727 return pageScrollChanged;
728 }
729
Sunny Goyala1fbd842015-05-20 13:40:27 -0700730 protected int getChildGap() {
731 return 0;
732 }
733
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700734 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800735 mMinScroll = computeMinScroll();
736 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700737 }
738
Vinit Nayaka406f722019-12-19 11:50:55 -0800739 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700740 return 0;
Tony50876bf2018-09-19 23:09:14 -0400741 }
742
Vinit Nayaka406f722019-12-19 11:50:55 -0800743 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700744 int childCount = getChildCount();
745 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700746 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700747 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700748 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700749 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700750 }
751 }
752
Adam Cohen3b185e22013-10-29 14:45:58 -0700753 public void setPageSpacing(int pageSpacing) {
754 mPageSpacing = pageSpacing;
755 requestLayout();
756 }
757
Tony50876bf2018-09-19 23:09:14 -0400758 public int getPageSpacing() {
759 return mPageSpacing;
760 }
761
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800762 private void dispatchPageCountChanged() {
763 if (mPageIndicator != null) {
764 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700765 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700766 // This ensures that when children are added, they get the correct transforms / alphas
767 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700768 invalidate();
769 }
770
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800772 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800773 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800774 dispatchPageCountChanged();
775 }
776
777 @Override
778 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800779 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700780 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800781 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700782 }
783
Adam Cohen73894962011-10-31 13:17:17 -0700784 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700785 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800786 View pageAtIndex = getPageAt(index);
787 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700788 }
789
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 @Override
791 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700792 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700793 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700794 if (immediate) {
795 setCurrentPage(page);
796 } else {
797 snapToPage(page);
798 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 return true;
800 }
801 return false;
802 }
803
804 @Override
805 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700806 int focusablePage;
807 if (mNextPage != INVALID_PAGE) {
808 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700810 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 }
Winson Chung86f77532010-08-24 11:08:22 -0700812 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700814 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 }
816 return false;
817 }
818
819 @Override
820 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800821 if (super.dispatchUnhandledMove(focused, direction)) {
822 return true;
823 }
824
825 if (mIsRtl) {
826 if (direction == View.FOCUS_LEFT) {
827 direction = View.FOCUS_RIGHT;
828 } else if (direction == View.FOCUS_RIGHT) {
829 direction = View.FOCUS_LEFT;
830 }
831 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700833 if (getCurrentPage() > 0) {
834 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700835 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 return true;
837 }
838 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700839 if (getCurrentPage() < getPageCount() - 1) {
840 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700841 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 return true;
843 }
844 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800845 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 }
847
848 @Override
849 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700850 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
851 return;
852 }
853
Adam Cohen0ffac432013-07-10 11:19:26 -0700854 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700855 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700856 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 }
858 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700860 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 }
862 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700863 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700864 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866 }
867 }
868
869 /**
870 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700871 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 *
Winson Chung86f77532010-08-24 11:08:22 -0700873 * This happens when live folders requery, and if they're off page, they
874 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 */
876 @Override
877 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700878 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 View v = focused;
880 while (true) {
881 if (v == current) {
882 super.focusableViewAvailable(focused);
883 return;
884 }
885 if (v == this) {
886 return;
887 }
888 ViewParent parent = v.getParent();
889 if (parent instanceof View) {
890 v = (View)v.getParent();
891 } else {
892 return;
893 }
894 }
895 }
896
897 /**
898 * {@inheritDoc}
899 */
900 @Override
901 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
902 if (disallowIntercept) {
903 // We need to make sure to cancel our long press if
904 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700905 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700906 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 }
908 super.requestDisallowInterceptTouchEvent(disallowIntercept);
909 }
910
911 @Override
912 public boolean onInterceptTouchEvent(MotionEvent ev) {
913 /*
914 * This method JUST determines whether we want to intercept the motion.
915 * If we return true, onTouchEvent will be called and we do the actual
916 * scrolling there.
917 */
918
Winson Chung45e1d6e2010-11-09 17:19:49 -0800919 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700920 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700921
922 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800923
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 /*
925 * Shortcut the most recurring case: the user is in the dragging
926 * state and he is moving his finger. We want to intercept this
927 * motion.
928 */
929 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700930 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 return true;
932 }
933
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 switch (action & MotionEvent.ACTION_MASK) {
935 case MotionEvent.ACTION_MOVE: {
936 /*
937 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800938 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700940 if (mActivePointerId != INVALID_POINTER) {
941 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700942 }
943 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800944 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700945 // i.e. fall through to the next case (don't break)
946 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
947 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700948 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 }
950
951 case MotionEvent.ACTION_DOWN: {
952 final float x = ev.getX();
953 final float y = ev.getY();
954 // Remember location of down touch
955 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700956 mDownMotionY = y;
Vinit Nayaka406f722019-12-19 11:50:55 -0800957 mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
958 mLastMotionRemainder = 0;
959 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700961
Tony Wickhama0aee212019-09-18 09:24:03 -0700962 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700963
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 break;
965 }
966
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800968 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700969 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 break;
971
972 case MotionEvent.ACTION_POINTER_UP:
973 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800974 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 break;
976 }
977
978 /*
979 * The only time we want to intercept motion events is if we are in the
980 * drag mode.
981 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700982 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 }
984
Tony Wickhama0aee212019-09-18 09:24:03 -0700985 /**
986 * If being flinged and user touches the screen, initiate drag; otherwise don't.
987 */
988 private void updateIsBeingDraggedOnTouchDown() {
989 // mScroller.isFinished should be false when being flinged.
990 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
991 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
992
993 if (finishedScrolling) {
994 mIsBeingDragged = false;
995 if (!mScroller.isFinished() && !mFreeScroll) {
996 setCurrentPage(getNextPage());
997 pageEndTransition();
998 }
999 } else {
1000 mIsBeingDragged = true;
1001 }
1002 }
1003
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001004 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001005 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001006 }
1007
Adam Cohenf8d28232011-02-01 21:47:00 -08001008 protected void determineScrollingStart(MotionEvent ev) {
1009 determineScrollingStart(ev, 1.0f);
1010 }
1011
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 /*
1013 * Determines if we should change the touch state to start scrolling after the
1014 * user moves their touch point too far.
1015 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001016 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001017 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001019 if (pointerIndex == -1) return;
1020
Vinit Nayaka406f722019-12-19 11:50:55 -08001021 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1022 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001023 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -08001024 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001025
Vinit Nayaka406f722019-12-19 11:50:55 -08001026 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001027 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001028 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001029 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1030 mLastMotion = primaryDirection;
1031 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001032 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001033 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001034 // Stop listening for things like pinches.
1035 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001036 }
1037 }
1038
1039 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001040 // Try canceling the long press. It could also have been scheduled
1041 // by a distant descendant, so use the mAllowLongPress flag to block
1042 // everything
1043 final View currentPage = getPageAt(mCurrentPage);
1044 if (currentPage != null) {
1045 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 }
1047 }
1048
Adam Cohenb5ba0972011-09-07 18:02:31 -07001049 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001050 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001051
Adam Cohenedb40762013-07-18 16:45:45 -07001052 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001053 int count = getChildCount();
1054
1055 final int totalDistance;
1056
1057 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001058 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001059 adjacentPage = page - 1;
1060 }
1061
1062 if (adjacentPage < 0 || adjacentPage > count - 1) {
1063 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1064 } else {
1065 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1066 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001067
1068 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001069 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1070 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001071 return scrollProgress;
1072 }
1073
Adam Cohenedb40762013-07-18 16:45:45 -07001074 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001075 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001076 return 0;
1077 } else {
1078 return mPageScrolls[index];
1079 }
1080 }
1081
Adam Cohen564a2e72013-10-09 14:47:32 -07001082 // While layout transitions are occurring, a child's position may stray from its baseline
1083 // position. This method returns the magnitude of this stray at any given time.
1084 public int getLayoutTransitionOffsetForPage(int index) {
1085 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1086 return 0;
1087 } else {
1088 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001089
Sunny Goyal228153d2018-01-04 15:35:22 -08001090 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1091 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001092 return (int) (child.getX() - baselineX);
1093 }
1094 }
1095
Jon Miranda71cb80c2019-01-24 21:25:13 -08001096 @Override
1097 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001098 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001099 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001100 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001101 super.dispatchDraw(canvas);
1102
1103 canvas.restoreToCount(saveCount);
1104 } else {
1105 super.dispatchDraw(canvas);
1106 }
1107 }
1108
Sunny Goyalb7b01352018-08-09 12:31:20 -07001109 protected void dampedOverScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001110 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001111 if (amount == 0) {
1112 return;
1113 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001114
Vinit Nayaka406f722019-12-19 11:50:55 -08001115 int size = mOrientationHandler.getMeasuredSize(this);
1116 int overScrollAmount = OverScroll.dampedScroll(amount, size);
1117 mSpringOverScroll = overScrollAmount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001118 if (mScroller.isSpringing()) {
1119 invalidate();
1120 return;
1121 }
1122
Vinit Nayaka406f722019-12-19 11:50:55 -08001123 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1124 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1125 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001126 invalidate();
1127 }
1128
Sunny Goyalb7b01352018-08-09 12:31:20 -07001129 protected void overScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001130 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001131 if (mScroller.isSpringing()) {
1132 invalidate();
1133 return;
1134 }
1135
Sunny Goyalb7b01352018-08-09 12:31:20 -07001136 if (amount == 0) return;
1137
1138 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001139 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1140 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001141 } else {
1142 dampedOverScroll(amount);
1143 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001144 }
1145
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001146
Tony Wickham023f4042019-01-29 10:52:31 -08001147 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001148 if (mFreeScroll == freeScroll) {
1149 return;
1150 }
1151
Tony Wickham8f7ead32016-04-07 18:46:44 -07001152 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001153 mFreeScroll = freeScroll;
1154
Adam Cohenf9618852013-11-08 06:45:03 -08001155 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001156 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001157 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001158 if (getScrollForPage(getNextPage()) != getScrollX()) {
1159 snapToPage(getNextPage());
1160 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001161 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001162 }
1163
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001164 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001165 mAllowOverScroll = enable;
1166 }
1167
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 @Override
1169 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001170 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001171 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001172
Michael Jurkab8f06722010-10-10 15:58:46 -07001173 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001174
1175 final int action = ev.getAction();
1176
1177 switch (action & MotionEvent.ACTION_MASK) {
1178 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001179 updateIsBeingDraggedOnTouchDown();
1180
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 /*
1182 * If being flinged and user touches, stop the fling. isFinished
1183 * will be false if being flinged.
1184 */
1185 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001186 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 }
1188
1189 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001190 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001191 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001192 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1193 mLastMotionRemainder = 0;
1194 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001196 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001197 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001198 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001199 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 break;
1201
Vinit Nayaka406f722019-12-19 11:50:55 -08001202 case MotionEvent.ACTION_MOVE:
1203 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 // Scroll to follow the motion event
1205 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001206
1207 if (pointerIndex == -1) return true;
1208
Vinit Nayaka406f722019-12-19 11:50:55 -08001209 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1210 float delta = mLastMotion + mLastMotionRemainder - direction;
1211 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001212
Winson Chungc0844aa2011-02-02 15:25:58 -08001213 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1214 // keep the remainder because we are actually testing if we've moved from the last
1215 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001216 if (Math.abs(delta) >= 1.0f) {
1217 mLastMotion = direction;
1218 mLastMotionRemainder = delta - (int) delta;
1219
1220 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 } else {
1222 awakenScrollBars();
1223 }
Adam Cohen564976a2010-10-13 18:52:07 -07001224 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 determineScrollingStart(ev);
1226 }
1227 break;
1228
1229 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001230 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 final int activePointerId = mActivePointerId;
1232 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001233 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1234 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001235 final VelocityTracker velocityTracker = mVelocityTracker;
1236 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001237
Vinit Nayaka406f722019-12-19 11:50:55 -08001238 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1239 mActivePointerId);
1240 int delta = (int) (primaryDirection - mDownMotionPrimary);
1241 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1242
1243 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1244 SIGNIFICANT_MOVE_THRESHOLD;
1245
1246 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1247 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1248 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1249 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001250
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 if (!mFreeScroll) {
1252 // In the case that the page is moved far to one direction and then is flung
1253 // in the opposite direction, we use a threshold to determine whether we should
1254 // just return to the starting page, or if we should skip one further.
1255 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001256 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1257 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001258 returnToOriginalPage = true;
1259 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001260
Adam Cohenf358a4b2013-07-23 16:47:31 -07001261 int finalPage;
1262 // We give flings precedence over large moves, which is why we short-circuit our
1263 // test for a large move if a fling has been registered. That is, a large
1264 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001265
Vinit Nayaka406f722019-12-19 11:50:55 -08001266 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1267 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001268 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001269 snapToPageWithVelocity(finalPage, velocity);
1270 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1271 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001272 mCurrentPage < getChildCount() - 1) {
1273 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001274 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001275 } else {
1276 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001277 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001279 if (!mScroller.isFinished()) {
1280 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001281 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001282
Vinit Nayaka406f722019-12-19 11:50:55 -08001283 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1284 int maxScroll = mMaxScroll;
1285 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001286
Vinit Nayaka406f722019-12-19 11:50:55 -08001287 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1288 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1289 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001290 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001291 } else {
1292 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001293 mScroller.fling(initialScroll, -velocity,
1294 minScroll, maxScroll,
1295 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001296
Vinit Nayaka406f722019-12-19 11:50:55 -08001297 int finalPos = mScroller.getFinalPos();
1298 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001299
1300 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1301 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001302 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001303 // If scrolling ends in the half of the added space that is closer to
1304 // the end, settle to the end. Otherwise snap to the nearest page.
1305 // If flinging past one of the ends, don't change the velocity as it
1306 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001307 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1308 ? minScroll
1309 : finalPos > (lastPageScroll + maxScroll) / 2
1310 ? maxScroll
1311 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001312
Vinit Nayaka406f722019-12-19 11:50:55 -08001313 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001314 // Ensure the scroll/snap doesn't happen too fast;
1315 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001316 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001317 if (extraScrollDuration > 0) {
1318 mScroller.extendDuration(extraScrollDuration);
1319 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001320 }
1321 }
1322 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001323 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001324 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001326
Adam Cohen7d30a372013-07-01 17:03:59 -07001327 // End any intermediate reordering states
1328 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 break;
1330
1331 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001332 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001333 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001334 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001335 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001336 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 break;
1338
1339 case MotionEvent.ACTION_POINTER_UP:
1340 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001341 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 break;
1343 }
1344
1345 return true;
1346 }
1347
Vinit Nayaka406f722019-12-19 11:50:55 -08001348 protected boolean shouldFlingForVelocity(int velocity) {
1349 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001350 }
1351
Adam Cohen7d30a372013-07-01 17:03:59 -07001352 private void resetTouchState() {
1353 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001354 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001355 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001356 }
1357
Adam Cohenc2d6e892014-10-16 09:49:24 -07001358 /**
1359 * Triggered by scrolling via touch
1360 */
1361 protected void onScrollInteractionBegin() {
1362 }
1363
1364 protected void onScrollInteractionEnd() {
1365 }
1366
Winson Chung185d7162011-02-28 13:47:29 -08001367 @Override
1368 public boolean onGenericMotionEvent(MotionEvent event) {
1369 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1370 switch (event.getAction()) {
1371 case MotionEvent.ACTION_SCROLL: {
Samuel Fufa9fd21052019-08-28 17:06:51 -07001372 Launcher launcher = Launcher.getLauncher(getContext());
1373 if (launcher != null) {
1374 AbstractFloatingView.closeAllOpenViews(launcher);
1375 }
Winson Chung185d7162011-02-28 13:47:29 -08001376 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1377 final float vscroll;
1378 final float hscroll;
1379 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1380 vscroll = 0;
1381 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1382 } else {
1383 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1384 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1385 }
Samuel Fufa59cba192019-08-22 18:31:34 -07001386 if (Math.abs(vscroll) > Math.abs(hscroll) && !isVerticalScrollable()) {
1387 return true;
1388 }
Winson Chung185d7162011-02-28 13:47:29 -08001389 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001390 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001391 : (hscroll > 0 || vscroll > 0);
1392 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001393 scrollRight();
1394 } else {
1395 scrollLeft();
1396 }
1397 return true;
1398 }
1399 }
1400 }
1401 }
1402 return super.onGenericMotionEvent(event);
1403 }
1404
Samuel Fufa59cba192019-08-22 18:31:34 -07001405 protected boolean isVerticalScrollable() {
1406 return true;
1407 }
1408
Michael Jurkab8f06722010-10-10 15:58:46 -07001409 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1410 if (mVelocityTracker == null) {
1411 mVelocityTracker = VelocityTracker.obtain();
1412 }
1413 mVelocityTracker.addMovement(ev);
1414 }
1415
1416 private void releaseVelocityTracker() {
1417 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001419 mVelocityTracker.recycle();
1420 mVelocityTracker = null;
1421 }
1422 }
1423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 private void onSecondaryPointerUp(MotionEvent ev) {
1425 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1426 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1427 final int pointerId = ev.getPointerId(pointerIndex);
1428 if (pointerId == mActivePointerId) {
1429 // This was our active pointer going up. Choose a new
1430 // active pointer and adjust accordingly.
1431 // TODO: Make this decision more intelligent.
1432 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001433 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1434 newPointerIndex);
1435 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 mActivePointerId = ev.getPointerId(newPointerIndex);
1437 if (mVelocityTracker != null) {
1438 mVelocityTracker.clear();
1439 }
1440 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001441 }
1442
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 @Override
1444 public void requestChildFocus(View child, View focused) {
1445 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001446 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001447 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001448 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001449 }
1450 }
1451
Sunny Goyal228153d2018-01-04 15:35:22 -08001452 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001453 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001454 }
1455
Vinit Nayaka406f722019-12-19 11:50:55 -08001456 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1457 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1458 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001459 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 final int childCount = getChildCount();
1462 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001463 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001464 int childSize = mOrientationHandler.getMeasuredSize(layout);
1465 int halfChildSize = (childSize / 2);
1466 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1468 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1469 minDistanceFromScreenCenter = distanceFromScreenCenter;
1470 minDistanceFromScreenCenterIndex = i;
1471 }
1472 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001473 return minDistanceFromScreenCenterIndex;
1474 }
1475
1476 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001477 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1478 }
1479
1480 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001481 int scroll = mOrientationHandler.getPrimaryScroll(this);
1482 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001483 }
1484
1485 protected int getPageSnapDuration() {
1486 if (isInOverScroll()) {
1487 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1488 }
1489 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 }
1491
Adam Cohene0f66b52010-11-23 15:06:07 -08001492 // We want the duration of the page snap animation to be influenced by the distance that
1493 // the screen has to travel, however, we don't want this duration to be effected in a
1494 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1495 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001496 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001497 f -= 0.5f; // center the values about 0.
1498 f *= 0.3f * Math.PI / 2.0f;
1499 return (float) Math.sin(f);
1500 }
1501
Winson Chung05a31ed2018-02-08 17:08:43 -08001502 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001503 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001504 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001505
Vinit Nayaka406f722019-12-19 11:50:55 -08001506 final int newLoc = getScrollForPage(whichPage);
1507 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001508 int duration = 0;
1509
Sunny Goyal4d113a52015-05-27 10:05:28 -07001510 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001511 // If the velocity is low enough, then treat this more as an automatic page advance
1512 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001513 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001514 }
1515
1516 // Here we compute a "distance" that will be used in the computation of the overall
1517 // snap duration. This is a function of the actual distance that needs to be traveled;
1518 // we keep this value close to half screen size in order to reduce the variance in snap
1519 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001520 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001521 float distance = halfScreenSize + halfScreenSize *
1522 distanceInfluenceForSnapDuration(distanceRatio);
1523
1524 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001525 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001526
1527 // we want the page's snap velocity to approximately match the velocity at which the
1528 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001529 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1530 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001531
Jon Miranda9e198662020-03-11 14:42:02 -07001532 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001533 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001534 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001535 } else {
1536 return snapToPage(whichPage, delta, duration);
1537 }
Michael Jurka0142d492010-08-25 17:46:15 -07001538 }
1539
Winson Chung05a31ed2018-02-08 17:08:43 -08001540 public boolean snapToPage(int whichPage) {
1541 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 }
1543
Winson Chung05a31ed2018-02-08 17:08:43 -08001544 public boolean snapToPageImmediately(int whichPage) {
1545 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001546 }
1547
Winson Chung05a31ed2018-02-08 17:08:43 -08001548 public boolean snapToPage(int whichPage, int duration) {
1549 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001550 }
1551
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001552 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001553 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001554 }
1555
Winson Chung05a31ed2018-02-08 17:08:43 -08001556 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001557 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001558 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001559
Vinit Nayaka406f722019-12-19 11:50:55 -08001560 int newLoc = getScrollForPage(whichPage);
1561 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001562 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001563 }
1564
Winson Chung05a31ed2018-02-08 17:08:43 -08001565 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001566 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001567 }
Michael Jurka0142d492010-08-25 17:46:15 -07001568
Winson Chung05a31ed2018-02-08 17:08:43 -08001569 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001570 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001571 if (mFirstLayout) {
1572 setCurrentPage(whichPage);
1573 return false;
1574 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001575
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001576 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001577 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1578 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001579 }
1580
Adam Cohen6f127a62014-06-12 14:54:41 -07001581 whichPage = validateNewPage(whichPage);
1582
Adam Cohen7d30a372013-07-01 17:03:59 -07001583 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001584
Winson Chung321e9ee2010-08-09 13:37:56 -07001585 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001586 if (immediate) {
1587 duration = 0;
1588 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 duration = Math.abs(delta);
1590 }
1591
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001592 if (duration != 0) {
1593 pageBeginTransition();
1594 }
1595
Adam Cohenf358a4b2013-07-23 16:47:31 -07001596 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001597 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001598 }
Adam Cohenf9618852013-11-08 06:45:03 -08001599
1600 if (interpolator != null) {
1601 mScroller.setInterpolator(interpolator);
1602 } else {
1603 mScroller.setInterpolator(mDefaultInterpolator);
1604 }
1605
Jon Miranda71cb80c2019-01-24 21:25:13 -08001606 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001607 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001608 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001609 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001610 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001611
Adam Cohen674531f2013-12-13 15:07:14 -08001612 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001613
1614 // Trigger a compute() to finish switching pages if necessary
1615 if (immediate) {
1616 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001617 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001618 }
1619
Winson Chung321e9ee2010-08-09 13:37:56 -07001620 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001621 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001622 }
1623
Vadim Tryshev98913d02018-05-18 18:41:34 -07001624 public boolean scrollLeft() {
1625 if (getNextPage() > 0) {
1626 snapToPage(getNextPage() - 1);
1627 return true;
1628 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001629 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001630 }
1631
Vadim Tryshev98913d02018-05-18 18:41:34 -07001632 public boolean scrollRight() {
1633 if (getNextPage() < getChildCount() - 1) {
1634 snapToPage(getNextPage() + 1);
1635 return true;
1636 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001637 return onOverscroll(getMeasuredWidth());
1638 }
1639
1640 protected boolean onOverscroll(int amount) {
1641 if (!mAllowOverScroll) return false;
1642 onScrollInteractionBegin();
1643 overScroll(amount);
1644 onScrollInteractionEnd();
1645 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001646 }
1647
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001648 @Override
1649 public CharSequence getAccessibilityClassName() {
1650 // Some accessibility services have special logic for ScrollView. Since we provide same
1651 // accessibility info as ScrollView, inform the service to handle use the same way.
1652 return ScrollView.class.getName();
1653 }
1654
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001655 protected boolean isPageOrderFlipped() {
1656 return false;
1657 }
1658
Winson Chung6a0f57d2011-06-29 20:10:49 -07001659 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001660 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001661 @Override
1662 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1663 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001664 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001665 int offset = (mAllowOverScroll ? 0 : 1);
1666 info.setScrollable(getPageCount() > offset);
1667 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001668 info.addAction(pagesFlipped ?
1669 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1670 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1671 info.addAction(mIsRtl ?
1672 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1673 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001674 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001675 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001676 info.addAction(pagesFlipped ?
1677 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1678 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1679 info.addAction(mIsRtl ?
1680 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1681 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001682 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001683 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1684 // Besides disabling the accessibility long-click, this also prevents this view from getting
1685 // accessibility focus.
1686 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001687 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001688 }
1689
1690 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001691 public void sendAccessibilityEvent(int eventType) {
1692 // Don't let the view send real scroll events.
1693 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1694 super.sendAccessibilityEvent(eventType);
1695 }
1696 }
1697
1698 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001699 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1700 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001701 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001702 }
1703
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001704 @Override
1705 public boolean performAccessibilityAction(int action, Bundle arguments) {
1706 if (super.performAccessibilityAction(action, arguments)) {
1707 return true;
1708 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001709 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001710 switch (action) {
1711 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001712 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001713 return true;
1714 }
1715 } break;
1716 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001717 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001718 return true;
1719 }
yingleiw02cc8482019-08-02 16:14:27 -07001720 } break;
1721 case android.R.id.accessibilityActionPageRight: {
1722 if (!mIsRtl) {
1723 return scrollRight();
1724 } else {
1725 return scrollLeft();
1726 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001727 }
yingleiw02cc8482019-08-02 16:14:27 -07001728 case android.R.id.accessibilityActionPageLeft: {
1729 if (!mIsRtl) {
1730 return scrollLeft();
1731 } else {
1732 return scrollRight();
1733 }
1734 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001735 }
1736 return false;
1737 }
1738
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001739 protected boolean canAnnouncePageDescription() {
1740 return true;
1741 }
1742
Adam Cohen0ffac432013-07-10 11:19:26 -07001743 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001744 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001745 getNextPage() + 1, getChildCount());
1746 }
1747
Tony Mak2e564402018-02-27 17:19:34 +00001748 protected float getDownMotionX() {
1749 return mDownMotionX;
1750 }
1751
1752 protected float getDownMotionY() {
1753 return mDownMotionY;
1754 }
1755
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001756 protected interface ComputePageScrollsLogic {
1757
1758 boolean shouldIncludeView(View view);
1759 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001760
1761 public int[] getVisibleChildrenRange() {
1762 float visibleLeft = 0;
1763 float visibleRight = visibleLeft + getMeasuredWidth();
1764 float scaleX = getScaleX();
1765 if (scaleX < 1 && scaleX > 0) {
1766 float mid = getMeasuredWidth() / 2;
1767 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1768 visibleRight = mid + ((visibleRight - mid) / scaleX);
1769 }
1770
1771 int leftChild = -1;
1772 int rightChild = -1;
1773 final int childCount = getChildCount();
1774 for (int i = 0; i < childCount; i++) {
1775 final View child = getPageAt(i);
1776
1777 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1778 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1779 if (leftChild == -1) {
1780 leftChild = i;
1781 }
1782 rightChild = i;
1783 }
1784 }
1785 mTmpIntPair[0] = leftChild;
1786 mTmpIntPair[1] = rightChild;
1787 return mTmpIntPair;
1788 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001789}