blob: af2d94a278bc16f1013836e00706fc8e64da31ac [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;
Winson Chung4941f652020-06-18 15:11:53 -070074 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070075
Winson Chung4941f652020-06-18 15:11:53 -070076 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053077 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070078 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Vadim Tryshevfedca432015-08-19 17:55:02 -070080 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Sunny Goyalb72d8b22017-07-14 00:02:27 -070082 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070083 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070084
Adam Cohenb64cb5a2011-02-15 13:53:42 -080085 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080086 // The page is moved more than halfway, automatically move to the next page on touch up.
87 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080088
Sunny Goyal8e2133b2015-05-06 13:39:07 -070089 private static final float MAX_SCROLL_PROGRESS = 1.0f;
90
Adam Cohen265b9a62011-12-07 14:37:18 -080091 // The following constants need to be scaled based on density. The scaled versions will be
92 // assigned to the corresponding member variables below.
93 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070094 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080095 private static final int MIN_SNAP_VELOCITY = 1500;
96 private static final int MIN_FLING_VELOCITY = 250;
97
Adam Cohenf358a4b2013-07-23 16:47:31 -070098 private boolean mFreeScroll = false;
Steven Ng391404f2021-02-17 15:58:23 +000099 /** If {@code false}, disable swipe gesture to switch between pages. */
100 private boolean mSwipeGestureEnabled = true;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700101
Winson Chung4941f652020-06-18 15:11:53 -0700102 protected final int mFlingThresholdVelocity;
103 protected final int mEasyFlingThresholdVelocity;
104 protected final int mMinFlingVelocity;
105 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected boolean mFirstLayout = true;
108
Sunny Goyal4ffec482016-02-09 11:28:52 -0800109 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700111
Sunny Goyal4ffec482016-02-09 11:28:52 -0800112 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800114 protected int mMaxScroll;
115 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700116 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800117 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800119 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
121 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700122 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800123 private float mDownMotionPrimary;
124 private float mLastMotion;
125 private float mLastMotionRemainder;
126 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700127 // Used in special cases where the fling checks can be relaxed for an intentional gesture
128 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700129 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700130
Vadim Trysheve628c472018-05-16 14:28:18 -0700131 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700132 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800133
Winson Chung4941f652020-06-18 15:11:53 -0700134 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700136 // The amount of movement to begin paging
137 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800139 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700145 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700146 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147
Vinit Nayaka406f722019-12-19 11:50:55 -0800148 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800149
Sunny Goyal061380a2016-02-29 15:15:03 -0800150 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700151
Vinit Nayaka406f722019-12-19 11:50:55 -0800152 protected int mUnboundedScroll;
153
Winson Chungd2be3812013-07-16 11:11:32 -0700154 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800156 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700157
John Spurlock77e1f472013-09-11 10:09:51 -0400158 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800159 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400160
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800161 // Similar to the platform implementation of isLayoutValid();
162 protected boolean mIsLayoutValid;
163
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700164 private int[] mTmpIntPair = new int[2];
165
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 public PagedView(Context context) {
167 this(context, null);
168 }
169
170 public PagedView(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public PagedView(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700176
Adam Cohen9c4949e2010-10-05 12:27:22 -0700177 TypedArray a = context.obtainStyledAttributes(attrs,
178 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700179 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700180 a.recycle();
181
Winson Chung321e9ee2010-08-09 13:37:56 -0700182 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700183 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Vinit Nayaka406f722019-12-19 11:50:55 -0800185 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700186 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700187 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700188
Vinit Nayaka406f722019-12-19 11:50:55 -0800189 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700190 mTouchSlop = configuration.getScaledTouchSlop();
191 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800193
Sunny Goyalcf25b522015-07-09 00:01:18 -0700194 float density = getResources().getDisplayMetrics().density;
195 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700196 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700197 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
198 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700199
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700200 setDefaultFocusHighlightEnabled(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202
Adam Cohenf9618852013-11-08 06:45:03 -0800203 protected void setDefaultInterpolator(Interpolator interpolator) {
204 mDefaultInterpolator = interpolator;
205 mScroller.setInterpolator(mDefaultInterpolator);
206 }
207
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700208 public void initParentViews(View parent) {
209 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800210 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700211 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700212 }
213 }
214
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800215 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700216 return mPageIndicator;
217 }
218
Adam Cohen674531f2013-12-13 15:07:14 -0800219 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700220 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
221 * that the user was on before entering free scroll mode (e.g. the home screen page they
222 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
223 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700225 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700226 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700228
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700229 /**
230 * Returns the index of page to be shown immediately afterwards.
231 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700232 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700233 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
234 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700235
Adam Cohenf9c184a2016-01-15 16:47:43 -0800236 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 return getChildCount();
238 }
239
Sunny Goyal83a8f042015-05-19 12:52:12 -0700240 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 return getChildAt(index);
242 }
243
Adam Cohenae4f1552011-10-20 00:15:42 -0700244 protected int indexToPage(int index) {
245 return index;
246 }
247
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800249 * Updates the scroll of the current page immediately to its final scroll position. We use this
250 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
251 * the previous tab page.
252 */
253 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700256 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800257 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700258 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800259 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
260 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700261 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800262 }
263
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700264 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700265 mScroller.abortAnimation();
266 // We need to clean up the next page here to avoid computeScrollHelper from
267 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700268 if (resetNextPage) {
269 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800270 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700271 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700272 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273
Tony Wickham8f7ead32016-04-07 18:46:44 -0700274 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700275 mScroller.forceFinished(true);
276 // We need to clean up the next page here to avoid computeScrollHelper from
277 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700278 if (resetNextPage) {
279 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800280 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700281 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700282 }
283
Adam Cohen6f127a62014-06-12 14:54:41 -0700284 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700285 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700287 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700288 }
289
Chet Haasebc2f0822012-10-26 17:59:53 -0700290 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700291 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
292 */
293 private int ensureWithinScrollBounds(int page) {
294 int dir = !mIsRtl ? 1 : - 1;
295 int currScroll = getScrollForPage(page);
296 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800297 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700298 page += dir;
299 prevScroll = currScroll;
300 currScroll = getScrollForPage(page);
301 if (currScroll <= prevScroll) {
302 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
303 break;
304 }
305 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800306 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700307 page -= dir;
308 prevScroll = currScroll;
309 currScroll = getScrollForPage(page);
310 if (currScroll >= prevScroll) {
311 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
312 break;
313 }
314 }
315 return page;
316 }
317
Tony Wickham03f27012019-04-25 14:22:54 -0700318 public void setCurrentPage(int currentPage) {
319 setCurrentPage(currentPage, INVALID_PAGE);
320 }
321
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700322 /**
Winson Chung86f77532010-08-24 11:08:22 -0700323 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 */
Tony Wickham03f27012019-04-25 14:22:54 -0700325 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800326 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700327 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800328 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800329 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
330 // the default
331 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800332 return;
333 }
Tony Wickham03f27012019-04-25 14:22:54 -0700334 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700335 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700336 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700337 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800338 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 }
340
Winson Chung8c87cd82013-07-23 16:20:10 -0700341 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800342 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
343 * has settled.
344 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700345 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800346 updatePageIndicator();
347 }
348
349 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700350 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000351 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700352 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700354 protected void pageBeginTransition() {
355 if (!mIsPageInTransition) {
356 mIsPageInTransition = true;
357 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700358 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700359 }
360
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700361 protected void pageEndTransition() {
362 if (mIsPageInTransition) {
363 mIsPageInTransition = false;
364 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700365 }
Michael Jurka0142d492010-08-25 17:46:15 -0700366 }
367
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700368 protected boolean isPageInTransition() {
369 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700370 }
371
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700372 /**
373 * Called when the page starts moving as part of the scroll. Subclasses can override this
374 * to provide custom behavior during animation.
375 */
376 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700377 }
378
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700379 /**
380 * Called when the page ends moving as part of the scroll. Subclasses can override this
381 * to provide custom behavior during animation.
382 */
383 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700384 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700385 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800386 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
387 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700388 if (mOnPageTransitionEndCallback != null) {
389 mOnPageTransitionEndCallback.run();
390 mOnPageTransitionEndCallback = null;
391 }
392 }
393
394 /**
395 * Sets a callback to run once when the scrolling finishes. If there is currently
396 * no page in transition, then the callback is called immediately.
397 */
398 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
399 if (mIsPageInTransition || callback == null) {
400 mOnPageTransitionEndCallback = callback;
401 } else {
402 callback.run();
403 }
Michael Jurka0142d492010-08-25 17:46:15 -0700404 }
405
Vinit Nayaka406f722019-12-19 11:50:55 -0800406 protected int getUnboundedScroll() {
407 return mUnboundedScroll;
408 }
409
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800411 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800412 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800413 }
414
415 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700416 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800417 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
418 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
419 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700420
Vinit Nayaka406f722019-12-19 11:50:55 -0800421 boolean isBeforeFirstPage = mIsRtl ?
422 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
423 boolean isAfterLastPage = mIsRtl ?
424 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
425 if (!isBeforeFirstPage && !isAfterLastPage) {
426 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800427 }
428
Vinit Nayaka406f722019-12-19 11:50:55 -0800429 if (isBeforeFirstPage) {
430 mOrientationHandler.delegateScrollTo(this,
431 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800432 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700433 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800434 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800435 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800436 } else if (isAfterLastPage) {
437 mOrientationHandler.delegateScrollTo(this,
438 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800439 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700440 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800441 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800442 }
443 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700444 if (mWasInOverscroll) {
445 overScroll(0);
446 mWasInOverscroll = false;
447 }
Adam Cohen68d73932010-11-15 10:50:58 -0800448 super.scrollTo(x, y);
449 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800450 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700451
Vinit Nayaka406f722019-12-19 11:50:55 -0800452 /**
453 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
454 */
455 public void superScrollTo(int x, int y) {
456 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700457 }
458
Adam Cohen53805212013-10-01 10:39:23 -0700459 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800460 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700461 if (mCurrentPage != getNextPage()) {
462 AccessibilityEvent ev =
463 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700464 ev.setScrollable(true);
465 ev.setScrollX(getScrollX());
466 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800467 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700468 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700469 }
Adam Cohen53805212013-10-01 10:39:23 -0700470 }
471 }
472
Michael Jurka0142d492010-08-25 17:46:15 -0700473 // we moved this functionality to a helper function so SmoothPagedView can reuse it
474 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800475 return computeScrollHelper(true);
476 }
477
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700478 protected void announcePageForAccessibility() {
479 if (isAccessibilityEnabled(getContext())) {
480 // Notify the user when the page changes
481 announceForAccessibility(getCurrentPageDescription());
482 }
483 }
484
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800485 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700487 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800488 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
489 if (mUnboundedScroll != mScroller.getCurrPos()
490 || currentScroll != mScroller.getCurrPos()) {
491 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700492 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800493 if (shouldInvalidate) {
494 invalidate();
495 }
Michael Jurka0142d492010-08-25 17:46:15 -0700496 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800497 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700498 sendScrollAccessibilityEvent();
499
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700500 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700501 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700502 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700503 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700504
Adam Cohen73aa9752010-11-24 16:26:58 -0800505 // We don't want to trigger a page end moving unless the page has settled
506 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700507 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700508 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800509 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700510
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700511 if (canAnnouncePageDescription()) {
512 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 }
Michael Jurka0142d492010-08-25 17:46:15 -0700515 return false;
516 }
517
518 @Override
519 public void computeScroll() {
520 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 }
522
Sunny Goyalce8809a2018-01-18 14:02:47 -0800523 public int getExpectedHeight() {
524 return getMeasuredHeight();
525 }
526
Adam Cohen410f3cd2013-09-22 12:09:32 -0700527 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800528 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800529 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700530 }
531
Sunny Goyalce8809a2018-01-18 14:02:47 -0800532 public int getExpectedWidth() {
533 return getMeasuredWidth();
534 }
535
Sunny Goyalbc683e92017-12-06 10:25:07 -0800536 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800537 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800538 - mInsets.left - mInsets.right;
539 }
540
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800542 public void requestLayout() {
543 mIsLayoutValid = false;
544 super.requestLayout();
545 }
546
547 @Override
548 public void forceLayout() {
549 mIsLayoutValid = false;
550 super.forceLayout();
551 }
552
553 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700555 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700556 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
557 return;
558 }
559
Adam Cohen7d30a372013-07-01 17:03:59 -0700560 // We measure the dimensions of the PagedView to be larger than the pages so that when we
561 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700562 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
563 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
564 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700565 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700566
Adam Cohen7d30a372013-07-01 17:03:59 -0700567 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
568 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
569 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571
Winson Chung8aad6102012-05-11 16:27:49 -0700572 // Return early if we aren't given a proper dimension
573 if (widthSize <= 0 || heightSize <= 0) {
574 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
575 return;
576 }
577
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700579 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700580 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700581
Sunny Goyalac00cba2017-11-13 15:58:01 -0800582 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800583 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800584 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800585 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700586
Sunny Goyalac00cba2017-11-13 15:58:01 -0800587 // measureChildren takes accounts for content padding, we only need to care about extra
588 // space due to insets.
589 measureChildren(myWidthSpec, myHeightSpec);
590 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800591 }
592
Sunny Goyalcf25b522015-07-09 00:01:18 -0700593 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700595 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800596 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700597 final int childCount = getChildCount();
598 boolean pageScrollChanged = false;
599 if (mPageScrolls == null || childCount != mPageScrolls.length) {
600 mPageScrolls = new int[childCount];
601 pageScrollChanged = true;
602 }
603
604 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700605 return;
606 }
607
Winson Chung785d2eb2011-04-14 16:08:02 -0700608 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700609
Vinit Nayaka406f722019-12-19 11:50:55 -0800610 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
611 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700612 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700614
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700615 final LayoutTransition transition = getLayoutTransition();
616 // If the transition is running defer updating max scroll, as some empty pages could
617 // still be present, and a max scroll change could cause sudden jumps in scroll.
618 if (transition != null && transition.isRunning()) {
619 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
620
621 @Override
622 public void startTransition(LayoutTransition transition, ViewGroup container,
623 View view, int transitionType) { }
624
625 @Override
626 public void endTransition(LayoutTransition transition, ViewGroup container,
627 View view, int transitionType) {
628 // Wait until all transitions are complete.
629 if (!transition.isRunning()) {
630 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700631 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700632 }
633 }
634 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700635 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700636 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700637 }
638
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700639 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
640 updateCurrentPageScroll();
641 mFirstLayout = false;
642 }
643
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800644 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700645 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700646 }
Winson Chunge3193b92010-09-10 11:44:42 -0700647 }
648
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700649 /**
650 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
651 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700652 */
653 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
654 ComputePageScrollsLogic scrollLogic) {
655 final int childCount = getChildCount();
656
657 final int startIndex = mIsRtl ? childCount - 1 : 0;
658 final int endIndex = mIsRtl ? -1 : childCount;
659 final int delta = mIsRtl ? -1 : 1;
660
Vinit Nayaka406f722019-12-19 11:50:55 -0800661 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700662
Vinit Nayaka406f722019-12-19 11:50:55 -0800663 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
664 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700665 boolean pageScrollChanged = false;
666
Vinit Nayaka406f722019-12-19 11:50:55 -0800667 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700668 final View child = getPageAt(i);
669 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800670 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
671 pageCenter, layoutChildren);
672 final int primaryDimension = bounds.primaryDimension;
673 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700674
Sunny Goyalc82c6392018-06-06 15:39:13 -0700675 // In case the pages are of different width, align the page to left or right edge
676 // based on the orientation.
677 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800678 ? (childStart - scrollOffsetStart)
679 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700680 if (outPageScrolls[i] != pageScroll) {
681 pageScrollChanged = true;
682 outPageScrolls[i] = pageScroll;
683 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800684 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700685 }
686 }
687 return pageScrollChanged;
688 }
689
Sunny Goyala1fbd842015-05-20 13:40:27 -0700690 protected int getChildGap() {
691 return 0;
692 }
693
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700694 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800695 mMinScroll = computeMinScroll();
696 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700697 }
698
Vinit Nayaka406f722019-12-19 11:50:55 -0800699 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700700 return 0;
Tony50876bf2018-09-19 23:09:14 -0400701 }
702
Vinit Nayaka406f722019-12-19 11:50:55 -0800703 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700704 int childCount = getChildCount();
705 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700706 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700707 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700708 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700709 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700710 }
711 }
712
Adam Cohen3b185e22013-10-29 14:45:58 -0700713 public void setPageSpacing(int pageSpacing) {
714 mPageSpacing = pageSpacing;
715 requestLayout();
716 }
717
Tony50876bf2018-09-19 23:09:14 -0400718 public int getPageSpacing() {
719 return mPageSpacing;
720 }
721
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800722 private void dispatchPageCountChanged() {
723 if (mPageIndicator != null) {
724 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700725 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700726 // This ensures that when children are added, they get the correct transforms / alphas
727 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700728 invalidate();
729 }
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800732 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800733 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800734 dispatchPageCountChanged();
735 }
736
737 @Override
738 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800739 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700740 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800741 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700742 }
743
Adam Cohen73894962011-10-31 13:17:17 -0700744 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700745 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800746 View pageAtIndex = getPageAt(index);
747 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700748 }
749
Alex Chau7944ee52021-01-08 19:07:25 +0000750 protected int getChildVisibleSize(int index) {
751 View layout = getPageAt(index);
752 return mOrientationHandler.getMeasuredSize(layout);
753 }
754
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 @Override
756 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700757 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700758 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700759 if (immediate) {
760 setCurrentPage(page);
761 } else {
762 snapToPage(page);
763 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 return true;
765 }
766 return false;
767 }
768
769 @Override
770 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700771 int focusablePage;
772 if (mNextPage != INVALID_PAGE) {
773 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700775 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 }
Winson Chung86f77532010-08-24 11:08:22 -0700777 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700779 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 }
781 return false;
782 }
783
784 @Override
785 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800786 if (super.dispatchUnhandledMove(focused, direction)) {
787 return true;
788 }
789
790 if (mIsRtl) {
791 if (direction == View.FOCUS_LEFT) {
792 direction = View.FOCUS_RIGHT;
793 } else if (direction == View.FOCUS_RIGHT) {
794 direction = View.FOCUS_LEFT;
795 }
796 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700798 if (getCurrentPage() > 0) {
799 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700800 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 return true;
802 }
803 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700804 if (getCurrentPage() < getPageCount() - 1) {
805 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700806 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 return true;
808 }
809 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800810 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 }
812
813 @Override
814 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700815 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
816 return;
817 }
818
Adam Cohen0ffac432013-07-10 11:19:26 -0700819 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700820 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700821 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 }
823 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700824 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700825 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 }
827 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700828 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700829 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 }
831 }
832 }
833
834 /**
835 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700836 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 *
Winson Chung86f77532010-08-24 11:08:22 -0700838 * This happens when live folders requery, and if they're off page, they
839 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 */
841 @Override
842 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700843 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 View v = focused;
845 while (true) {
846 if (v == current) {
847 super.focusableViewAvailable(focused);
848 return;
849 }
850 if (v == this) {
851 return;
852 }
853 ViewParent parent = v.getParent();
854 if (parent instanceof View) {
855 v = (View)v.getParent();
856 } else {
857 return;
858 }
859 }
860 }
861
862 /**
Steven Ng391404f2021-02-17 15:58:23 +0000863 * If {@code enableSwipeGesture} is {@code true}, enables swipe gesture to navigate between
864 * pages. Otherwise, disables the navigation gesture.
865 */
866 public void setSwipeGestureEnabled(boolean swipeGestureEnabled) {
867 mSwipeGestureEnabled = swipeGestureEnabled;
868 }
869
870 /**
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 * {@inheritDoc}
872 */
873 @Override
874 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
875 if (disallowIntercept) {
876 // We need to make sure to cancel our long press if
877 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700878 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700879 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 }
881 super.requestDisallowInterceptTouchEvent(disallowIntercept);
882 }
883
884 @Override
885 public boolean onInterceptTouchEvent(MotionEvent ev) {
886 /*
887 * This method JUST determines whether we want to intercept the motion.
888 * If we return true, onTouchEvent will be called and we do the actual
889 * scrolling there.
890 */
891
Steven Ng391404f2021-02-17 15:58:23 +0000892 if (!mSwipeGestureEnabled) return false;
893
Winson Chung45e1d6e2010-11-09 17:19:49 -0800894 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700895 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700896
897 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800898
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 /*
900 * Shortcut the most recurring case: the user is in the dragging
901 * state and he is moving his finger. We want to intercept this
902 * motion.
903 */
904 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700905 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 return true;
907 }
908
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 switch (action & MotionEvent.ACTION_MASK) {
910 case MotionEvent.ACTION_MOVE: {
911 /*
912 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800913 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700915 if (mActivePointerId != INVALID_POINTER) {
916 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700917 }
918 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800919 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700920 // i.e. fall through to the next case (don't break)
921 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
922 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700923 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 }
925
926 case MotionEvent.ACTION_DOWN: {
927 final float x = ev.getX();
928 final float y = ev.getY();
929 // Remember location of down touch
930 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700931 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700932 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800933 mLastMotionRemainder = 0;
934 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700935 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700937
Tony Wickhama0aee212019-09-18 09:24:03 -0700938 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700939
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 break;
941 }
942
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800944 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700945 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 break;
947
948 case MotionEvent.ACTION_POINTER_UP:
949 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800950 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 break;
952 }
953
954 /*
955 * The only time we want to intercept motion events is if we are in the
956 * drag mode.
957 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700958 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 }
960
Tony Wickhama0aee212019-09-18 09:24:03 -0700961 /**
962 * If being flinged and user touches the screen, initiate drag; otherwise don't.
963 */
964 private void updateIsBeingDraggedOnTouchDown() {
965 // mScroller.isFinished should be false when being flinged.
966 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -0700967 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -0700968
969 if (finishedScrolling) {
970 mIsBeingDragged = false;
971 if (!mScroller.isFinished() && !mFreeScroll) {
972 setCurrentPage(getNextPage());
973 pageEndTransition();
974 }
975 } else {
976 mIsBeingDragged = true;
977 }
978 }
979
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800980 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700981 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800982 }
983
Adam Cohenf8d28232011-02-01 21:47:00 -0800984 protected void determineScrollingStart(MotionEvent ev) {
985 determineScrollingStart(ev, 1.0f);
986 }
987
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 /*
989 * Determines if we should change the touch state to start scrolling after the
990 * user moves their touch point too far.
991 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800992 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700993 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700995 if (pointerIndex == -1) return;
996
Vinit Nayaka406f722019-12-19 11:50:55 -0800997 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
998 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -0800999 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001000 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001001
Vinit Nayaka406f722019-12-19 11:50:55 -08001002 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001003 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001004 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001005 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1006 mLastMotion = primaryDirection;
1007 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001008 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001009 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001010 // Stop listening for things like pinches.
1011 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001012 }
1013 }
1014
1015 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001016 // Try canceling the long press. It could also have been scheduled
1017 // by a distant descendant, so use the mAllowLongPress flag to block
1018 // everything
1019 final View currentPage = getPageAt(mCurrentPage);
1020 if (currentPage != null) {
1021 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 }
1023 }
1024
Adam Cohenb5ba0972011-09-07 18:02:31 -07001025 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001026 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001027
Adam Cohenedb40762013-07-18 16:45:45 -07001028 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001029 int count = getChildCount();
1030
1031 final int totalDistance;
1032
1033 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001034 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001035 adjacentPage = page - 1;
1036 }
1037
1038 if (adjacentPage < 0 || adjacentPage > count - 1) {
1039 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1040 } else {
1041 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1042 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001043
1044 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001045 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1046 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001047 return scrollProgress;
1048 }
1049
Adam Cohenedb40762013-07-18 16:45:45 -07001050 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001051 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001052 return 0;
1053 } else {
1054 return mPageScrolls[index];
1055 }
1056 }
1057
Adam Cohen564a2e72013-10-09 14:47:32 -07001058 // While layout transitions are occurring, a child's position may stray from its baseline
1059 // position. This method returns the magnitude of this stray at any given time.
1060 public int getLayoutTransitionOffsetForPage(int index) {
1061 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1062 return 0;
1063 } else {
1064 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001065
Sunny Goyal228153d2018-01-04 15:35:22 -08001066 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1067 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001068 return (int) (child.getX() - baselineX);
1069 }
1070 }
1071
Jon Miranda71cb80c2019-01-24 21:25:13 -08001072 @Override
1073 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001074 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001075 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001076 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001077 super.dispatchDraw(canvas);
1078
1079 canvas.restoreToCount(saveCount);
1080 } else {
1081 super.dispatchDraw(canvas);
1082 }
1083 }
1084
Jon Miranda57dab6f2020-04-10 13:38:14 -07001085 /**
1086 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1087 */
1088 private int getSpringOverScroll(int amount) {
1089 if (mScroller.isSpringing()) {
1090 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001091 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001092 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1093 } else {
1094 return 0;
1095 }
1096 }
1097
Sunny Goyalb7b01352018-08-09 12:31:20 -07001098 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001099 if (amount == 0) {
1100 return;
1101 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001102
Vinit Nayaka406f722019-12-19 11:50:55 -08001103 int size = mOrientationHandler.getMeasuredSize(this);
1104 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001105 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001106 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001107 invalidate();
1108 return;
1109 }
1110
Vinit Nayaka406f722019-12-19 11:50:55 -08001111 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1112 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1113 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001114 invalidate();
1115 }
1116
Sunny Goyalb7b01352018-08-09 12:31:20 -07001117 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001118 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001119 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001120 invalidate();
1121 return;
1122 }
1123
Sunny Goyalb7b01352018-08-09 12:31:20 -07001124 if (amount == 0) return;
1125
1126 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001127 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1128 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001129 } else {
1130 dampedOverScroll(amount);
1131 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001132 }
1133
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001134
Tony Wickham023f4042019-01-29 10:52:31 -08001135 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001136 if (mFreeScroll == freeScroll) {
1137 return;
1138 }
1139
Tony Wickham8f7ead32016-04-07 18:46:44 -07001140 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001141 mFreeScroll = freeScroll;
1142
Adam Cohenf9618852013-11-08 06:45:03 -08001143 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001144 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001145 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001146 if (getScrollForPage(getNextPage()) != getScrollX()) {
1147 snapToPage(getNextPage());
1148 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001149 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001150 }
1151
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001152 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001153 mAllowOverScroll = enable;
1154 }
1155
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 @Override
1157 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001158 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001159 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001160
Michael Jurkab8f06722010-10-10 15:58:46 -07001161 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001162
1163 final int action = ev.getAction();
1164
1165 switch (action & MotionEvent.ACTION_MASK) {
1166 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001167 updateIsBeingDraggedOnTouchDown();
1168
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 /*
1170 * If being flinged and user touches, stop the fling. isFinished
1171 * will be false if being flinged.
1172 */
1173 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001174 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 }
1176
1177 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001178 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001179 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001180 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1181 mLastMotionRemainder = 0;
1182 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001183 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001185 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001186 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001187 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001188 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 break;
1190
Winson Chung4941f652020-06-18 15:11:53 -07001191 case ACTION_MOVE_ALLOW_EASY_FLING:
1192 // Start scrolling immediately
1193 determineScrollingStart(ev);
1194 mAllowEasyFling = true;
1195 break;
1196
1197 case MotionEvent.ACTION_MOVE:
1198 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 // Scroll to follow the motion event
1200 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001201
1202 if (pointerIndex == -1) return true;
1203
Vinit Nayaka406f722019-12-19 11:50:55 -08001204 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1205 float delta = mLastMotion + mLastMotionRemainder - direction;
1206 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001207
Winson Chungc0844aa2011-02-02 15:25:58 -08001208 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1209 // keep the remainder because we are actually testing if we've moved from the last
1210 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001211 if (Math.abs(delta) >= 1.0f) {
1212 mLastMotion = direction;
1213 mLastMotionRemainder = delta - (int) delta;
1214
1215 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 } else {
1217 awakenScrollBars();
1218 }
Adam Cohen564976a2010-10-13 18:52:07 -07001219 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 determineScrollingStart(ev);
1221 }
1222 break;
1223
1224 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001225 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 final int activePointerId = mActivePointerId;
1227 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001228 if (pointerIndex == -1) return true;
1229
Vinit Nayaka406f722019-12-19 11:50:55 -08001230 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1231 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001232 final VelocityTracker velocityTracker = mVelocityTracker;
1233 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001234
Vinit Nayaka406f722019-12-19 11:50:55 -08001235 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1236 mActivePointerId);
1237 int delta = (int) (primaryDirection - mDownMotionPrimary);
1238 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1239
1240 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1241 SIGNIFICANT_MOVE_THRESHOLD;
1242
1243 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001244 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1245 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001246 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1247 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001248 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1249 Log.d("Quickswitch", "isFling=false vel=" + velocity
1250 + " threshold=" + mEasyFlingThresholdVelocity);
1251 }
Adam Cohen00481b32011-11-18 12:03:48 -08001252
Adam Cohenf358a4b2013-07-23 16:47:31 -07001253 if (!mFreeScroll) {
1254 // In the case that the page is moved far to one direction and then is flung
1255 // in the opposite direction, we use a threshold to determine whether we should
1256 // just return to the starting page, or if we should skip one further.
1257 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001258 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1259 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 returnToOriginalPage = true;
1261 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001262
Adam Cohenf358a4b2013-07-23 16:47:31 -07001263 int finalPage;
1264 // We give flings precedence over large moves, which is why we short-circuit our
1265 // test for a large move if a fling has been registered. That is, a large
1266 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001267
Vinit Nayaka406f722019-12-19 11:50:55 -08001268 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1269 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001270 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001271 snapToPageWithVelocity(finalPage, velocity);
1272 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1273 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001274 mCurrentPage < getChildCount() - 1) {
1275 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001276 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001277 } else {
1278 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001279 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001281 if (!mScroller.isFinished()) {
1282 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001283 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001284
Vinit Nayaka406f722019-12-19 11:50:55 -08001285 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1286 int maxScroll = mMaxScroll;
1287 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001288
Vinit Nayaka406f722019-12-19 11:50:55 -08001289 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1290 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1291 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001292 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001293 } else {
1294 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001295 mScroller.fling(initialScroll, -velocity,
1296 minScroll, maxScroll,
1297 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001298
Vinit Nayaka406f722019-12-19 11:50:55 -08001299 int finalPos = mScroller.getFinalPos();
1300 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001301
1302 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1303 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001304 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001305 // If scrolling ends in the half of the added space that is closer to
1306 // the end, settle to the end. Otherwise snap to the nearest page.
1307 // If flinging past one of the ends, don't change the velocity as it
1308 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001309 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1310 ? minScroll
1311 : finalPos > (lastPageScroll + maxScroll) / 2
1312 ? maxScroll
1313 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001314
Vinit Nayaka406f722019-12-19 11:50:55 -08001315 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001316 // Ensure the scroll/snap doesn't happen too fast;
1317 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001318 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001319 if (extraScrollDuration > 0) {
1320 mScroller.extendDuration(extraScrollDuration);
1321 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001322 }
1323 }
1324 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001326 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001328
Adam Cohen7d30a372013-07-01 17:03:59 -07001329 // End any intermediate reordering states
1330 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 break;
1332
1333 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001334 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001335 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001336 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001337 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001338 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 break;
1340
1341 case MotionEvent.ACTION_POINTER_UP:
1342 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001343 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 break;
1345 }
1346
1347 return true;
1348 }
1349
Vinit Nayaka406f722019-12-19 11:50:55 -08001350 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001351 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1352 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001353 }
1354
Adam Cohen7d30a372013-07-01 17:03:59 -07001355 private void resetTouchState() {
1356 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001357 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001358 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001359 }
1360
Adam Cohenc2d6e892014-10-16 09:49:24 -07001361 /**
1362 * Triggered by scrolling via touch
1363 */
1364 protected void onScrollInteractionBegin() {
1365 }
1366
1367 protected void onScrollInteractionEnd() {
1368 }
1369
Winson Chung185d7162011-02-28 13:47:29 -08001370 @Override
1371 public boolean onGenericMotionEvent(MotionEvent event) {
1372 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1373 switch (event.getAction()) {
1374 case MotionEvent.ACTION_SCROLL: {
1375 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1376 final float vscroll;
1377 final float hscroll;
1378 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1379 vscroll = 0;
1380 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1381 } else {
1382 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1383 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1384 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001385 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1386 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001387 }
Winson Chung185d7162011-02-28 13:47:29 -08001388 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001389 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001390 : (hscroll > 0 || vscroll > 0);
1391 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001392 scrollRight();
1393 } else {
1394 scrollLeft();
1395 }
1396 return true;
1397 }
1398 }
1399 }
1400 }
1401 return super.onGenericMotionEvent(event);
1402 }
1403
Sunny Goyal8b37c572020-03-31 12:12:14 -07001404 /**
1405 * Returns true if the paged view can scroll for the provided vertical and horizontal
1406 * scroll values
1407 */
1408 protected boolean canScroll(float absVScroll, float absHScroll) {
1409 ActivityContext ac = ActivityContext.lookupContext(getContext());
1410 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001411 }
1412
Michael Jurkab8f06722010-10-10 15:58:46 -07001413 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1414 if (mVelocityTracker == null) {
1415 mVelocityTracker = VelocityTracker.obtain();
1416 }
1417 mVelocityTracker.addMovement(ev);
1418 }
1419
1420 private void releaseVelocityTracker() {
1421 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001422 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001423 mVelocityTracker.recycle();
1424 mVelocityTracker = null;
1425 }
1426 }
1427
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001429 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 final int pointerId = ev.getPointerId(pointerIndex);
1431 if (pointerId == mActivePointerId) {
1432 // This was our active pointer going up. Choose a new
1433 // active pointer and adjust accordingly.
1434 // TODO: Make this decision more intelligent.
1435 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001436 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1437 newPointerIndex);
1438 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 mActivePointerId = ev.getPointerId(newPointerIndex);
1440 if (mVelocityTracker != null) {
1441 mVelocityTracker.clear();
1442 }
1443 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001444 }
1445
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 @Override
1447 public void requestChildFocus(View child, View focused) {
1448 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001449 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001450 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001451 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 }
1453 }
1454
Sunny Goyal228153d2018-01-04 15:35:22 -08001455 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001456 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001457 }
1458
Vinit Nayaka406f722019-12-19 11:50:55 -08001459 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1460 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1461 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001462 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 final int childCount = getChildCount();
1465 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001466 int distanceFromScreenCenter = Math.abs(
1467 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1469 minDistanceFromScreenCenter = distanceFromScreenCenter;
1470 minDistanceFromScreenCenterIndex = i;
1471 }
1472 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001473 return minDistanceFromScreenCenterIndex;
1474 }
1475
Tony Wickham7383d4e2020-07-07 19:25:25 -07001476 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chau7944ee52021-01-08 19:07:25 +00001477 int childSize = getChildVisibleSize(childIndex);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001478 int halfChildSize = (childSize / 2);
1479 int childCenter = getChildOffset(childIndex) + halfChildSize;
1480 return childCenter - screenCenter;
1481 }
1482
1483 protected int getDisplacementFromScreenCenter(int childIndex) {
1484 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1485 int screenCenter = mOrientationHandler.getPrimaryScroll(this) + (pageOrientationSize / 2);
1486 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1487 }
1488
Adam Cohend19d3ca2010-09-15 14:43:42 -07001489 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001490 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1491 }
1492
1493 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001494 int scroll = mOrientationHandler.getPrimaryScroll(this);
1495 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001496 }
1497
1498 protected int getPageSnapDuration() {
1499 if (isInOverScroll()) {
1500 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1501 }
1502 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 }
1504
Adam Cohene0f66b52010-11-23 15:06:07 -08001505 // We want the duration of the page snap animation to be influenced by the distance that
1506 // the screen has to travel, however, we don't want this duration to be effected in a
1507 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1508 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001509 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001510 f -= 0.5f; // center the values about 0.
1511 f *= 0.3f * Math.PI / 2.0f;
1512 return (float) Math.sin(f);
1513 }
1514
Winson Chung05a31ed2018-02-08 17:08:43 -08001515 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001516 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001517 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001518
Vinit Nayaka406f722019-12-19 11:50:55 -08001519 final int newLoc = getScrollForPage(whichPage);
1520 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001521 int duration = 0;
1522
Sunny Goyal4d113a52015-05-27 10:05:28 -07001523 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001524 // If the velocity is low enough, then treat this more as an automatic page advance
1525 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001526 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001527 }
1528
1529 // Here we compute a "distance" that will be used in the computation of the overall
1530 // snap duration. This is a function of the actual distance that needs to be traveled;
1531 // we keep this value close to half screen size in order to reduce the variance in snap
1532 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001533 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001534 float distance = halfScreenSize + halfScreenSize *
1535 distanceInfluenceForSnapDuration(distanceRatio);
1536
1537 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001538 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001539
1540 // we want the page's snap velocity to approximately match the velocity at which the
1541 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001542 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1543 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001544
Jon Miranda9e198662020-03-11 14:42:02 -07001545 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001546 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001547 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001548 } else {
1549 return snapToPage(whichPage, delta, duration);
1550 }
Michael Jurka0142d492010-08-25 17:46:15 -07001551 }
1552
Winson Chung05a31ed2018-02-08 17:08:43 -08001553 public boolean snapToPage(int whichPage) {
1554 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001555 }
1556
Winson Chung05a31ed2018-02-08 17:08:43 -08001557 public boolean snapToPageImmediately(int whichPage) {
1558 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001559 }
1560
Winson Chung05a31ed2018-02-08 17:08:43 -08001561 public boolean snapToPage(int whichPage, int duration) {
1562 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001563 }
1564
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001565 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001566 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001567 }
1568
Winson Chung05a31ed2018-02-08 17:08:43 -08001569 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001570 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001571 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001572
Vinit Nayaka406f722019-12-19 11:50:55 -08001573 int newLoc = getScrollForPage(whichPage);
1574 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001575 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001576 }
1577
Winson Chung05a31ed2018-02-08 17:08:43 -08001578 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001579 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001580 }
Michael Jurka0142d492010-08-25 17:46:15 -07001581
Winson Chung05a31ed2018-02-08 17:08:43 -08001582 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001583 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001584 if (mFirstLayout) {
1585 setCurrentPage(whichPage);
1586 return false;
1587 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001588
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001589 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001590 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1591 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001592 }
1593
Adam Cohen6f127a62014-06-12 14:54:41 -07001594 whichPage = validateNewPage(whichPage);
1595
Adam Cohen7d30a372013-07-01 17:03:59 -07001596 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001597
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001599 if (immediate) {
1600 duration = 0;
1601 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001602 duration = Math.abs(delta);
1603 }
1604
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001605 if (duration != 0) {
1606 pageBeginTransition();
1607 }
1608
Adam Cohenf358a4b2013-07-23 16:47:31 -07001609 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001610 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001611 }
Adam Cohenf9618852013-11-08 06:45:03 -08001612
1613 if (interpolator != null) {
1614 mScroller.setInterpolator(interpolator);
1615 } else {
1616 mScroller.setInterpolator(mDefaultInterpolator);
1617 }
1618
Jon Miranda71cb80c2019-01-24 21:25:13 -08001619 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001620 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001621 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001622 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001623 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001624
Adam Cohen674531f2013-12-13 15:07:14 -08001625 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001626
1627 // Trigger a compute() to finish switching pages if necessary
1628 if (immediate) {
1629 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001630 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001631 }
1632
Winson Chung321e9ee2010-08-09 13:37:56 -07001633 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001634 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001635 }
1636
Vadim Tryshev98913d02018-05-18 18:41:34 -07001637 public boolean scrollLeft() {
1638 if (getNextPage() > 0) {
1639 snapToPage(getNextPage() - 1);
1640 return true;
1641 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001642 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001643 }
1644
Vadim Tryshev98913d02018-05-18 18:41:34 -07001645 public boolean scrollRight() {
1646 if (getNextPage() < getChildCount() - 1) {
1647 snapToPage(getNextPage() + 1);
1648 return true;
1649 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001650 return onOverscroll(getMeasuredWidth());
1651 }
1652
1653 protected boolean onOverscroll(int amount) {
1654 if (!mAllowOverScroll) return false;
1655 onScrollInteractionBegin();
1656 overScroll(amount);
1657 onScrollInteractionEnd();
1658 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 }
1660
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001661 @Override
1662 public CharSequence getAccessibilityClassName() {
1663 // Some accessibility services have special logic for ScrollView. Since we provide same
1664 // accessibility info as ScrollView, inform the service to handle use the same way.
1665 return ScrollView.class.getName();
1666 }
1667
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001668 protected boolean isPageOrderFlipped() {
1669 return false;
1670 }
1671
Winson Chung6a0f57d2011-06-29 20:10:49 -07001672 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001673 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001674 @Override
1675 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1676 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001677 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001678 int offset = (mAllowOverScroll ? 0 : 1);
1679 info.setScrollable(getPageCount() > offset);
1680 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001681 info.addAction(pagesFlipped ?
1682 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1683 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1684 info.addAction(mIsRtl ?
1685 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1686 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001687 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001688 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001689 info.addAction(pagesFlipped ?
1690 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1691 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1692 info.addAction(mIsRtl ?
1693 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1694 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001695 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001696 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1697 // Besides disabling the accessibility long-click, this also prevents this view from getting
1698 // accessibility focus.
1699 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001700 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001701 }
1702
1703 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001704 public void sendAccessibilityEvent(int eventType) {
1705 // Don't let the view send real scroll events.
1706 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1707 super.sendAccessibilityEvent(eventType);
1708 }
1709 }
1710
1711 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001712 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1713 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001714 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001715 }
1716
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001717 @Override
1718 public boolean performAccessibilityAction(int action, Bundle arguments) {
1719 if (super.performAccessibilityAction(action, arguments)) {
1720 return true;
1721 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001722 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001723 switch (action) {
1724 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001725 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001726 return true;
1727 }
1728 } break;
1729 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001730 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001731 return true;
1732 }
yingleiw02cc8482019-08-02 16:14:27 -07001733 } break;
1734 case android.R.id.accessibilityActionPageRight: {
1735 if (!mIsRtl) {
1736 return scrollRight();
1737 } else {
1738 return scrollLeft();
1739 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001740 }
yingleiw02cc8482019-08-02 16:14:27 -07001741 case android.R.id.accessibilityActionPageLeft: {
1742 if (!mIsRtl) {
1743 return scrollLeft();
1744 } else {
1745 return scrollRight();
1746 }
1747 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001748 }
1749 return false;
1750 }
1751
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001752 protected boolean canAnnouncePageDescription() {
1753 return true;
1754 }
1755
Adam Cohen0ffac432013-07-10 11:19:26 -07001756 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001757 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001758 getNextPage() + 1, getChildCount());
1759 }
1760
Tony Mak2e564402018-02-27 17:19:34 +00001761 protected float getDownMotionX() {
1762 return mDownMotionX;
1763 }
1764
1765 protected float getDownMotionY() {
1766 return mDownMotionY;
1767 }
1768
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001769 protected interface ComputePageScrollsLogic {
1770
1771 boolean shouldIncludeView(View view);
1772 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001773
1774 public int[] getVisibleChildrenRange() {
1775 float visibleLeft = 0;
1776 float visibleRight = visibleLeft + getMeasuredWidth();
1777 float scaleX = getScaleX();
1778 if (scaleX < 1 && scaleX > 0) {
1779 float mid = getMeasuredWidth() / 2;
1780 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1781 visibleRight = mid + ((visibleRight - mid) / scaleX);
1782 }
1783
1784 int leftChild = -1;
1785 int rightChild = -1;
1786 final int childCount = getChildCount();
1787 for (int i = 0; i < childCount; i++) {
1788 final View child = getPageAt(i);
1789
1790 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1791 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1792 if (leftChild == -1) {
1793 leftChild = i;
1794 }
1795 rightChild = i;
1796 }
1797 }
1798 mTmpIntPair[0] = leftChild;
1799 mTmpIntPair[1] = rightChild;
1800 return mTmpIntPair;
1801 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001802}