blob: e29faacd9788b33fadf485e9e4b378e84b20d570 [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
198 if (Utilities.ATLEAST_OREO) {
199 setDefaultFocusHighlightEnabled(false);
200 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202
Adam Cohenf9618852013-11-08 06:45:03 -0800203 protected void setDefaultInterpolator(Interpolator interpolator) {
204 mDefaultInterpolator = interpolator;
205 mScroller.setInterpolator(mDefaultInterpolator);
206 }
207
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700208 public void initParentViews(View parent) {
209 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800210 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700211 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700212 }
213 }
214
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800215 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700216 return mPageIndicator;
217 }
218
Adam Cohen674531f2013-12-13 15:07:14 -0800219 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700220 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
221 * that the user was on before entering free scroll mode (e.g. the home screen page they
222 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
223 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700225 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700226 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700228
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700229 /**
230 * Returns the index of page to be shown immediately afterwards.
231 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700232 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700233 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
234 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700235
Adam Cohenf9c184a2016-01-15 16:47:43 -0800236 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 return getChildCount();
238 }
239
Sunny Goyal83a8f042015-05-19 12:52:12 -0700240 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 return getChildAt(index);
242 }
243
Adam Cohenae4f1552011-10-20 00:15:42 -0700244 protected int indexToPage(int index) {
245 return index;
246 }
247
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800249 * Updates the scroll of the current page immediately to its final scroll position. We use this
250 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
251 * the previous tab page.
252 */
253 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700256 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800257 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700258 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800259 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
260 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700261 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800262 }
263
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700264 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700265 mScroller.abortAnimation();
266 // We need to clean up the next page here to avoid computeScrollHelper from
267 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700268 if (resetNextPage) {
269 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800270 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700271 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700272 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273
Tony Wickham8f7ead32016-04-07 18:46:44 -0700274 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700275 mScroller.forceFinished(true);
276 // We need to clean up the next page here to avoid computeScrollHelper from
277 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700278 if (resetNextPage) {
279 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800280 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700281 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700282 }
283
Adam Cohen6f127a62014-06-12 14:54:41 -0700284 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700285 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700287 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700288 }
289
Chet Haasebc2f0822012-10-26 17:59:53 -0700290 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700291 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
292 */
293 private int ensureWithinScrollBounds(int page) {
294 int dir = !mIsRtl ? 1 : - 1;
295 int currScroll = getScrollForPage(page);
296 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800297 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700298 page += dir;
299 prevScroll = currScroll;
300 currScroll = getScrollForPage(page);
301 if (currScroll <= prevScroll) {
302 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
303 break;
304 }
305 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800306 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700307 page -= dir;
308 prevScroll = currScroll;
309 currScroll = getScrollForPage(page);
310 if (currScroll >= prevScroll) {
311 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
312 break;
313 }
314 }
315 return page;
316 }
317
Tony Wickham03f27012019-04-25 14:22:54 -0700318 public void setCurrentPage(int currentPage) {
319 setCurrentPage(currentPage, INVALID_PAGE);
320 }
321
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700322 /**
Winson Chung86f77532010-08-24 11:08:22 -0700323 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 */
Tony Wickham03f27012019-04-25 14:22:54 -0700325 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800326 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700327 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800328 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800329 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
330 // the default
331 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800332 return;
333 }
Tony Wickham03f27012019-04-25 14:22:54 -0700334 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700335 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700336 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700337 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800338 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 }
340
Winson Chung8c87cd82013-07-23 16:20:10 -0700341 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800342 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
343 * has settled.
344 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700345 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800346 updatePageIndicator();
347 }
348
349 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700350 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000351 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700352 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700354 protected void pageBeginTransition() {
355 if (!mIsPageInTransition) {
356 mIsPageInTransition = true;
357 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700358 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700359 }
360
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700361 protected void pageEndTransition() {
362 if (mIsPageInTransition) {
363 mIsPageInTransition = false;
364 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700365 }
Michael Jurka0142d492010-08-25 17:46:15 -0700366 }
367
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700368 protected boolean isPageInTransition() {
369 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700370 }
371
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700372 /**
373 * Called when the page starts moving as part of the scroll. Subclasses can override this
374 * to provide custom behavior during animation.
375 */
376 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700377 }
378
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700379 /**
380 * Called when the page ends moving as part of the scroll. Subclasses can override this
381 * to provide custom behavior during animation.
382 */
383 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700384 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700385 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800386 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
387 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700388 if (mOnPageTransitionEndCallback != null) {
389 mOnPageTransitionEndCallback.run();
390 mOnPageTransitionEndCallback = null;
391 }
392 }
393
394 /**
395 * Sets a callback to run once when the scrolling finishes. If there is currently
396 * no page in transition, then the callback is called immediately.
397 */
398 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
399 if (mIsPageInTransition || callback == null) {
400 mOnPageTransitionEndCallback = callback;
401 } else {
402 callback.run();
403 }
Michael Jurka0142d492010-08-25 17:46:15 -0700404 }
405
Vinit Nayaka406f722019-12-19 11:50:55 -0800406 protected int getUnboundedScroll() {
407 return mUnboundedScroll;
408 }
409
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800411 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800412 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800413 }
414
415 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700416 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800417 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
418 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
419 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700420
Vinit Nayaka406f722019-12-19 11:50:55 -0800421 boolean isBeforeFirstPage = mIsRtl ?
422 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
423 boolean isAfterLastPage = mIsRtl ?
424 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
425 if (!isBeforeFirstPage && !isAfterLastPage) {
426 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800427 }
428
Vinit Nayaka406f722019-12-19 11:50:55 -0800429 if (isBeforeFirstPage) {
430 mOrientationHandler.delegateScrollTo(this,
431 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800432 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700433 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800434 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800435 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800436 } else if (isAfterLastPage) {
437 mOrientationHandler.delegateScrollTo(this,
438 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800439 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700440 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800441 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800442 }
443 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700444 if (mWasInOverscroll) {
445 overScroll(0);
446 mWasInOverscroll = false;
447 }
Adam Cohen68d73932010-11-15 10:50:58 -0800448 super.scrollTo(x, y);
449 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800450 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700451
Vinit Nayaka406f722019-12-19 11:50:55 -0800452 /**
453 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
454 */
455 public void superScrollTo(int x, int y) {
456 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700457 }
458
Adam Cohen53805212013-10-01 10:39:23 -0700459 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800460 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700461 if (mCurrentPage != getNextPage()) {
462 AccessibilityEvent ev =
463 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700464 ev.setScrollable(true);
465 ev.setScrollX(getScrollX());
466 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800467 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700468 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700469 }
Adam Cohen53805212013-10-01 10:39:23 -0700470 }
471 }
472
Michael Jurka0142d492010-08-25 17:46:15 -0700473 // we moved this functionality to a helper function so SmoothPagedView can reuse it
474 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800475 return computeScrollHelper(true);
476 }
477
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700478 protected void announcePageForAccessibility() {
479 if (isAccessibilityEnabled(getContext())) {
480 // Notify the user when the page changes
481 announceForAccessibility(getCurrentPageDescription());
482 }
483 }
484
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800485 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700487 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800488 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
489 if (mUnboundedScroll != mScroller.getCurrPos()
490 || currentScroll != mScroller.getCurrPos()) {
491 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700492 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800493 if (shouldInvalidate) {
494 invalidate();
495 }
Michael Jurka0142d492010-08-25 17:46:15 -0700496 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800497 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700498 sendScrollAccessibilityEvent();
499
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700500 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700501 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700502 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700503 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700504
Adam Cohen73aa9752010-11-24 16:26:58 -0800505 // We don't want to trigger a page end moving unless the page has settled
506 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700507 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700508 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800509 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700510
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700511 if (canAnnouncePageDescription()) {
512 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 }
Michael Jurka0142d492010-08-25 17:46:15 -0700515 return false;
516 }
517
518 @Override
519 public void computeScroll() {
520 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 }
522
Sunny Goyalce8809a2018-01-18 14:02:47 -0800523 public int getExpectedHeight() {
524 return getMeasuredHeight();
525 }
526
Adam Cohen410f3cd2013-09-22 12:09:32 -0700527 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800528 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800529 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700530 }
531
Sunny Goyalce8809a2018-01-18 14:02:47 -0800532 public int getExpectedWidth() {
533 return getMeasuredWidth();
534 }
535
Sunny Goyalbc683e92017-12-06 10:25:07 -0800536 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800537 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800538 - mInsets.left - mInsets.right;
539 }
540
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800542 public void requestLayout() {
543 mIsLayoutValid = false;
544 super.requestLayout();
545 }
546
547 @Override
548 public void forceLayout() {
549 mIsLayoutValid = false;
550 super.forceLayout();
551 }
552
553 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700555 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700556 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
557 return;
558 }
559
Adam Cohen7d30a372013-07-01 17:03:59 -0700560 // We measure the dimensions of the PagedView to be larger than the pages so that when we
561 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700562 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
563 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
564 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700565 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700566
Adam Cohen7d30a372013-07-01 17:03:59 -0700567 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
568 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
569 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571
Winson Chung8aad6102012-05-11 16:27:49 -0700572 // Return early if we aren't given a proper dimension
573 if (widthSize <= 0 || heightSize <= 0) {
574 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
575 return;
576 }
577
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700579 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700580 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700581
Sunny Goyalac00cba2017-11-13 15:58:01 -0800582 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800583 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800584 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800585 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700586
Sunny Goyalac00cba2017-11-13 15:58:01 -0800587 // measureChildren takes accounts for content padding, we only need to care about extra
588 // space due to insets.
589 measureChildren(myWidthSpec, myHeightSpec);
590 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800591 }
592
Sunny Goyalcf25b522015-07-09 00:01:18 -0700593 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700595 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800596 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700597 final int childCount = getChildCount();
598 boolean pageScrollChanged = false;
599 if (mPageScrolls == null || childCount != mPageScrolls.length) {
600 mPageScrolls = new int[childCount];
601 pageScrollChanged = true;
602 }
603
604 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700605 return;
606 }
607
Winson Chung785d2eb2011-04-14 16:08:02 -0700608 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700609
Vinit Nayaka406f722019-12-19 11:50:55 -0800610 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
611 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700612 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700614
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700615 final LayoutTransition transition = getLayoutTransition();
616 // If the transition is running defer updating max scroll, as some empty pages could
617 // still be present, and a max scroll change could cause sudden jumps in scroll.
618 if (transition != null && transition.isRunning()) {
619 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
620
621 @Override
622 public void startTransition(LayoutTransition transition, ViewGroup container,
623 View view, int transitionType) { }
624
625 @Override
626 public void endTransition(LayoutTransition transition, ViewGroup container,
627 View view, int transitionType) {
628 // Wait until all transitions are complete.
629 if (!transition.isRunning()) {
630 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700631 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700632 }
633 }
634 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700635 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700636 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700637 }
638
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700639 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
640 updateCurrentPageScroll();
641 mFirstLayout = false;
642 }
643
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800644 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700645 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700646 }
Winson Chunge3193b92010-09-10 11:44:42 -0700647 }
648
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700649 /**
650 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
651 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700652 */
653 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
654 ComputePageScrollsLogic scrollLogic) {
655 final int childCount = getChildCount();
656
657 final int startIndex = mIsRtl ? childCount - 1 : 0;
658 final int endIndex = mIsRtl ? -1 : childCount;
659 final int delta = mIsRtl ? -1 : 1;
660
Vinit Nayaka406f722019-12-19 11:50:55 -0800661 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700662
Vinit Nayaka406f722019-12-19 11:50:55 -0800663 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
664 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700665 boolean pageScrollChanged = false;
666
Vinit Nayaka406f722019-12-19 11:50:55 -0800667 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700668 final View child = getPageAt(i);
669 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800670 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
671 pageCenter, layoutChildren);
672 final int primaryDimension = bounds.primaryDimension;
673 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700674
Sunny Goyalc82c6392018-06-06 15:39:13 -0700675 // In case the pages are of different width, align the page to left or right edge
676 // based on the orientation.
677 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800678 ? (childStart - scrollOffsetStart)
679 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700680 if (outPageScrolls[i] != pageScroll) {
681 pageScrollChanged = true;
682 outPageScrolls[i] = pageScroll;
683 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800684 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700685 }
686 }
687 return pageScrollChanged;
688 }
689
Sunny Goyala1fbd842015-05-20 13:40:27 -0700690 protected int getChildGap() {
691 return 0;
692 }
693
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700694 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800695 mMinScroll = computeMinScroll();
696 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700697 }
698
Vinit Nayaka406f722019-12-19 11:50:55 -0800699 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700700 return 0;
Tony50876bf2018-09-19 23:09:14 -0400701 }
702
Vinit Nayaka406f722019-12-19 11:50:55 -0800703 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700704 int childCount = getChildCount();
705 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700706 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700707 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700708 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700709 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700710 }
711 }
712
Adam Cohen3b185e22013-10-29 14:45:58 -0700713 public void setPageSpacing(int pageSpacing) {
714 mPageSpacing = pageSpacing;
715 requestLayout();
716 }
717
Tony50876bf2018-09-19 23:09:14 -0400718 public int getPageSpacing() {
719 return mPageSpacing;
720 }
721
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800722 private void dispatchPageCountChanged() {
723 if (mPageIndicator != null) {
724 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700725 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700726 // This ensures that when children are added, they get the correct transforms / alphas
727 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700728 invalidate();
729 }
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800732 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800733 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800734 dispatchPageCountChanged();
735 }
736
737 @Override
738 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800739 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700740 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800741 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700742 }
743
Adam Cohen73894962011-10-31 13:17:17 -0700744 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700745 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800746 View pageAtIndex = getPageAt(index);
747 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700748 }
749
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 @Override
751 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700752 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700753 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700754 if (immediate) {
755 setCurrentPage(page);
756 } else {
757 snapToPage(page);
758 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 return true;
760 }
761 return false;
762 }
763
764 @Override
765 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700766 int focusablePage;
767 if (mNextPage != INVALID_PAGE) {
768 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700770 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 }
Winson Chung86f77532010-08-24 11:08:22 -0700772 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700774 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 }
776 return false;
777 }
778
779 @Override
780 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800781 if (super.dispatchUnhandledMove(focused, direction)) {
782 return true;
783 }
784
785 if (mIsRtl) {
786 if (direction == View.FOCUS_LEFT) {
787 direction = View.FOCUS_RIGHT;
788 } else if (direction == View.FOCUS_RIGHT) {
789 direction = View.FOCUS_LEFT;
790 }
791 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700793 if (getCurrentPage() > 0) {
794 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700795 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 return true;
797 }
798 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700799 if (getCurrentPage() < getPageCount() - 1) {
800 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700801 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 return true;
803 }
804 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800805 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 }
807
808 @Override
809 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700810 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
811 return;
812 }
813
Adam Cohen0ffac432013-07-10 11:19:26 -0700814 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700815 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700816 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 }
818 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700819 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700820 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 }
822 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700823 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700824 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700825 }
826 }
827 }
828
829 /**
830 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700831 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 *
Winson Chung86f77532010-08-24 11:08:22 -0700833 * This happens when live folders requery, and if they're off page, they
834 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 */
836 @Override
837 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700838 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 View v = focused;
840 while (true) {
841 if (v == current) {
842 super.focusableViewAvailable(focused);
843 return;
844 }
845 if (v == this) {
846 return;
847 }
848 ViewParent parent = v.getParent();
849 if (parent instanceof View) {
850 v = (View)v.getParent();
851 } else {
852 return;
853 }
854 }
855 }
856
857 /**
858 * {@inheritDoc}
859 */
860 @Override
861 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
862 if (disallowIntercept) {
863 // We need to make sure to cancel our long press if
864 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700865 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700866 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 }
868 super.requestDisallowInterceptTouchEvent(disallowIntercept);
869 }
870
871 @Override
872 public boolean onInterceptTouchEvent(MotionEvent ev) {
873 /*
874 * This method JUST determines whether we want to intercept the motion.
875 * If we return true, onTouchEvent will be called and we do the actual
876 * scrolling there.
877 */
878
Winson Chung45e1d6e2010-11-09 17:19:49 -0800879 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700880 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700881
882 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800883
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 /*
885 * Shortcut the most recurring case: the user is in the dragging
886 * state and he is moving his finger. We want to intercept this
887 * motion.
888 */
889 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700890 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 return true;
892 }
893
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 switch (action & MotionEvent.ACTION_MASK) {
895 case MotionEvent.ACTION_MOVE: {
896 /*
897 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800898 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700900 if (mActivePointerId != INVALID_POINTER) {
901 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700902 }
903 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800904 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700905 // i.e. fall through to the next case (don't break)
906 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
907 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700908 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 }
910
911 case MotionEvent.ACTION_DOWN: {
912 final float x = ev.getX();
913 final float y = ev.getY();
914 // Remember location of down touch
915 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700916 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700917 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800918 mLastMotionRemainder = 0;
919 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700920 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700922
Tony Wickhama0aee212019-09-18 09:24:03 -0700923 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 break;
926 }
927
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800929 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700930 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 break;
932
933 case MotionEvent.ACTION_POINTER_UP:
934 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800935 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 break;
937 }
938
939 /*
940 * The only time we want to intercept motion events is if we are in the
941 * drag mode.
942 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700943 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 }
945
Tony Wickhama0aee212019-09-18 09:24:03 -0700946 /**
947 * If being flinged and user touches the screen, initiate drag; otherwise don't.
948 */
949 private void updateIsBeingDraggedOnTouchDown() {
950 // mScroller.isFinished should be false when being flinged.
951 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -0700952 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -0700953
954 if (finishedScrolling) {
955 mIsBeingDragged = false;
956 if (!mScroller.isFinished() && !mFreeScroll) {
957 setCurrentPage(getNextPage());
958 pageEndTransition();
959 }
960 } else {
961 mIsBeingDragged = true;
962 }
963 }
964
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800965 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700966 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800967 }
968
Adam Cohenf8d28232011-02-01 21:47:00 -0800969 protected void determineScrollingStart(MotionEvent ev) {
970 determineScrollingStart(ev, 1.0f);
971 }
972
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 /*
974 * Determines if we should change the touch state to start scrolling after the
975 * user moves their touch point too far.
976 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800977 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700978 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 if (pointerIndex == -1) return;
981
Vinit Nayaka406f722019-12-19 11:50:55 -0800982 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
983 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -0800984 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -0700985 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -0700986
Vinit Nayaka406f722019-12-19 11:50:55 -0800987 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700988 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700989 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800990 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
991 mLastMotion = primaryDirection;
992 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700993 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700994 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800995 // Stop listening for things like pinches.
996 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800997 }
998 }
999
1000 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001001 // Try canceling the long press. It could also have been scheduled
1002 // by a distant descendant, so use the mAllowLongPress flag to block
1003 // everything
1004 final View currentPage = getPageAt(mCurrentPage);
1005 if (currentPage != null) {
1006 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 }
1008 }
1009
Adam Cohenb5ba0972011-09-07 18:02:31 -07001010 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001011 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001012
Adam Cohenedb40762013-07-18 16:45:45 -07001013 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001014 int count = getChildCount();
1015
1016 final int totalDistance;
1017
1018 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001019 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001020 adjacentPage = page - 1;
1021 }
1022
1023 if (adjacentPage < 0 || adjacentPage > count - 1) {
1024 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1025 } else {
1026 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1027 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001028
1029 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001030 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1031 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001032 return scrollProgress;
1033 }
1034
Adam Cohenedb40762013-07-18 16:45:45 -07001035 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001036 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001037 return 0;
1038 } else {
1039 return mPageScrolls[index];
1040 }
1041 }
1042
Adam Cohen564a2e72013-10-09 14:47:32 -07001043 // While layout transitions are occurring, a child's position may stray from its baseline
1044 // position. This method returns the magnitude of this stray at any given time.
1045 public int getLayoutTransitionOffsetForPage(int index) {
1046 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1047 return 0;
1048 } else {
1049 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001050
Sunny Goyal228153d2018-01-04 15:35:22 -08001051 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1052 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001053 return (int) (child.getX() - baselineX);
1054 }
1055 }
1056
Jon Miranda71cb80c2019-01-24 21:25:13 -08001057 @Override
1058 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001059 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001060 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001061 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001062 super.dispatchDraw(canvas);
1063
1064 canvas.restoreToCount(saveCount);
1065 } else {
1066 super.dispatchDraw(canvas);
1067 }
1068 }
1069
Jon Miranda57dab6f2020-04-10 13:38:14 -07001070 /**
1071 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1072 */
1073 private int getSpringOverScroll(int amount) {
1074 if (mScroller.isSpringing()) {
1075 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001076 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001077 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1078 } else {
1079 return 0;
1080 }
1081 }
1082
Sunny Goyalb7b01352018-08-09 12:31:20 -07001083 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001084 if (amount == 0) {
1085 return;
1086 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001087
Vinit Nayaka406f722019-12-19 11:50:55 -08001088 int size = mOrientationHandler.getMeasuredSize(this);
1089 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001090 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001091 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001092 invalidate();
1093 return;
1094 }
1095
Vinit Nayaka406f722019-12-19 11:50:55 -08001096 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1097 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1098 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001099 invalidate();
1100 }
1101
Sunny Goyalb7b01352018-08-09 12:31:20 -07001102 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001103 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001104 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001105 invalidate();
1106 return;
1107 }
1108
Sunny Goyalb7b01352018-08-09 12:31:20 -07001109 if (amount == 0) return;
1110
1111 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001112 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1113 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001114 } else {
1115 dampedOverScroll(amount);
1116 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001117 }
1118
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001119
Tony Wickham023f4042019-01-29 10:52:31 -08001120 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001121 if (mFreeScroll == freeScroll) {
1122 return;
1123 }
1124
Tony Wickham8f7ead32016-04-07 18:46:44 -07001125 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001126 mFreeScroll = freeScroll;
1127
Adam Cohenf9618852013-11-08 06:45:03 -08001128 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001129 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001130 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001131 if (getScrollForPage(getNextPage()) != getScrollX()) {
1132 snapToPage(getNextPage());
1133 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001134 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001135 }
1136
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001137 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001138 mAllowOverScroll = enable;
1139 }
1140
Winson Chung321e9ee2010-08-09 13:37:56 -07001141 @Override
1142 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001143 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001144 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001145
Michael Jurkab8f06722010-10-10 15:58:46 -07001146 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001147
1148 final int action = ev.getAction();
1149
1150 switch (action & MotionEvent.ACTION_MASK) {
1151 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001152 updateIsBeingDraggedOnTouchDown();
1153
Winson Chung321e9ee2010-08-09 13:37:56 -07001154 /*
1155 * If being flinged and user touches, stop the fling. isFinished
1156 * will be false if being flinged.
1157 */
1158 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001159 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 }
1161
1162 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001163 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001164 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001165 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1166 mLastMotionRemainder = 0;
1167 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001168 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001170 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001171 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001172 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001173 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 break;
1175
Winson Chung4941f652020-06-18 15:11:53 -07001176 case ACTION_MOVE_ALLOW_EASY_FLING:
1177 // Start scrolling immediately
1178 determineScrollingStart(ev);
1179 mAllowEasyFling = true;
1180 break;
1181
1182 case MotionEvent.ACTION_MOVE:
1183 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 // Scroll to follow the motion event
1185 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001186
1187 if (pointerIndex == -1) return true;
1188
Vinit Nayaka406f722019-12-19 11:50:55 -08001189 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1190 float delta = mLastMotion + mLastMotionRemainder - direction;
1191 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001192
Winson Chungc0844aa2011-02-02 15:25:58 -08001193 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1194 // keep the remainder because we are actually testing if we've moved from the last
1195 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001196 if (Math.abs(delta) >= 1.0f) {
1197 mLastMotion = direction;
1198 mLastMotionRemainder = delta - (int) delta;
1199
1200 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 } else {
1202 awakenScrollBars();
1203 }
Adam Cohen564976a2010-10-13 18:52:07 -07001204 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 determineScrollingStart(ev);
1206 }
1207 break;
1208
1209 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001210 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 final int activePointerId = mActivePointerId;
1212 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001213 if (pointerIndex == -1) return true;
1214
Vinit Nayaka406f722019-12-19 11:50:55 -08001215 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1216 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001217 final VelocityTracker velocityTracker = mVelocityTracker;
1218 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001219
Vinit Nayaka406f722019-12-19 11:50:55 -08001220 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1221 mActivePointerId);
1222 int delta = (int) (primaryDirection - mDownMotionPrimary);
1223 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1224
1225 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1226 SIGNIFICANT_MOVE_THRESHOLD;
1227
1228 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001229 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1230 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001231 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1232 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001233 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1234 Log.d("Quickswitch", "isFling=false vel=" + velocity
1235 + " threshold=" + mEasyFlingThresholdVelocity);
1236 }
Adam Cohen00481b32011-11-18 12:03:48 -08001237
Adam Cohenf358a4b2013-07-23 16:47:31 -07001238 if (!mFreeScroll) {
1239 // In the case that the page is moved far to one direction and then is flung
1240 // in the opposite direction, we use a threshold to determine whether we should
1241 // just return to the starting page, or if we should skip one further.
1242 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001243 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1244 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001245 returnToOriginalPage = true;
1246 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001247
Adam Cohenf358a4b2013-07-23 16:47:31 -07001248 int finalPage;
1249 // We give flings precedence over large moves, which is why we short-circuit our
1250 // test for a large move if a fling has been registered. That is, a large
1251 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001252
Vinit Nayaka406f722019-12-19 11:50:55 -08001253 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1254 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001255 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001256 snapToPageWithVelocity(finalPage, velocity);
1257 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1258 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001259 mCurrentPage < getChildCount() - 1) {
1260 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001261 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 } else {
1263 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001264 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001266 if (!mScroller.isFinished()) {
1267 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001268 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001269
Vinit Nayaka406f722019-12-19 11:50:55 -08001270 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1271 int maxScroll = mMaxScroll;
1272 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001273
Vinit Nayaka406f722019-12-19 11:50:55 -08001274 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1275 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1276 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001277 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001278 } else {
1279 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001280 mScroller.fling(initialScroll, -velocity,
1281 minScroll, maxScroll,
1282 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001283
Vinit Nayaka406f722019-12-19 11:50:55 -08001284 int finalPos = mScroller.getFinalPos();
1285 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001286
1287 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1288 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001289 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001290 // If scrolling ends in the half of the added space that is closer to
1291 // the end, settle to the end. Otherwise snap to the nearest page.
1292 // If flinging past one of the ends, don't change the velocity as it
1293 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001294 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1295 ? minScroll
1296 : finalPos > (lastPageScroll + maxScroll) / 2
1297 ? maxScroll
1298 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001299
Vinit Nayaka406f722019-12-19 11:50:55 -08001300 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001301 // Ensure the scroll/snap doesn't happen too fast;
1302 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001303 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001304 if (extraScrollDuration > 0) {
1305 mScroller.extendDuration(extraScrollDuration);
1306 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001307 }
1308 }
1309 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001311 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001313
Adam Cohen7d30a372013-07-01 17:03:59 -07001314 // End any intermediate reordering states
1315 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 break;
1317
1318 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001319 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001320 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001321 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001322 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001323 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 break;
1325
1326 case MotionEvent.ACTION_POINTER_UP:
1327 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001328 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 break;
1330 }
1331
1332 return true;
1333 }
1334
Vinit Nayaka406f722019-12-19 11:50:55 -08001335 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001336 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1337 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001338 }
1339
Adam Cohen7d30a372013-07-01 17:03:59 -07001340 private void resetTouchState() {
1341 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001342 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 }
1345
Adam Cohenc2d6e892014-10-16 09:49:24 -07001346 /**
1347 * Triggered by scrolling via touch
1348 */
1349 protected void onScrollInteractionBegin() {
1350 }
1351
1352 protected void onScrollInteractionEnd() {
1353 }
1354
Winson Chung185d7162011-02-28 13:47:29 -08001355 @Override
1356 public boolean onGenericMotionEvent(MotionEvent event) {
1357 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1358 switch (event.getAction()) {
1359 case MotionEvent.ACTION_SCROLL: {
1360 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1361 final float vscroll;
1362 final float hscroll;
1363 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1364 vscroll = 0;
1365 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1366 } else {
1367 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1368 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1369 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001370 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1371 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001372 }
Winson Chung185d7162011-02-28 13:47:29 -08001373 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001374 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001375 : (hscroll > 0 || vscroll > 0);
1376 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001377 scrollRight();
1378 } else {
1379 scrollLeft();
1380 }
1381 return true;
1382 }
1383 }
1384 }
1385 }
1386 return super.onGenericMotionEvent(event);
1387 }
1388
Sunny Goyal8b37c572020-03-31 12:12:14 -07001389 /**
1390 * Returns true if the paged view can scroll for the provided vertical and horizontal
1391 * scroll values
1392 */
1393 protected boolean canScroll(float absVScroll, float absHScroll) {
1394 ActivityContext ac = ActivityContext.lookupContext(getContext());
1395 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001396 }
1397
Michael Jurkab8f06722010-10-10 15:58:46 -07001398 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1399 if (mVelocityTracker == null) {
1400 mVelocityTracker = VelocityTracker.obtain();
1401 }
1402 mVelocityTracker.addMovement(ev);
1403 }
1404
1405 private void releaseVelocityTracker() {
1406 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001408 mVelocityTracker.recycle();
1409 mVelocityTracker = null;
1410 }
1411 }
1412
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001414 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 final int pointerId = ev.getPointerId(pointerIndex);
1416 if (pointerId == mActivePointerId) {
1417 // This was our active pointer going up. Choose a new
1418 // active pointer and adjust accordingly.
1419 // TODO: Make this decision more intelligent.
1420 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001421 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1422 newPointerIndex);
1423 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 mActivePointerId = ev.getPointerId(newPointerIndex);
1425 if (mVelocityTracker != null) {
1426 mVelocityTracker.clear();
1427 }
1428 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001429 }
1430
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 @Override
1432 public void requestChildFocus(View child, View focused) {
1433 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001434 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001435 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001436 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 }
1438 }
1439
Sunny Goyal228153d2018-01-04 15:35:22 -08001440 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001441 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001442 }
1443
Vinit Nayaka406f722019-12-19 11:50:55 -08001444 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1445 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1446 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001447 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001449 final int childCount = getChildCount();
1450 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001451 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001452 int childSize = mOrientationHandler.getMeasuredSize(layout);
1453 int halfChildSize = (childSize / 2);
1454 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001455 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1456 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1457 minDistanceFromScreenCenter = distanceFromScreenCenter;
1458 minDistanceFromScreenCenterIndex = i;
1459 }
1460 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001461 return minDistanceFromScreenCenterIndex;
1462 }
1463
1464 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001465 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1466 }
1467
1468 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001469 int scroll = mOrientationHandler.getPrimaryScroll(this);
1470 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001471 }
1472
1473 protected int getPageSnapDuration() {
1474 if (isInOverScroll()) {
1475 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1476 }
1477 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 }
1479
Adam Cohene0f66b52010-11-23 15:06:07 -08001480 // We want the duration of the page snap animation to be influenced by the distance that
1481 // the screen has to travel, however, we don't want this duration to be effected in a
1482 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1483 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001484 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001485 f -= 0.5f; // center the values about 0.
1486 f *= 0.3f * Math.PI / 2.0f;
1487 return (float) Math.sin(f);
1488 }
1489
Winson Chung05a31ed2018-02-08 17:08:43 -08001490 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001491 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001492 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001493
Vinit Nayaka406f722019-12-19 11:50:55 -08001494 final int newLoc = getScrollForPage(whichPage);
1495 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001496 int duration = 0;
1497
Sunny Goyal4d113a52015-05-27 10:05:28 -07001498 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001499 // If the velocity is low enough, then treat this more as an automatic page advance
1500 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001501 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001502 }
1503
1504 // Here we compute a "distance" that will be used in the computation of the overall
1505 // snap duration. This is a function of the actual distance that needs to be traveled;
1506 // we keep this value close to half screen size in order to reduce the variance in snap
1507 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001508 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001509 float distance = halfScreenSize + halfScreenSize *
1510 distanceInfluenceForSnapDuration(distanceRatio);
1511
1512 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001513 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001514
1515 // we want the page's snap velocity to approximately match the velocity at which the
1516 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001517 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1518 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001519
Jon Miranda9e198662020-03-11 14:42:02 -07001520 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001521 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001522 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001523 } else {
1524 return snapToPage(whichPage, delta, duration);
1525 }
Michael Jurka0142d492010-08-25 17:46:15 -07001526 }
1527
Winson Chung05a31ed2018-02-08 17:08:43 -08001528 public boolean snapToPage(int whichPage) {
1529 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 }
1531
Winson Chung05a31ed2018-02-08 17:08:43 -08001532 public boolean snapToPageImmediately(int whichPage) {
1533 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001534 }
1535
Winson Chung05a31ed2018-02-08 17:08:43 -08001536 public boolean snapToPage(int whichPage, int duration) {
1537 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001538 }
1539
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001540 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001541 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001542 }
1543
Winson Chung05a31ed2018-02-08 17:08:43 -08001544 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001545 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001546 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001547
Vinit Nayaka406f722019-12-19 11:50:55 -08001548 int newLoc = getScrollForPage(whichPage);
1549 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001550 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001551 }
1552
Winson Chung05a31ed2018-02-08 17:08:43 -08001553 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001554 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001555 }
Michael Jurka0142d492010-08-25 17:46:15 -07001556
Winson Chung05a31ed2018-02-08 17:08:43 -08001557 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001558 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001559 if (mFirstLayout) {
1560 setCurrentPage(whichPage);
1561 return false;
1562 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001563
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001564 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001565 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1566 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001567 }
1568
Adam Cohen6f127a62014-06-12 14:54:41 -07001569 whichPage = validateNewPage(whichPage);
1570
Adam Cohen7d30a372013-07-01 17:03:59 -07001571 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001572
Winson Chung321e9ee2010-08-09 13:37:56 -07001573 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001574 if (immediate) {
1575 duration = 0;
1576 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001577 duration = Math.abs(delta);
1578 }
1579
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001580 if (duration != 0) {
1581 pageBeginTransition();
1582 }
1583
Adam Cohenf358a4b2013-07-23 16:47:31 -07001584 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001585 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001586 }
Adam Cohenf9618852013-11-08 06:45:03 -08001587
1588 if (interpolator != null) {
1589 mScroller.setInterpolator(interpolator);
1590 } else {
1591 mScroller.setInterpolator(mDefaultInterpolator);
1592 }
1593
Jon Miranda71cb80c2019-01-24 21:25:13 -08001594 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001595 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001596 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001597 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001598 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001599
Adam Cohen674531f2013-12-13 15:07:14 -08001600 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001601
1602 // Trigger a compute() to finish switching pages if necessary
1603 if (immediate) {
1604 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001605 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001606 }
1607
Winson Chung321e9ee2010-08-09 13:37:56 -07001608 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001609 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001610 }
1611
Vadim Tryshev98913d02018-05-18 18:41:34 -07001612 public boolean scrollLeft() {
1613 if (getNextPage() > 0) {
1614 snapToPage(getNextPage() - 1);
1615 return true;
1616 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001617 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001618 }
1619
Vadim Tryshev98913d02018-05-18 18:41:34 -07001620 public boolean scrollRight() {
1621 if (getNextPage() < getChildCount() - 1) {
1622 snapToPage(getNextPage() + 1);
1623 return true;
1624 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001625 return onOverscroll(getMeasuredWidth());
1626 }
1627
1628 protected boolean onOverscroll(int amount) {
1629 if (!mAllowOverScroll) return false;
1630 onScrollInteractionBegin();
1631 overScroll(amount);
1632 onScrollInteractionEnd();
1633 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 }
1635
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001636 @Override
1637 public CharSequence getAccessibilityClassName() {
1638 // Some accessibility services have special logic for ScrollView. Since we provide same
1639 // accessibility info as ScrollView, inform the service to handle use the same way.
1640 return ScrollView.class.getName();
1641 }
1642
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001643 protected boolean isPageOrderFlipped() {
1644 return false;
1645 }
1646
Winson Chung6a0f57d2011-06-29 20:10:49 -07001647 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001648 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001649 @Override
1650 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1651 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001652 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001653 int offset = (mAllowOverScroll ? 0 : 1);
1654 info.setScrollable(getPageCount() > offset);
1655 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001656 info.addAction(pagesFlipped ?
1657 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1658 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1659 info.addAction(mIsRtl ?
1660 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1661 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001662 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001663 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001664 info.addAction(pagesFlipped ?
1665 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1666 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1667 info.addAction(mIsRtl ?
1668 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1669 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001670 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001671 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1672 // Besides disabling the accessibility long-click, this also prevents this view from getting
1673 // accessibility focus.
1674 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001675 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001676 }
1677
1678 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001679 public void sendAccessibilityEvent(int eventType) {
1680 // Don't let the view send real scroll events.
1681 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1682 super.sendAccessibilityEvent(eventType);
1683 }
1684 }
1685
1686 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001687 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1688 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001689 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001690 }
1691
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001692 @Override
1693 public boolean performAccessibilityAction(int action, Bundle arguments) {
1694 if (super.performAccessibilityAction(action, arguments)) {
1695 return true;
1696 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001697 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001698 switch (action) {
1699 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001700 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001701 return true;
1702 }
1703 } break;
1704 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001705 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001706 return true;
1707 }
yingleiw02cc8482019-08-02 16:14:27 -07001708 } break;
1709 case android.R.id.accessibilityActionPageRight: {
1710 if (!mIsRtl) {
1711 return scrollRight();
1712 } else {
1713 return scrollLeft();
1714 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001715 }
yingleiw02cc8482019-08-02 16:14:27 -07001716 case android.R.id.accessibilityActionPageLeft: {
1717 if (!mIsRtl) {
1718 return scrollLeft();
1719 } else {
1720 return scrollRight();
1721 }
1722 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001723 }
1724 return false;
1725 }
1726
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001727 protected boolean canAnnouncePageDescription() {
1728 return true;
1729 }
1730
Adam Cohen0ffac432013-07-10 11:19:26 -07001731 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001732 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001733 getNextPage() + 1, getChildCount());
1734 }
1735
Tony Mak2e564402018-02-27 17:19:34 +00001736 protected float getDownMotionX() {
1737 return mDownMotionX;
1738 }
1739
1740 protected float getDownMotionY() {
1741 return mDownMotionY;
1742 }
1743
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001744 protected interface ComputePageScrollsLogic {
1745
1746 boolean shouldIncludeView(View view);
1747 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001748
1749 public int[] getVisibleChildrenRange() {
1750 float visibleLeft = 0;
1751 float visibleRight = visibleLeft + getMeasuredWidth();
1752 float scaleX = getScaleX();
1753 if (scaleX < 1 && scaleX > 0) {
1754 float mid = getMeasuredWidth() / 2;
1755 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1756 visibleRight = mid + ((visibleRight - mid) / scaleX);
1757 }
1758
1759 int leftChild = -1;
1760 int rightChild = -1;
1761 final int childCount = getChildCount();
1762 for (int i = 0; i < childCount; i++) {
1763 final View child = getPageAt(i);
1764
1765 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1766 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1767 if (leftChild == -1) {
1768 leftChild = i;
1769 }
1770 rightChild = i;
1771 }
1772 }
1773 mTmpIntPair[0] = leftChild;
1774 mTmpIntPair[1] = rightChild;
1775 return mTmpIntPair;
1776 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001777}