blob: 5343424dfcbd6101b7c77204a3cc3a1105fc467b [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
Jon Miranda57dab6f2020-04-10 13:38:14 -07001072 /**
1073 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1074 */
1075 private int getSpringOverScroll(int amount) {
1076 if (mScroller.isSpringing()) {
1077 return amount < 0
1078 ? mScroller.getCurrPos()
1079 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1080 } else {
1081 return 0;
1082 }
1083 }
1084
Sunny Goyalb7b01352018-08-09 12:31:20 -07001085 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001086 if (amount == 0) {
1087 return;
1088 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001089
Vinit Nayaka406f722019-12-19 11:50:55 -08001090 int size = mOrientationHandler.getMeasuredSize(this);
1091 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001092 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001093 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001094 invalidate();
1095 return;
1096 }
1097
Vinit Nayaka406f722019-12-19 11:50:55 -08001098 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1099 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1100 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001101 invalidate();
1102 }
1103
Sunny Goyalb7b01352018-08-09 12:31:20 -07001104 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001105 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001106 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001107 invalidate();
1108 return;
1109 }
1110
Sunny Goyalb7b01352018-08-09 12:31:20 -07001111 if (amount == 0) return;
1112
1113 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001114 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1115 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001116 } else {
1117 dampedOverScroll(amount);
1118 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001119 }
1120
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001121
Tony Wickham023f4042019-01-29 10:52:31 -08001122 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001123 if (mFreeScroll == freeScroll) {
1124 return;
1125 }
1126
Tony Wickham8f7ead32016-04-07 18:46:44 -07001127 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001128 mFreeScroll = freeScroll;
1129
Adam Cohenf9618852013-11-08 06:45:03 -08001130 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001131 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001132 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001133 if (getScrollForPage(getNextPage()) != getScrollX()) {
1134 snapToPage(getNextPage());
1135 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001136 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001137 }
1138
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001139 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001140 mAllowOverScroll = enable;
1141 }
1142
Winson Chung321e9ee2010-08-09 13:37:56 -07001143 @Override
1144 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001145 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001146 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001147
Michael Jurkab8f06722010-10-10 15:58:46 -07001148 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001149
1150 final int action = ev.getAction();
1151
1152 switch (action & MotionEvent.ACTION_MASK) {
1153 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001154 updateIsBeingDraggedOnTouchDown();
1155
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 /*
1157 * If being flinged and user touches, stop the fling. isFinished
1158 * will be false if being flinged.
1159 */
1160 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001161 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 }
1163
1164 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001165 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001166 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001167 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1168 mLastMotionRemainder = 0;
1169 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001171 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001173 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001174 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 break;
1176
Vinit Nayaka406f722019-12-19 11:50:55 -08001177 case MotionEvent.ACTION_MOVE:
1178 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 // Scroll to follow the motion event
1180 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001181
1182 if (pointerIndex == -1) return true;
1183
Vinit Nayaka406f722019-12-19 11:50:55 -08001184 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1185 float delta = mLastMotion + mLastMotionRemainder - direction;
1186 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001187
Winson Chungc0844aa2011-02-02 15:25:58 -08001188 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1189 // keep the remainder because we are actually testing if we've moved from the last
1190 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001191 if (Math.abs(delta) >= 1.0f) {
1192 mLastMotion = direction;
1193 mLastMotionRemainder = delta - (int) delta;
1194
1195 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 } else {
1197 awakenScrollBars();
1198 }
Adam Cohen564976a2010-10-13 18:52:07 -07001199 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 determineScrollingStart(ev);
1201 }
1202 break;
1203
1204 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001205 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 final int activePointerId = mActivePointerId;
1207 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001208 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1209 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001210 final VelocityTracker velocityTracker = mVelocityTracker;
1211 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001212
Vinit Nayaka406f722019-12-19 11:50:55 -08001213 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1214 mActivePointerId);
1215 int delta = (int) (primaryDirection - mDownMotionPrimary);
1216 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1217
1218 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1219 SIGNIFICANT_MOVE_THRESHOLD;
1220
1221 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1222 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1223 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1224 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001225
Adam Cohenf358a4b2013-07-23 16:47:31 -07001226 if (!mFreeScroll) {
1227 // In the case that the page is moved far to one direction and then is flung
1228 // in the opposite direction, we use a threshold to determine whether we should
1229 // just return to the starting page, or if we should skip one further.
1230 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001231 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1232 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001233 returnToOriginalPage = true;
1234 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001235
Adam Cohenf358a4b2013-07-23 16:47:31 -07001236 int finalPage;
1237 // We give flings precedence over large moves, which is why we short-circuit our
1238 // test for a large move if a fling has been registered. That is, a large
1239 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001240
Vinit Nayaka406f722019-12-19 11:50:55 -08001241 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1242 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001243 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001244 snapToPageWithVelocity(finalPage, velocity);
1245 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1246 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001247 mCurrentPage < getChildCount() - 1) {
1248 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001249 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001250 } else {
1251 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001252 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001254 if (!mScroller.isFinished()) {
1255 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001257
Vinit Nayaka406f722019-12-19 11:50:55 -08001258 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1259 int maxScroll = mMaxScroll;
1260 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001261
Vinit Nayaka406f722019-12-19 11:50:55 -08001262 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1263 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1264 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001265 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001266 } else {
1267 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001268 mScroller.fling(initialScroll, -velocity,
1269 minScroll, maxScroll,
1270 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001271
Vinit Nayaka406f722019-12-19 11:50:55 -08001272 int finalPos = mScroller.getFinalPos();
1273 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001274
1275 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1276 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001277 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001278 // If scrolling ends in the half of the added space that is closer to
1279 // the end, settle to the end. Otherwise snap to the nearest page.
1280 // If flinging past one of the ends, don't change the velocity as it
1281 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001282 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1283 ? minScroll
1284 : finalPos > (lastPageScroll + maxScroll) / 2
1285 ? maxScroll
1286 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001287
Vinit Nayaka406f722019-12-19 11:50:55 -08001288 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001289 // Ensure the scroll/snap doesn't happen too fast;
1290 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001291 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001292 if (extraScrollDuration > 0) {
1293 mScroller.extendDuration(extraScrollDuration);
1294 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001295 }
1296 }
1297 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001299 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001301
Adam Cohen7d30a372013-07-01 17:03:59 -07001302 // End any intermediate reordering states
1303 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 break;
1305
1306 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001307 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001308 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001309 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001310 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001311 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 break;
1313
1314 case MotionEvent.ACTION_POINTER_UP:
1315 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001316 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001317 break;
1318 }
1319
1320 return true;
1321 }
1322
Vinit Nayaka406f722019-12-19 11:50:55 -08001323 protected boolean shouldFlingForVelocity(int velocity) {
1324 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001325 }
1326
Adam Cohen7d30a372013-07-01 17:03:59 -07001327 private void resetTouchState() {
1328 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001329 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001330 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001331 }
1332
Adam Cohenc2d6e892014-10-16 09:49:24 -07001333 /**
1334 * Triggered by scrolling via touch
1335 */
1336 protected void onScrollInteractionBegin() {
1337 }
1338
1339 protected void onScrollInteractionEnd() {
1340 }
1341
Winson Chung185d7162011-02-28 13:47:29 -08001342 @Override
1343 public boolean onGenericMotionEvent(MotionEvent event) {
1344 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1345 switch (event.getAction()) {
1346 case MotionEvent.ACTION_SCROLL: {
1347 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1348 final float vscroll;
1349 final float hscroll;
1350 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1351 vscroll = 0;
1352 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1353 } else {
1354 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1355 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1356 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001357 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1358 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001359 }
Winson Chung185d7162011-02-28 13:47:29 -08001360 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001361 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001362 : (hscroll > 0 || vscroll > 0);
1363 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001364 scrollRight();
1365 } else {
1366 scrollLeft();
1367 }
1368 return true;
1369 }
1370 }
1371 }
1372 }
1373 return super.onGenericMotionEvent(event);
1374 }
1375
Sunny Goyal8b37c572020-03-31 12:12:14 -07001376 /**
1377 * Returns true if the paged view can scroll for the provided vertical and horizontal
1378 * scroll values
1379 */
1380 protected boolean canScroll(float absVScroll, float absHScroll) {
1381 ActivityContext ac = ActivityContext.lookupContext(getContext());
1382 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001383 }
1384
Michael Jurkab8f06722010-10-10 15:58:46 -07001385 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1386 if (mVelocityTracker == null) {
1387 mVelocityTracker = VelocityTracker.obtain();
1388 }
1389 mVelocityTracker.addMovement(ev);
1390 }
1391
1392 private void releaseVelocityTracker() {
1393 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001394 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001395 mVelocityTracker.recycle();
1396 mVelocityTracker = null;
1397 }
1398 }
1399
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 private void onSecondaryPointerUp(MotionEvent ev) {
1401 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1402 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1403 final int pointerId = ev.getPointerId(pointerIndex);
1404 if (pointerId == mActivePointerId) {
1405 // This was our active pointer going up. Choose a new
1406 // active pointer and adjust accordingly.
1407 // TODO: Make this decision more intelligent.
1408 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001409 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1410 newPointerIndex);
1411 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 mActivePointerId = ev.getPointerId(newPointerIndex);
1413 if (mVelocityTracker != null) {
1414 mVelocityTracker.clear();
1415 }
1416 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001417 }
1418
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 @Override
1420 public void requestChildFocus(View child, View focused) {
1421 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001422 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001423 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001424 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 }
1426 }
1427
Sunny Goyal228153d2018-01-04 15:35:22 -08001428 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001429 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001430 }
1431
Vinit Nayaka406f722019-12-19 11:50:55 -08001432 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1433 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1434 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001435 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 final int childCount = getChildCount();
1438 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001439 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001440 int childSize = mOrientationHandler.getMeasuredSize(layout);
1441 int halfChildSize = (childSize / 2);
1442 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1444 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1445 minDistanceFromScreenCenter = distanceFromScreenCenter;
1446 minDistanceFromScreenCenterIndex = i;
1447 }
1448 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001449 return minDistanceFromScreenCenterIndex;
1450 }
1451
1452 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001453 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1454 }
1455
1456 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001457 int scroll = mOrientationHandler.getPrimaryScroll(this);
1458 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001459 }
1460
1461 protected int getPageSnapDuration() {
1462 if (isInOverScroll()) {
1463 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1464 }
1465 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 }
1467
Adam Cohene0f66b52010-11-23 15:06:07 -08001468 // We want the duration of the page snap animation to be influenced by the distance that
1469 // the screen has to travel, however, we don't want this duration to be effected in a
1470 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1471 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001472 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001473 f -= 0.5f; // center the values about 0.
1474 f *= 0.3f * Math.PI / 2.0f;
1475 return (float) Math.sin(f);
1476 }
1477
Winson Chung05a31ed2018-02-08 17:08:43 -08001478 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001479 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001480 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001481
Vinit Nayaka406f722019-12-19 11:50:55 -08001482 final int newLoc = getScrollForPage(whichPage);
1483 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001484 int duration = 0;
1485
Sunny Goyal4d113a52015-05-27 10:05:28 -07001486 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001487 // If the velocity is low enough, then treat this more as an automatic page advance
1488 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001489 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001490 }
1491
1492 // Here we compute a "distance" that will be used in the computation of the overall
1493 // snap duration. This is a function of the actual distance that needs to be traveled;
1494 // we keep this value close to half screen size in order to reduce the variance in snap
1495 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001496 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001497 float distance = halfScreenSize + halfScreenSize *
1498 distanceInfluenceForSnapDuration(distanceRatio);
1499
1500 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001501 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001502
1503 // we want the page's snap velocity to approximately match the velocity at which the
1504 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001505 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1506 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001507
Jon Miranda9e198662020-03-11 14:42:02 -07001508 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001509 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001510 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001511 } else {
1512 return snapToPage(whichPage, delta, duration);
1513 }
Michael Jurka0142d492010-08-25 17:46:15 -07001514 }
1515
Winson Chung05a31ed2018-02-08 17:08:43 -08001516 public boolean snapToPage(int whichPage) {
1517 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 }
1519
Winson Chung05a31ed2018-02-08 17:08:43 -08001520 public boolean snapToPageImmediately(int whichPage) {
1521 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001522 }
1523
Winson Chung05a31ed2018-02-08 17:08:43 -08001524 public boolean snapToPage(int whichPage, int duration) {
1525 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 }
1527
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001528 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001529 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001530 }
1531
Winson Chung05a31ed2018-02-08 17:08:43 -08001532 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001533 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001534 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001535
Vinit Nayaka406f722019-12-19 11:50:55 -08001536 int newLoc = getScrollForPage(whichPage);
1537 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001538 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001539 }
1540
Winson Chung05a31ed2018-02-08 17:08:43 -08001541 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001542 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001543 }
Michael Jurka0142d492010-08-25 17:46:15 -07001544
Winson Chung05a31ed2018-02-08 17:08:43 -08001545 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001546 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001547 if (mFirstLayout) {
1548 setCurrentPage(whichPage);
1549 return false;
1550 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001551
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001552 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001553 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1554 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001555 }
1556
Adam Cohen6f127a62014-06-12 14:54:41 -07001557 whichPage = validateNewPage(whichPage);
1558
Adam Cohen7d30a372013-07-01 17:03:59 -07001559 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001560
Winson Chung321e9ee2010-08-09 13:37:56 -07001561 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001562 if (immediate) {
1563 duration = 0;
1564 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001565 duration = Math.abs(delta);
1566 }
1567
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001568 if (duration != 0) {
1569 pageBeginTransition();
1570 }
1571
Adam Cohenf358a4b2013-07-23 16:47:31 -07001572 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001573 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001574 }
Adam Cohenf9618852013-11-08 06:45:03 -08001575
1576 if (interpolator != null) {
1577 mScroller.setInterpolator(interpolator);
1578 } else {
1579 mScroller.setInterpolator(mDefaultInterpolator);
1580 }
1581
Jon Miranda71cb80c2019-01-24 21:25:13 -08001582 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001583 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001584 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001585 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001586 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001587
Adam Cohen674531f2013-12-13 15:07:14 -08001588 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001589
1590 // Trigger a compute() to finish switching pages if necessary
1591 if (immediate) {
1592 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001593 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001594 }
1595
Winson Chung321e9ee2010-08-09 13:37:56 -07001596 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001597 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 }
1599
Vadim Tryshev98913d02018-05-18 18:41:34 -07001600 public boolean scrollLeft() {
1601 if (getNextPage() > 0) {
1602 snapToPage(getNextPage() - 1);
1603 return true;
1604 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001605 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001606 }
1607
Vadim Tryshev98913d02018-05-18 18:41:34 -07001608 public boolean scrollRight() {
1609 if (getNextPage() < getChildCount() - 1) {
1610 snapToPage(getNextPage() + 1);
1611 return true;
1612 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001613 return onOverscroll(getMeasuredWidth());
1614 }
1615
1616 protected boolean onOverscroll(int amount) {
1617 if (!mAllowOverScroll) return false;
1618 onScrollInteractionBegin();
1619 overScroll(amount);
1620 onScrollInteractionEnd();
1621 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001622 }
1623
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001624 @Override
1625 public CharSequence getAccessibilityClassName() {
1626 // Some accessibility services have special logic for ScrollView. Since we provide same
1627 // accessibility info as ScrollView, inform the service to handle use the same way.
1628 return ScrollView.class.getName();
1629 }
1630
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001631 protected boolean isPageOrderFlipped() {
1632 return false;
1633 }
1634
Winson Chung6a0f57d2011-06-29 20:10:49 -07001635 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001636 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001637 @Override
1638 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1639 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001640 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001641 int offset = (mAllowOverScroll ? 0 : 1);
1642 info.setScrollable(getPageCount() > offset);
1643 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001644 info.addAction(pagesFlipped ?
1645 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1646 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1647 info.addAction(mIsRtl ?
1648 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1649 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001650 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001651 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001652 info.addAction(pagesFlipped ?
1653 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1654 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1655 info.addAction(mIsRtl ?
1656 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1657 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001658 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001659 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1660 // Besides disabling the accessibility long-click, this also prevents this view from getting
1661 // accessibility focus.
1662 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001663 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001664 }
1665
1666 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001667 public void sendAccessibilityEvent(int eventType) {
1668 // Don't let the view send real scroll events.
1669 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1670 super.sendAccessibilityEvent(eventType);
1671 }
1672 }
1673
1674 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001675 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1676 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001677 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001678 }
1679
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001680 @Override
1681 public boolean performAccessibilityAction(int action, Bundle arguments) {
1682 if (super.performAccessibilityAction(action, arguments)) {
1683 return true;
1684 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001685 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001686 switch (action) {
1687 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001688 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001689 return true;
1690 }
1691 } break;
1692 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001693 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001694 return true;
1695 }
yingleiw02cc8482019-08-02 16:14:27 -07001696 } break;
1697 case android.R.id.accessibilityActionPageRight: {
1698 if (!mIsRtl) {
1699 return scrollRight();
1700 } else {
1701 return scrollLeft();
1702 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001703 }
yingleiw02cc8482019-08-02 16:14:27 -07001704 case android.R.id.accessibilityActionPageLeft: {
1705 if (!mIsRtl) {
1706 return scrollLeft();
1707 } else {
1708 return scrollRight();
1709 }
1710 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001711 }
1712 return false;
1713 }
1714
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001715 protected boolean canAnnouncePageDescription() {
1716 return true;
1717 }
1718
Adam Cohen0ffac432013-07-10 11:19:26 -07001719 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001720 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001721 getNextPage() + 1, getChildCount());
1722 }
1723
Tony Mak2e564402018-02-27 17:19:34 +00001724 protected float getDownMotionX() {
1725 return mDownMotionX;
1726 }
1727
1728 protected float getDownMotionY() {
1729 return mDownMotionY;
1730 }
1731
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001732 protected interface ComputePageScrollsLogic {
1733
1734 boolean shouldIncludeView(View view);
1735 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001736
1737 public int[] getVisibleChildrenRange() {
1738 float visibleLeft = 0;
1739 float visibleRight = visibleLeft + getMeasuredWidth();
1740 float scaleX = getScaleX();
1741 if (scaleX < 1 && scaleX > 0) {
1742 float mid = getMeasuredWidth() / 2;
1743 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1744 visibleRight = mid + ((visibleRight - mid) / scaleX);
1745 }
1746
1747 int leftChild = -1;
1748 int rightChild = -1;
1749 final int childCount = getChildCount();
1750 for (int i = 0; i < childCount; i++) {
1751 final View child = getPageAt(i);
1752
1753 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1754 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1755 if (leftChild == -1) {
1756 leftChild = i;
1757 }
1758 rightChild = i;
1759 }
1760 }
1761 mTmpIntPair[0] = leftChild;
1762 mTmpIntPair[1] = rightChild;
1763 return mTmpIntPair;
1764 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001765}