blob: 7474613a18f6455228189c9168b325af91c3e1c4 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyal8b37c572020-03-31 12:12:14 -070019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
22import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
23import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
24import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
25import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
26
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070027import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070031import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080032import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070034import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070035import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
48import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070050import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
Tony Wickham62117d72020-03-26 16:56:26 -070052import androidx.annotation.Nullable;
53
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.anim.Interpolators;
55import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
58import com.android.launcher3.touch.OverScroll;
59import com.android.launcher3.touch.PagedOrientationHandler;
60import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Jon Miranda9e198662020-03-11 14:42:02 -070061import com.android.launcher3.util.OverScroller;
62import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070063import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070064
65import java.util.ArrayList;
66
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080071public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070074
Sunny Goyala7a5bf32020-01-05 15:35:29 +053075 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070076 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070077
Vadim Tryshevfedca432015-08-19 17:55:02 -070078 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Sunny Goyalb72d8b22017-07-14 00:02:27 -070080 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070081 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Sunny Goyal8e2133b2015-05-06 13:39:07 -070087 private static final float MAX_SCROLL_PROGRESS = 1.0f;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 // The following constants need to be scaled based on density. The scaled versions will be
90 // assigned to the corresponding member variables below.
91 private static final int FLING_THRESHOLD_VELOCITY = 500;
92 private static final int MIN_SNAP_VELOCITY = 1500;
93 private static final int MIN_FLING_VELOCITY = 250;
94
Adam Cohenf358a4b2013-07-23 16:47:31 -070095 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070096
Adam Cohen265b9a62011-12-07 14:37:18 -080097 protected int mFlingThresholdVelocity;
98 protected int mMinFlingVelocity;
99 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700100
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected boolean mFirstLayout = true;
102
Sunny Goyal4ffec482016-02-09 11:28:52 -0800103 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700105
Sunny Goyal4ffec482016-02-09 11:28:52 -0800106 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800108 protected int mMaxScroll;
109 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700110 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800111 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800113 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
115 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700116 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800117 private float mDownMotionPrimary;
118 private float mLastMotion;
119 private float mLastMotionRemainder;
120 private float mTotalMotion;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700121 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700122
Vadim Trysheve628c472018-05-16 14:28:18 -0700123 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700124 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800125
Michael Jurka7426c422010-11-11 15:23:47 -0800126 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800128 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Michael Jurka5f1c5092010-09-03 14:15:02 -0700130 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Michael Jurka5f1c5092010-09-03 14:15:02 -0700132 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700134 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700135 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700136
Vinit Nayaka406f722019-12-19 11:50:55 -0800137 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800138
Sunny Goyal061380a2016-02-29 15:15:03 -0800139 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700140
Vinit Nayaka406f722019-12-19 11:50:55 -0800141 protected int mUnboundedScroll;
142
Winson Chungd2be3812013-07-16 11:11:32 -0700143 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700144 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800145 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700146
John Spurlock77e1f472013-09-11 10:09:51 -0400147 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800148 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400149
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800150 // Similar to the platform implementation of isLayoutValid();
151 protected boolean mIsLayoutValid;
152
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700153 private int[] mTmpIntPair = new int[2];
154
Winson Chung321e9ee2010-08-09 13:37:56 -0700155 public PagedView(Context context) {
156 this(context, null);
157 }
158
159 public PagedView(Context context, AttributeSet attrs) {
160 this(context, attrs, 0);
161 }
162
163 public PagedView(Context context, AttributeSet attrs, int defStyle) {
164 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700165
Adam Cohen9c4949e2010-10-05 12:27:22 -0700166 TypedArray a = context.obtainStyledAttributes(attrs,
167 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700168 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700169 a.recycle();
170
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700172 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700173 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 }
175
176 /**
177 * Initializes various states for this workspace.
178 */
Michael Jurka0142d492010-08-25 17:46:15 -0700179 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800180 Context context = getContext();
181 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700182 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700183 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Vinit Nayaka406f722019-12-19 11:50:55 -0800185 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700186 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800188
Sunny Goyalcf25b522015-07-09 00:01:18 -0700189 float density = getResources().getDisplayMetrics().density;
190 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
191 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
192 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700193
194 if (Utilities.ATLEAST_OREO) {
195 setDefaultFocusHighlightEnabled(false);
196 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198
Adam Cohenf9618852013-11-08 06:45:03 -0800199 protected void setDefaultInterpolator(Interpolator interpolator) {
200 mDefaultInterpolator = interpolator;
201 mScroller.setInterpolator(mDefaultInterpolator);
202 }
203
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700204 public void initParentViews(View parent) {
205 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800206 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700207 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700208 }
209 }
210
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800211 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700212 return mPageIndicator;
213 }
214
Adam Cohen674531f2013-12-13 15:07:14 -0800215 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700216 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
217 * that the user was on before entering free scroll mode (e.g. the home screen page they
218 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
219 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700221 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700222 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700224
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700225 /**
226 * Returns the index of page to be shown immediately afterwards.
227 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700228 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700229 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
230 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700231
Adam Cohenf9c184a2016-01-15 16:47:43 -0800232 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 return getChildCount();
234 }
235
Sunny Goyal83a8f042015-05-19 12:52:12 -0700236 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 return getChildAt(index);
238 }
239
Adam Cohenae4f1552011-10-20 00:15:42 -0700240 protected int indexToPage(int index) {
241 return index;
242 }
243
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800245 * Updates the scroll of the current page immediately to its final scroll position. We use this
246 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
247 * the previous tab page.
248 */
249 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700250 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800251 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700252 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800253 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
256 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700257 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800258 }
259
Sreyase3e7e632020-01-29 10:42:10 -0800260 /**
261 * Returns left offset of a page. This is the gap between pages and prevents overlap.
262 */
263 public int scrollOffsetLeft() {
264 return mInsets.left + getPaddingLeft();
265 }
266
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700267 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700268 mScroller.abortAnimation();
269 // We need to clean up the next page here to avoid computeScrollHelper from
270 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700271 if (resetNextPage) {
272 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800273 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700274 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700275 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700276
Tony Wickham8f7ead32016-04-07 18:46:44 -0700277 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700278 mScroller.forceFinished(true);
279 // We need to clean up the next page here to avoid computeScrollHelper from
280 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700281 if (resetNextPage) {
282 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800283 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700284 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700285 }
286
Adam Cohen6f127a62014-06-12 14:54:41 -0700287 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700288 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700289 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700290 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700291 }
292
Chet Haasebc2f0822012-10-26 17:59:53 -0700293 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700294 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
295 */
296 private int ensureWithinScrollBounds(int page) {
297 int dir = !mIsRtl ? 1 : - 1;
298 int currScroll = getScrollForPage(page);
299 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800300 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700301 page += dir;
302 prevScroll = currScroll;
303 currScroll = getScrollForPage(page);
304 if (currScroll <= prevScroll) {
305 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
306 break;
307 }
308 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800309 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700310 page -= dir;
311 prevScroll = currScroll;
312 currScroll = getScrollForPage(page);
313 if (currScroll >= prevScroll) {
314 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
315 break;
316 }
317 }
318 return page;
319 }
320
Tony Wickham03f27012019-04-25 14:22:54 -0700321 public void setCurrentPage(int currentPage) {
322 setCurrentPage(currentPage, INVALID_PAGE);
323 }
324
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700325 /**
Winson Chung86f77532010-08-24 11:08:22 -0700326 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 */
Tony Wickham03f27012019-04-25 14:22:54 -0700328 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800329 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700330 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800331 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800332 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
333 // the default
334 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800335 return;
336 }
Tony Wickham03f27012019-04-25 14:22:54 -0700337 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700338 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700339 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700340 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800341 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 }
343
Winson Chung8c87cd82013-07-23 16:20:10 -0700344 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800345 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
346 * has settled.
347 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700348 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800349 updatePageIndicator();
350 }
351
352 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700353 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000354 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700355 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700357 protected void pageBeginTransition() {
358 if (!mIsPageInTransition) {
359 mIsPageInTransition = true;
360 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700361 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700362 }
363
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700364 protected void pageEndTransition() {
365 if (mIsPageInTransition) {
366 mIsPageInTransition = false;
367 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700368 }
Michael Jurka0142d492010-08-25 17:46:15 -0700369 }
370
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700371 protected boolean isPageInTransition() {
372 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700373 }
374
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700375 /**
376 * Called when the page starts moving as part of the scroll. Subclasses can override this
377 * to provide custom behavior during animation.
378 */
379 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700380 }
381
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700382 /**
383 * Called when the page ends moving as part of the scroll. Subclasses can override this
384 * to provide custom behavior during animation.
385 */
386 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700387 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700388 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800389 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
390 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700391 if (mOnPageTransitionEndCallback != null) {
392 mOnPageTransitionEndCallback.run();
393 mOnPageTransitionEndCallback = null;
394 }
395 }
396
397 /**
398 * Sets a callback to run once when the scrolling finishes. If there is currently
399 * no page in transition, then the callback is called immediately.
400 */
401 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
402 if (mIsPageInTransition || callback == null) {
403 mOnPageTransitionEndCallback = callback;
404 } else {
405 callback.run();
406 }
Michael Jurka0142d492010-08-25 17:46:15 -0700407 }
408
Vinit Nayaka406f722019-12-19 11:50:55 -0800409 protected int getUnboundedScroll() {
410 return mUnboundedScroll;
411 }
412
Winson Chung321e9ee2010-08-09 13:37:56 -0700413 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800414 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800415 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800416 }
417
418 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700419 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800420 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
421 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
422 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700423
Vinit Nayaka406f722019-12-19 11:50:55 -0800424 boolean isBeforeFirstPage = mIsRtl ?
425 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
426 boolean isAfterLastPage = mIsRtl ?
427 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
428 if (!isBeforeFirstPage && !isAfterLastPage) {
429 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800430 }
431
Vinit Nayaka406f722019-12-19 11:50:55 -0800432 if (isBeforeFirstPage) {
433 mOrientationHandler.delegateScrollTo(this,
434 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800435 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700436 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800437 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800438 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800439 } else if (isAfterLastPage) {
440 mOrientationHandler.delegateScrollTo(this,
441 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800442 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700443 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800444 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800445 }
446 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700447 if (mWasInOverscroll) {
448 overScroll(0);
449 mWasInOverscroll = false;
450 }
Adam Cohen68d73932010-11-15 10:50:58 -0800451 super.scrollTo(x, y);
452 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800453 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700454
Vinit Nayaka406f722019-12-19 11:50:55 -0800455 /**
456 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
457 */
458 public void superScrollTo(int x, int y) {
459 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700460 }
461
Adam Cohen53805212013-10-01 10:39:23 -0700462 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800463 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700464 if (mCurrentPage != getNextPage()) {
465 AccessibilityEvent ev =
466 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700467 ev.setScrollable(true);
468 ev.setScrollX(getScrollX());
469 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800470 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700471 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700472 }
Adam Cohen53805212013-10-01 10:39:23 -0700473 }
474 }
475
Michael Jurka0142d492010-08-25 17:46:15 -0700476 // we moved this functionality to a helper function so SmoothPagedView can reuse it
477 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800478 return computeScrollHelper(true);
479 }
480
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700481 protected void announcePageForAccessibility() {
482 if (isAccessibilityEnabled(getContext())) {
483 // Notify the user when the page changes
484 announceForAccessibility(getCurrentPageDescription());
485 }
486 }
487
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800488 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700490 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800491 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
492 if (mUnboundedScroll != mScroller.getCurrPos()
493 || currentScroll != mScroller.getCurrPos()) {
494 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700495 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800496 if (shouldInvalidate) {
497 invalidate();
498 }
Michael Jurka0142d492010-08-25 17:46:15 -0700499 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800500 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700501 sendScrollAccessibilityEvent();
502
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700503 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700504 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700505 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700506 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700507
Adam Cohen73aa9752010-11-24 16:26:58 -0800508 // We don't want to trigger a page end moving unless the page has settled
509 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700510 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700511 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800512 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700513
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700514 if (canAnnouncePageDescription()) {
515 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700516 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 }
Michael Jurka0142d492010-08-25 17:46:15 -0700518 return false;
519 }
520
521 @Override
522 public void computeScroll() {
523 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525
Sunny Goyalce8809a2018-01-18 14:02:47 -0800526 public int getExpectedHeight() {
527 return getMeasuredHeight();
528 }
529
Adam Cohen410f3cd2013-09-22 12:09:32 -0700530 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800531 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800532 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700533 }
534
Sunny Goyalce8809a2018-01-18 14:02:47 -0800535 public int getExpectedWidth() {
536 return getMeasuredWidth();
537 }
538
Sunny Goyalbc683e92017-12-06 10:25:07 -0800539 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800540 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800541 - mInsets.left - mInsets.right;
542 }
543
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800545 public void requestLayout() {
546 mIsLayoutValid = false;
547 super.requestLayout();
548 }
549
550 @Override
551 public void forceLayout() {
552 mIsLayoutValid = false;
553 super.forceLayout();
554 }
555
556 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700558 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700559 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
560 return;
561 }
562
Adam Cohen7d30a372013-07-01 17:03:59 -0700563 // We measure the dimensions of the PagedView to be larger than the pages so that when we
564 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700565 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
566 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
567 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700568 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700569
Adam Cohen7d30a372013-07-01 17:03:59 -0700570 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
571 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
572 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 }
574
Winson Chung8aad6102012-05-11 16:27:49 -0700575 // Return early if we aren't given a proper dimension
576 if (widthSize <= 0 || heightSize <= 0) {
577 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
578 return;
579 }
580
Winson Chung321e9ee2010-08-09 13:37:56 -0700581 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700582 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700583 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700584
Sunny Goyalac00cba2017-11-13 15:58:01 -0800585 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800586 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800587 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800588 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700589
Sunny Goyalac00cba2017-11-13 15:58:01 -0800590 // measureChildren takes accounts for content padding, we only need to care about extra
591 // space due to insets.
592 measureChildren(myWidthSpec, myHeightSpec);
593 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800594 }
595
Sunny Goyalcf25b522015-07-09 00:01:18 -0700596 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700598 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800599 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700600 final int childCount = getChildCount();
601 boolean pageScrollChanged = false;
602 if (mPageScrolls == null || childCount != mPageScrolls.length) {
603 mPageScrolls = new int[childCount];
604 pageScrollChanged = true;
605 }
606
607 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700608 return;
609 }
610
Winson Chung785d2eb2011-04-14 16:08:02 -0700611 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700612
Vinit Nayaka406f722019-12-19 11:50:55 -0800613 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
614 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700615 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700617
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700618 final LayoutTransition transition = getLayoutTransition();
619 // If the transition is running defer updating max scroll, as some empty pages could
620 // still be present, and a max scroll change could cause sudden jumps in scroll.
621 if (transition != null && transition.isRunning()) {
622 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
623
624 @Override
625 public void startTransition(LayoutTransition transition, ViewGroup container,
626 View view, int transitionType) { }
627
628 @Override
629 public void endTransition(LayoutTransition transition, ViewGroup container,
630 View view, int transitionType) {
631 // Wait until all transitions are complete.
632 if (!transition.isRunning()) {
633 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700634 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700635 }
636 }
637 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700638 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700639 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700640 }
641
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700642 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
643 updateCurrentPageScroll();
644 mFirstLayout = false;
645 }
646
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800647 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700648 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700649 }
Winson Chunge3193b92010-09-10 11:44:42 -0700650 }
651
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700652 /**
653 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
654 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700655 */
656 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
657 ComputePageScrollsLogic scrollLogic) {
658 final int childCount = getChildCount();
659
660 final int startIndex = mIsRtl ? childCount - 1 : 0;
661 final int endIndex = mIsRtl ? -1 : childCount;
662 final int delta = mIsRtl ? -1 : 1;
663
Vinit Nayaka406f722019-12-19 11:50:55 -0800664 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700665
Vinit Nayaka406f722019-12-19 11:50:55 -0800666 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
667 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700668 boolean pageScrollChanged = false;
669
Vinit Nayaka406f722019-12-19 11:50:55 -0800670 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700671 final View child = getPageAt(i);
672 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800673 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
674 pageCenter, layoutChildren);
675 final int primaryDimension = bounds.primaryDimension;
676 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700677
Sunny Goyalc82c6392018-06-06 15:39:13 -0700678 // In case the pages are of different width, align the page to left or right edge
679 // based on the orientation.
680 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800681 ? (childStart - scrollOffsetStart)
682 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700683 if (outPageScrolls[i] != pageScroll) {
684 pageScrollChanged = true;
685 outPageScrolls[i] = pageScroll;
686 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800687 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700688 }
689 }
690 return pageScrollChanged;
691 }
692
Sunny Goyala1fbd842015-05-20 13:40:27 -0700693 protected int getChildGap() {
694 return 0;
695 }
696
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700697 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800698 mMinScroll = computeMinScroll();
699 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700700 }
701
Vinit Nayaka406f722019-12-19 11:50:55 -0800702 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700703 return 0;
Tony50876bf2018-09-19 23:09:14 -0400704 }
705
Vinit Nayaka406f722019-12-19 11:50:55 -0800706 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700707 int childCount = getChildCount();
708 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700709 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700710 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700711 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700712 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700713 }
714 }
715
Adam Cohen3b185e22013-10-29 14:45:58 -0700716 public void setPageSpacing(int pageSpacing) {
717 mPageSpacing = pageSpacing;
718 requestLayout();
719 }
720
Tony50876bf2018-09-19 23:09:14 -0400721 public int getPageSpacing() {
722 return mPageSpacing;
723 }
724
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800725 private void dispatchPageCountChanged() {
726 if (mPageIndicator != null) {
727 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700728 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700729 // This ensures that when children are added, they get the correct transforms / alphas
730 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700731 invalidate();
732 }
733
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800735 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800736 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800737 dispatchPageCountChanged();
738 }
739
740 @Override
741 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800742 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700743 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800744 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700745 }
746
Adam Cohen73894962011-10-31 13:17:17 -0700747 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700748 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800749 View pageAtIndex = getPageAt(index);
750 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700751 }
752
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 @Override
754 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700755 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700756 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700757 if (immediate) {
758 setCurrentPage(page);
759 } else {
760 snapToPage(page);
761 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 return true;
763 }
764 return false;
765 }
766
767 @Override
768 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700769 int focusablePage;
770 if (mNextPage != INVALID_PAGE) {
771 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700773 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 }
Winson Chung86f77532010-08-24 11:08:22 -0700775 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700777 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 }
779 return false;
780 }
781
782 @Override
783 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800784 if (super.dispatchUnhandledMove(focused, direction)) {
785 return true;
786 }
787
788 if (mIsRtl) {
789 if (direction == View.FOCUS_LEFT) {
790 direction = View.FOCUS_RIGHT;
791 } else if (direction == View.FOCUS_RIGHT) {
792 direction = View.FOCUS_LEFT;
793 }
794 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700796 if (getCurrentPage() > 0) {
797 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700798 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 return true;
800 }
801 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700802 if (getCurrentPage() < getPageCount() - 1) {
803 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700804 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 return true;
806 }
807 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800808 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 }
810
811 @Override
812 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700813 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
814 return;
815 }
816
Adam Cohen0ffac432013-07-10 11:19:26 -0700817 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700818 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700819 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 }
821 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700822 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700823 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
825 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700826 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700827 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 }
829 }
830 }
831
832 /**
833 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700834 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 *
Winson Chung86f77532010-08-24 11:08:22 -0700836 * This happens when live folders requery, and if they're off page, they
837 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 */
839 @Override
840 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700841 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 View v = focused;
843 while (true) {
844 if (v == current) {
845 super.focusableViewAvailable(focused);
846 return;
847 }
848 if (v == this) {
849 return;
850 }
851 ViewParent parent = v.getParent();
852 if (parent instanceof View) {
853 v = (View)v.getParent();
854 } else {
855 return;
856 }
857 }
858 }
859
860 /**
861 * {@inheritDoc}
862 */
863 @Override
864 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
865 if (disallowIntercept) {
866 // We need to make sure to cancel our long press if
867 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700868 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700869 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 }
871 super.requestDisallowInterceptTouchEvent(disallowIntercept);
872 }
873
874 @Override
875 public boolean onInterceptTouchEvent(MotionEvent ev) {
876 /*
877 * This method JUST determines whether we want to intercept the motion.
878 * If we return true, onTouchEvent will be called and we do the actual
879 * scrolling there.
880 */
881
Winson Chung45e1d6e2010-11-09 17:19:49 -0800882 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700883 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700884
885 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800886
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 /*
888 * Shortcut the most recurring case: the user is in the dragging
889 * state and he is moving his finger. We want to intercept this
890 * motion.
891 */
892 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700893 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 return true;
895 }
896
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 switch (action & MotionEvent.ACTION_MASK) {
898 case MotionEvent.ACTION_MOVE: {
899 /*
900 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800901 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700903 if (mActivePointerId != INVALID_POINTER) {
904 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700905 }
906 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800907 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700908 // i.e. fall through to the next case (don't break)
909 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
910 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700911 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 }
913
914 case MotionEvent.ACTION_DOWN: {
915 final float x = ev.getX();
916 final float y = ev.getY();
917 // Remember location of down touch
918 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700919 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700920 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800921 mLastMotionRemainder = 0;
922 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700924
Tony Wickhama0aee212019-09-18 09:24:03 -0700925 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700926
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 break;
928 }
929
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800931 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700932 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 break;
934
935 case MotionEvent.ACTION_POINTER_UP:
936 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800937 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 break;
939 }
940
941 /*
942 * The only time we want to intercept motion events is if we are in the
943 * drag mode.
944 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700945 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 }
947
Tony Wickhama0aee212019-09-18 09:24:03 -0700948 /**
949 * If being flinged and user touches the screen, initiate drag; otherwise don't.
950 */
951 private void updateIsBeingDraggedOnTouchDown() {
952 // mScroller.isFinished should be false when being flinged.
953 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
954 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
955
956 if (finishedScrolling) {
957 mIsBeingDragged = false;
958 if (!mScroller.isFinished() && !mFreeScroll) {
959 setCurrentPage(getNextPage());
960 pageEndTransition();
961 }
962 } else {
963 mIsBeingDragged = true;
964 }
965 }
966
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800967 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700968 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800969 }
970
Adam Cohenf8d28232011-02-01 21:47:00 -0800971 protected void determineScrollingStart(MotionEvent ev) {
972 determineScrollingStart(ev, 1.0f);
973 }
974
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 /*
976 * Determines if we should change the touch state to start scrolling after the
977 * user moves their touch point too far.
978 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800979 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700982 if (pointerIndex == -1) return;
983
Vinit Nayaka406f722019-12-19 11:50:55 -0800984 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
985 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -0800986 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -0800987 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700988
Vinit Nayaka406f722019-12-19 11:50:55 -0800989 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700990 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700991 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800992 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
993 mLastMotion = primaryDirection;
994 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700995 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700996 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800997 // Stop listening for things like pinches.
998 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800999 }
1000 }
1001
1002 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001003 // Try canceling the long press. It could also have been scheduled
1004 // by a distant descendant, so use the mAllowLongPress flag to block
1005 // everything
1006 final View currentPage = getPageAt(mCurrentPage);
1007 if (currentPage != null) {
1008 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 }
1010 }
1011
Adam Cohenb5ba0972011-09-07 18:02:31 -07001012 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001013 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001014
Adam Cohenedb40762013-07-18 16:45:45 -07001015 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001016 int count = getChildCount();
1017
1018 final int totalDistance;
1019
1020 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001021 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001022 adjacentPage = page - 1;
1023 }
1024
1025 if (adjacentPage < 0 || adjacentPage > count - 1) {
1026 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1027 } else {
1028 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1029 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001030
1031 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001032 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1033 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001034 return scrollProgress;
1035 }
1036
Adam Cohenedb40762013-07-18 16:45:45 -07001037 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001038 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001039 return 0;
1040 } else {
1041 return mPageScrolls[index];
1042 }
1043 }
1044
Adam Cohen564a2e72013-10-09 14:47:32 -07001045 // While layout transitions are occurring, a child's position may stray from its baseline
1046 // position. This method returns the magnitude of this stray at any given time.
1047 public int getLayoutTransitionOffsetForPage(int index) {
1048 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1049 return 0;
1050 } else {
1051 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001052
Sunny Goyal228153d2018-01-04 15:35:22 -08001053 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1054 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001055 return (int) (child.getX() - baselineX);
1056 }
1057 }
1058
Jon Miranda71cb80c2019-01-24 21:25:13 -08001059 @Override
1060 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001061 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001062 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001063 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001064 super.dispatchDraw(canvas);
1065
1066 canvas.restoreToCount(saveCount);
1067 } else {
1068 super.dispatchDraw(canvas);
1069 }
1070 }
1071
Sunny Goyalb7b01352018-08-09 12:31:20 -07001072 protected void dampedOverScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001073 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001074 if (amount == 0) {
1075 return;
1076 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001077
Vinit Nayaka406f722019-12-19 11:50:55 -08001078 int size = mOrientationHandler.getMeasuredSize(this);
1079 int overScrollAmount = OverScroll.dampedScroll(amount, size);
1080 mSpringOverScroll = overScrollAmount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001081 if (mScroller.isSpringing()) {
1082 invalidate();
1083 return;
1084 }
1085
Vinit Nayaka406f722019-12-19 11:50:55 -08001086 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1087 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1088 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001089 invalidate();
1090 }
1091
Sunny Goyalb7b01352018-08-09 12:31:20 -07001092 protected void overScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001093 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001094 if (mScroller.isSpringing()) {
1095 invalidate();
1096 return;
1097 }
1098
Sunny Goyalb7b01352018-08-09 12:31:20 -07001099 if (amount == 0) return;
1100
1101 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001102 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1103 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001104 } else {
1105 dampedOverScroll(amount);
1106 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001107 }
1108
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001109
Tony Wickham023f4042019-01-29 10:52:31 -08001110 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001111 if (mFreeScroll == freeScroll) {
1112 return;
1113 }
1114
Tony Wickham8f7ead32016-04-07 18:46:44 -07001115 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001116 mFreeScroll = freeScroll;
1117
Adam Cohenf9618852013-11-08 06:45:03 -08001118 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001119 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001120 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001121 if (getScrollForPage(getNextPage()) != getScrollX()) {
1122 snapToPage(getNextPage());
1123 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001124 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001125 }
1126
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001127 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001128 mAllowOverScroll = enable;
1129 }
1130
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 @Override
1132 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001133 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001134 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001135
Michael Jurkab8f06722010-10-10 15:58:46 -07001136 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001137
1138 final int action = ev.getAction();
1139
1140 switch (action & MotionEvent.ACTION_MASK) {
1141 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001142 updateIsBeingDraggedOnTouchDown();
1143
Winson Chung321e9ee2010-08-09 13:37:56 -07001144 /*
1145 * If being flinged and user touches, stop the fling. isFinished
1146 * will be false if being flinged.
1147 */
1148 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001149 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 }
1151
1152 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001153 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001154 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001155 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1156 mLastMotionRemainder = 0;
1157 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001158 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001159 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001160 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001161 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001162 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 break;
1164
Vinit Nayaka406f722019-12-19 11:50:55 -08001165 case MotionEvent.ACTION_MOVE:
1166 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 // Scroll to follow the motion event
1168 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001169
1170 if (pointerIndex == -1) return true;
1171
Vinit Nayaka406f722019-12-19 11:50:55 -08001172 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1173 float delta = mLastMotion + mLastMotionRemainder - direction;
1174 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001175
Winson Chungc0844aa2011-02-02 15:25:58 -08001176 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1177 // keep the remainder because we are actually testing if we've moved from the last
1178 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001179 if (Math.abs(delta) >= 1.0f) {
1180 mLastMotion = direction;
1181 mLastMotionRemainder = delta - (int) delta;
1182
1183 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 } else {
1185 awakenScrollBars();
1186 }
Adam Cohen564976a2010-10-13 18:52:07 -07001187 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 determineScrollingStart(ev);
1189 }
1190 break;
1191
1192 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001193 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 final int activePointerId = mActivePointerId;
1195 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001196 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1197 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001198 final VelocityTracker velocityTracker = mVelocityTracker;
1199 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001200
Vinit Nayaka406f722019-12-19 11:50:55 -08001201 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1202 mActivePointerId);
1203 int delta = (int) (primaryDirection - mDownMotionPrimary);
1204 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1205
1206 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1207 SIGNIFICANT_MOVE_THRESHOLD;
1208
1209 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1210 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1211 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1212 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001213
Adam Cohenf358a4b2013-07-23 16:47:31 -07001214 if (!mFreeScroll) {
1215 // In the case that the page is moved far to one direction and then is flung
1216 // in the opposite direction, we use a threshold to determine whether we should
1217 // just return to the starting page, or if we should skip one further.
1218 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001219 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1220 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001221 returnToOriginalPage = true;
1222 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001223
Adam Cohenf358a4b2013-07-23 16:47:31 -07001224 int finalPage;
1225 // We give flings precedence over large moves, which is why we short-circuit our
1226 // test for a large move if a fling has been registered. That is, a large
1227 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001228
Vinit Nayaka406f722019-12-19 11:50:55 -08001229 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1230 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001231 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001232 snapToPageWithVelocity(finalPage, velocity);
1233 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1234 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001235 mCurrentPage < getChildCount() - 1) {
1236 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001237 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001238 } else {
1239 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001240 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001242 if (!mScroller.isFinished()) {
1243 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001244 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001245
Vinit Nayaka406f722019-12-19 11:50:55 -08001246 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1247 int maxScroll = mMaxScroll;
1248 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001249
Vinit Nayaka406f722019-12-19 11:50:55 -08001250 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1251 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1252 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001253 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001254 } else {
1255 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001256 mScroller.fling(initialScroll, -velocity,
1257 minScroll, maxScroll,
1258 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001259
Vinit Nayaka406f722019-12-19 11:50:55 -08001260 int finalPos = mScroller.getFinalPos();
1261 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001262
1263 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1264 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001265 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001266 // If scrolling ends in the half of the added space that is closer to
1267 // the end, settle to the end. Otherwise snap to the nearest page.
1268 // If flinging past one of the ends, don't change the velocity as it
1269 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001270 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1271 ? minScroll
1272 : finalPos > (lastPageScroll + maxScroll) / 2
1273 ? maxScroll
1274 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001275
Vinit Nayaka406f722019-12-19 11:50:55 -08001276 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001277 // Ensure the scroll/snap doesn't happen too fast;
1278 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001279 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001280 if (extraScrollDuration > 0) {
1281 mScroller.extendDuration(extraScrollDuration);
1282 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001283 }
1284 }
1285 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001287 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001289
Adam Cohen7d30a372013-07-01 17:03:59 -07001290 // End any intermediate reordering states
1291 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001292 break;
1293
1294 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001295 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001296 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001297 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001298 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001299 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 break;
1301
1302 case MotionEvent.ACTION_POINTER_UP:
1303 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001304 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 break;
1306 }
1307
1308 return true;
1309 }
1310
Vinit Nayaka406f722019-12-19 11:50:55 -08001311 protected boolean shouldFlingForVelocity(int velocity) {
1312 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001313 }
1314
Adam Cohen7d30a372013-07-01 17:03:59 -07001315 private void resetTouchState() {
1316 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001317 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001318 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001319 }
1320
Adam Cohenc2d6e892014-10-16 09:49:24 -07001321 /**
1322 * Triggered by scrolling via touch
1323 */
1324 protected void onScrollInteractionBegin() {
1325 }
1326
1327 protected void onScrollInteractionEnd() {
1328 }
1329
Winson Chung185d7162011-02-28 13:47:29 -08001330 @Override
1331 public boolean onGenericMotionEvent(MotionEvent event) {
1332 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1333 switch (event.getAction()) {
1334 case MotionEvent.ACTION_SCROLL: {
1335 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1336 final float vscroll;
1337 final float hscroll;
1338 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1339 vscroll = 0;
1340 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1341 } else {
1342 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1343 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1344 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001345 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1346 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001347 }
Winson Chung185d7162011-02-28 13:47:29 -08001348 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001349 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001350 : (hscroll > 0 || vscroll > 0);
1351 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001352 scrollRight();
1353 } else {
1354 scrollLeft();
1355 }
1356 return true;
1357 }
1358 }
1359 }
1360 }
1361 return super.onGenericMotionEvent(event);
1362 }
1363
Sunny Goyal8b37c572020-03-31 12:12:14 -07001364 /**
1365 * Returns true if the paged view can scroll for the provided vertical and horizontal
1366 * scroll values
1367 */
1368 protected boolean canScroll(float absVScroll, float absHScroll) {
1369 ActivityContext ac = ActivityContext.lookupContext(getContext());
1370 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001371 }
1372
Michael Jurkab8f06722010-10-10 15:58:46 -07001373 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1374 if (mVelocityTracker == null) {
1375 mVelocityTracker = VelocityTracker.obtain();
1376 }
1377 mVelocityTracker.addMovement(ev);
1378 }
1379
1380 private void releaseVelocityTracker() {
1381 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001382 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001383 mVelocityTracker.recycle();
1384 mVelocityTracker = null;
1385 }
1386 }
1387
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 private void onSecondaryPointerUp(MotionEvent ev) {
1389 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1390 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1391 final int pointerId = ev.getPointerId(pointerIndex);
1392 if (pointerId == mActivePointerId) {
1393 // This was our active pointer going up. Choose a new
1394 // active pointer and adjust accordingly.
1395 // TODO: Make this decision more intelligent.
1396 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001397 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1398 newPointerIndex);
1399 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 mActivePointerId = ev.getPointerId(newPointerIndex);
1401 if (mVelocityTracker != null) {
1402 mVelocityTracker.clear();
1403 }
1404 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001405 }
1406
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 @Override
1408 public void requestChildFocus(View child, View focused) {
1409 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001410 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001411 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001412 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 }
1414 }
1415
Sunny Goyal228153d2018-01-04 15:35:22 -08001416 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001417 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001418 }
1419
Vinit Nayaka406f722019-12-19 11:50:55 -08001420 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1421 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1422 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001423 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 final int childCount = getChildCount();
1426 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001427 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001428 int childSize = mOrientationHandler.getMeasuredSize(layout);
1429 int halfChildSize = (childSize / 2);
1430 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1432 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1433 minDistanceFromScreenCenter = distanceFromScreenCenter;
1434 minDistanceFromScreenCenterIndex = i;
1435 }
1436 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001437 return minDistanceFromScreenCenterIndex;
1438 }
1439
1440 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001441 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1442 }
1443
1444 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001445 int scroll = mOrientationHandler.getPrimaryScroll(this);
1446 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001447 }
1448
1449 protected int getPageSnapDuration() {
1450 if (isInOverScroll()) {
1451 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1452 }
1453 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 }
1455
Adam Cohene0f66b52010-11-23 15:06:07 -08001456 // We want the duration of the page snap animation to be influenced by the distance that
1457 // the screen has to travel, however, we don't want this duration to be effected in a
1458 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1459 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001460 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001461 f -= 0.5f; // center the values about 0.
1462 f *= 0.3f * Math.PI / 2.0f;
1463 return (float) Math.sin(f);
1464 }
1465
Winson Chung05a31ed2018-02-08 17:08:43 -08001466 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001467 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001468 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001469
Vinit Nayaka406f722019-12-19 11:50:55 -08001470 final int newLoc = getScrollForPage(whichPage);
1471 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001472 int duration = 0;
1473
Sunny Goyal4d113a52015-05-27 10:05:28 -07001474 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001475 // If the velocity is low enough, then treat this more as an automatic page advance
1476 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001477 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001478 }
1479
1480 // Here we compute a "distance" that will be used in the computation of the overall
1481 // snap duration. This is a function of the actual distance that needs to be traveled;
1482 // we keep this value close to half screen size in order to reduce the variance in snap
1483 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001484 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001485 float distance = halfScreenSize + halfScreenSize *
1486 distanceInfluenceForSnapDuration(distanceRatio);
1487
1488 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001489 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001490
1491 // we want the page's snap velocity to approximately match the velocity at which the
1492 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001493 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1494 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001495
Jon Miranda9e198662020-03-11 14:42:02 -07001496 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001497 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001498 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001499 } else {
1500 return snapToPage(whichPage, delta, duration);
1501 }
Michael Jurka0142d492010-08-25 17:46:15 -07001502 }
1503
Winson Chung05a31ed2018-02-08 17:08:43 -08001504 public boolean snapToPage(int whichPage) {
1505 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 }
1507
Winson Chung05a31ed2018-02-08 17:08:43 -08001508 public boolean snapToPageImmediately(int whichPage) {
1509 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001510 }
1511
Winson Chung05a31ed2018-02-08 17:08:43 -08001512 public boolean snapToPage(int whichPage, int duration) {
1513 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 }
1515
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001516 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001517 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001518 }
1519
Winson Chung05a31ed2018-02-08 17:08:43 -08001520 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001521 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001522 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001523
Vinit Nayaka406f722019-12-19 11:50:55 -08001524 int newLoc = getScrollForPage(whichPage);
1525 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001526 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001527 }
1528
Winson Chung05a31ed2018-02-08 17:08:43 -08001529 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001530 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 }
Michael Jurka0142d492010-08-25 17:46:15 -07001532
Winson Chung05a31ed2018-02-08 17:08:43 -08001533 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001534 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001535 if (mFirstLayout) {
1536 setCurrentPage(whichPage);
1537 return false;
1538 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001539
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001540 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001541 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1542 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001543 }
1544
Adam Cohen6f127a62014-06-12 14:54:41 -07001545 whichPage = validateNewPage(whichPage);
1546
Adam Cohen7d30a372013-07-01 17:03:59 -07001547 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001548
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001550 if (immediate) {
1551 duration = 0;
1552 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001553 duration = Math.abs(delta);
1554 }
1555
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001556 if (duration != 0) {
1557 pageBeginTransition();
1558 }
1559
Adam Cohenf358a4b2013-07-23 16:47:31 -07001560 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001561 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001562 }
Adam Cohenf9618852013-11-08 06:45:03 -08001563
1564 if (interpolator != null) {
1565 mScroller.setInterpolator(interpolator);
1566 } else {
1567 mScroller.setInterpolator(mDefaultInterpolator);
1568 }
1569
Jon Miranda71cb80c2019-01-24 21:25:13 -08001570 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001571 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001572 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001573 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001574 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001575
Adam Cohen674531f2013-12-13 15:07:14 -08001576 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001577
1578 // Trigger a compute() to finish switching pages if necessary
1579 if (immediate) {
1580 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001581 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001582 }
1583
Winson Chung321e9ee2010-08-09 13:37:56 -07001584 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001585 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001586 }
1587
Vadim Tryshev98913d02018-05-18 18:41:34 -07001588 public boolean scrollLeft() {
1589 if (getNextPage() > 0) {
1590 snapToPage(getNextPage() - 1);
1591 return true;
1592 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001593 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001594 }
1595
Vadim Tryshev98913d02018-05-18 18:41:34 -07001596 public boolean scrollRight() {
1597 if (getNextPage() < getChildCount() - 1) {
1598 snapToPage(getNextPage() + 1);
1599 return true;
1600 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001601 return onOverscroll(getMeasuredWidth());
1602 }
1603
1604 protected boolean onOverscroll(int amount) {
1605 if (!mAllowOverScroll) return false;
1606 onScrollInteractionBegin();
1607 overScroll(amount);
1608 onScrollInteractionEnd();
1609 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001610 }
1611
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001612 @Override
1613 public CharSequence getAccessibilityClassName() {
1614 // Some accessibility services have special logic for ScrollView. Since we provide same
1615 // accessibility info as ScrollView, inform the service to handle use the same way.
1616 return ScrollView.class.getName();
1617 }
1618
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001619 protected boolean isPageOrderFlipped() {
1620 return false;
1621 }
1622
Winson Chung6a0f57d2011-06-29 20:10:49 -07001623 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001624 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001625 @Override
1626 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1627 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001628 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001629 int offset = (mAllowOverScroll ? 0 : 1);
1630 info.setScrollable(getPageCount() > offset);
1631 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001632 info.addAction(pagesFlipped ?
1633 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1634 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1635 info.addAction(mIsRtl ?
1636 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1637 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001638 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001639 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001640 info.addAction(pagesFlipped ?
1641 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1642 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1643 info.addAction(mIsRtl ?
1644 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1645 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001646 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001647 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1648 // Besides disabling the accessibility long-click, this also prevents this view from getting
1649 // accessibility focus.
1650 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001651 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001652 }
1653
1654 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001655 public void sendAccessibilityEvent(int eventType) {
1656 // Don't let the view send real scroll events.
1657 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1658 super.sendAccessibilityEvent(eventType);
1659 }
1660 }
1661
1662 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001663 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1664 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001665 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001666 }
1667
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001668 @Override
1669 public boolean performAccessibilityAction(int action, Bundle arguments) {
1670 if (super.performAccessibilityAction(action, arguments)) {
1671 return true;
1672 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001673 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001674 switch (action) {
1675 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001676 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001677 return true;
1678 }
1679 } break;
1680 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001681 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001682 return true;
1683 }
yingleiw02cc8482019-08-02 16:14:27 -07001684 } break;
1685 case android.R.id.accessibilityActionPageRight: {
1686 if (!mIsRtl) {
1687 return scrollRight();
1688 } else {
1689 return scrollLeft();
1690 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001691 }
yingleiw02cc8482019-08-02 16:14:27 -07001692 case android.R.id.accessibilityActionPageLeft: {
1693 if (!mIsRtl) {
1694 return scrollLeft();
1695 } else {
1696 return scrollRight();
1697 }
1698 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001699 }
1700 return false;
1701 }
1702
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001703 protected boolean canAnnouncePageDescription() {
1704 return true;
1705 }
1706
Adam Cohen0ffac432013-07-10 11:19:26 -07001707 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001708 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001709 getNextPage() + 1, getChildCount());
1710 }
1711
Tony Mak2e564402018-02-27 17:19:34 +00001712 protected float getDownMotionX() {
1713 return mDownMotionX;
1714 }
1715
1716 protected float getDownMotionY() {
1717 return mDownMotionY;
1718 }
1719
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001720 protected interface ComputePageScrollsLogic {
1721
1722 boolean shouldIncludeView(View view);
1723 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001724
1725 public int[] getVisibleChildrenRange() {
1726 float visibleLeft = 0;
1727 float visibleRight = visibleLeft + getMeasuredWidth();
1728 float scaleX = getScaleX();
1729 if (scaleX < 1 && scaleX > 0) {
1730 float mid = getMeasuredWidth() / 2;
1731 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1732 visibleRight = mid + ((visibleRight - mid) / scaleX);
1733 }
1734
1735 int leftChild = -1;
1736 int rightChild = -1;
1737 final int childCount = getChildCount();
1738 for (int i = 0; i < childCount; i++) {
1739 final View child = getPageAt(i);
1740
1741 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1742 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1743 if (leftChild == -1) {
1744 leftChild = i;
1745 }
1746 rightChild = i;
1747 }
1748 }
1749 mTmpIntPair[0] = leftChild;
1750 mTmpIntPair[1] = rightChild;
1751 return mTmpIntPair;
1752 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001753}