blob: ada297f34027be1d957ec2cbc6aac02d02db255d [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;
Adam Cohenf358a4b2013-07-23 16:47:31 -070099
Winson Chung4941f652020-06-18 15:11:53 -0700100 protected final int mFlingThresholdVelocity;
101 protected final int mEasyFlingThresholdVelocity;
102 protected final int mMinFlingVelocity;
103 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700104
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected boolean mFirstLayout = true;
106
Sunny Goyal4ffec482016-02-09 11:28:52 -0800107 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700108 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700109
Sunny Goyal4ffec482016-02-09 11:28:52 -0800110 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700111 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800112 protected int mMaxScroll;
113 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700114 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800115 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800117 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
119 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700120 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800121 private float mDownMotionPrimary;
122 private float mLastMotion;
123 private float mLastMotionRemainder;
124 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700125 // Used in special cases where the fling checks can be relaxed for an intentional gesture
126 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700127 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700128
Vadim Trysheve628c472018-05-16 14:28:18 -0700129 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700130 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800131
Winson Chung4941f652020-06-18 15:11:53 -0700132 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700134 // The amount of movement to begin paging
135 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700143 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700144 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Vinit Nayaka406f722019-12-19 11:50:55 -0800146 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800147
Sunny Goyal061380a2016-02-29 15:15:03 -0800148 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700149
Vinit Nayaka406f722019-12-19 11:50:55 -0800150 protected int mUnboundedScroll;
151
Winson Chungd2be3812013-07-16 11:11:32 -0700152 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800154 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700155
John Spurlock77e1f472013-09-11 10:09:51 -0400156 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800157 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400158
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800159 // Similar to the platform implementation of isLayoutValid();
160 protected boolean mIsLayoutValid;
161
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700162 private int[] mTmpIntPair = new int[2];
163
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 public PagedView(Context context) {
165 this(context, null);
166 }
167
168 public PagedView(Context context, AttributeSet attrs) {
169 this(context, attrs, 0);
170 }
171
172 public PagedView(Context context, AttributeSet attrs, int defStyle) {
173 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700174
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 TypedArray a = context.obtainStyledAttributes(attrs,
176 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700177 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700178 a.recycle();
179
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700181 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Vinit Nayaka406f722019-12-19 11:50:55 -0800183 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700184 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700185 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Vinit Nayaka406f722019-12-19 11:50:55 -0800187 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700188 mTouchSlop = configuration.getScaledTouchSlop();
189 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800191
Sunny Goyalcf25b522015-07-09 00:01:18 -0700192 float density = getResources().getDisplayMetrics().density;
193 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700194 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700195 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
196 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700197
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700198 setDefaultFocusHighlightEnabled(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 }
200
Adam Cohenf9618852013-11-08 06:45:03 -0800201 protected void setDefaultInterpolator(Interpolator interpolator) {
202 mDefaultInterpolator = interpolator;
203 mScroller.setInterpolator(mDefaultInterpolator);
204 }
205
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700206 public void initParentViews(View parent) {
207 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800208 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700209 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700210 }
211 }
212
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800213 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700214 return mPageIndicator;
215 }
216
Adam Cohen674531f2013-12-13 15:07:14 -0800217 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700218 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
219 * that the user was on before entering free scroll mode (e.g. the home screen page they
220 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
221 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700223 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700224 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700226
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700227 /**
228 * Returns the index of page to be shown immediately afterwards.
229 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700230 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700231 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
232 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700233
Adam Cohenf9c184a2016-01-15 16:47:43 -0800234 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 return getChildCount();
236 }
237
Sunny Goyal83a8f042015-05-19 12:52:12 -0700238 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 return getChildAt(index);
240 }
241
Adam Cohenae4f1552011-10-20 00:15:42 -0700242 protected int indexToPage(int index) {
243 return index;
244 }
245
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800247 * Updates the scroll of the current page immediately to its final scroll position. We use this
248 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
249 * the previous tab page.
250 */
251 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700252 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800253 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700256 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800257 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
258 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700259 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800260 }
261
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700262 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700263 mScroller.abortAnimation();
264 // We need to clean up the next page here to avoid computeScrollHelper from
265 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700266 if (resetNextPage) {
267 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800268 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700269 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700270 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271
Tony Wickham8f7ead32016-04-07 18:46:44 -0700272 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273 mScroller.forceFinished(true);
274 // We need to clean up the next page here to avoid computeScrollHelper from
275 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700276 if (resetNextPage) {
277 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800278 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700279 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700280 }
281
Adam Cohen6f127a62014-06-12 14:54:41 -0700282 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700283 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700284 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700285 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 }
287
Chet Haasebc2f0822012-10-26 17:59:53 -0700288 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700289 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
290 */
291 private int ensureWithinScrollBounds(int page) {
292 int dir = !mIsRtl ? 1 : - 1;
293 int currScroll = getScrollForPage(page);
294 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800295 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700296 page += dir;
297 prevScroll = currScroll;
298 currScroll = getScrollForPage(page);
299 if (currScroll <= prevScroll) {
300 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
301 break;
302 }
303 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800304 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700305 page -= dir;
306 prevScroll = currScroll;
307 currScroll = getScrollForPage(page);
308 if (currScroll >= prevScroll) {
309 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
310 break;
311 }
312 }
313 return page;
314 }
315
Tony Wickham03f27012019-04-25 14:22:54 -0700316 public void setCurrentPage(int currentPage) {
317 setCurrentPage(currentPage, INVALID_PAGE);
318 }
319
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700320 /**
Winson Chung86f77532010-08-24 11:08:22 -0700321 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700322 */
Tony Wickham03f27012019-04-25 14:22:54 -0700323 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800324 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700325 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800326 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800327 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
328 // the default
329 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800330 return;
331 }
Tony Wickham03f27012019-04-25 14:22:54 -0700332 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700333 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700334 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700335 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800336 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 }
338
Winson Chung8c87cd82013-07-23 16:20:10 -0700339 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800340 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
341 * has settled.
342 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700343 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800344 updatePageIndicator();
345 }
346
347 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700348 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000349 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700350 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700352 protected void pageBeginTransition() {
353 if (!mIsPageInTransition) {
354 mIsPageInTransition = true;
355 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700356 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700357 }
358
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700359 protected void pageEndTransition() {
360 if (mIsPageInTransition) {
361 mIsPageInTransition = false;
362 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700363 }
Michael Jurka0142d492010-08-25 17:46:15 -0700364 }
365
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700366 protected boolean isPageInTransition() {
367 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700368 }
369
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700370 /**
371 * Called when the page starts moving as part of the scroll. Subclasses can override this
372 * to provide custom behavior during animation.
373 */
374 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700375 }
376
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700377 /**
378 * Called when the page ends moving as part of the scroll. Subclasses can override this
379 * to provide custom behavior during animation.
380 */
381 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700382 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700383 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800384 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
385 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700386 if (mOnPageTransitionEndCallback != null) {
387 mOnPageTransitionEndCallback.run();
388 mOnPageTransitionEndCallback = null;
389 }
390 }
391
392 /**
393 * Sets a callback to run once when the scrolling finishes. If there is currently
394 * no page in transition, then the callback is called immediately.
395 */
396 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
397 if (mIsPageInTransition || callback == null) {
398 mOnPageTransitionEndCallback = callback;
399 } else {
400 callback.run();
401 }
Michael Jurka0142d492010-08-25 17:46:15 -0700402 }
403
Vinit Nayaka406f722019-12-19 11:50:55 -0800404 protected int getUnboundedScroll() {
405 return mUnboundedScroll;
406 }
407
Winson Chung321e9ee2010-08-09 13:37:56 -0700408 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800409 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800410 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800411 }
412
413 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700414 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800415 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
416 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
417 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700418
Vinit Nayaka406f722019-12-19 11:50:55 -0800419 boolean isBeforeFirstPage = mIsRtl ?
420 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
421 boolean isAfterLastPage = mIsRtl ?
422 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
423 if (!isBeforeFirstPage && !isAfterLastPage) {
424 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800425 }
426
Vinit Nayaka406f722019-12-19 11:50:55 -0800427 if (isBeforeFirstPage) {
428 mOrientationHandler.delegateScrollTo(this,
429 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800430 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700431 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800432 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800433 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800434 } else if (isAfterLastPage) {
435 mOrientationHandler.delegateScrollTo(this,
436 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800437 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700438 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800439 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800440 }
441 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700442 if (mWasInOverscroll) {
443 overScroll(0);
444 mWasInOverscroll = false;
445 }
Adam Cohen68d73932010-11-15 10:50:58 -0800446 super.scrollTo(x, y);
447 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800448 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700449
Vinit Nayaka406f722019-12-19 11:50:55 -0800450 /**
451 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
452 */
453 public void superScrollTo(int x, int y) {
454 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700455 }
456
Adam Cohen53805212013-10-01 10:39:23 -0700457 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800458 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700459 if (mCurrentPage != getNextPage()) {
460 AccessibilityEvent ev =
461 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700462 ev.setScrollable(true);
463 ev.setScrollX(getScrollX());
464 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800465 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700466 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700467 }
Adam Cohen53805212013-10-01 10:39:23 -0700468 }
469 }
470
Michael Jurka0142d492010-08-25 17:46:15 -0700471 // we moved this functionality to a helper function so SmoothPagedView can reuse it
472 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800473 return computeScrollHelper(true);
474 }
475
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700476 protected void announcePageForAccessibility() {
477 if (isAccessibilityEnabled(getContext())) {
478 // Notify the user when the page changes
479 announceForAccessibility(getCurrentPageDescription());
480 }
481 }
482
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800483 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700485 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800486 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
487 if (mUnboundedScroll != mScroller.getCurrPos()
488 || currentScroll != mScroller.getCurrPos()) {
489 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700490 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800491 if (shouldInvalidate) {
492 invalidate();
493 }
Michael Jurka0142d492010-08-25 17:46:15 -0700494 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800495 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700496 sendScrollAccessibilityEvent();
497
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700498 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700499 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700500 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700501 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700502
Adam Cohen73aa9752010-11-24 16:26:58 -0800503 // We don't want to trigger a page end moving unless the page has settled
504 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700505 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700506 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800507 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700508
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700509 if (canAnnouncePageDescription()) {
510 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700511 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 }
Michael Jurka0142d492010-08-25 17:46:15 -0700513 return false;
514 }
515
516 @Override
517 public void computeScroll() {
518 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
520
Sunny Goyalce8809a2018-01-18 14:02:47 -0800521 public int getExpectedHeight() {
522 return getMeasuredHeight();
523 }
524
Adam Cohen410f3cd2013-09-22 12:09:32 -0700525 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800526 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800527 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700528 }
529
Sunny Goyalce8809a2018-01-18 14:02:47 -0800530 public int getExpectedWidth() {
531 return getMeasuredWidth();
532 }
533
Sunny Goyalbc683e92017-12-06 10:25:07 -0800534 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800535 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800536 - mInsets.left - mInsets.right;
537 }
538
Winson Chung321e9ee2010-08-09 13:37:56 -0700539 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800540 public void requestLayout() {
541 mIsLayoutValid = false;
542 super.requestLayout();
543 }
544
545 @Override
546 public void forceLayout() {
547 mIsLayoutValid = false;
548 super.forceLayout();
549 }
550
551 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700553 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700554 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
555 return;
556 }
557
Adam Cohen7d30a372013-07-01 17:03:59 -0700558 // We measure the dimensions of the PagedView to be larger than the pages so that when we
559 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700560 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
561 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
562 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700563 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700564
Adam Cohen7d30a372013-07-01 17:03:59 -0700565 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
566 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
567 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569
Winson Chung8aad6102012-05-11 16:27:49 -0700570 // Return early if we aren't given a proper dimension
571 if (widthSize <= 0 || heightSize <= 0) {
572 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
573 return;
574 }
575
Winson Chung321e9ee2010-08-09 13:37:56 -0700576 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700577 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700578 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700579
Sunny Goyalac00cba2017-11-13 15:58:01 -0800580 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800581 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800582 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800583 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700584
Sunny Goyalac00cba2017-11-13 15:58:01 -0800585 // measureChildren takes accounts for content padding, we only need to care about extra
586 // space due to insets.
587 measureChildren(myWidthSpec, myHeightSpec);
588 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800589 }
590
Sunny Goyalcf25b522015-07-09 00:01:18 -0700591 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700592 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700593 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800594 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700595 final int childCount = getChildCount();
596 boolean pageScrollChanged = false;
597 if (mPageScrolls == null || childCount != mPageScrolls.length) {
598 mPageScrolls = new int[childCount];
599 pageScrollChanged = true;
600 }
601
602 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700603 return;
604 }
605
Winson Chung785d2eb2011-04-14 16:08:02 -0700606 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700607
Vinit Nayaka406f722019-12-19 11:50:55 -0800608 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
609 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700610 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700612
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700613 final LayoutTransition transition = getLayoutTransition();
614 // If the transition is running defer updating max scroll, as some empty pages could
615 // still be present, and a max scroll change could cause sudden jumps in scroll.
616 if (transition != null && transition.isRunning()) {
617 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
618
619 @Override
620 public void startTransition(LayoutTransition transition, ViewGroup container,
621 View view, int transitionType) { }
622
623 @Override
624 public void endTransition(LayoutTransition transition, ViewGroup container,
625 View view, int transitionType) {
626 // Wait until all transitions are complete.
627 if (!transition.isRunning()) {
628 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700629 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700630 }
631 }
632 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700633 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700634 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700635 }
636
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700637 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
638 updateCurrentPageScroll();
639 mFirstLayout = false;
640 }
641
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800642 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700643 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700644 }
Winson Chunge3193b92010-09-10 11:44:42 -0700645 }
646
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700647 /**
648 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
649 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700650 */
651 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
652 ComputePageScrollsLogic scrollLogic) {
653 final int childCount = getChildCount();
654
655 final int startIndex = mIsRtl ? childCount - 1 : 0;
656 final int endIndex = mIsRtl ? -1 : childCount;
657 final int delta = mIsRtl ? -1 : 1;
658
Vinit Nayaka406f722019-12-19 11:50:55 -0800659 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700660
Vinit Nayaka406f722019-12-19 11:50:55 -0800661 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
662 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700663 boolean pageScrollChanged = false;
664
Vinit Nayaka406f722019-12-19 11:50:55 -0800665 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700666 final View child = getPageAt(i);
667 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800668 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
669 pageCenter, layoutChildren);
670 final int primaryDimension = bounds.primaryDimension;
671 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700672
Sunny Goyalc82c6392018-06-06 15:39:13 -0700673 // In case the pages are of different width, align the page to left or right edge
674 // based on the orientation.
675 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800676 ? (childStart - scrollOffsetStart)
677 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700678 if (outPageScrolls[i] != pageScroll) {
679 pageScrollChanged = true;
680 outPageScrolls[i] = pageScroll;
681 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800682 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700683 }
684 }
685 return pageScrollChanged;
686 }
687
Sunny Goyala1fbd842015-05-20 13:40:27 -0700688 protected int getChildGap() {
689 return 0;
690 }
691
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700692 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800693 mMinScroll = computeMinScroll();
694 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700695 }
696
Vinit Nayaka406f722019-12-19 11:50:55 -0800697 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700698 return 0;
Tony50876bf2018-09-19 23:09:14 -0400699 }
700
Vinit Nayaka406f722019-12-19 11:50:55 -0800701 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700702 int childCount = getChildCount();
703 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700704 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700705 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700706 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700707 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700708 }
709 }
710
Adam Cohen3b185e22013-10-29 14:45:58 -0700711 public void setPageSpacing(int pageSpacing) {
712 mPageSpacing = pageSpacing;
713 requestLayout();
714 }
715
Tony50876bf2018-09-19 23:09:14 -0400716 public int getPageSpacing() {
717 return mPageSpacing;
718 }
719
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800720 private void dispatchPageCountChanged() {
721 if (mPageIndicator != null) {
722 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700723 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700724 // This ensures that when children are added, they get the correct transforms / alphas
725 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700726 invalidate();
727 }
728
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800730 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800731 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800732 dispatchPageCountChanged();
733 }
734
735 @Override
736 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800737 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700738 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800739 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700740 }
741
Adam Cohen73894962011-10-31 13:17:17 -0700742 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700743 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800744 View pageAtIndex = getPageAt(index);
745 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700746 }
747
Alex Chau7944ee52021-01-08 19:07:25 +0000748 protected int getChildVisibleSize(int index) {
749 View layout = getPageAt(index);
750 return mOrientationHandler.getMeasuredSize(layout);
751 }
752
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 @Override
754 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700755 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700756 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700757 if (immediate) {
758 setCurrentPage(page);
759 } else {
760 snapToPage(page);
761 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 return true;
763 }
764 return false;
765 }
766
767 @Override
768 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700769 int focusablePage;
770 if (mNextPage != INVALID_PAGE) {
771 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700773 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 }
Winson Chung86f77532010-08-24 11:08:22 -0700775 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700777 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 }
779 return false;
780 }
781
782 @Override
783 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800784 if (super.dispatchUnhandledMove(focused, direction)) {
785 return true;
786 }
787
788 if (mIsRtl) {
789 if (direction == View.FOCUS_LEFT) {
790 direction = View.FOCUS_RIGHT;
791 } else if (direction == View.FOCUS_RIGHT) {
792 direction = View.FOCUS_LEFT;
793 }
794 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700796 if (getCurrentPage() > 0) {
797 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700798 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 return true;
800 }
801 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700802 if (getCurrentPage() < getPageCount() - 1) {
803 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700804 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 return true;
806 }
807 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800808 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 }
810
811 @Override
812 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700813 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
814 return;
815 }
816
Adam Cohen0ffac432013-07-10 11:19:26 -0700817 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700818 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700819 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 }
821 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700822 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700823 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
825 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700826 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700827 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 }
829 }
830 }
831
832 /**
833 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700834 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 *
Winson Chung86f77532010-08-24 11:08:22 -0700836 * This happens when live folders requery, and if they're off page, they
837 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 */
839 @Override
840 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700841 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 View v = focused;
843 while (true) {
844 if (v == current) {
845 super.focusableViewAvailable(focused);
846 return;
847 }
848 if (v == this) {
849 return;
850 }
851 ViewParent parent = v.getParent();
852 if (parent instanceof View) {
853 v = (View)v.getParent();
854 } else {
855 return;
856 }
857 }
858 }
859
860 /**
861 * {@inheritDoc}
862 */
863 @Override
864 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
865 if (disallowIntercept) {
866 // We need to make sure to cancel our long press if
867 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700868 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700869 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 }
871 super.requestDisallowInterceptTouchEvent(disallowIntercept);
872 }
873
874 @Override
875 public boolean onInterceptTouchEvent(MotionEvent ev) {
876 /*
877 * This method JUST determines whether we want to intercept the motion.
878 * If we return true, onTouchEvent will be called and we do the actual
879 * scrolling there.
880 */
881
Winson Chung45e1d6e2010-11-09 17:19:49 -0800882 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700883 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700884
885 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800886
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 /*
888 * Shortcut the most recurring case: the user is in the dragging
889 * state and he is moving his finger. We want to intercept this
890 * motion.
891 */
892 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700893 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 return true;
895 }
896
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 switch (action & MotionEvent.ACTION_MASK) {
898 case MotionEvent.ACTION_MOVE: {
899 /*
900 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800901 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700903 if (mActivePointerId != INVALID_POINTER) {
904 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700905 }
906 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800907 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700908 // i.e. fall through to the next case (don't break)
909 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
910 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700911 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 }
913
914 case MotionEvent.ACTION_DOWN: {
915 final float x = ev.getX();
916 final float y = ev.getY();
917 // Remember location of down touch
918 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700919 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700920 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800921 mLastMotionRemainder = 0;
922 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700923 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700925
Tony Wickhama0aee212019-09-18 09:24:03 -0700926 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700927
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 break;
929 }
930
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800932 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700933 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 break;
935
936 case MotionEvent.ACTION_POINTER_UP:
937 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800938 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 break;
940 }
941
942 /*
943 * The only time we want to intercept motion events is if we are in the
944 * drag mode.
945 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700946 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 }
948
Tony Wickhama0aee212019-09-18 09:24:03 -0700949 /**
950 * If being flinged and user touches the screen, initiate drag; otherwise don't.
951 */
952 private void updateIsBeingDraggedOnTouchDown() {
953 // mScroller.isFinished should be false when being flinged.
954 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -0700955 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -0700956
957 if (finishedScrolling) {
958 mIsBeingDragged = false;
959 if (!mScroller.isFinished() && !mFreeScroll) {
960 setCurrentPage(getNextPage());
961 pageEndTransition();
962 }
963 } else {
964 mIsBeingDragged = true;
965 }
966 }
967
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800968 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700969 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800970 }
971
Adam Cohenf8d28232011-02-01 21:47:00 -0800972 protected void determineScrollingStart(MotionEvent ev) {
973 determineScrollingStart(ev, 1.0f);
974 }
975
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 /*
977 * Determines if we should change the touch state to start scrolling after the
978 * user moves their touch point too far.
979 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800980 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700981 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700983 if (pointerIndex == -1) return;
984
Vinit Nayaka406f722019-12-19 11:50:55 -0800985 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
986 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -0800987 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -0700988 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -0700989
Vinit Nayaka406f722019-12-19 11:50:55 -0800990 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700991 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700992 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800993 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
994 mLastMotion = primaryDirection;
995 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700996 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700997 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800998 // Stop listening for things like pinches.
999 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001000 }
1001 }
1002
1003 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001004 // Try canceling the long press. It could also have been scheduled
1005 // by a distant descendant, so use the mAllowLongPress flag to block
1006 // everything
1007 final View currentPage = getPageAt(mCurrentPage);
1008 if (currentPage != null) {
1009 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 }
1011 }
1012
Adam Cohenb5ba0972011-09-07 18:02:31 -07001013 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001014 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001015
Adam Cohenedb40762013-07-18 16:45:45 -07001016 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001017 int count = getChildCount();
1018
1019 final int totalDistance;
1020
1021 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001022 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001023 adjacentPage = page - 1;
1024 }
1025
1026 if (adjacentPage < 0 || adjacentPage > count - 1) {
1027 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1028 } else {
1029 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1030 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001031
1032 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001033 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1034 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001035 return scrollProgress;
1036 }
1037
Adam Cohenedb40762013-07-18 16:45:45 -07001038 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001039 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001040 return 0;
1041 } else {
1042 return mPageScrolls[index];
1043 }
1044 }
1045
Adam Cohen564a2e72013-10-09 14:47:32 -07001046 // While layout transitions are occurring, a child's position may stray from its baseline
1047 // position. This method returns the magnitude of this stray at any given time.
1048 public int getLayoutTransitionOffsetForPage(int index) {
1049 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1050 return 0;
1051 } else {
1052 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001053
Sunny Goyal228153d2018-01-04 15:35:22 -08001054 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1055 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001056 return (int) (child.getX() - baselineX);
1057 }
1058 }
1059
Jon Miranda71cb80c2019-01-24 21:25:13 -08001060 @Override
1061 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001062 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001063 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001064 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001065 super.dispatchDraw(canvas);
1066
1067 canvas.restoreToCount(saveCount);
1068 } else {
1069 super.dispatchDraw(canvas);
1070 }
1071 }
1072
Jon Miranda57dab6f2020-04-10 13:38:14 -07001073 /**
1074 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1075 */
1076 private int getSpringOverScroll(int amount) {
1077 if (mScroller.isSpringing()) {
1078 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001079 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001080 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1081 } else {
1082 return 0;
1083 }
1084 }
1085
Sunny Goyalb7b01352018-08-09 12:31:20 -07001086 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001087 if (amount == 0) {
1088 return;
1089 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001090
Vinit Nayaka406f722019-12-19 11:50:55 -08001091 int size = mOrientationHandler.getMeasuredSize(this);
1092 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001093 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001094 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001095 invalidate();
1096 return;
1097 }
1098
Vinit Nayaka406f722019-12-19 11:50:55 -08001099 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1100 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1101 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001102 invalidate();
1103 }
1104
Sunny Goyalb7b01352018-08-09 12:31:20 -07001105 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001106 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001107 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001108 invalidate();
1109 return;
1110 }
1111
Sunny Goyalb7b01352018-08-09 12:31:20 -07001112 if (amount == 0) return;
1113
1114 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001115 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1116 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001117 } else {
1118 dampedOverScroll(amount);
1119 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001120 }
1121
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001122
Tony Wickham023f4042019-01-29 10:52:31 -08001123 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001124 if (mFreeScroll == freeScroll) {
1125 return;
1126 }
1127
Tony Wickham8f7ead32016-04-07 18:46:44 -07001128 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001129 mFreeScroll = freeScroll;
1130
Adam Cohenf9618852013-11-08 06:45:03 -08001131 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001132 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001133 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001134 if (getScrollForPage(getNextPage()) != getScrollX()) {
1135 snapToPage(getNextPage());
1136 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001137 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001138 }
1139
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001140 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001141 mAllowOverScroll = enable;
1142 }
1143
Winson Chung321e9ee2010-08-09 13:37:56 -07001144 @Override
1145 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001146 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001147 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001148
Michael Jurkab8f06722010-10-10 15:58:46 -07001149 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001150
1151 final int action = ev.getAction();
1152
1153 switch (action & MotionEvent.ACTION_MASK) {
1154 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001155 updateIsBeingDraggedOnTouchDown();
1156
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 /*
1158 * If being flinged and user touches, stop the fling. isFinished
1159 * will be false if being flinged.
1160 */
1161 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001162 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 }
1164
1165 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001166 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001167 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001168 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1169 mLastMotionRemainder = 0;
1170 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001171 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001173 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001174 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001175 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001176 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 break;
1178
Winson Chung4941f652020-06-18 15:11:53 -07001179 case ACTION_MOVE_ALLOW_EASY_FLING:
1180 // Start scrolling immediately
1181 determineScrollingStart(ev);
1182 mAllowEasyFling = true;
1183 break;
1184
1185 case MotionEvent.ACTION_MOVE:
1186 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 // Scroll to follow the motion event
1188 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001189
1190 if (pointerIndex == -1) return true;
1191
Vinit Nayaka406f722019-12-19 11:50:55 -08001192 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1193 float delta = mLastMotion + mLastMotionRemainder - direction;
1194 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001195
Winson Chungc0844aa2011-02-02 15:25:58 -08001196 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1197 // keep the remainder because we are actually testing if we've moved from the last
1198 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001199 if (Math.abs(delta) >= 1.0f) {
1200 mLastMotion = direction;
1201 mLastMotionRemainder = delta - (int) delta;
1202
1203 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 } else {
1205 awakenScrollBars();
1206 }
Adam Cohen564976a2010-10-13 18:52:07 -07001207 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 determineScrollingStart(ev);
1209 }
1210 break;
1211
1212 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001213 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 final int activePointerId = mActivePointerId;
1215 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001216 if (pointerIndex == -1) return true;
1217
Vinit Nayaka406f722019-12-19 11:50:55 -08001218 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1219 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001220 final VelocityTracker velocityTracker = mVelocityTracker;
1221 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001222
Vinit Nayaka406f722019-12-19 11:50:55 -08001223 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1224 mActivePointerId);
1225 int delta = (int) (primaryDirection - mDownMotionPrimary);
1226 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1227
1228 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1229 SIGNIFICANT_MOVE_THRESHOLD;
1230
1231 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001232 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1233 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001234 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1235 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001236 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1237 Log.d("Quickswitch", "isFling=false vel=" + velocity
1238 + " threshold=" + mEasyFlingThresholdVelocity);
1239 }
Adam Cohen00481b32011-11-18 12:03:48 -08001240
Adam Cohenf358a4b2013-07-23 16:47:31 -07001241 if (!mFreeScroll) {
1242 // In the case that the page is moved far to one direction and then is flung
1243 // in the opposite direction, we use a threshold to determine whether we should
1244 // just return to the starting page, or if we should skip one further.
1245 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001246 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1247 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001248 returnToOriginalPage = true;
1249 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001250
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 int finalPage;
1252 // We give flings precedence over large moves, which is why we short-circuit our
1253 // test for a large move if a fling has been registered. That is, a large
1254 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001255
Vinit Nayaka406f722019-12-19 11:50:55 -08001256 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1257 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001258 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001259 snapToPageWithVelocity(finalPage, velocity);
1260 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1261 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 mCurrentPage < getChildCount() - 1) {
1263 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001264 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001265 } else {
1266 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001267 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001269 if (!mScroller.isFinished()) {
1270 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001271 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001272
Vinit Nayaka406f722019-12-19 11:50:55 -08001273 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1274 int maxScroll = mMaxScroll;
1275 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001276
Vinit Nayaka406f722019-12-19 11:50:55 -08001277 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1278 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1279 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001280 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001281 } else {
1282 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001283 mScroller.fling(initialScroll, -velocity,
1284 minScroll, maxScroll,
1285 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001286
Vinit Nayaka406f722019-12-19 11:50:55 -08001287 int finalPos = mScroller.getFinalPos();
1288 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001289
1290 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1291 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001292 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001293 // If scrolling ends in the half of the added space that is closer to
1294 // the end, settle to the end. Otherwise snap to the nearest page.
1295 // If flinging past one of the ends, don't change the velocity as it
1296 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001297 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1298 ? minScroll
1299 : finalPos > (lastPageScroll + maxScroll) / 2
1300 ? maxScroll
1301 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001302
Vinit Nayaka406f722019-12-19 11:50:55 -08001303 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001304 // Ensure the scroll/snap doesn't happen too fast;
1305 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001306 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001307 if (extraScrollDuration > 0) {
1308 mScroller.extendDuration(extraScrollDuration);
1309 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001310 }
1311 }
1312 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001313 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001314 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001316
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 // End any intermediate reordering states
1318 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001319 break;
1320
1321 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001322 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001323 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001324 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001325 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001326 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 break;
1328
1329 case MotionEvent.ACTION_POINTER_UP:
1330 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001331 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 break;
1333 }
1334
1335 return true;
1336 }
1337
Vinit Nayaka406f722019-12-19 11:50:55 -08001338 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001339 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1340 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001341 }
1342
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 private void resetTouchState() {
1344 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001345 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001346 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001347 }
1348
Adam Cohenc2d6e892014-10-16 09:49:24 -07001349 /**
1350 * Triggered by scrolling via touch
1351 */
1352 protected void onScrollInteractionBegin() {
1353 }
1354
1355 protected void onScrollInteractionEnd() {
1356 }
1357
Winson Chung185d7162011-02-28 13:47:29 -08001358 @Override
1359 public boolean onGenericMotionEvent(MotionEvent event) {
1360 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1361 switch (event.getAction()) {
1362 case MotionEvent.ACTION_SCROLL: {
1363 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1364 final float vscroll;
1365 final float hscroll;
1366 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1367 vscroll = 0;
1368 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1369 } else {
1370 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1371 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1372 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001373 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1374 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001375 }
Winson Chung185d7162011-02-28 13:47:29 -08001376 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001377 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001378 : (hscroll > 0 || vscroll > 0);
1379 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001380 scrollRight();
1381 } else {
1382 scrollLeft();
1383 }
1384 return true;
1385 }
1386 }
1387 }
1388 }
1389 return super.onGenericMotionEvent(event);
1390 }
1391
Sunny Goyal8b37c572020-03-31 12:12:14 -07001392 /**
1393 * Returns true if the paged view can scroll for the provided vertical and horizontal
1394 * scroll values
1395 */
1396 protected boolean canScroll(float absVScroll, float absHScroll) {
1397 ActivityContext ac = ActivityContext.lookupContext(getContext());
1398 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001399 }
1400
Michael Jurkab8f06722010-10-10 15:58:46 -07001401 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1402 if (mVelocityTracker == null) {
1403 mVelocityTracker = VelocityTracker.obtain();
1404 }
1405 mVelocityTracker.addMovement(ev);
1406 }
1407
1408 private void releaseVelocityTracker() {
1409 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001411 mVelocityTracker.recycle();
1412 mVelocityTracker = null;
1413 }
1414 }
1415
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001417 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 final int pointerId = ev.getPointerId(pointerIndex);
1419 if (pointerId == mActivePointerId) {
1420 // This was our active pointer going up. Choose a new
1421 // active pointer and adjust accordingly.
1422 // TODO: Make this decision more intelligent.
1423 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001424 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1425 newPointerIndex);
1426 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 mActivePointerId = ev.getPointerId(newPointerIndex);
1428 if (mVelocityTracker != null) {
1429 mVelocityTracker.clear();
1430 }
1431 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001432 }
1433
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 @Override
1435 public void requestChildFocus(View child, View focused) {
1436 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001437 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001438 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001439 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 }
1441 }
1442
Sunny Goyal228153d2018-01-04 15:35:22 -08001443 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001444 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001445 }
1446
Vinit Nayaka406f722019-12-19 11:50:55 -08001447 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1448 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1449 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001450 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 final int childCount = getChildCount();
1453 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001454 int distanceFromScreenCenter = Math.abs(
1455 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1457 minDistanceFromScreenCenter = distanceFromScreenCenter;
1458 minDistanceFromScreenCenterIndex = i;
1459 }
1460 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001461 return minDistanceFromScreenCenterIndex;
1462 }
1463
Tony Wickham7383d4e2020-07-07 19:25:25 -07001464 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chau7944ee52021-01-08 19:07:25 +00001465 int childSize = getChildVisibleSize(childIndex);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001466 int halfChildSize = (childSize / 2);
1467 int childCenter = getChildOffset(childIndex) + halfChildSize;
1468 return childCenter - screenCenter;
1469 }
1470
1471 protected int getDisplacementFromScreenCenter(int childIndex) {
1472 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1473 int screenCenter = mOrientationHandler.getPrimaryScroll(this) + (pageOrientationSize / 2);
1474 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1475 }
1476
Adam Cohend19d3ca2010-09-15 14:43:42 -07001477 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001478 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1479 }
1480
1481 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001482 int scroll = mOrientationHandler.getPrimaryScroll(this);
1483 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001484 }
1485
1486 protected int getPageSnapDuration() {
1487 if (isInOverScroll()) {
1488 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1489 }
1490 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 }
1492
Adam Cohene0f66b52010-11-23 15:06:07 -08001493 // We want the duration of the page snap animation to be influenced by the distance that
1494 // the screen has to travel, however, we don't want this duration to be effected in a
1495 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1496 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001497 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001498 f -= 0.5f; // center the values about 0.
1499 f *= 0.3f * Math.PI / 2.0f;
1500 return (float) Math.sin(f);
1501 }
1502
Winson Chung05a31ed2018-02-08 17:08:43 -08001503 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001504 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001505 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001506
Vinit Nayaka406f722019-12-19 11:50:55 -08001507 final int newLoc = getScrollForPage(whichPage);
1508 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001509 int duration = 0;
1510
Sunny Goyal4d113a52015-05-27 10:05:28 -07001511 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001512 // If the velocity is low enough, then treat this more as an automatic page advance
1513 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001514 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001515 }
1516
1517 // Here we compute a "distance" that will be used in the computation of the overall
1518 // snap duration. This is a function of the actual distance that needs to be traveled;
1519 // we keep this value close to half screen size in order to reduce the variance in snap
1520 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001521 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001522 float distance = halfScreenSize + halfScreenSize *
1523 distanceInfluenceForSnapDuration(distanceRatio);
1524
1525 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001526 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001527
1528 // we want the page's snap velocity to approximately match the velocity at which the
1529 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001530 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1531 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001532
Jon Miranda9e198662020-03-11 14:42:02 -07001533 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001534 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001535 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001536 } else {
1537 return snapToPage(whichPage, delta, duration);
1538 }
Michael Jurka0142d492010-08-25 17:46:15 -07001539 }
1540
Winson Chung05a31ed2018-02-08 17:08:43 -08001541 public boolean snapToPage(int whichPage) {
1542 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001543 }
1544
Winson Chung05a31ed2018-02-08 17:08:43 -08001545 public boolean snapToPageImmediately(int whichPage) {
1546 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001547 }
1548
Winson Chung05a31ed2018-02-08 17:08:43 -08001549 public boolean snapToPage(int whichPage, int duration) {
1550 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001551 }
1552
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001553 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001554 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001555 }
1556
Winson Chung05a31ed2018-02-08 17:08:43 -08001557 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001558 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001559 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001560
Vinit Nayaka406f722019-12-19 11:50:55 -08001561 int newLoc = getScrollForPage(whichPage);
1562 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001563 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001564 }
1565
Winson Chung05a31ed2018-02-08 17:08:43 -08001566 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001567 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001568 }
Michael Jurka0142d492010-08-25 17:46:15 -07001569
Winson Chung05a31ed2018-02-08 17:08:43 -08001570 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001571 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001572 if (mFirstLayout) {
1573 setCurrentPage(whichPage);
1574 return false;
1575 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001576
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001577 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001578 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1579 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001580 }
1581
Adam Cohen6f127a62014-06-12 14:54:41 -07001582 whichPage = validateNewPage(whichPage);
1583
Adam Cohen7d30a372013-07-01 17:03:59 -07001584 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001585
Winson Chung321e9ee2010-08-09 13:37:56 -07001586 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001587 if (immediate) {
1588 duration = 0;
1589 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 duration = Math.abs(delta);
1591 }
1592
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001593 if (duration != 0) {
1594 pageBeginTransition();
1595 }
1596
Adam Cohenf358a4b2013-07-23 16:47:31 -07001597 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001598 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001599 }
Adam Cohenf9618852013-11-08 06:45:03 -08001600
1601 if (interpolator != null) {
1602 mScroller.setInterpolator(interpolator);
1603 } else {
1604 mScroller.setInterpolator(mDefaultInterpolator);
1605 }
1606
Jon Miranda71cb80c2019-01-24 21:25:13 -08001607 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001608 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001609 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001610 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001611 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001612
Adam Cohen674531f2013-12-13 15:07:14 -08001613 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001614
1615 // Trigger a compute() to finish switching pages if necessary
1616 if (immediate) {
1617 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001618 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001619 }
1620
Winson Chung321e9ee2010-08-09 13:37:56 -07001621 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001622 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001623 }
1624
Vadim Tryshev98913d02018-05-18 18:41:34 -07001625 public boolean scrollLeft() {
1626 if (getNextPage() > 0) {
1627 snapToPage(getNextPage() - 1);
1628 return true;
1629 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001630 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001631 }
1632
Vadim Tryshev98913d02018-05-18 18:41:34 -07001633 public boolean scrollRight() {
1634 if (getNextPage() < getChildCount() - 1) {
1635 snapToPage(getNextPage() + 1);
1636 return true;
1637 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001638 return onOverscroll(getMeasuredWidth());
1639 }
1640
1641 protected boolean onOverscroll(int amount) {
1642 if (!mAllowOverScroll) return false;
1643 onScrollInteractionBegin();
1644 overScroll(amount);
1645 onScrollInteractionEnd();
1646 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001647 }
1648
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001649 @Override
1650 public CharSequence getAccessibilityClassName() {
1651 // Some accessibility services have special logic for ScrollView. Since we provide same
1652 // accessibility info as ScrollView, inform the service to handle use the same way.
1653 return ScrollView.class.getName();
1654 }
1655
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001656 protected boolean isPageOrderFlipped() {
1657 return false;
1658 }
1659
Winson Chung6a0f57d2011-06-29 20:10:49 -07001660 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001661 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001662 @Override
1663 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1664 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001665 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001666 int offset = (mAllowOverScroll ? 0 : 1);
1667 info.setScrollable(getPageCount() > offset);
1668 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001669 info.addAction(pagesFlipped ?
1670 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1671 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1672 info.addAction(mIsRtl ?
1673 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1674 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001675 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001676 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001677 info.addAction(pagesFlipped ?
1678 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1679 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1680 info.addAction(mIsRtl ?
1681 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1682 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001683 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001684 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1685 // Besides disabling the accessibility long-click, this also prevents this view from getting
1686 // accessibility focus.
1687 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001688 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001689 }
1690
1691 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001692 public void sendAccessibilityEvent(int eventType) {
1693 // Don't let the view send real scroll events.
1694 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1695 super.sendAccessibilityEvent(eventType);
1696 }
1697 }
1698
1699 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001700 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1701 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001702 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001703 }
1704
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001705 @Override
1706 public boolean performAccessibilityAction(int action, Bundle arguments) {
1707 if (super.performAccessibilityAction(action, arguments)) {
1708 return true;
1709 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001710 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001711 switch (action) {
1712 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001713 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001714 return true;
1715 }
1716 } break;
1717 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001718 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001719 return true;
1720 }
yingleiw02cc8482019-08-02 16:14:27 -07001721 } break;
1722 case android.R.id.accessibilityActionPageRight: {
1723 if (!mIsRtl) {
1724 return scrollRight();
1725 } else {
1726 return scrollLeft();
1727 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001728 }
yingleiw02cc8482019-08-02 16:14:27 -07001729 case android.R.id.accessibilityActionPageLeft: {
1730 if (!mIsRtl) {
1731 return scrollLeft();
1732 } else {
1733 return scrollRight();
1734 }
1735 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001736 }
1737 return false;
1738 }
1739
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001740 protected boolean canAnnouncePageDescription() {
1741 return true;
1742 }
1743
Adam Cohen0ffac432013-07-10 11:19:26 -07001744 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001745 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001746 getNextPage() + 1, getChildCount());
1747 }
1748
Tony Mak2e564402018-02-27 17:19:34 +00001749 protected float getDownMotionX() {
1750 return mDownMotionX;
1751 }
1752
1753 protected float getDownMotionY() {
1754 return mDownMotionY;
1755 }
1756
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001757 protected interface ComputePageScrollsLogic {
1758
1759 boolean shouldIncludeView(View view);
1760 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001761
1762 public int[] getVisibleChildrenRange() {
1763 float visibleLeft = 0;
1764 float visibleRight = visibleLeft + getMeasuredWidth();
1765 float scaleX = getScaleX();
1766 if (scaleX < 1 && scaleX > 0) {
1767 float mid = getMeasuredWidth() / 2;
1768 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1769 visibleRight = mid + ((visibleRight - mid) / scaleX);
1770 }
1771
1772 int leftChild = -1;
1773 int rightChild = -1;
1774 final int childCount = getChildCount();
1775 for (int i = 0; i < childCount; i++) {
1776 final View child = getPageAt(i);
1777
1778 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1779 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1780 if (leftChild == -1) {
1781 leftChild = i;
1782 }
1783 rightChild = i;
1784 }
1785 }
1786 mTmpIntPair[0] = leftChild;
1787 mTmpIntPair[1] = rightChild;
1788 return mTmpIntPair;
1789 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001790}