blob: e38631d0c1ba02a36b3145e718d34885eb2842a4 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070019import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070020import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070021import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080024import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070026import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070027import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070029import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080030import android.view.InputDevice;
31import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.MotionEvent;
Vinit Nayaka406f722019-12-19 11:50:55 -080033import android.view.Surface;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080037import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.ViewGroup;
39import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070043import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070044
Vinit Nayaka406f722019-12-19 11:50:55 -080045import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
46import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
47import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
48import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
49import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
50import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
51import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
52
Jon Miranda9e198662020-03-11 14:42:02 -070053import com.android.launcher3.anim.Interpolators;
54import com.android.launcher3.compat.AccessibilityManagerCompat;
55import com.android.launcher3.config.FeatureFlags;
56import com.android.launcher3.model.PagedViewOrientedState;
57import com.android.launcher3.pageindicators.PageIndicator;
58import com.android.launcher3.touch.OverScroll;
59import com.android.launcher3.touch.PagedOrientationHandler;
60import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
61import com.android.launcher3.touch.PortraitPagedViewHandler;
62import com.android.launcher3.util.OverScroller;
63import com.android.launcher3.util.Thunk;
64
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;
121 protected PagedOrientationHandler mOrientationHandler = new PortraitPagedViewHandler();
122 protected final PagedViewOrientedState mOrientationState = new PagedViewOrientedState();
Adam Cohenedb40762013-07-18 16:45:45 -0700123
Vadim Trysheve628c472018-05-16 14:28:18 -0700124 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700125 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800126
Michael Jurka7426c422010-11-11 15:23:47 -0800127 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800129 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Michael Jurka5f1c5092010-09-03 14:15:02 -0700131 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka5f1c5092010-09-03 14:15:02 -0700133 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700135 protected boolean mIsPageInTransition = false;
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
143 protected int mLayoutRotation = Surface.ROTATION_0;
144 protected int mDisplayRotation = Surface.ROTATION_0;
Adam Cohen8d769d62017-06-23 17:27:38 -0700145
Winson Chungd2be3812013-07-16 11:11:32 -0700146 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800148 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700149
John Spurlock77e1f472013-09-11 10:09:51 -0400150 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800151 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400152
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800153 // Similar to the platform implementation of isLayoutValid();
154 protected boolean mIsLayoutValid;
155
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700156 private int[] mTmpIntPair = new int[2];
157
Winson Chung321e9ee2010-08-09 13:37:56 -0700158 public PagedView(Context context) {
159 this(context, null);
160 }
161
162 public PagedView(Context context, AttributeSet attrs) {
163 this(context, attrs, 0);
164 }
165
166 public PagedView(Context context, AttributeSet attrs, int defStyle) {
167 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700168
Adam Cohen9c4949e2010-10-05 12:27:22 -0700169 TypedArray a = context.obtainStyledAttributes(attrs,
170 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700171 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700172 a.recycle();
173
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700175 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700176 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 }
178
179 /**
180 * Initializes various states for this workspace.
181 */
Michael Jurka0142d492010-08-25 17:46:15 -0700182 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800183 Context context = getContext();
184 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700185 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700186 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700187
Vinit Nayaka406f722019-12-19 11:50:55 -0800188 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700189 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800191
Sunny Goyalcf25b522015-07-09 00:01:18 -0700192 float density = getResources().getDisplayMetrics().density;
193 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
194 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
195 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700196
197 if (Utilities.ATLEAST_OREO) {
198 setDefaultFocusHighlightEnabled(false);
199 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
Adam Cohenf9618852013-11-08 06:45:03 -0800202 protected void setDefaultInterpolator(Interpolator interpolator) {
203 mDefaultInterpolator = interpolator;
204 mScroller.setInterpolator(mDefaultInterpolator);
205 }
206
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700207 public void initParentViews(View parent) {
208 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800209 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700210 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700211 }
212 }
213
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800214 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700215 return mPageIndicator;
216 }
217
Adam Cohen674531f2013-12-13 15:07:14 -0800218 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700219 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
220 * that the user was on before entering free scroll mode (e.g. the home screen page they
221 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
222 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700224 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700225 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700227
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700228 /**
229 * Returns the index of page to be shown immediately afterwards.
230 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700231 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700232 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
233 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700234
Adam Cohenf9c184a2016-01-15 16:47:43 -0800235 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Sunny Goyal83a8f042015-05-19 12:52:12 -0700239 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
Adam Cohenae4f1552011-10-20 00:15:42 -0700243 protected int indexToPage(int index) {
244 return index;
245 }
246
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 * Updates the scroll of the current page immediately to its final scroll position. We use this
249 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
250 * the previous tab page.
251 */
252 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700253 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800254 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700255 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800256 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700257 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800258 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
259 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700260 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800261 }
262
Sreyase3e7e632020-01-29 10:42:10 -0800263 /**
264 * Returns left offset of a page. This is the gap between pages and prevents overlap.
265 */
266 public int scrollOffsetLeft() {
267 return mInsets.left + getPaddingLeft();
268 }
269
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700270 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271 mScroller.abortAnimation();
272 // We need to clean up the next page here to avoid computeScrollHelper from
273 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700274 if (resetNextPage) {
275 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800276 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700277 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700278 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700279
Tony Wickham8f7ead32016-04-07 18:46:44 -0700280 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700281 mScroller.forceFinished(true);
282 // We need to clean up the next page here to avoid computeScrollHelper from
283 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700284 if (resetNextPage) {
285 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800286 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700287 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700288 }
289
Adam Cohen6f127a62014-06-12 14:54:41 -0700290 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700291 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700292 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700293 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700294 }
295
Chet Haasebc2f0822012-10-26 17:59:53 -0700296 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700297 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
298 */
299 private int ensureWithinScrollBounds(int page) {
300 int dir = !mIsRtl ? 1 : - 1;
301 int currScroll = getScrollForPage(page);
302 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800303 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700304 page += dir;
305 prevScroll = currScroll;
306 currScroll = getScrollForPage(page);
307 if (currScroll <= prevScroll) {
308 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
309 break;
310 }
311 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800312 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700313 page -= dir;
314 prevScroll = currScroll;
315 currScroll = getScrollForPage(page);
316 if (currScroll >= prevScroll) {
317 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
318 break;
319 }
320 }
321 return page;
322 }
323
Tony Wickham03f27012019-04-25 14:22:54 -0700324 public void setCurrentPage(int currentPage) {
325 setCurrentPage(currentPage, INVALID_PAGE);
326 }
327
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700328 /**
Winson Chung86f77532010-08-24 11:08:22 -0700329 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 */
Tony Wickham03f27012019-04-25 14:22:54 -0700331 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800332 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700333 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800334 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800335 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
336 // the default
337 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800338 return;
339 }
Tony Wickham03f27012019-04-25 14:22:54 -0700340 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700341 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700342 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700343 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800344 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 }
346
Winson Chung8c87cd82013-07-23 16:20:10 -0700347 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800348 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
349 * has settled.
350 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700351 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800352 updatePageIndicator();
353 }
354
355 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700356 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000357 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700358 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700359 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700360 protected void pageBeginTransition() {
361 if (!mIsPageInTransition) {
362 mIsPageInTransition = true;
363 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700364 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700365 }
366
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700367 protected void pageEndTransition() {
368 if (mIsPageInTransition) {
369 mIsPageInTransition = false;
370 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700371 }
Michael Jurka0142d492010-08-25 17:46:15 -0700372 }
373
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700374 protected boolean isPageInTransition() {
375 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700376 }
377
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700378 /**
379 * Called when the page starts moving as part of the scroll. Subclasses can override this
380 * to provide custom behavior during animation.
381 */
382 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700383 }
384
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700385 /**
386 * Called when the page ends moving as part of the scroll. Subclasses can override this
387 * to provide custom behavior during animation.
388 */
389 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700390 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700391 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800392 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
393 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Michael Jurka0142d492010-08-25 17:46:15 -0700394 }
395
Vinit Nayaka406f722019-12-19 11:50:55 -0800396 protected int getUnboundedScroll() {
397 return mUnboundedScroll;
398 }
399
400 protected void updateLayoutRotation(int touchRotation) {
401 setLayoutRotation(touchRotation, mDisplayRotation);
402 }
403
404 /** @param touchRotation Must be one of {@link android.view.Surface.ROTATION_0/90/180/270} */
405 public void setLayoutRotation(int touchRotation, int displayRotation) {
406 if (mLayoutRotation == touchRotation && mDisplayRotation == displayRotation) {
407 return;
408 }
409
410 mOrientationState.update(touchRotation, displayRotation);
411 mOrientationHandler = mOrientationState.getOrientationHandler();
412 mLayoutRotation = touchRotation;
413 mDisplayRotation = displayRotation;
414 requestLayout();
415 }
416
417 public PagedViewOrientedState getPagedViewOrientedState() {
418 return mOrientationState;
419 }
420
421 public PagedOrientationHandler getPagedOrientationHandler() {
422 return getPagedViewOrientedState().getOrientationHandler();
423 }
424
425 public void disableMultipleLayoutRotations(boolean disable) {
426 mOrientationState.disableMultipleOrientations(disable);
427 mOrientationHandler = mOrientationState.getOrientationHandler();
428 requestLayout();
Sunny Goyalc86df472016-02-25 09:19:38 -0800429 }
430
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800432 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800433 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800434 }
435
436 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700437 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800438 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
439 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
440 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700441
Vinit Nayaka406f722019-12-19 11:50:55 -0800442 boolean isBeforeFirstPage = mIsRtl ?
443 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
444 boolean isAfterLastPage = mIsRtl ?
445 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
446 if (!isBeforeFirstPage && !isAfterLastPage) {
447 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800448 }
449
Vinit Nayaka406f722019-12-19 11:50:55 -0800450 if (isBeforeFirstPage) {
451 mOrientationHandler.delegateScrollTo(this,
452 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800453 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700454 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800455 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800456 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800457 } else if (isAfterLastPage) {
458 mOrientationHandler.delegateScrollTo(this,
459 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800460 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700461 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800462 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800463 }
464 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700465 if (mWasInOverscroll) {
466 overScroll(0);
467 mWasInOverscroll = false;
468 }
Adam Cohen68d73932010-11-15 10:50:58 -0800469 super.scrollTo(x, y);
470 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800471 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700472
Vinit Nayaka406f722019-12-19 11:50:55 -0800473 /**
474 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
475 */
476 public void superScrollTo(int x, int y) {
477 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700478 }
479
Adam Cohen53805212013-10-01 10:39:23 -0700480 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800481 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700482 if (mCurrentPage != getNextPage()) {
483 AccessibilityEvent ev =
484 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700485 ev.setScrollable(true);
486 ev.setScrollX(getScrollX());
487 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800488 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700489 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700490 }
Adam Cohen53805212013-10-01 10:39:23 -0700491 }
492 }
493
Michael Jurka0142d492010-08-25 17:46:15 -0700494 // we moved this functionality to a helper function so SmoothPagedView can reuse it
495 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800496 return computeScrollHelper(true);
497 }
498
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700499 protected void announcePageForAccessibility() {
500 if (isAccessibilityEnabled(getContext())) {
501 // Notify the user when the page changes
502 announceForAccessibility(getCurrentPageDescription());
503 }
504 }
505
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800506 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700508 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800509 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
510 if (mUnboundedScroll != mScroller.getCurrPos()
511 || currentScroll != mScroller.getCurrPos()) {
512 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700513 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800514 if (shouldInvalidate) {
515 invalidate();
516 }
Michael Jurka0142d492010-08-25 17:46:15 -0700517 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800518 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700519 sendScrollAccessibilityEvent();
520
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700521 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700522 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700523 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700524 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700525
Adam Cohen73aa9752010-11-24 16:26:58 -0800526 // We don't want to trigger a page end moving unless the page has settled
527 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700528 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700529 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800530 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700531
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700532 if (canAnnouncePageDescription()) {
533 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700534 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 }
Michael Jurka0142d492010-08-25 17:46:15 -0700536 return false;
537 }
538
539 @Override
540 public void computeScroll() {
541 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700542 }
543
Sunny Goyalce8809a2018-01-18 14:02:47 -0800544 public int getExpectedHeight() {
545 return getMeasuredHeight();
546 }
547
Adam Cohen410f3cd2013-09-22 12:09:32 -0700548 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800549 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800550 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700551 }
552
Sunny Goyalce8809a2018-01-18 14:02:47 -0800553 public int getExpectedWidth() {
554 return getMeasuredWidth();
555 }
556
Sunny Goyalbc683e92017-12-06 10:25:07 -0800557 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800558 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800559 - mInsets.left - mInsets.right;
560 }
561
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800563 public void requestLayout() {
564 mIsLayoutValid = false;
565 super.requestLayout();
566 }
567
568 @Override
569 public void forceLayout() {
570 mIsLayoutValid = false;
571 super.forceLayout();
572 }
573
574 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700576 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700577 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
578 return;
579 }
580
Adam Cohen7d30a372013-07-01 17:03:59 -0700581 // We measure the dimensions of the PagedView to be larger than the pages so that when we
582 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700583 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
584 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
585 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700586 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700587
Adam Cohen7d30a372013-07-01 17:03:59 -0700588 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
589 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
590 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700591 }
592
Winson Chung8aad6102012-05-11 16:27:49 -0700593 // Return early if we aren't given a proper dimension
594 if (widthSize <= 0 || heightSize <= 0) {
595 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
596 return;
597 }
598
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700600 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700601 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700602
Sunny Goyalac00cba2017-11-13 15:58:01 -0800603 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800604 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800605 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800606 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700607
Sunny Goyalac00cba2017-11-13 15:58:01 -0800608 // measureChildren takes accounts for content padding, we only need to care about extra
609 // space due to insets.
610 measureChildren(myWidthSpec, myHeightSpec);
611 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800612 }
613
Sunny Goyalcf25b522015-07-09 00:01:18 -0700614 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700616 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800617 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700618 final int childCount = getChildCount();
619 boolean pageScrollChanged = false;
620 if (mPageScrolls == null || childCount != mPageScrolls.length) {
621 mPageScrolls = new int[childCount];
622 pageScrollChanged = true;
623 }
624
625 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700626 return;
627 }
628
Winson Chung785d2eb2011-04-14 16:08:02 -0700629 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700630
Vinit Nayaka406f722019-12-19 11:50:55 -0800631 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
632 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700633 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700635
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700636 final LayoutTransition transition = getLayoutTransition();
637 // If the transition is running defer updating max scroll, as some empty pages could
638 // still be present, and a max scroll change could cause sudden jumps in scroll.
639 if (transition != null && transition.isRunning()) {
640 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
641
642 @Override
643 public void startTransition(LayoutTransition transition, ViewGroup container,
644 View view, int transitionType) { }
645
646 @Override
647 public void endTransition(LayoutTransition transition, ViewGroup container,
648 View view, int transitionType) {
649 // Wait until all transitions are complete.
650 if (!transition.isRunning()) {
651 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700652 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700653 }
654 }
655 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700656 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700657 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700658 }
659
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700660 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
661 updateCurrentPageScroll();
662 mFirstLayout = false;
663 }
664
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800665 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700666 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700667 }
Winson Chunge3193b92010-09-10 11:44:42 -0700668 }
669
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700670 /**
671 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
672 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700673 */
674 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
675 ComputePageScrollsLogic scrollLogic) {
676 final int childCount = getChildCount();
677
678 final int startIndex = mIsRtl ? childCount - 1 : 0;
679 final int endIndex = mIsRtl ? -1 : childCount;
680 final int delta = mIsRtl ? -1 : 1;
681
Vinit Nayaka406f722019-12-19 11:50:55 -0800682 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700683
Vinit Nayaka406f722019-12-19 11:50:55 -0800684 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
685 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700686 boolean pageScrollChanged = false;
687
Vinit Nayaka406f722019-12-19 11:50:55 -0800688 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700689 final View child = getPageAt(i);
690 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800691 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
692 pageCenter, layoutChildren);
693 final int primaryDimension = bounds.primaryDimension;
694 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700695
Sunny Goyalc82c6392018-06-06 15:39:13 -0700696 // In case the pages are of different width, align the page to left or right edge
697 // based on the orientation.
698 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800699 ? (childStart - scrollOffsetStart)
700 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700701 if (outPageScrolls[i] != pageScroll) {
702 pageScrollChanged = true;
703 outPageScrolls[i] = pageScroll;
704 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800705 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700706 }
707 }
708 return pageScrollChanged;
709 }
710
Sunny Goyala1fbd842015-05-20 13:40:27 -0700711 protected int getChildGap() {
712 return 0;
713 }
714
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700715 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800716 mMinScroll = computeMinScroll();
717 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700718 }
719
Vinit Nayaka406f722019-12-19 11:50:55 -0800720 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700721 return 0;
Tony50876bf2018-09-19 23:09:14 -0400722 }
723
Vinit Nayaka406f722019-12-19 11:50:55 -0800724 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700725 int childCount = getChildCount();
726 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700727 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700728 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700729 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700730 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700731 }
732 }
733
Adam Cohen3b185e22013-10-29 14:45:58 -0700734 public void setPageSpacing(int pageSpacing) {
735 mPageSpacing = pageSpacing;
736 requestLayout();
737 }
738
Tony50876bf2018-09-19 23:09:14 -0400739 public int getPageSpacing() {
740 return mPageSpacing;
741 }
742
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800743 private void dispatchPageCountChanged() {
744 if (mPageIndicator != null) {
745 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700746 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700747 // This ensures that when children are added, they get the correct transforms / alphas
748 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700749 invalidate();
750 }
751
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800753 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800754 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800755 dispatchPageCountChanged();
756 }
757
758 @Override
759 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800760 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700761 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800762 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700763 }
764
Adam Cohen73894962011-10-31 13:17:17 -0700765 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700766 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800767 View pageAtIndex = getPageAt(index);
768 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700769 }
770
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 @Override
772 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700773 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700774 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700775 if (immediate) {
776 setCurrentPage(page);
777 } else {
778 snapToPage(page);
779 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 return true;
781 }
782 return false;
783 }
784
785 @Override
786 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700787 int focusablePage;
788 if (mNextPage != INVALID_PAGE) {
789 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700791 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 }
Winson Chung86f77532010-08-24 11:08:22 -0700793 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700795 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 }
797 return false;
798 }
799
800 @Override
801 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800802 if (super.dispatchUnhandledMove(focused, direction)) {
803 return true;
804 }
805
806 if (mIsRtl) {
807 if (direction == View.FOCUS_LEFT) {
808 direction = View.FOCUS_RIGHT;
809 } else if (direction == View.FOCUS_RIGHT) {
810 direction = View.FOCUS_LEFT;
811 }
812 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700814 if (getCurrentPage() > 0) {
815 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700816 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 return true;
818 }
819 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700820 if (getCurrentPage() < getPageCount() - 1) {
821 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700822 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 return true;
824 }
825 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800826 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 }
828
829 @Override
830 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700831 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
832 return;
833 }
834
Adam Cohen0ffac432013-07-10 11:19:26 -0700835 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700836 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700837 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700841 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700844 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700845 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 }
847 }
848 }
849
850 /**
851 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700852 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 *
Winson Chung86f77532010-08-24 11:08:22 -0700854 * This happens when live folders requery, and if they're off page, they
855 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 */
857 @Override
858 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 View v = focused;
861 while (true) {
862 if (v == current) {
863 super.focusableViewAvailable(focused);
864 return;
865 }
866 if (v == this) {
867 return;
868 }
869 ViewParent parent = v.getParent();
870 if (parent instanceof View) {
871 v = (View)v.getParent();
872 } else {
873 return;
874 }
875 }
876 }
877
878 /**
879 * {@inheritDoc}
880 */
881 @Override
882 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
883 if (disallowIntercept) {
884 // We need to make sure to cancel our long press if
885 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700886 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700887 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 }
889 super.requestDisallowInterceptTouchEvent(disallowIntercept);
890 }
891
892 @Override
893 public boolean onInterceptTouchEvent(MotionEvent ev) {
894 /*
895 * This method JUST determines whether we want to intercept the motion.
896 * If we return true, onTouchEvent will be called and we do the actual
897 * scrolling there.
898 */
899
Winson Chung45e1d6e2010-11-09 17:19:49 -0800900 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700901 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700902
903 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800904
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 /*
906 * Shortcut the most recurring case: the user is in the dragging
907 * state and he is moving his finger. We want to intercept this
908 * motion.
909 */
910 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700911 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 return true;
913 }
914
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 switch (action & MotionEvent.ACTION_MASK) {
916 case MotionEvent.ACTION_MOVE: {
917 /*
918 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800919 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700921 if (mActivePointerId != INVALID_POINTER) {
922 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700923 }
924 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800925 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700926 // i.e. fall through to the next case (don't break)
927 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
928 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700929 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 }
931
932 case MotionEvent.ACTION_DOWN: {
933 final float x = ev.getX();
934 final float y = ev.getY();
935 // Remember location of down touch
936 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700937 mDownMotionY = y;
Vinit Nayaka406f722019-12-19 11:50:55 -0800938 mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
939 mLastMotionRemainder = 0;
940 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700942
Tony Wickhama0aee212019-09-18 09:24:03 -0700943 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700944
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 break;
946 }
947
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800949 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700950 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 break;
952
953 case MotionEvent.ACTION_POINTER_UP:
954 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800955 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 break;
957 }
958
959 /*
960 * The only time we want to intercept motion events is if we are in the
961 * drag mode.
962 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700963 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 }
965
Tony Wickhama0aee212019-09-18 09:24:03 -0700966 /**
967 * If being flinged and user touches the screen, initiate drag; otherwise don't.
968 */
969 private void updateIsBeingDraggedOnTouchDown() {
970 // mScroller.isFinished should be false when being flinged.
971 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
972 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
973
974 if (finishedScrolling) {
975 mIsBeingDragged = false;
976 if (!mScroller.isFinished() && !mFreeScroll) {
977 setCurrentPage(getNextPage());
978 pageEndTransition();
979 }
980 } else {
981 mIsBeingDragged = true;
982 }
983 }
984
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800985 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700986 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800987 }
988
Adam Cohenf8d28232011-02-01 21:47:00 -0800989 protected void determineScrollingStart(MotionEvent ev) {
990 determineScrollingStart(ev, 1.0f);
991 }
992
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 /*
994 * Determines if we should change the touch state to start scrolling after the
995 * user moves their touch point too far.
996 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800997 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700998 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001000 if (pointerIndex == -1) return;
1001
Vinit Nayaka406f722019-12-19 11:50:55 -08001002 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1003 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001004 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -08001005 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001006
Vinit Nayaka406f722019-12-19 11:50:55 -08001007 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001008 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001009 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001010 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1011 mLastMotion = primaryDirection;
1012 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001013 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001014 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001015 // Stop listening for things like pinches.
1016 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001017 }
1018 }
1019
1020 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001021 // Try canceling the long press. It could also have been scheduled
1022 // by a distant descendant, so use the mAllowLongPress flag to block
1023 // everything
1024 final View currentPage = getPageAt(mCurrentPage);
1025 if (currentPage != null) {
1026 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 }
1028 }
1029
Adam Cohenb5ba0972011-09-07 18:02:31 -07001030 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001031 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001032
Adam Cohenedb40762013-07-18 16:45:45 -07001033 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001034 int count = getChildCount();
1035
1036 final int totalDistance;
1037
1038 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001039 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001040 adjacentPage = page - 1;
1041 }
1042
1043 if (adjacentPage < 0 || adjacentPage > count - 1) {
1044 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1045 } else {
1046 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1047 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001048
1049 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001050 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1051 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001052 return scrollProgress;
1053 }
1054
Adam Cohenedb40762013-07-18 16:45:45 -07001055 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001056 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001057 return 0;
1058 } else {
1059 return mPageScrolls[index];
1060 }
1061 }
1062
Adam Cohen564a2e72013-10-09 14:47:32 -07001063 // While layout transitions are occurring, a child's position may stray from its baseline
1064 // position. This method returns the magnitude of this stray at any given time.
1065 public int getLayoutTransitionOffsetForPage(int index) {
1066 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1067 return 0;
1068 } else {
1069 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001070
Sunny Goyal228153d2018-01-04 15:35:22 -08001071 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1072 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001073 return (int) (child.getX() - baselineX);
1074 }
1075 }
1076
Jon Miranda71cb80c2019-01-24 21:25:13 -08001077 @Override
1078 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001079 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001080 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001081 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001082 super.dispatchDraw(canvas);
1083
1084 canvas.restoreToCount(saveCount);
1085 } else {
1086 super.dispatchDraw(canvas);
1087 }
1088 }
1089
Sunny Goyalb7b01352018-08-09 12:31:20 -07001090 protected void dampedOverScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001091 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001092 if (amount == 0) {
1093 return;
1094 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001095
Vinit Nayaka406f722019-12-19 11:50:55 -08001096 int size = mOrientationHandler.getMeasuredSize(this);
1097 int overScrollAmount = OverScroll.dampedScroll(amount, size);
1098 mSpringOverScroll = overScrollAmount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001099 if (mScroller.isSpringing()) {
1100 invalidate();
1101 return;
1102 }
1103
Vinit Nayaka406f722019-12-19 11:50:55 -08001104 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1105 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1106 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001107 invalidate();
1108 }
1109
Sunny Goyalb7b01352018-08-09 12:31:20 -07001110 protected void overScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001111 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001112 if (mScroller.isSpringing()) {
1113 invalidate();
1114 return;
1115 }
1116
Sunny Goyalb7b01352018-08-09 12:31:20 -07001117 if (amount == 0) return;
1118
1119 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001120 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1121 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001122 } else {
1123 dampedOverScroll(amount);
1124 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001125 }
1126
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001127
Tony Wickham023f4042019-01-29 10:52:31 -08001128 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001129 if (mFreeScroll == freeScroll) {
1130 return;
1131 }
1132
Tony Wickham8f7ead32016-04-07 18:46:44 -07001133 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001134 mFreeScroll = freeScroll;
1135
Adam Cohenf9618852013-11-08 06:45:03 -08001136 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001137 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001138 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001139 if (getScrollForPage(getNextPage()) != getScrollX()) {
1140 snapToPage(getNextPage());
1141 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001142 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001143 }
1144
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001145 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001146 mAllowOverScroll = enable;
1147 }
1148
Winson Chung321e9ee2010-08-09 13:37:56 -07001149 @Override
1150 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001151 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001152 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001153
Michael Jurkab8f06722010-10-10 15:58:46 -07001154 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001155
1156 final int action = ev.getAction();
1157
1158 switch (action & MotionEvent.ACTION_MASK) {
1159 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001160 updateIsBeingDraggedOnTouchDown();
1161
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 /*
1163 * If being flinged and user touches, stop the fling. isFinished
1164 * will be false if being flinged.
1165 */
1166 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001167 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 }
1169
1170 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001171 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001172 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001173 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1174 mLastMotionRemainder = 0;
1175 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001177 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001178 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001179 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001180 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 break;
1182
Vinit Nayaka406f722019-12-19 11:50:55 -08001183 case MotionEvent.ACTION_MOVE:
1184 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 // Scroll to follow the motion event
1186 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001187
1188 if (pointerIndex == -1) return true;
1189
Vinit Nayaka406f722019-12-19 11:50:55 -08001190 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1191 float delta = mLastMotion + mLastMotionRemainder - direction;
1192 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001193
Winson Chungc0844aa2011-02-02 15:25:58 -08001194 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1195 // keep the remainder because we are actually testing if we've moved from the last
1196 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001197 if (Math.abs(delta) >= 1.0f) {
1198 mLastMotion = direction;
1199 mLastMotionRemainder = delta - (int) delta;
1200
1201 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 } else {
1203 awakenScrollBars();
1204 }
Adam Cohen564976a2010-10-13 18:52:07 -07001205 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 determineScrollingStart(ev);
1207 }
1208 break;
1209
1210 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001211 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 final int activePointerId = mActivePointerId;
1213 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001214 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1215 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001216 final VelocityTracker velocityTracker = mVelocityTracker;
1217 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001218
Vinit Nayaka406f722019-12-19 11:50:55 -08001219 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1220 mActivePointerId);
1221 int delta = (int) (primaryDirection - mDownMotionPrimary);
1222 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1223
1224 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1225 SIGNIFICANT_MOVE_THRESHOLD;
1226
1227 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1228 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1229 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1230 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001231
Adam Cohenf358a4b2013-07-23 16:47:31 -07001232 if (!mFreeScroll) {
1233 // In the case that the page is moved far to one direction and then is flung
1234 // in the opposite direction, we use a threshold to determine whether we should
1235 // just return to the starting page, or if we should skip one further.
1236 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001237 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1238 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001239 returnToOriginalPage = true;
1240 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001241
Adam Cohenf358a4b2013-07-23 16:47:31 -07001242 int finalPage;
1243 // We give flings precedence over large moves, which is why we short-circuit our
1244 // test for a large move if a fling has been registered. That is, a large
1245 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001246
Vinit Nayaka406f722019-12-19 11:50:55 -08001247 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1248 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001249 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001250 snapToPageWithVelocity(finalPage, velocity);
1251 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1252 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001253 mCurrentPage < getChildCount() - 1) {
1254 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001255 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 } else {
1257 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001258 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001260 if (!mScroller.isFinished()) {
1261 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001263
Vinit Nayaka406f722019-12-19 11:50:55 -08001264 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1265 int maxScroll = mMaxScroll;
1266 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001267
Vinit Nayaka406f722019-12-19 11:50:55 -08001268 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1269 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1270 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001271 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001272 } else {
1273 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001274 mScroller.fling(initialScroll, -velocity,
1275 minScroll, maxScroll,
1276 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001277
Vinit Nayaka406f722019-12-19 11:50:55 -08001278 int finalPos = mScroller.getFinalPos();
1279 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001280
1281 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1282 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001283 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001284 // If scrolling ends in the half of the added space that is closer to
1285 // the end, settle to the end. Otherwise snap to the nearest page.
1286 // If flinging past one of the ends, don't change the velocity as it
1287 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001288 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1289 ? minScroll
1290 : finalPos > (lastPageScroll + maxScroll) / 2
1291 ? maxScroll
1292 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001293
Vinit Nayaka406f722019-12-19 11:50:55 -08001294 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001295 // Ensure the scroll/snap doesn't happen too fast;
1296 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001297 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001298 if (extraScrollDuration > 0) {
1299 mScroller.extendDuration(extraScrollDuration);
1300 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001301 }
1302 }
1303 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001305 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001307
Adam Cohen7d30a372013-07-01 17:03:59 -07001308 // End any intermediate reordering states
1309 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 break;
1311
1312 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001313 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001314 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001315 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001316 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 break;
1319
1320 case MotionEvent.ACTION_POINTER_UP:
1321 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001322 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001323 break;
1324 }
1325
1326 return true;
1327 }
1328
Vinit Nayaka406f722019-12-19 11:50:55 -08001329 protected boolean shouldFlingForVelocity(int velocity) {
1330 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001331 }
1332
Adam Cohen7d30a372013-07-01 17:03:59 -07001333 private void resetTouchState() {
1334 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001335 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001336 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001337 }
1338
Adam Cohenc2d6e892014-10-16 09:49:24 -07001339 /**
1340 * Triggered by scrolling via touch
1341 */
1342 protected void onScrollInteractionBegin() {
1343 }
1344
1345 protected void onScrollInteractionEnd() {
1346 }
1347
Winson Chung185d7162011-02-28 13:47:29 -08001348 @Override
1349 public boolean onGenericMotionEvent(MotionEvent event) {
1350 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1351 switch (event.getAction()) {
1352 case MotionEvent.ACTION_SCROLL: {
Samuel Fufa9fd21052019-08-28 17:06:51 -07001353 Launcher launcher = Launcher.getLauncher(getContext());
1354 if (launcher != null) {
1355 AbstractFloatingView.closeAllOpenViews(launcher);
1356 }
Winson Chung185d7162011-02-28 13:47:29 -08001357 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1358 final float vscroll;
1359 final float hscroll;
1360 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1361 vscroll = 0;
1362 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1363 } else {
1364 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1365 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1366 }
Samuel Fufa59cba192019-08-22 18:31:34 -07001367 if (Math.abs(vscroll) > Math.abs(hscroll) && !isVerticalScrollable()) {
1368 return true;
1369 }
Winson Chung185d7162011-02-28 13:47:29 -08001370 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001371 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001372 : (hscroll > 0 || vscroll > 0);
1373 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001374 scrollRight();
1375 } else {
1376 scrollLeft();
1377 }
1378 return true;
1379 }
1380 }
1381 }
1382 }
1383 return super.onGenericMotionEvent(event);
1384 }
1385
Samuel Fufa59cba192019-08-22 18:31:34 -07001386 protected boolean isVerticalScrollable() {
1387 return true;
1388 }
1389
Michael Jurkab8f06722010-10-10 15:58:46 -07001390 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1391 if (mVelocityTracker == null) {
1392 mVelocityTracker = VelocityTracker.obtain();
1393 }
1394 mVelocityTracker.addMovement(ev);
1395 }
1396
1397 private void releaseVelocityTracker() {
1398 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001399 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001400 mVelocityTracker.recycle();
1401 mVelocityTracker = null;
1402 }
1403 }
1404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 private void onSecondaryPointerUp(MotionEvent ev) {
1406 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1407 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1408 final int pointerId = ev.getPointerId(pointerIndex);
1409 if (pointerId == mActivePointerId) {
1410 // This was our active pointer going up. Choose a new
1411 // active pointer and adjust accordingly.
1412 // TODO: Make this decision more intelligent.
1413 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001414 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1415 newPointerIndex);
1416 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001417 mActivePointerId = ev.getPointerId(newPointerIndex);
1418 if (mVelocityTracker != null) {
1419 mVelocityTracker.clear();
1420 }
1421 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001422 }
1423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 @Override
1425 public void requestChildFocus(View child, View focused) {
1426 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001427 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001428 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001429 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 }
1431 }
1432
Sunny Goyal228153d2018-01-04 15:35:22 -08001433 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001434 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001435 }
1436
Vinit Nayaka406f722019-12-19 11:50:55 -08001437 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1438 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1439 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001440 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 final int childCount = getChildCount();
1443 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001444 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001445 int childSize = mOrientationHandler.getMeasuredSize(layout);
1446 int halfChildSize = (childSize / 2);
1447 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1449 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1450 minDistanceFromScreenCenter = distanceFromScreenCenter;
1451 minDistanceFromScreenCenterIndex = i;
1452 }
1453 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001454 return minDistanceFromScreenCenterIndex;
1455 }
1456
1457 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001458 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1459 }
1460
1461 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001462 int scroll = mOrientationHandler.getPrimaryScroll(this);
1463 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001464 }
1465
1466 protected int getPageSnapDuration() {
1467 if (isInOverScroll()) {
1468 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1469 }
1470 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001471 }
1472
Adam Cohene0f66b52010-11-23 15:06:07 -08001473 // We want the duration of the page snap animation to be influenced by the distance that
1474 // the screen has to travel, however, we don't want this duration to be effected in a
1475 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1476 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001477 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001478 f -= 0.5f; // center the values about 0.
1479 f *= 0.3f * Math.PI / 2.0f;
1480 return (float) Math.sin(f);
1481 }
1482
Winson Chung05a31ed2018-02-08 17:08:43 -08001483 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001484 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001485 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001486
Vinit Nayaka406f722019-12-19 11:50:55 -08001487 final int newLoc = getScrollForPage(whichPage);
1488 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001489 int duration = 0;
1490
Sunny Goyal4d113a52015-05-27 10:05:28 -07001491 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001492 // If the velocity is low enough, then treat this more as an automatic page advance
1493 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001494 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001495 }
1496
1497 // Here we compute a "distance" that will be used in the computation of the overall
1498 // snap duration. This is a function of the actual distance that needs to be traveled;
1499 // we keep this value close to half screen size in order to reduce the variance in snap
1500 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001501 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001502 float distance = halfScreenSize + halfScreenSize *
1503 distanceInfluenceForSnapDuration(distanceRatio);
1504
1505 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001506 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001507
1508 // we want the page's snap velocity to approximately match the velocity at which the
1509 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001510 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1511 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001512
Jon Miranda9e198662020-03-11 14:42:02 -07001513 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001514 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001515 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001516 } else {
1517 return snapToPage(whichPage, delta, duration);
1518 }
Michael Jurka0142d492010-08-25 17:46:15 -07001519 }
1520
Winson Chung05a31ed2018-02-08 17:08:43 -08001521 public boolean snapToPage(int whichPage) {
1522 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001523 }
1524
Winson Chung05a31ed2018-02-08 17:08:43 -08001525 public boolean snapToPageImmediately(int whichPage) {
1526 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001527 }
1528
Winson Chung05a31ed2018-02-08 17:08:43 -08001529 public boolean snapToPage(int whichPage, int duration) {
1530 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 }
1532
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001533 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001534 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001535 }
1536
Winson Chung05a31ed2018-02-08 17:08:43 -08001537 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001538 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001539 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001540
Vinit Nayaka406f722019-12-19 11:50:55 -08001541 int newLoc = getScrollForPage(whichPage);
1542 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001543 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001544 }
1545
Winson Chung05a31ed2018-02-08 17:08:43 -08001546 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001547 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001548 }
Michael Jurka0142d492010-08-25 17:46:15 -07001549
Winson Chung05a31ed2018-02-08 17:08:43 -08001550 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001551 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001552 if (mFirstLayout) {
1553 setCurrentPage(whichPage);
1554 return false;
1555 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001556
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001557 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001558 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1559 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001560 }
1561
Adam Cohen6f127a62014-06-12 14:54:41 -07001562 whichPage = validateNewPage(whichPage);
1563
Adam Cohen7d30a372013-07-01 17:03:59 -07001564 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001565
Winson Chung321e9ee2010-08-09 13:37:56 -07001566 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001567 if (immediate) {
1568 duration = 0;
1569 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 duration = Math.abs(delta);
1571 }
1572
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001573 if (duration != 0) {
1574 pageBeginTransition();
1575 }
1576
Adam Cohenf358a4b2013-07-23 16:47:31 -07001577 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001578 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001579 }
Adam Cohenf9618852013-11-08 06:45:03 -08001580
1581 if (interpolator != null) {
1582 mScroller.setInterpolator(interpolator);
1583 } else {
1584 mScroller.setInterpolator(mDefaultInterpolator);
1585 }
1586
Jon Miranda71cb80c2019-01-24 21:25:13 -08001587 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001588 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001589 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001590 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001591 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001592
Adam Cohen674531f2013-12-13 15:07:14 -08001593 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001594
1595 // Trigger a compute() to finish switching pages if necessary
1596 if (immediate) {
1597 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001598 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001599 }
1600
Winson Chung321e9ee2010-08-09 13:37:56 -07001601 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001602 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001603 }
1604
Vadim Tryshev98913d02018-05-18 18:41:34 -07001605 public boolean scrollLeft() {
1606 if (getNextPage() > 0) {
1607 snapToPage(getNextPage() - 1);
1608 return true;
1609 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001610 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001611 }
1612
Vadim Tryshev98913d02018-05-18 18:41:34 -07001613 public boolean scrollRight() {
1614 if (getNextPage() < getChildCount() - 1) {
1615 snapToPage(getNextPage() + 1);
1616 return true;
1617 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001618 return onOverscroll(getMeasuredWidth());
1619 }
1620
1621 protected boolean onOverscroll(int amount) {
1622 if (!mAllowOverScroll) return false;
1623 onScrollInteractionBegin();
1624 overScroll(amount);
1625 onScrollInteractionEnd();
1626 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001627 }
1628
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001629 @Override
1630 public CharSequence getAccessibilityClassName() {
1631 // Some accessibility services have special logic for ScrollView. Since we provide same
1632 // accessibility info as ScrollView, inform the service to handle use the same way.
1633 return ScrollView.class.getName();
1634 }
1635
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001636 protected boolean isPageOrderFlipped() {
1637 return false;
1638 }
1639
Winson Chung6a0f57d2011-06-29 20:10:49 -07001640 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001641 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001642 @Override
1643 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1644 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001645 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001646 int offset = (mAllowOverScroll ? 0 : 1);
1647 info.setScrollable(getPageCount() > offset);
1648 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001649 info.addAction(pagesFlipped ?
1650 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1651 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1652 info.addAction(mIsRtl ?
1653 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1654 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001655 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001656 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001657 info.addAction(pagesFlipped ?
1658 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1659 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1660 info.addAction(mIsRtl ?
1661 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1662 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001663 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001664 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1665 // Besides disabling the accessibility long-click, this also prevents this view from getting
1666 // accessibility focus.
1667 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001668 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001669 }
1670
1671 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001672 public void sendAccessibilityEvent(int eventType) {
1673 // Don't let the view send real scroll events.
1674 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1675 super.sendAccessibilityEvent(eventType);
1676 }
1677 }
1678
1679 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001680 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1681 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001682 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001683 }
1684
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001685 @Override
1686 public boolean performAccessibilityAction(int action, Bundle arguments) {
1687 if (super.performAccessibilityAction(action, arguments)) {
1688 return true;
1689 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001690 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001691 switch (action) {
1692 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001693 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001694 return true;
1695 }
1696 } break;
1697 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001698 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001699 return true;
1700 }
yingleiw02cc8482019-08-02 16:14:27 -07001701 } break;
1702 case android.R.id.accessibilityActionPageRight: {
1703 if (!mIsRtl) {
1704 return scrollRight();
1705 } else {
1706 return scrollLeft();
1707 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001708 }
yingleiw02cc8482019-08-02 16:14:27 -07001709 case android.R.id.accessibilityActionPageLeft: {
1710 if (!mIsRtl) {
1711 return scrollLeft();
1712 } else {
1713 return scrollRight();
1714 }
1715 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001716 }
1717 return false;
1718 }
1719
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001720 protected boolean canAnnouncePageDescription() {
1721 return true;
1722 }
1723
Adam Cohen0ffac432013-07-10 11:19:26 -07001724 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001725 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001726 getNextPage() + 1, getChildCount());
1727 }
1728
Tony Mak2e564402018-02-27 17:19:34 +00001729 protected float getDownMotionX() {
1730 return mDownMotionX;
1731 }
1732
1733 protected float getDownMotionY() {
1734 return mDownMotionY;
1735 }
1736
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001737 protected interface ComputePageScrollsLogic {
1738
1739 boolean shouldIncludeView(View view);
1740 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001741
1742 public int[] getVisibleChildrenRange() {
1743 float visibleLeft = 0;
1744 float visibleRight = visibleLeft + getMeasuredWidth();
1745 float scaleX = getScaleX();
1746 if (scaleX < 1 && scaleX > 0) {
1747 float mid = getMeasuredWidth() / 2;
1748 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1749 visibleRight = mid + ((visibleRight - mid) / scaleX);
1750 }
1751
1752 int leftChild = -1;
1753 int rightChild = -1;
1754 final int childCount = getChildCount();
1755 for (int i = 0; i < childCount; i++) {
1756 final View child = getPageAt(i);
1757
1758 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1759 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1760 if (leftChild == -1) {
1761 leftChild = i;
1762 }
1763 rightChild = i;
1764 }
1765 }
1766 mTmpIntPair[0] = leftChild;
1767 mTmpIntPair[1] = rightChild;
1768 return mTmpIntPair;
1769 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001770}