blob: dc1ff6695b8256bad9c8b1abe69ccbd244bfeec9 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyal8b37c572020-03-31 12:12:14 -070019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
22import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
23import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
24import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
25import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
26
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070027import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070031import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080032import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070034import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070035import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
48import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070050import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
Tony Wickham62117d72020-03-26 16:56:26 -070052import androidx.annotation.Nullable;
53
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.anim.Interpolators;
55import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
58import com.android.launcher3.touch.OverScroll;
59import com.android.launcher3.touch.PagedOrientationHandler;
60import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Jon Miranda9e198662020-03-11 14:42:02 -070061import com.android.launcher3.util.OverScroller;
62import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070063import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070064
65import java.util.ArrayList;
66
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080071public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070074
Sunny Goyala7a5bf32020-01-05 15:35:29 +053075 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070076 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070077
Vadim Tryshevfedca432015-08-19 17:55:02 -070078 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Sunny Goyalb72d8b22017-07-14 00:02:27 -070080 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070081 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Sunny Goyal8e2133b2015-05-06 13:39:07 -070087 private static final float MAX_SCROLL_PROGRESS = 1.0f;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 // The following constants need to be scaled based on density. The scaled versions will be
90 // assigned to the corresponding member variables below.
91 private static final int FLING_THRESHOLD_VELOCITY = 500;
92 private static final int MIN_SNAP_VELOCITY = 1500;
93 private static final int MIN_FLING_VELOCITY = 250;
94
Adam Cohenf358a4b2013-07-23 16:47:31 -070095 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070096
Adam Cohen265b9a62011-12-07 14:37:18 -080097 protected int mFlingThresholdVelocity;
98 protected int mMinFlingVelocity;
99 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700100
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected boolean mFirstLayout = true;
102
Sunny Goyal4ffec482016-02-09 11:28:52 -0800103 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700105
Sunny Goyal4ffec482016-02-09 11:28:52 -0800106 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800108 protected int mMaxScroll;
109 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700110 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800111 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800113 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
115 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700116 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800117 private float mDownMotionPrimary;
118 private float mLastMotion;
119 private float mLastMotionRemainder;
120 private float mTotalMotion;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700121 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700122
Vadim Trysheve628c472018-05-16 14:28:18 -0700123 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700124 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800125
Michael Jurka7426c422010-11-11 15:23:47 -0800126 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800128 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Michael Jurka5f1c5092010-09-03 14:15:02 -0700130 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Michael Jurka5f1c5092010-09-03 14:15:02 -0700132 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700134 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700135 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700136
Vinit Nayaka406f722019-12-19 11:50:55 -0800137 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800138
Sunny Goyal061380a2016-02-29 15:15:03 -0800139 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700140
Vinit Nayaka406f722019-12-19 11:50:55 -0800141 protected int mUnboundedScroll;
142
Winson Chungd2be3812013-07-16 11:11:32 -0700143 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700144 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800145 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700146
John Spurlock77e1f472013-09-11 10:09:51 -0400147 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800148 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400149
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800150 // Similar to the platform implementation of isLayoutValid();
151 protected boolean mIsLayoutValid;
152
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700153 private int[] mTmpIntPair = new int[2];
154
Winson Chung321e9ee2010-08-09 13:37:56 -0700155 public PagedView(Context context) {
156 this(context, null);
157 }
158
159 public PagedView(Context context, AttributeSet attrs) {
160 this(context, attrs, 0);
161 }
162
163 public PagedView(Context context, AttributeSet attrs, int defStyle) {
164 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700165
Adam Cohen9c4949e2010-10-05 12:27:22 -0700166 TypedArray a = context.obtainStyledAttributes(attrs,
167 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700168 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700169 a.recycle();
170
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700172 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700173 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 }
175
176 /**
177 * Initializes various states for this workspace.
178 */
Michael Jurka0142d492010-08-25 17:46:15 -0700179 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800180 Context context = getContext();
181 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700182 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700183 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Vinit Nayaka406f722019-12-19 11:50:55 -0800185 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700186 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800188
Sunny Goyalcf25b522015-07-09 00:01:18 -0700189 float density = getResources().getDisplayMetrics().density;
190 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
191 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
192 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700193
194 if (Utilities.ATLEAST_OREO) {
195 setDefaultFocusHighlightEnabled(false);
196 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198
Adam Cohenf9618852013-11-08 06:45:03 -0800199 protected void setDefaultInterpolator(Interpolator interpolator) {
200 mDefaultInterpolator = interpolator;
201 mScroller.setInterpolator(mDefaultInterpolator);
202 }
203
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700204 public void initParentViews(View parent) {
205 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800206 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700207 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700208 }
209 }
210
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800211 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700212 return mPageIndicator;
213 }
214
Adam Cohen674531f2013-12-13 15:07:14 -0800215 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700216 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
217 * that the user was on before entering free scroll mode (e.g. the home screen page they
218 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
219 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700221 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700222 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700224
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700225 /**
226 * Returns the index of page to be shown immediately afterwards.
227 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700228 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700229 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
230 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700231
Adam Cohenf9c184a2016-01-15 16:47:43 -0800232 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 return getChildCount();
234 }
235
Sunny Goyal83a8f042015-05-19 12:52:12 -0700236 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 return getChildAt(index);
238 }
239
Adam Cohenae4f1552011-10-20 00:15:42 -0700240 protected int indexToPage(int index) {
241 return index;
242 }
243
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800245 * Updates the scroll of the current page immediately to its final scroll position. We use this
246 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
247 * the previous tab page.
248 */
249 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700250 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800251 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700252 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800253 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
256 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700257 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800258 }
259
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700260 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700261 mScroller.abortAnimation();
262 // We need to clean up the next page here to avoid computeScrollHelper from
263 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700264 if (resetNextPage) {
265 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800266 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700267 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700268 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700269
Tony Wickham8f7ead32016-04-07 18:46:44 -0700270 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271 mScroller.forceFinished(true);
272 // We need to clean up the next page here to avoid computeScrollHelper from
273 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700274 if (resetNextPage) {
275 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800276 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700277 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700278 }
279
Adam Cohen6f127a62014-06-12 14:54:41 -0700280 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700281 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700282 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700283 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700284 }
285
Chet Haasebc2f0822012-10-26 17:59:53 -0700286 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700287 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
288 */
289 private int ensureWithinScrollBounds(int page) {
290 int dir = !mIsRtl ? 1 : - 1;
291 int currScroll = getScrollForPage(page);
292 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800293 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700294 page += dir;
295 prevScroll = currScroll;
296 currScroll = getScrollForPage(page);
297 if (currScroll <= prevScroll) {
298 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
299 break;
300 }
301 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800302 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700303 page -= dir;
304 prevScroll = currScroll;
305 currScroll = getScrollForPage(page);
306 if (currScroll >= prevScroll) {
307 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
308 break;
309 }
310 }
311 return page;
312 }
313
Tony Wickham03f27012019-04-25 14:22:54 -0700314 public void setCurrentPage(int currentPage) {
315 setCurrentPage(currentPage, INVALID_PAGE);
316 }
317
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700318 /**
Winson Chung86f77532010-08-24 11:08:22 -0700319 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 */
Tony Wickham03f27012019-04-25 14:22:54 -0700321 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800322 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700323 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800324 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800325 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
326 // the default
327 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800328 return;
329 }
Tony Wickham03f27012019-04-25 14:22:54 -0700330 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700331 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700332 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700333 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800334 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 }
336
Winson Chung8c87cd82013-07-23 16:20:10 -0700337 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800338 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
339 * has settled.
340 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700341 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800342 updatePageIndicator();
343 }
344
345 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700346 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000347 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700348 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700350 protected void pageBeginTransition() {
351 if (!mIsPageInTransition) {
352 mIsPageInTransition = true;
353 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700354 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700355 }
356
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700357 protected void pageEndTransition() {
358 if (mIsPageInTransition) {
359 mIsPageInTransition = false;
360 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700361 }
Michael Jurka0142d492010-08-25 17:46:15 -0700362 }
363
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700364 protected boolean isPageInTransition() {
365 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700366 }
367
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700368 /**
369 * Called when the page starts moving as part of the scroll. Subclasses can override this
370 * to provide custom behavior during animation.
371 */
372 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700373 }
374
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700375 /**
376 * Called when the page ends moving as part of the scroll. Subclasses can override this
377 * to provide custom behavior during animation.
378 */
379 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700380 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700381 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800382 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
383 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700384 if (mOnPageTransitionEndCallback != null) {
385 mOnPageTransitionEndCallback.run();
386 mOnPageTransitionEndCallback = null;
387 }
388 }
389
390 /**
391 * Sets a callback to run once when the scrolling finishes. If there is currently
392 * no page in transition, then the callback is called immediately.
393 */
394 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
395 if (mIsPageInTransition || callback == null) {
396 mOnPageTransitionEndCallback = callback;
397 } else {
398 callback.run();
399 }
Michael Jurka0142d492010-08-25 17:46:15 -0700400 }
401
Vinit Nayaka406f722019-12-19 11:50:55 -0800402 protected int getUnboundedScroll() {
403 return mUnboundedScroll;
404 }
405
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800407 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800408 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800409 }
410
411 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700412 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800413 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
414 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
415 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700416
Vinit Nayaka406f722019-12-19 11:50:55 -0800417 boolean isBeforeFirstPage = mIsRtl ?
418 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
419 boolean isAfterLastPage = mIsRtl ?
420 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
421 if (!isBeforeFirstPage && !isAfterLastPage) {
422 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800423 }
424
Vinit Nayaka406f722019-12-19 11:50:55 -0800425 if (isBeforeFirstPage) {
426 mOrientationHandler.delegateScrollTo(this,
427 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800428 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700429 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800430 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800431 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800432 } else if (isAfterLastPage) {
433 mOrientationHandler.delegateScrollTo(this,
434 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800435 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700436 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800437 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800438 }
439 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700440 if (mWasInOverscroll) {
441 overScroll(0);
442 mWasInOverscroll = false;
443 }
Adam Cohen68d73932010-11-15 10:50:58 -0800444 super.scrollTo(x, y);
445 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800446 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700447
Vinit Nayaka406f722019-12-19 11:50:55 -0800448 /**
449 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
450 */
451 public void superScrollTo(int x, int y) {
452 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700453 }
454
Adam Cohen53805212013-10-01 10:39:23 -0700455 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800456 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700457 if (mCurrentPage != getNextPage()) {
458 AccessibilityEvent ev =
459 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700460 ev.setScrollable(true);
461 ev.setScrollX(getScrollX());
462 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800463 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700464 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700465 }
Adam Cohen53805212013-10-01 10:39:23 -0700466 }
467 }
468
Michael Jurka0142d492010-08-25 17:46:15 -0700469 // we moved this functionality to a helper function so SmoothPagedView can reuse it
470 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800471 return computeScrollHelper(true);
472 }
473
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700474 protected void announcePageForAccessibility() {
475 if (isAccessibilityEnabled(getContext())) {
476 // Notify the user when the page changes
477 announceForAccessibility(getCurrentPageDescription());
478 }
479 }
480
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800481 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700483 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800484 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
485 if (mUnboundedScroll != mScroller.getCurrPos()
486 || currentScroll != mScroller.getCurrPos()) {
487 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700488 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800489 if (shouldInvalidate) {
490 invalidate();
491 }
Michael Jurka0142d492010-08-25 17:46:15 -0700492 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800493 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700494 sendScrollAccessibilityEvent();
495
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700496 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700497 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700498 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700499 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700500
Adam Cohen73aa9752010-11-24 16:26:58 -0800501 // We don't want to trigger a page end moving unless the page has settled
502 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700503 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700504 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800505 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700506
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700507 if (canAnnouncePageDescription()) {
508 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700509 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 }
Michael Jurka0142d492010-08-25 17:46:15 -0700511 return false;
512 }
513
514 @Override
515 public void computeScroll() {
516 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 }
518
Sunny Goyalce8809a2018-01-18 14:02:47 -0800519 public int getExpectedHeight() {
520 return getMeasuredHeight();
521 }
522
Adam Cohen410f3cd2013-09-22 12:09:32 -0700523 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800524 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800525 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700526 }
527
Sunny Goyalce8809a2018-01-18 14:02:47 -0800528 public int getExpectedWidth() {
529 return getMeasuredWidth();
530 }
531
Sunny Goyalbc683e92017-12-06 10:25:07 -0800532 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800533 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800534 - mInsets.left - mInsets.right;
535 }
536
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800538 public void requestLayout() {
539 mIsLayoutValid = false;
540 super.requestLayout();
541 }
542
543 @Override
544 public void forceLayout() {
545 mIsLayoutValid = false;
546 super.forceLayout();
547 }
548
549 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700551 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700552 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
553 return;
554 }
555
Adam Cohen7d30a372013-07-01 17:03:59 -0700556 // We measure the dimensions of the PagedView to be larger than the pages so that when we
557 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700558 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
559 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
560 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700561 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700562
Adam Cohen7d30a372013-07-01 17:03:59 -0700563 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
564 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
565 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 }
567
Winson Chung8aad6102012-05-11 16:27:49 -0700568 // Return early if we aren't given a proper dimension
569 if (widthSize <= 0 || heightSize <= 0) {
570 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
571 return;
572 }
573
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700575 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700576 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700577
Sunny Goyalac00cba2017-11-13 15:58:01 -0800578 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800579 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800580 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800581 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700582
Sunny Goyalac00cba2017-11-13 15:58:01 -0800583 // measureChildren takes accounts for content padding, we only need to care about extra
584 // space due to insets.
585 measureChildren(myWidthSpec, myHeightSpec);
586 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800587 }
588
Sunny Goyalcf25b522015-07-09 00:01:18 -0700589 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700590 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700591 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800592 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700593 final int childCount = getChildCount();
594 boolean pageScrollChanged = false;
595 if (mPageScrolls == null || childCount != mPageScrolls.length) {
596 mPageScrolls = new int[childCount];
597 pageScrollChanged = true;
598 }
599
600 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700601 return;
602 }
603
Winson Chung785d2eb2011-04-14 16:08:02 -0700604 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700605
Vinit Nayaka406f722019-12-19 11:50:55 -0800606 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
607 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700608 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700610
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700611 final LayoutTransition transition = getLayoutTransition();
612 // If the transition is running defer updating max scroll, as some empty pages could
613 // still be present, and a max scroll change could cause sudden jumps in scroll.
614 if (transition != null && transition.isRunning()) {
615 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
616
617 @Override
618 public void startTransition(LayoutTransition transition, ViewGroup container,
619 View view, int transitionType) { }
620
621 @Override
622 public void endTransition(LayoutTransition transition, ViewGroup container,
623 View view, int transitionType) {
624 // Wait until all transitions are complete.
625 if (!transition.isRunning()) {
626 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700627 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700628 }
629 }
630 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700631 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700632 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700633 }
634
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700635 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
636 updateCurrentPageScroll();
637 mFirstLayout = false;
638 }
639
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800640 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700641 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700642 }
Winson Chunge3193b92010-09-10 11:44:42 -0700643 }
644
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700645 /**
646 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
647 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700648 */
649 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
650 ComputePageScrollsLogic scrollLogic) {
651 final int childCount = getChildCount();
652
653 final int startIndex = mIsRtl ? childCount - 1 : 0;
654 final int endIndex = mIsRtl ? -1 : childCount;
655 final int delta = mIsRtl ? -1 : 1;
656
Vinit Nayaka406f722019-12-19 11:50:55 -0800657 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700658
Vinit Nayaka406f722019-12-19 11:50:55 -0800659 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
660 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700661 boolean pageScrollChanged = false;
662
Vinit Nayaka406f722019-12-19 11:50:55 -0800663 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700664 final View child = getPageAt(i);
665 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800666 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
667 pageCenter, layoutChildren);
668 final int primaryDimension = bounds.primaryDimension;
669 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700670
Sunny Goyalc82c6392018-06-06 15:39:13 -0700671 // In case the pages are of different width, align the page to left or right edge
672 // based on the orientation.
673 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800674 ? (childStart - scrollOffsetStart)
675 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700676 if (outPageScrolls[i] != pageScroll) {
677 pageScrollChanged = true;
678 outPageScrolls[i] = pageScroll;
679 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800680 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700681 }
682 }
683 return pageScrollChanged;
684 }
685
Sunny Goyala1fbd842015-05-20 13:40:27 -0700686 protected int getChildGap() {
687 return 0;
688 }
689
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700690 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800691 mMinScroll = computeMinScroll();
692 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700693 }
694
Vinit Nayaka406f722019-12-19 11:50:55 -0800695 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700696 return 0;
Tony50876bf2018-09-19 23:09:14 -0400697 }
698
Vinit Nayaka406f722019-12-19 11:50:55 -0800699 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700700 int childCount = getChildCount();
701 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700702 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700703 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700704 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700705 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700706 }
707 }
708
Adam Cohen3b185e22013-10-29 14:45:58 -0700709 public void setPageSpacing(int pageSpacing) {
710 mPageSpacing = pageSpacing;
711 requestLayout();
712 }
713
Tony50876bf2018-09-19 23:09:14 -0400714 public int getPageSpacing() {
715 return mPageSpacing;
716 }
717
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800718 private void dispatchPageCountChanged() {
719 if (mPageIndicator != null) {
720 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700721 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700722 // This ensures that when children are added, they get the correct transforms / alphas
723 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700724 invalidate();
725 }
726
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800728 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800729 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800730 dispatchPageCountChanged();
731 }
732
733 @Override
734 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800735 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700736 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800737 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700738 }
739
Adam Cohen73894962011-10-31 13:17:17 -0700740 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700741 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800742 View pageAtIndex = getPageAt(index);
743 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700744 }
745
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 @Override
747 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700748 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700749 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700750 if (immediate) {
751 setCurrentPage(page);
752 } else {
753 snapToPage(page);
754 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 return true;
756 }
757 return false;
758 }
759
760 @Override
761 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700762 int focusablePage;
763 if (mNextPage != INVALID_PAGE) {
764 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700766 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 }
Winson Chung86f77532010-08-24 11:08:22 -0700768 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700770 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 }
772 return false;
773 }
774
775 @Override
776 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800777 if (super.dispatchUnhandledMove(focused, direction)) {
778 return true;
779 }
780
781 if (mIsRtl) {
782 if (direction == View.FOCUS_LEFT) {
783 direction = View.FOCUS_RIGHT;
784 } else if (direction == View.FOCUS_RIGHT) {
785 direction = View.FOCUS_LEFT;
786 }
787 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700789 if (getCurrentPage() > 0) {
790 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700791 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 return true;
793 }
794 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700795 if (getCurrentPage() < getPageCount() - 1) {
796 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700797 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 return true;
799 }
800 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800801 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 }
803
804 @Override
805 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700806 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
807 return;
808 }
809
Adam Cohen0ffac432013-07-10 11:19:26 -0700810 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700811 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700812 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 }
814 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700815 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700816 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 }
818 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700819 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700820 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 }
822 }
823 }
824
825 /**
826 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700827 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 *
Winson Chung86f77532010-08-24 11:08:22 -0700829 * This happens when live folders requery, and if they're off page, they
830 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 */
832 @Override
833 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700834 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 View v = focused;
836 while (true) {
837 if (v == current) {
838 super.focusableViewAvailable(focused);
839 return;
840 }
841 if (v == this) {
842 return;
843 }
844 ViewParent parent = v.getParent();
845 if (parent instanceof View) {
846 v = (View)v.getParent();
847 } else {
848 return;
849 }
850 }
851 }
852
853 /**
854 * {@inheritDoc}
855 */
856 @Override
857 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
858 if (disallowIntercept) {
859 // We need to make sure to cancel our long press if
860 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700861 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700862 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 }
864 super.requestDisallowInterceptTouchEvent(disallowIntercept);
865 }
866
867 @Override
868 public boolean onInterceptTouchEvent(MotionEvent ev) {
869 /*
870 * This method JUST determines whether we want to intercept the motion.
871 * If we return true, onTouchEvent will be called and we do the actual
872 * scrolling there.
873 */
874
Winson Chung45e1d6e2010-11-09 17:19:49 -0800875 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700876 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700877
878 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800879
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 /*
881 * Shortcut the most recurring case: the user is in the dragging
882 * state and he is moving his finger. We want to intercept this
883 * motion.
884 */
885 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700886 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 return true;
888 }
889
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 switch (action & MotionEvent.ACTION_MASK) {
891 case MotionEvent.ACTION_MOVE: {
892 /*
893 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800894 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700896 if (mActivePointerId != INVALID_POINTER) {
897 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700898 }
899 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800900 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700901 // i.e. fall through to the next case (don't break)
902 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
903 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700904 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 }
906
907 case MotionEvent.ACTION_DOWN: {
908 final float x = ev.getX();
909 final float y = ev.getY();
910 // Remember location of down touch
911 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700912 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700913 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800914 mLastMotionRemainder = 0;
915 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700917
Tony Wickhama0aee212019-09-18 09:24:03 -0700918 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700919
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 break;
921 }
922
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800924 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700925 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700926 break;
927
928 case MotionEvent.ACTION_POINTER_UP:
929 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800930 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 break;
932 }
933
934 /*
935 * The only time we want to intercept motion events is if we are in the
936 * drag mode.
937 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700938 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 }
940
Tony Wickhama0aee212019-09-18 09:24:03 -0700941 /**
942 * If being flinged and user touches the screen, initiate drag; otherwise don't.
943 */
944 private void updateIsBeingDraggedOnTouchDown() {
945 // mScroller.isFinished should be false when being flinged.
946 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
947 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
948
949 if (finishedScrolling) {
950 mIsBeingDragged = false;
951 if (!mScroller.isFinished() && !mFreeScroll) {
952 setCurrentPage(getNextPage());
953 pageEndTransition();
954 }
955 } else {
956 mIsBeingDragged = true;
957 }
958 }
959
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800960 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700961 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800962 }
963
Adam Cohenf8d28232011-02-01 21:47:00 -0800964 protected void determineScrollingStart(MotionEvent ev) {
965 determineScrollingStart(ev, 1.0f);
966 }
967
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 /*
969 * Determines if we should change the touch state to start scrolling after the
970 * user moves their touch point too far.
971 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800972 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700973 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700975 if (pointerIndex == -1) return;
976
Vinit Nayaka406f722019-12-19 11:50:55 -0800977 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
978 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -0800979 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -0800980 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700981
Vinit Nayaka406f722019-12-19 11:50:55 -0800982 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700983 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700984 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800985 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
986 mLastMotion = primaryDirection;
987 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700988 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700989 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800990 // Stop listening for things like pinches.
991 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800992 }
993 }
994
995 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700996 // Try canceling the long press. It could also have been scheduled
997 // by a distant descendant, so use the mAllowLongPress flag to block
998 // everything
999 final View currentPage = getPageAt(mCurrentPage);
1000 if (currentPage != null) {
1001 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 }
1003 }
1004
Adam Cohenb5ba0972011-09-07 18:02:31 -07001005 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001006 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001007
Adam Cohenedb40762013-07-18 16:45:45 -07001008 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001009 int count = getChildCount();
1010
1011 final int totalDistance;
1012
1013 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001014 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001015 adjacentPage = page - 1;
1016 }
1017
1018 if (adjacentPage < 0 || adjacentPage > count - 1) {
1019 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1020 } else {
1021 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1022 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001023
1024 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001025 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1026 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001027 return scrollProgress;
1028 }
1029
Adam Cohenedb40762013-07-18 16:45:45 -07001030 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001031 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001032 return 0;
1033 } else {
1034 return mPageScrolls[index];
1035 }
1036 }
1037
Adam Cohen564a2e72013-10-09 14:47:32 -07001038 // While layout transitions are occurring, a child's position may stray from its baseline
1039 // position. This method returns the magnitude of this stray at any given time.
1040 public int getLayoutTransitionOffsetForPage(int index) {
1041 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1042 return 0;
1043 } else {
1044 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001045
Sunny Goyal228153d2018-01-04 15:35:22 -08001046 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1047 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001048 return (int) (child.getX() - baselineX);
1049 }
1050 }
1051
Jon Miranda71cb80c2019-01-24 21:25:13 -08001052 @Override
1053 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001054 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001055 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001056 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001057 super.dispatchDraw(canvas);
1058
1059 canvas.restoreToCount(saveCount);
1060 } else {
1061 super.dispatchDraw(canvas);
1062 }
1063 }
1064
Jon Miranda57dab6f2020-04-10 13:38:14 -07001065 /**
1066 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1067 */
1068 private int getSpringOverScroll(int amount) {
1069 if (mScroller.isSpringing()) {
1070 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001071 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001072 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1073 } else {
1074 return 0;
1075 }
1076 }
1077
Sunny Goyalb7b01352018-08-09 12:31:20 -07001078 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001079 if (amount == 0) {
1080 return;
1081 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001082
Vinit Nayaka406f722019-12-19 11:50:55 -08001083 int size = mOrientationHandler.getMeasuredSize(this);
1084 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001085 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001086 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001087 invalidate();
1088 return;
1089 }
1090
Vinit Nayaka406f722019-12-19 11:50:55 -08001091 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1092 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1093 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001094 invalidate();
1095 }
1096
Sunny Goyalb7b01352018-08-09 12:31:20 -07001097 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001098 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001099 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001100 invalidate();
1101 return;
1102 }
1103
Sunny Goyalb7b01352018-08-09 12:31:20 -07001104 if (amount == 0) return;
1105
1106 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001107 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1108 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001109 } else {
1110 dampedOverScroll(amount);
1111 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001112 }
1113
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001114
Tony Wickham023f4042019-01-29 10:52:31 -08001115 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001116 if (mFreeScroll == freeScroll) {
1117 return;
1118 }
1119
Tony Wickham8f7ead32016-04-07 18:46:44 -07001120 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001121 mFreeScroll = freeScroll;
1122
Adam Cohenf9618852013-11-08 06:45:03 -08001123 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001124 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001125 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001126 if (getScrollForPage(getNextPage()) != getScrollX()) {
1127 snapToPage(getNextPage());
1128 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001129 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001130 }
1131
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001132 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001133 mAllowOverScroll = enable;
1134 }
1135
Winson Chung321e9ee2010-08-09 13:37:56 -07001136 @Override
1137 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001138 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001139 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001140
Michael Jurkab8f06722010-10-10 15:58:46 -07001141 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001142
1143 final int action = ev.getAction();
1144
1145 switch (action & MotionEvent.ACTION_MASK) {
1146 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001147 updateIsBeingDraggedOnTouchDown();
1148
Winson Chung321e9ee2010-08-09 13:37:56 -07001149 /*
1150 * If being flinged and user touches, stop the fling. isFinished
1151 * will be false if being flinged.
1152 */
1153 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001154 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001155 }
1156
1157 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001158 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001159 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001160 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1161 mLastMotionRemainder = 0;
1162 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001164 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001165 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001166 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001167 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 break;
1169
Vinit Nayaka406f722019-12-19 11:50:55 -08001170 case MotionEvent.ACTION_MOVE:
1171 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 // Scroll to follow the motion event
1173 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001174
1175 if (pointerIndex == -1) return true;
1176
Vinit Nayaka406f722019-12-19 11:50:55 -08001177 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1178 float delta = mLastMotion + mLastMotionRemainder - direction;
1179 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001180
Winson Chungc0844aa2011-02-02 15:25:58 -08001181 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1182 // keep the remainder because we are actually testing if we've moved from the last
1183 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001184 if (Math.abs(delta) >= 1.0f) {
1185 mLastMotion = direction;
1186 mLastMotionRemainder = delta - (int) delta;
1187
1188 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 } else {
1190 awakenScrollBars();
1191 }
Adam Cohen564976a2010-10-13 18:52:07 -07001192 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 determineScrollingStart(ev);
1194 }
1195 break;
1196
1197 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001198 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 final int activePointerId = mActivePointerId;
1200 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001201 if (pointerIndex == -1) return true;
1202
Vinit Nayaka406f722019-12-19 11:50:55 -08001203 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1204 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001205 final VelocityTracker velocityTracker = mVelocityTracker;
1206 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001207
Vinit Nayaka406f722019-12-19 11:50:55 -08001208 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1209 mActivePointerId);
1210 int delta = (int) (primaryDirection - mDownMotionPrimary);
1211 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1212
1213 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1214 SIGNIFICANT_MOVE_THRESHOLD;
1215
1216 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1217 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1218 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1219 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001220
Adam Cohenf358a4b2013-07-23 16:47:31 -07001221 if (!mFreeScroll) {
1222 // In the case that the page is moved far to one direction and then is flung
1223 // in the opposite direction, we use a threshold to determine whether we should
1224 // just return to the starting page, or if we should skip one further.
1225 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001226 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1227 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001228 returnToOriginalPage = true;
1229 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001230
Adam Cohenf358a4b2013-07-23 16:47:31 -07001231 int finalPage;
1232 // We give flings precedence over large moves, which is why we short-circuit our
1233 // test for a large move if a fling has been registered. That is, a large
1234 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001235
Vinit Nayaka406f722019-12-19 11:50:55 -08001236 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1237 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001238 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001239 snapToPageWithVelocity(finalPage, velocity);
1240 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1241 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001242 mCurrentPage < getChildCount() - 1) {
1243 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001244 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001245 } else {
1246 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001247 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001249 if (!mScroller.isFinished()) {
1250 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001252
Vinit Nayaka406f722019-12-19 11:50:55 -08001253 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1254 int maxScroll = mMaxScroll;
1255 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001256
Vinit Nayaka406f722019-12-19 11:50:55 -08001257 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1258 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1259 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001260 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001261 } else {
1262 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001263 mScroller.fling(initialScroll, -velocity,
1264 minScroll, maxScroll,
1265 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001266
Vinit Nayaka406f722019-12-19 11:50:55 -08001267 int finalPos = mScroller.getFinalPos();
1268 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001269
1270 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1271 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001272 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001273 // If scrolling ends in the half of the added space that is closer to
1274 // the end, settle to the end. Otherwise snap to the nearest page.
1275 // If flinging past one of the ends, don't change the velocity as it
1276 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001277 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1278 ? minScroll
1279 : finalPos > (lastPageScroll + maxScroll) / 2
1280 ? maxScroll
1281 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001282
Vinit Nayaka406f722019-12-19 11:50:55 -08001283 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001284 // Ensure the scroll/snap doesn't happen too fast;
1285 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001286 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001287 if (extraScrollDuration > 0) {
1288 mScroller.extendDuration(extraScrollDuration);
1289 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001290 }
1291 }
1292 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001294 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001296
Adam Cohen7d30a372013-07-01 17:03:59 -07001297 // End any intermediate reordering states
1298 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 break;
1300
1301 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001302 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001303 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001304 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001305 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001306 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001307 break;
1308
1309 case MotionEvent.ACTION_POINTER_UP:
1310 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001311 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 break;
1313 }
1314
1315 return true;
1316 }
1317
Vinit Nayaka406f722019-12-19 11:50:55 -08001318 protected boolean shouldFlingForVelocity(int velocity) {
1319 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001320 }
1321
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 private void resetTouchState() {
1323 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001324 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001325 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001326 }
1327
Adam Cohenc2d6e892014-10-16 09:49:24 -07001328 /**
1329 * Triggered by scrolling via touch
1330 */
1331 protected void onScrollInteractionBegin() {
1332 }
1333
1334 protected void onScrollInteractionEnd() {
1335 }
1336
Winson Chung185d7162011-02-28 13:47:29 -08001337 @Override
1338 public boolean onGenericMotionEvent(MotionEvent event) {
1339 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1340 switch (event.getAction()) {
1341 case MotionEvent.ACTION_SCROLL: {
1342 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1343 final float vscroll;
1344 final float hscroll;
1345 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1346 vscroll = 0;
1347 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1348 } else {
1349 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1350 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1351 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001352 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1353 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001354 }
Winson Chung185d7162011-02-28 13:47:29 -08001355 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001356 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001357 : (hscroll > 0 || vscroll > 0);
1358 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001359 scrollRight();
1360 } else {
1361 scrollLeft();
1362 }
1363 return true;
1364 }
1365 }
1366 }
1367 }
1368 return super.onGenericMotionEvent(event);
1369 }
1370
Sunny Goyal8b37c572020-03-31 12:12:14 -07001371 /**
1372 * Returns true if the paged view can scroll for the provided vertical and horizontal
1373 * scroll values
1374 */
1375 protected boolean canScroll(float absVScroll, float absHScroll) {
1376 ActivityContext ac = ActivityContext.lookupContext(getContext());
1377 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001378 }
1379
Michael Jurkab8f06722010-10-10 15:58:46 -07001380 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1381 if (mVelocityTracker == null) {
1382 mVelocityTracker = VelocityTracker.obtain();
1383 }
1384 mVelocityTracker.addMovement(ev);
1385 }
1386
1387 private void releaseVelocityTracker() {
1388 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001389 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001390 mVelocityTracker.recycle();
1391 mVelocityTracker = null;
1392 }
1393 }
1394
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 private void onSecondaryPointerUp(MotionEvent ev) {
1396 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1397 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1398 final int pointerId = ev.getPointerId(pointerIndex);
1399 if (pointerId == mActivePointerId) {
1400 // This was our active pointer going up. Choose a new
1401 // active pointer and adjust accordingly.
1402 // TODO: Make this decision more intelligent.
1403 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001404 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1405 newPointerIndex);
1406 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 mActivePointerId = ev.getPointerId(newPointerIndex);
1408 if (mVelocityTracker != null) {
1409 mVelocityTracker.clear();
1410 }
1411 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001412 }
1413
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 @Override
1415 public void requestChildFocus(View child, View focused) {
1416 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001417 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001418 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001419 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 }
1421 }
1422
Sunny Goyal228153d2018-01-04 15:35:22 -08001423 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001424 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001425 }
1426
Vinit Nayaka406f722019-12-19 11:50:55 -08001427 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1428 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1429 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001430 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 final int childCount = getChildCount();
1433 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001434 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001435 int childSize = mOrientationHandler.getMeasuredSize(layout);
1436 int halfChildSize = (childSize / 2);
1437 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1439 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1440 minDistanceFromScreenCenter = distanceFromScreenCenter;
1441 minDistanceFromScreenCenterIndex = i;
1442 }
1443 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001444 return minDistanceFromScreenCenterIndex;
1445 }
1446
1447 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001448 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1449 }
1450
1451 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001452 int scroll = mOrientationHandler.getPrimaryScroll(this);
1453 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001454 }
1455
1456 protected int getPageSnapDuration() {
1457 if (isInOverScroll()) {
1458 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1459 }
1460 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 }
1462
Adam Cohene0f66b52010-11-23 15:06:07 -08001463 // We want the duration of the page snap animation to be influenced by the distance that
1464 // the screen has to travel, however, we don't want this duration to be effected in a
1465 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1466 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001467 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001468 f -= 0.5f; // center the values about 0.
1469 f *= 0.3f * Math.PI / 2.0f;
1470 return (float) Math.sin(f);
1471 }
1472
Winson Chung05a31ed2018-02-08 17:08:43 -08001473 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001474 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001475 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001476
Vinit Nayaka406f722019-12-19 11:50:55 -08001477 final int newLoc = getScrollForPage(whichPage);
1478 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001479 int duration = 0;
1480
Sunny Goyal4d113a52015-05-27 10:05:28 -07001481 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001482 // If the velocity is low enough, then treat this more as an automatic page advance
1483 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001484 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001485 }
1486
1487 // Here we compute a "distance" that will be used in the computation of the overall
1488 // snap duration. This is a function of the actual distance that needs to be traveled;
1489 // we keep this value close to half screen size in order to reduce the variance in snap
1490 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001491 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001492 float distance = halfScreenSize + halfScreenSize *
1493 distanceInfluenceForSnapDuration(distanceRatio);
1494
1495 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001496 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001497
1498 // we want the page's snap velocity to approximately match the velocity at which the
1499 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001500 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1501 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001502
Jon Miranda9e198662020-03-11 14:42:02 -07001503 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001504 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001505 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001506 } else {
1507 return snapToPage(whichPage, delta, duration);
1508 }
Michael Jurka0142d492010-08-25 17:46:15 -07001509 }
1510
Winson Chung05a31ed2018-02-08 17:08:43 -08001511 public boolean snapToPage(int whichPage) {
1512 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 }
1514
Winson Chung05a31ed2018-02-08 17:08:43 -08001515 public boolean snapToPageImmediately(int whichPage) {
1516 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001517 }
1518
Winson Chung05a31ed2018-02-08 17:08:43 -08001519 public boolean snapToPage(int whichPage, int duration) {
1520 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001521 }
1522
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001523 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001524 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001525 }
1526
Winson Chung05a31ed2018-02-08 17:08:43 -08001527 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001528 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001529 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001530
Vinit Nayaka406f722019-12-19 11:50:55 -08001531 int newLoc = getScrollForPage(whichPage);
1532 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001533 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001534 }
1535
Winson Chung05a31ed2018-02-08 17:08:43 -08001536 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001537 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001538 }
Michael Jurka0142d492010-08-25 17:46:15 -07001539
Winson Chung05a31ed2018-02-08 17:08:43 -08001540 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001541 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001542 if (mFirstLayout) {
1543 setCurrentPage(whichPage);
1544 return false;
1545 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001546
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001547 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001548 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1549 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001550 }
1551
Adam Cohen6f127a62014-06-12 14:54:41 -07001552 whichPage = validateNewPage(whichPage);
1553
Adam Cohen7d30a372013-07-01 17:03:59 -07001554 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001555
Winson Chung321e9ee2010-08-09 13:37:56 -07001556 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001557 if (immediate) {
1558 duration = 0;
1559 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001560 duration = Math.abs(delta);
1561 }
1562
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001563 if (duration != 0) {
1564 pageBeginTransition();
1565 }
1566
Adam Cohenf358a4b2013-07-23 16:47:31 -07001567 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001568 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001569 }
Adam Cohenf9618852013-11-08 06:45:03 -08001570
1571 if (interpolator != null) {
1572 mScroller.setInterpolator(interpolator);
1573 } else {
1574 mScroller.setInterpolator(mDefaultInterpolator);
1575 }
1576
Jon Miranda71cb80c2019-01-24 21:25:13 -08001577 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001578 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001579 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001580 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001581 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001582
Adam Cohen674531f2013-12-13 15:07:14 -08001583 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001584
1585 // Trigger a compute() to finish switching pages if necessary
1586 if (immediate) {
1587 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001588 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001589 }
1590
Winson Chung321e9ee2010-08-09 13:37:56 -07001591 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001592 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001593 }
1594
Vadim Tryshev98913d02018-05-18 18:41:34 -07001595 public boolean scrollLeft() {
1596 if (getNextPage() > 0) {
1597 snapToPage(getNextPage() - 1);
1598 return true;
1599 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001600 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001601 }
1602
Vadim Tryshev98913d02018-05-18 18:41:34 -07001603 public boolean scrollRight() {
1604 if (getNextPage() < getChildCount() - 1) {
1605 snapToPage(getNextPage() + 1);
1606 return true;
1607 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001608 return onOverscroll(getMeasuredWidth());
1609 }
1610
1611 protected boolean onOverscroll(int amount) {
1612 if (!mAllowOverScroll) return false;
1613 onScrollInteractionBegin();
1614 overScroll(amount);
1615 onScrollInteractionEnd();
1616 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 }
1618
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001619 @Override
1620 public CharSequence getAccessibilityClassName() {
1621 // Some accessibility services have special logic for ScrollView. Since we provide same
1622 // accessibility info as ScrollView, inform the service to handle use the same way.
1623 return ScrollView.class.getName();
1624 }
1625
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001626 protected boolean isPageOrderFlipped() {
1627 return false;
1628 }
1629
Winson Chung6a0f57d2011-06-29 20:10:49 -07001630 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001631 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001632 @Override
1633 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1634 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001635 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001636 int offset = (mAllowOverScroll ? 0 : 1);
1637 info.setScrollable(getPageCount() > offset);
1638 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001639 info.addAction(pagesFlipped ?
1640 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1641 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1642 info.addAction(mIsRtl ?
1643 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1644 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001645 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001646 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001647 info.addAction(pagesFlipped ?
1648 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1649 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1650 info.addAction(mIsRtl ?
1651 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1652 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001653 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001654 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1655 // Besides disabling the accessibility long-click, this also prevents this view from getting
1656 // accessibility focus.
1657 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001658 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001659 }
1660
1661 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001662 public void sendAccessibilityEvent(int eventType) {
1663 // Don't let the view send real scroll events.
1664 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1665 super.sendAccessibilityEvent(eventType);
1666 }
1667 }
1668
1669 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001670 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1671 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001672 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001673 }
1674
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001675 @Override
1676 public boolean performAccessibilityAction(int action, Bundle arguments) {
1677 if (super.performAccessibilityAction(action, arguments)) {
1678 return true;
1679 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001680 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001681 switch (action) {
1682 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001683 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001684 return true;
1685 }
1686 } break;
1687 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001688 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001689 return true;
1690 }
yingleiw02cc8482019-08-02 16:14:27 -07001691 } break;
1692 case android.R.id.accessibilityActionPageRight: {
1693 if (!mIsRtl) {
1694 return scrollRight();
1695 } else {
1696 return scrollLeft();
1697 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001698 }
yingleiw02cc8482019-08-02 16:14:27 -07001699 case android.R.id.accessibilityActionPageLeft: {
1700 if (!mIsRtl) {
1701 return scrollLeft();
1702 } else {
1703 return scrollRight();
1704 }
1705 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001706 }
1707 return false;
1708 }
1709
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001710 protected boolean canAnnouncePageDescription() {
1711 return true;
1712 }
1713
Adam Cohen0ffac432013-07-10 11:19:26 -07001714 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001715 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001716 getNextPage() + 1, getChildCount());
1717 }
1718
Tony Mak2e564402018-02-27 17:19:34 +00001719 protected float getDownMotionX() {
1720 return mDownMotionX;
1721 }
1722
1723 protected float getDownMotionY() {
1724 return mDownMotionY;
1725 }
1726
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001727 protected interface ComputePageScrollsLogic {
1728
1729 boolean shouldIncludeView(View view);
1730 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001731
1732 public int[] getVisibleChildrenRange() {
1733 float visibleLeft = 0;
1734 float visibleRight = visibleLeft + getMeasuredWidth();
1735 float scaleX = getScaleX();
1736 if (scaleX < 1 && scaleX > 0) {
1737 float mid = getMeasuredWidth() / 2;
1738 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1739 visibleRight = mid + ((visibleRight - mid) / scaleX);
1740 }
1741
1742 int leftChild = -1;
1743 int rightChild = -1;
1744 final int childCount = getChildCount();
1745 for (int i = 0; i < childCount; i++) {
1746 final View child = getPageAt(i);
1747
1748 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1749 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1750 if (leftChild == -1) {
1751 leftChild = i;
1752 }
1753 rightChild = i;
1754 }
1755 }
1756 mTmpIntPair[0] = leftChild;
1757 mTmpIntPair[1] = rightChild;
1758 return mTmpIntPair;
1759 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001760}