blob: 608d60c4ad9b2300da6593035022476882c491bb [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070019import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070020import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070021import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080024import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070026import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070027import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070029import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080030import android.view.InputDevice;
31import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.MotionEvent;
Vinit Nayaka406f722019-12-19 11:50:55 -080033import android.view.Surface;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080037import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.ViewGroup;
39import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070043import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070044
Vinit Nayaka406f722019-12-19 11:50:55 -080045import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
46import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
47import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
48import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
49import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
50import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
51import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
52
Jon Miranda9e198662020-03-11 14:42:02 -070053import com.android.launcher3.anim.Interpolators;
54import com.android.launcher3.compat.AccessibilityManagerCompat;
55import com.android.launcher3.config.FeatureFlags;
56import com.android.launcher3.model.PagedViewOrientedState;
57import com.android.launcher3.pageindicators.PageIndicator;
58import com.android.launcher3.touch.OverScroll;
59import com.android.launcher3.touch.PagedOrientationHandler;
60import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
61import com.android.launcher3.touch.PortraitPagedViewHandler;
62import com.android.launcher3.util.OverScroller;
63import com.android.launcher3.util.Thunk;
64
65import java.util.ArrayList;
66
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080071public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070074
Vinit Nayaka406f722019-12-19 11:50:55 -080075 public static boolean sFlagForcedRotation = false;
76
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;
94 private static final int MIN_SNAP_VELOCITY = 1500;
95 private static final int MIN_FLING_VELOCITY = 250;
96
Adam Cohenf358a4b2013-07-23 16:47:31 -070097 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070098
Adam Cohen265b9a62011-12-07 14:37:18 -080099 protected int mFlingThresholdVelocity;
100 protected int mMinFlingVelocity;
101 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700102
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected boolean mFirstLayout = true;
104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700107
Sunny Goyal4ffec482016-02-09 11:28:52 -0800108 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700109 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800110 protected int mMaxScroll;
111 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700112 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800113 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800115 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
117 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700118 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800119 private float mDownMotionPrimary;
120 private float mLastMotion;
121 private float mLastMotionRemainder;
122 private float mTotalMotion;
123 protected PagedOrientationHandler mOrientationHandler = new PortraitPagedViewHandler();
124 protected final PagedViewOrientedState mOrientationState = new PagedViewOrientedState();
Adam Cohenedb40762013-07-18 16:45:45 -0700125
Vadim Trysheve628c472018-05-16 14:28:18 -0700126 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700127 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800128
Michael Jurka7426c422010-11-11 15:23:47 -0800129 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800131 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka5f1c5092010-09-03 14:15:02 -0700133 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka5f1c5092010-09-03 14:15:02 -0700135 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700137 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700138
Vinit Nayaka406f722019-12-19 11:50:55 -0800139 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800140
Sunny Goyal061380a2016-02-29 15:15:03 -0800141 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700142
Vinit Nayaka406f722019-12-19 11:50:55 -0800143 protected int mUnboundedScroll;
144
145 protected int mLayoutRotation = Surface.ROTATION_0;
146 protected int mDisplayRotation = Surface.ROTATION_0;
Adam Cohen8d769d62017-06-23 17:27:38 -0700147
Winson Chungd2be3812013-07-16 11:11:32 -0700148 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700149 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800150 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700151
John Spurlock77e1f472013-09-11 10:09:51 -0400152 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800153 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400154
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800155 // Similar to the platform implementation of isLayoutValid();
156 protected boolean mIsLayoutValid;
157
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700158 private int[] mTmpIntPair = new int[2];
159
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 public PagedView(Context context) {
161 this(context, null);
162 }
163
164 public PagedView(Context context, AttributeSet attrs) {
165 this(context, attrs, 0);
166 }
167
168 public PagedView(Context context, AttributeSet attrs, int defStyle) {
169 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700170
Adam Cohen9c4949e2010-10-05 12:27:22 -0700171 TypedArray a = context.obtainStyledAttributes(attrs,
172 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700173 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700174 a.recycle();
175
Winson Chung321e9ee2010-08-09 13:37:56 -0700176 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700177 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700178 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700179 }
180
181 /**
182 * Initializes various states for this workspace.
183 */
Michael Jurka0142d492010-08-25 17:46:15 -0700184 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800185 Context context = getContext();
186 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700187 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700188 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700189
Vinit Nayaka406f722019-12-19 11:50:55 -0800190 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700191 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800193
Sunny Goyalcf25b522015-07-09 00:01:18 -0700194 float density = getResources().getDisplayMetrics().density;
195 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
196 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
197 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700198
199 if (Utilities.ATLEAST_OREO) {
200 setDefaultFocusHighlightEnabled(false);
201 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800202
203 sFlagForcedRotation = Utilities.isForcedRotation(context);
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 }
205
Adam Cohenf9618852013-11-08 06:45:03 -0800206 protected void setDefaultInterpolator(Interpolator interpolator) {
207 mDefaultInterpolator = interpolator;
208 mScroller.setInterpolator(mDefaultInterpolator);
209 }
210
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700211 public void initParentViews(View parent) {
212 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800213 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700214 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700215 }
216 }
217
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800218 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700219 return mPageIndicator;
220 }
221
Adam Cohen674531f2013-12-13 15:07:14 -0800222 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700223 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
224 * that the user was on before entering free scroll mode (e.g. the home screen page they
225 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
226 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700228 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700229 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700231
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700232 /**
233 * Returns the index of page to be shown immediately afterwards.
234 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700235 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700236 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
237 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700238
Adam Cohenf9c184a2016-01-15 16:47:43 -0800239 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildCount();
241 }
242
Sunny Goyal83a8f042015-05-19 12:52:12 -0700243 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 return getChildAt(index);
245 }
246
Adam Cohenae4f1552011-10-20 00:15:42 -0700247 protected int indexToPage(int index) {
248 return index;
249 }
250
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800252 * Updates the scroll of the current page immediately to its final scroll position. We use this
253 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
254 * the previous tab page.
255 */
256 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700257 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800258 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700259 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800260 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700261 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800262 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
263 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700264 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800265 }
266
Sreyase3e7e632020-01-29 10:42:10 -0800267 /**
268 * Returns left offset of a page. This is the gap between pages and prevents overlap.
269 */
270 public int scrollOffsetLeft() {
271 return mInsets.left + getPaddingLeft();
272 }
273
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700274 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700275 mScroller.abortAnimation();
276 // We need to clean up the next page here to avoid computeScrollHelper from
277 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700278 if (resetNextPage) {
279 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800280 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700281 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700282 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700283
Tony Wickham8f7ead32016-04-07 18:46:44 -0700284 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700285 mScroller.forceFinished(true);
286 // We need to clean up the next page here to avoid computeScrollHelper from
287 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700288 if (resetNextPage) {
289 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800290 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700291 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700292 }
293
Adam Cohen6f127a62014-06-12 14:54:41 -0700294 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700295 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700296 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700297 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700298 }
299
Chet Haasebc2f0822012-10-26 17:59:53 -0700300 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700301 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
302 */
303 private int ensureWithinScrollBounds(int page) {
304 int dir = !mIsRtl ? 1 : - 1;
305 int currScroll = getScrollForPage(page);
306 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800307 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700308 page += dir;
309 prevScroll = currScroll;
310 currScroll = getScrollForPage(page);
311 if (currScroll <= prevScroll) {
312 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
313 break;
314 }
315 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800316 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700317 page -= dir;
318 prevScroll = currScroll;
319 currScroll = getScrollForPage(page);
320 if (currScroll >= prevScroll) {
321 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
322 break;
323 }
324 }
325 return page;
326 }
327
Tony Wickham03f27012019-04-25 14:22:54 -0700328 public void setCurrentPage(int currentPage) {
329 setCurrentPage(currentPage, INVALID_PAGE);
330 }
331
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700332 /**
Winson Chung86f77532010-08-24 11:08:22 -0700333 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 */
Tony Wickham03f27012019-04-25 14:22:54 -0700335 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800336 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700337 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800338 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800339 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
340 // the default
341 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800342 return;
343 }
Tony Wickham03f27012019-04-25 14:22:54 -0700344 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700345 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700346 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700347 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800348 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 }
350
Winson Chung8c87cd82013-07-23 16:20:10 -0700351 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800352 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
353 * has settled.
354 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700355 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800356 updatePageIndicator();
357 }
358
359 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700360 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000361 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700362 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700364 protected void pageBeginTransition() {
365 if (!mIsPageInTransition) {
366 mIsPageInTransition = true;
367 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700368 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700369 }
370
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700371 protected void pageEndTransition() {
372 if (mIsPageInTransition) {
373 mIsPageInTransition = false;
374 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700375 }
Michael Jurka0142d492010-08-25 17:46:15 -0700376 }
377
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700378 protected boolean isPageInTransition() {
379 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700380 }
381
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700382 /**
383 * Called when the page starts moving as part of the scroll. Subclasses can override this
384 * to provide custom behavior during animation.
385 */
386 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700387 }
388
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700389 /**
390 * Called when the page ends moving as part of the scroll. Subclasses can override this
391 * to provide custom behavior during animation.
392 */
393 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700394 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700395 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800396 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
397 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Michael Jurka0142d492010-08-25 17:46:15 -0700398 }
399
Vinit Nayaka406f722019-12-19 11:50:55 -0800400 protected int getUnboundedScroll() {
401 return mUnboundedScroll;
402 }
403
404 protected void updateLayoutRotation(int touchRotation) {
405 setLayoutRotation(touchRotation, mDisplayRotation);
406 }
407
408 /** @param touchRotation Must be one of {@link android.view.Surface.ROTATION_0/90/180/270} */
409 public void setLayoutRotation(int touchRotation, int displayRotation) {
410 if (mLayoutRotation == touchRotation && mDisplayRotation == displayRotation) {
411 return;
412 }
413
414 mOrientationState.update(touchRotation, displayRotation);
415 mOrientationHandler = mOrientationState.getOrientationHandler();
416 mLayoutRotation = touchRotation;
417 mDisplayRotation = displayRotation;
418 requestLayout();
419 }
420
421 public PagedViewOrientedState getPagedViewOrientedState() {
422 return mOrientationState;
423 }
424
425 public PagedOrientationHandler getPagedOrientationHandler() {
426 return getPagedViewOrientedState().getOrientationHandler();
427 }
428
429 public void disableMultipleLayoutRotations(boolean disable) {
430 mOrientationState.disableMultipleOrientations(disable);
431 mOrientationHandler = mOrientationState.getOrientationHandler();
432 requestLayout();
Sunny Goyalc86df472016-02-25 09:19:38 -0800433 }
434
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800436 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800437 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800438 }
439
440 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700441 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800442 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
443 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
444 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700445
Vinit Nayaka406f722019-12-19 11:50:55 -0800446 boolean isBeforeFirstPage = mIsRtl ?
447 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
448 boolean isAfterLastPage = mIsRtl ?
449 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
450 if (!isBeforeFirstPage && !isAfterLastPage) {
451 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800452 }
453
Vinit Nayaka406f722019-12-19 11:50:55 -0800454 if (isBeforeFirstPage) {
455 mOrientationHandler.delegateScrollTo(this,
456 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800457 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700458 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800459 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800460 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800461 } else if (isAfterLastPage) {
462 mOrientationHandler.delegateScrollTo(this,
463 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800464 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700465 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800466 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800467 }
468 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700469 if (mWasInOverscroll) {
470 overScroll(0);
471 mWasInOverscroll = false;
472 }
Adam Cohen68d73932010-11-15 10:50:58 -0800473 super.scrollTo(x, y);
474 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800475 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700476
Vinit Nayaka406f722019-12-19 11:50:55 -0800477 /**
478 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
479 */
480 public void superScrollTo(int x, int y) {
481 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700482 }
483
Adam Cohen53805212013-10-01 10:39:23 -0700484 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800485 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700486 if (mCurrentPage != getNextPage()) {
487 AccessibilityEvent ev =
488 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700489 ev.setScrollable(true);
490 ev.setScrollX(getScrollX());
491 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800492 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700493 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700494 }
Adam Cohen53805212013-10-01 10:39:23 -0700495 }
496 }
497
Michael Jurka0142d492010-08-25 17:46:15 -0700498 // we moved this functionality to a helper function so SmoothPagedView can reuse it
499 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800500 return computeScrollHelper(true);
501 }
502
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700503 protected void announcePageForAccessibility() {
504 if (isAccessibilityEnabled(getContext())) {
505 // Notify the user when the page changes
506 announceForAccessibility(getCurrentPageDescription());
507 }
508 }
509
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800510 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700511 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700512 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800513 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
514 if (mUnboundedScroll != mScroller.getCurrPos()
515 || currentScroll != mScroller.getCurrPos()) {
516 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700517 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800518 if (shouldInvalidate) {
519 invalidate();
520 }
Michael Jurka0142d492010-08-25 17:46:15 -0700521 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800522 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700523 sendScrollAccessibilityEvent();
524
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700525 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700526 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700527 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700528 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700529
Adam Cohen73aa9752010-11-24 16:26:58 -0800530 // We don't want to trigger a page end moving unless the page has settled
531 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700532 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700533 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800534 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700535
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700536 if (canAnnouncePageDescription()) {
537 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700538 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700539 }
Michael Jurka0142d492010-08-25 17:46:15 -0700540 return false;
541 }
542
543 @Override
544 public void computeScroll() {
545 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700546 }
547
Sunny Goyalce8809a2018-01-18 14:02:47 -0800548 public int getExpectedHeight() {
549 return getMeasuredHeight();
550 }
551
Adam Cohen410f3cd2013-09-22 12:09:32 -0700552 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800553 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800554 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700555 }
556
Sunny Goyalce8809a2018-01-18 14:02:47 -0800557 public int getExpectedWidth() {
558 return getMeasuredWidth();
559 }
560
Sunny Goyalbc683e92017-12-06 10:25:07 -0800561 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800562 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800563 - mInsets.left - mInsets.right;
564 }
565
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800567 public void requestLayout() {
568 mIsLayoutValid = false;
569 super.requestLayout();
570 }
571
572 @Override
573 public void forceLayout() {
574 mIsLayoutValid = false;
575 super.forceLayout();
576 }
577
578 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700580 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700581 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
582 return;
583 }
584
Adam Cohen7d30a372013-07-01 17:03:59 -0700585 // We measure the dimensions of the PagedView to be larger than the pages so that when we
586 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700587 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
588 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
589 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700590 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700591
Adam Cohen7d30a372013-07-01 17:03:59 -0700592 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
593 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
594 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 }
596
Winson Chung8aad6102012-05-11 16:27:49 -0700597 // Return early if we aren't given a proper dimension
598 if (widthSize <= 0 || heightSize <= 0) {
599 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
600 return;
601 }
602
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700604 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700605 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700606
Sunny Goyalac00cba2017-11-13 15:58:01 -0800607 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800608 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800609 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800610 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700611
Sunny Goyalac00cba2017-11-13 15:58:01 -0800612 // measureChildren takes accounts for content padding, we only need to care about extra
613 // space due to insets.
614 measureChildren(myWidthSpec, myHeightSpec);
615 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800616 }
617
Sunny Goyalcf25b522015-07-09 00:01:18 -0700618 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700619 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700620 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800621 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700622 final int childCount = getChildCount();
623 boolean pageScrollChanged = false;
624 if (mPageScrolls == null || childCount != mPageScrolls.length) {
625 mPageScrolls = new int[childCount];
626 pageScrollChanged = true;
627 }
628
629 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700630 return;
631 }
632
Winson Chung785d2eb2011-04-14 16:08:02 -0700633 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700634
Vinit Nayaka406f722019-12-19 11:50:55 -0800635 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
636 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700637 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700639
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700640 final LayoutTransition transition = getLayoutTransition();
641 // If the transition is running defer updating max scroll, as some empty pages could
642 // still be present, and a max scroll change could cause sudden jumps in scroll.
643 if (transition != null && transition.isRunning()) {
644 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
645
646 @Override
647 public void startTransition(LayoutTransition transition, ViewGroup container,
648 View view, int transitionType) { }
649
650 @Override
651 public void endTransition(LayoutTransition transition, ViewGroup container,
652 View view, int transitionType) {
653 // Wait until all transitions are complete.
654 if (!transition.isRunning()) {
655 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700656 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700657 }
658 }
659 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700660 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700661 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700662 }
663
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700664 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
665 updateCurrentPageScroll();
666 mFirstLayout = false;
667 }
668
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800669 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700670 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700671 }
Winson Chunge3193b92010-09-10 11:44:42 -0700672 }
673
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700674 /**
675 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
676 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700677 */
678 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
679 ComputePageScrollsLogic scrollLogic) {
680 final int childCount = getChildCount();
681
682 final int startIndex = mIsRtl ? childCount - 1 : 0;
683 final int endIndex = mIsRtl ? -1 : childCount;
684 final int delta = mIsRtl ? -1 : 1;
685
Vinit Nayaka406f722019-12-19 11:50:55 -0800686 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700687
Vinit Nayaka406f722019-12-19 11:50:55 -0800688 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
689 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700690 boolean pageScrollChanged = false;
691
Vinit Nayaka406f722019-12-19 11:50:55 -0800692 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700693 final View child = getPageAt(i);
694 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800695 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
696 pageCenter, layoutChildren);
697 final int primaryDimension = bounds.primaryDimension;
698 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700699
Sunny Goyalc82c6392018-06-06 15:39:13 -0700700 // In case the pages are of different width, align the page to left or right edge
701 // based on the orientation.
702 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800703 ? (childStart - scrollOffsetStart)
704 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700705 if (outPageScrolls[i] != pageScroll) {
706 pageScrollChanged = true;
707 outPageScrolls[i] = pageScroll;
708 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800709 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700710 }
711 }
712 return pageScrollChanged;
713 }
714
Sunny Goyala1fbd842015-05-20 13:40:27 -0700715 protected int getChildGap() {
716 return 0;
717 }
718
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700719 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800720 mMinScroll = computeMinScroll();
721 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700722 }
723
Vinit Nayaka406f722019-12-19 11:50:55 -0800724 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700725 return 0;
Tony50876bf2018-09-19 23:09:14 -0400726 }
727
Vinit Nayaka406f722019-12-19 11:50:55 -0800728 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700729 int childCount = getChildCount();
730 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700731 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700732 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700733 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700734 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700735 }
736 }
737
Adam Cohen3b185e22013-10-29 14:45:58 -0700738 public void setPageSpacing(int pageSpacing) {
739 mPageSpacing = pageSpacing;
740 requestLayout();
741 }
742
Tony50876bf2018-09-19 23:09:14 -0400743 public int getPageSpacing() {
744 return mPageSpacing;
745 }
746
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800747 private void dispatchPageCountChanged() {
748 if (mPageIndicator != null) {
749 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700750 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700751 // This ensures that when children are added, they get the correct transforms / alphas
752 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700753 invalidate();
754 }
755
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800757 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800758 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800759 dispatchPageCountChanged();
760 }
761
762 @Override
763 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800764 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700765 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800766 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700767 }
768
Adam Cohen73894962011-10-31 13:17:17 -0700769 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700770 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800771 View pageAtIndex = getPageAt(index);
772 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700773 }
774
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 @Override
776 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700777 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700778 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700779 if (immediate) {
780 setCurrentPage(page);
781 } else {
782 snapToPage(page);
783 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 return true;
785 }
786 return false;
787 }
788
789 @Override
790 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700791 int focusablePage;
792 if (mNextPage != INVALID_PAGE) {
793 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700795 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 }
Winson Chung86f77532010-08-24 11:08:22 -0700797 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700799 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 }
801 return false;
802 }
803
804 @Override
805 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800806 if (super.dispatchUnhandledMove(focused, direction)) {
807 return true;
808 }
809
810 if (mIsRtl) {
811 if (direction == View.FOCUS_LEFT) {
812 direction = View.FOCUS_RIGHT;
813 } else if (direction == View.FOCUS_RIGHT) {
814 direction = View.FOCUS_LEFT;
815 }
816 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700818 if (getCurrentPage() > 0) {
819 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700820 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 return true;
822 }
823 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700824 if (getCurrentPage() < getPageCount() - 1) {
825 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700826 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 return true;
828 }
829 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800830 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 }
832
833 @Override
834 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700835 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
836 return;
837 }
838
Adam Cohen0ffac432013-07-10 11:19:26 -0700839 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700841 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700844 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700845 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 }
847 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700848 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700849 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 }
851 }
852 }
853
854 /**
855 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700856 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 *
Winson Chung86f77532010-08-24 11:08:22 -0700858 * This happens when live folders requery, and if they're off page, they
859 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 */
861 @Override
862 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700863 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 View v = focused;
865 while (true) {
866 if (v == current) {
867 super.focusableViewAvailable(focused);
868 return;
869 }
870 if (v == this) {
871 return;
872 }
873 ViewParent parent = v.getParent();
874 if (parent instanceof View) {
875 v = (View)v.getParent();
876 } else {
877 return;
878 }
879 }
880 }
881
882 /**
883 * {@inheritDoc}
884 */
885 @Override
886 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
887 if (disallowIntercept) {
888 // We need to make sure to cancel our long press if
889 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700890 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700891 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 }
893 super.requestDisallowInterceptTouchEvent(disallowIntercept);
894 }
895
896 @Override
897 public boolean onInterceptTouchEvent(MotionEvent ev) {
898 /*
899 * This method JUST determines whether we want to intercept the motion.
900 * If we return true, onTouchEvent will be called and we do the actual
901 * scrolling there.
902 */
903
Winson Chung45e1d6e2010-11-09 17:19:49 -0800904 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700905 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700906
907 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800908
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 /*
910 * Shortcut the most recurring case: the user is in the dragging
911 * state and he is moving his finger. We want to intercept this
912 * motion.
913 */
914 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700915 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 return true;
917 }
918
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 switch (action & MotionEvent.ACTION_MASK) {
920 case MotionEvent.ACTION_MOVE: {
921 /*
922 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800923 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700925 if (mActivePointerId != INVALID_POINTER) {
926 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700927 }
928 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800929 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700930 // i.e. fall through to the next case (don't break)
931 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
932 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700933 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 }
935
936 case MotionEvent.ACTION_DOWN: {
937 final float x = ev.getX();
938 final float y = ev.getY();
939 // Remember location of down touch
940 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700941 mDownMotionY = y;
Vinit Nayaka406f722019-12-19 11:50:55 -0800942 mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
943 mLastMotionRemainder = 0;
944 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700946
Tony Wickhama0aee212019-09-18 09:24:03 -0700947 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700948
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 break;
950 }
951
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800953 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700954 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 break;
956
957 case MotionEvent.ACTION_POINTER_UP:
958 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800959 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 break;
961 }
962
963 /*
964 * The only time we want to intercept motion events is if we are in the
965 * drag mode.
966 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700967 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 }
969
Tony Wickhama0aee212019-09-18 09:24:03 -0700970 /**
971 * If being flinged and user touches the screen, initiate drag; otherwise don't.
972 */
973 private void updateIsBeingDraggedOnTouchDown() {
974 // mScroller.isFinished should be false when being flinged.
975 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
976 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
977
978 if (finishedScrolling) {
979 mIsBeingDragged = false;
980 if (!mScroller.isFinished() && !mFreeScroll) {
981 setCurrentPage(getNextPage());
982 pageEndTransition();
983 }
984 } else {
985 mIsBeingDragged = true;
986 }
987 }
988
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800989 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700990 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800991 }
992
Adam Cohenf8d28232011-02-01 21:47:00 -0800993 protected void determineScrollingStart(MotionEvent ev) {
994 determineScrollingStart(ev, 1.0f);
995 }
996
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 /*
998 * Determines if we should change the touch state to start scrolling after the
999 * user moves their touch point too far.
1000 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001001 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001002 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001004 if (pointerIndex == -1) return;
1005
Vinit Nayaka406f722019-12-19 11:50:55 -08001006 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1007 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001008 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -08001009 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001010
Vinit Nayaka406f722019-12-19 11:50:55 -08001011 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001012 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001013 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001014 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1015 mLastMotion = primaryDirection;
1016 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001017 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001018 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001019 // Stop listening for things like pinches.
1020 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001021 }
1022 }
1023
1024 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001025 // Try canceling the long press. It could also have been scheduled
1026 // by a distant descendant, so use the mAllowLongPress flag to block
1027 // everything
1028 final View currentPage = getPageAt(mCurrentPage);
1029 if (currentPage != null) {
1030 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 }
1032 }
1033
Adam Cohenb5ba0972011-09-07 18:02:31 -07001034 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001035 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001036
Adam Cohenedb40762013-07-18 16:45:45 -07001037 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001038 int count = getChildCount();
1039
1040 final int totalDistance;
1041
1042 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001043 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001044 adjacentPage = page - 1;
1045 }
1046
1047 if (adjacentPage < 0 || adjacentPage > count - 1) {
1048 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1049 } else {
1050 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1051 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001052
1053 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001054 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1055 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001056 return scrollProgress;
1057 }
1058
Adam Cohenedb40762013-07-18 16:45:45 -07001059 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001060 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001061 return 0;
1062 } else {
1063 return mPageScrolls[index];
1064 }
1065 }
1066
Adam Cohen564a2e72013-10-09 14:47:32 -07001067 // While layout transitions are occurring, a child's position may stray from its baseline
1068 // position. This method returns the magnitude of this stray at any given time.
1069 public int getLayoutTransitionOffsetForPage(int index) {
1070 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1071 return 0;
1072 } else {
1073 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001074
Sunny Goyal228153d2018-01-04 15:35:22 -08001075 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1076 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001077 return (int) (child.getX() - baselineX);
1078 }
1079 }
1080
Jon Miranda71cb80c2019-01-24 21:25:13 -08001081 @Override
1082 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001083 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001084 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001085 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001086 super.dispatchDraw(canvas);
1087
1088 canvas.restoreToCount(saveCount);
1089 } else {
1090 super.dispatchDraw(canvas);
1091 }
1092 }
1093
Sunny Goyalb7b01352018-08-09 12:31:20 -07001094 protected void dampedOverScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001095 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001096 if (amount == 0) {
1097 return;
1098 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001099
Vinit Nayaka406f722019-12-19 11:50:55 -08001100 int size = mOrientationHandler.getMeasuredSize(this);
1101 int overScrollAmount = OverScroll.dampedScroll(amount, size);
1102 mSpringOverScroll = overScrollAmount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001103 if (mScroller.isSpringing()) {
1104 invalidate();
1105 return;
1106 }
1107
Vinit Nayaka406f722019-12-19 11:50:55 -08001108 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1109 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1110 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001111 invalidate();
1112 }
1113
Sunny Goyalb7b01352018-08-09 12:31:20 -07001114 protected void overScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001115 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001116 if (mScroller.isSpringing()) {
1117 invalidate();
1118 return;
1119 }
1120
Sunny Goyalb7b01352018-08-09 12:31:20 -07001121 if (amount == 0) return;
1122
1123 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001124 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1125 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001126 } else {
1127 dampedOverScroll(amount);
1128 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001129 }
1130
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001131
Tony Wickham023f4042019-01-29 10:52:31 -08001132 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001133 if (mFreeScroll == freeScroll) {
1134 return;
1135 }
1136
Tony Wickham8f7ead32016-04-07 18:46:44 -07001137 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001138 mFreeScroll = freeScroll;
1139
Adam Cohenf9618852013-11-08 06:45:03 -08001140 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001141 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001142 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001143 if (getScrollForPage(getNextPage()) != getScrollX()) {
1144 snapToPage(getNextPage());
1145 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001146 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001147 }
1148
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001149 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001150 mAllowOverScroll = enable;
1151 }
1152
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 @Override
1154 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001155 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001156 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001157
Michael Jurkab8f06722010-10-10 15:58:46 -07001158 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001159
1160 final int action = ev.getAction();
1161
1162 switch (action & MotionEvent.ACTION_MASK) {
1163 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001164 updateIsBeingDraggedOnTouchDown();
1165
Winson Chung321e9ee2010-08-09 13:37:56 -07001166 /*
1167 * If being flinged and user touches, stop the fling. isFinished
1168 * will be false if being flinged.
1169 */
1170 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001171 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 }
1173
1174 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001175 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001176 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001177 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1178 mLastMotionRemainder = 0;
1179 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001181 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001182 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001183 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001184 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 break;
1186
Vinit Nayaka406f722019-12-19 11:50:55 -08001187 case MotionEvent.ACTION_MOVE:
1188 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 // Scroll to follow the motion event
1190 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001191
1192 if (pointerIndex == -1) return true;
1193
Vinit Nayaka406f722019-12-19 11:50:55 -08001194 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1195 float delta = mLastMotion + mLastMotionRemainder - direction;
1196 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001197
Winson Chungc0844aa2011-02-02 15:25:58 -08001198 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1199 // keep the remainder because we are actually testing if we've moved from the last
1200 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001201 if (Math.abs(delta) >= 1.0f) {
1202 mLastMotion = direction;
1203 mLastMotionRemainder = delta - (int) delta;
1204
1205 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 } else {
1207 awakenScrollBars();
1208 }
Adam Cohen564976a2010-10-13 18:52:07 -07001209 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 determineScrollingStart(ev);
1211 }
1212 break;
1213
1214 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001215 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 final int activePointerId = mActivePointerId;
1217 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001218 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1219 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001220 final VelocityTracker velocityTracker = mVelocityTracker;
1221 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001222
Vinit Nayaka406f722019-12-19 11:50:55 -08001223 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1224 mActivePointerId);
1225 int delta = (int) (primaryDirection - mDownMotionPrimary);
1226 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1227
1228 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1229 SIGNIFICANT_MOVE_THRESHOLD;
1230
1231 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1232 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1233 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1234 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001235
Adam Cohenf358a4b2013-07-23 16:47:31 -07001236 if (!mFreeScroll) {
1237 // In the case that the page is moved far to one direction and then is flung
1238 // in the opposite direction, we use a threshold to determine whether we should
1239 // just return to the starting page, or if we should skip one further.
1240 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001241 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1242 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001243 returnToOriginalPage = true;
1244 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001245
Adam Cohenf358a4b2013-07-23 16:47:31 -07001246 int finalPage;
1247 // We give flings precedence over large moves, which is why we short-circuit our
1248 // test for a large move if a fling has been registered. That is, a large
1249 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001250
Vinit Nayaka406f722019-12-19 11:50:55 -08001251 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1252 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001253 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001254 snapToPageWithVelocity(finalPage, velocity);
1255 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1256 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001257 mCurrentPage < getChildCount() - 1) {
1258 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001259 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 } else {
1261 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001264 if (!mScroller.isFinished()) {
1265 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001266 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001267
Vinit Nayaka406f722019-12-19 11:50:55 -08001268 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1269 int maxScroll = mMaxScroll;
1270 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001271
Vinit Nayaka406f722019-12-19 11:50:55 -08001272 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1273 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1274 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001275 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001276 } else {
1277 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001278 mScroller.fling(initialScroll, -velocity,
1279 minScroll, maxScroll,
1280 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001281
Vinit Nayaka406f722019-12-19 11:50:55 -08001282 int finalPos = mScroller.getFinalPos();
1283 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001284
1285 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1286 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001287 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001288 // If scrolling ends in the half of the added space that is closer to
1289 // the end, settle to the end. Otherwise snap to the nearest page.
1290 // If flinging past one of the ends, don't change the velocity as it
1291 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001292 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1293 ? minScroll
1294 : finalPos > (lastPageScroll + maxScroll) / 2
1295 ? maxScroll
1296 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001297
Vinit Nayaka406f722019-12-19 11:50:55 -08001298 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001299 // Ensure the scroll/snap doesn't happen too fast;
1300 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001301 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001302 if (extraScrollDuration > 0) {
1303 mScroller.extendDuration(extraScrollDuration);
1304 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001305 }
1306 }
1307 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001308 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001309 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001311
Adam Cohen7d30a372013-07-01 17:03:59 -07001312 // End any intermediate reordering states
1313 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001314 break;
1315
1316 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001317 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001318 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001319 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001320 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001321 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 break;
1323
1324 case MotionEvent.ACTION_POINTER_UP:
1325 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001326 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 break;
1328 }
1329
1330 return true;
1331 }
1332
Vinit Nayaka406f722019-12-19 11:50:55 -08001333 protected boolean shouldFlingForVelocity(int velocity) {
1334 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001335 }
1336
Adam Cohen7d30a372013-07-01 17:03:59 -07001337 private void resetTouchState() {
1338 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001339 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001340 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001341 }
1342
Adam Cohenc2d6e892014-10-16 09:49:24 -07001343 /**
1344 * Triggered by scrolling via touch
1345 */
1346 protected void onScrollInteractionBegin() {
1347 }
1348
1349 protected void onScrollInteractionEnd() {
1350 }
1351
Winson Chung185d7162011-02-28 13:47:29 -08001352 @Override
1353 public boolean onGenericMotionEvent(MotionEvent event) {
1354 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1355 switch (event.getAction()) {
1356 case MotionEvent.ACTION_SCROLL: {
Samuel Fufa9fd21052019-08-28 17:06:51 -07001357 Launcher launcher = Launcher.getLauncher(getContext());
1358 if (launcher != null) {
1359 AbstractFloatingView.closeAllOpenViews(launcher);
1360 }
Winson Chung185d7162011-02-28 13:47:29 -08001361 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1362 final float vscroll;
1363 final float hscroll;
1364 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1365 vscroll = 0;
1366 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1367 } else {
1368 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1369 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1370 }
Samuel Fufa59cba192019-08-22 18:31:34 -07001371 if (Math.abs(vscroll) > Math.abs(hscroll) && !isVerticalScrollable()) {
1372 return true;
1373 }
Winson Chung185d7162011-02-28 13:47:29 -08001374 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001375 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001376 : (hscroll > 0 || vscroll > 0);
1377 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001378 scrollRight();
1379 } else {
1380 scrollLeft();
1381 }
1382 return true;
1383 }
1384 }
1385 }
1386 }
1387 return super.onGenericMotionEvent(event);
1388 }
1389
Samuel Fufa59cba192019-08-22 18:31:34 -07001390 protected boolean isVerticalScrollable() {
1391 return true;
1392 }
1393
Michael Jurkab8f06722010-10-10 15:58:46 -07001394 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1395 if (mVelocityTracker == null) {
1396 mVelocityTracker = VelocityTracker.obtain();
1397 }
1398 mVelocityTracker.addMovement(ev);
1399 }
1400
1401 private void releaseVelocityTracker() {
1402 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001404 mVelocityTracker.recycle();
1405 mVelocityTracker = null;
1406 }
1407 }
1408
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 private void onSecondaryPointerUp(MotionEvent ev) {
1410 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1411 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1412 final int pointerId = ev.getPointerId(pointerIndex);
1413 if (pointerId == mActivePointerId) {
1414 // This was our active pointer going up. Choose a new
1415 // active pointer and adjust accordingly.
1416 // TODO: Make this decision more intelligent.
1417 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001418 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1419 newPointerIndex);
1420 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 mActivePointerId = ev.getPointerId(newPointerIndex);
1422 if (mVelocityTracker != null) {
1423 mVelocityTracker.clear();
1424 }
1425 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001426 }
1427
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 @Override
1429 public void requestChildFocus(View child, View focused) {
1430 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001431 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001432 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001433 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 }
1435 }
1436
Sunny Goyal228153d2018-01-04 15:35:22 -08001437 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001438 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001439 }
1440
Vinit Nayaka406f722019-12-19 11:50:55 -08001441 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1442 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1443 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001444 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 final int childCount = getChildCount();
1447 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001448 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001449 int childSize = mOrientationHandler.getMeasuredSize(layout);
1450 int halfChildSize = (childSize / 2);
1451 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1453 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1454 minDistanceFromScreenCenter = distanceFromScreenCenter;
1455 minDistanceFromScreenCenterIndex = i;
1456 }
1457 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001458 return minDistanceFromScreenCenterIndex;
1459 }
1460
1461 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001462 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1463 }
1464
1465 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001466 int scroll = mOrientationHandler.getPrimaryScroll(this);
1467 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001468 }
1469
1470 protected int getPageSnapDuration() {
1471 if (isInOverScroll()) {
1472 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1473 }
1474 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 }
1476
Adam Cohene0f66b52010-11-23 15:06:07 -08001477 // We want the duration of the page snap animation to be influenced by the distance that
1478 // the screen has to travel, however, we don't want this duration to be effected in a
1479 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1480 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001481 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001482 f -= 0.5f; // center the values about 0.
1483 f *= 0.3f * Math.PI / 2.0f;
1484 return (float) Math.sin(f);
1485 }
1486
Winson Chung05a31ed2018-02-08 17:08:43 -08001487 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001488 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001489 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001490
Vinit Nayaka406f722019-12-19 11:50:55 -08001491 final int newLoc = getScrollForPage(whichPage);
1492 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001493 int duration = 0;
1494
Sunny Goyal4d113a52015-05-27 10:05:28 -07001495 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001496 // If the velocity is low enough, then treat this more as an automatic page advance
1497 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001498 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001499 }
1500
1501 // Here we compute a "distance" that will be used in the computation of the overall
1502 // snap duration. This is a function of the actual distance that needs to be traveled;
1503 // we keep this value close to half screen size in order to reduce the variance in snap
1504 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001505 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001506 float distance = halfScreenSize + halfScreenSize *
1507 distanceInfluenceForSnapDuration(distanceRatio);
1508
1509 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001510 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001511
1512 // we want the page's snap velocity to approximately match the velocity at which the
1513 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001514 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1515 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001516
Jon Miranda9e198662020-03-11 14:42:02 -07001517 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001518 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001519 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001520 } else {
1521 return snapToPage(whichPage, delta, duration);
1522 }
Michael Jurka0142d492010-08-25 17:46:15 -07001523 }
1524
Winson Chung05a31ed2018-02-08 17:08:43 -08001525 public boolean snapToPage(int whichPage) {
1526 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 }
1528
Winson Chung05a31ed2018-02-08 17:08:43 -08001529 public boolean snapToPageImmediately(int whichPage) {
1530 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 }
1532
Winson Chung05a31ed2018-02-08 17:08:43 -08001533 public boolean snapToPage(int whichPage, int duration) {
1534 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001535 }
1536
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001537 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001538 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001539 }
1540
Winson Chung05a31ed2018-02-08 17:08:43 -08001541 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001542 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001543 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001544
Vinit Nayaka406f722019-12-19 11:50:55 -08001545 int newLoc = getScrollForPage(whichPage);
1546 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001547 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001548 }
1549
Winson Chung05a31ed2018-02-08 17:08:43 -08001550 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001551 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001552 }
Michael Jurka0142d492010-08-25 17:46:15 -07001553
Winson Chung05a31ed2018-02-08 17:08:43 -08001554 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001555 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001556 if (mFirstLayout) {
1557 setCurrentPage(whichPage);
1558 return false;
1559 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001560
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001561 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001562 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1563 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001564 }
1565
Adam Cohen6f127a62014-06-12 14:54:41 -07001566 whichPage = validateNewPage(whichPage);
1567
Adam Cohen7d30a372013-07-01 17:03:59 -07001568 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001569
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001571 if (immediate) {
1572 duration = 0;
1573 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001574 duration = Math.abs(delta);
1575 }
1576
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001577 if (duration != 0) {
1578 pageBeginTransition();
1579 }
1580
Adam Cohenf358a4b2013-07-23 16:47:31 -07001581 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001582 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001583 }
Adam Cohenf9618852013-11-08 06:45:03 -08001584
1585 if (interpolator != null) {
1586 mScroller.setInterpolator(interpolator);
1587 } else {
1588 mScroller.setInterpolator(mDefaultInterpolator);
1589 }
1590
Jon Miranda71cb80c2019-01-24 21:25:13 -08001591 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001592 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001593 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001594 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001595 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001596
Adam Cohen674531f2013-12-13 15:07:14 -08001597 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001598
1599 // Trigger a compute() to finish switching pages if necessary
1600 if (immediate) {
1601 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001602 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001603 }
1604
Winson Chung321e9ee2010-08-09 13:37:56 -07001605 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001606 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 }
1608
Vadim Tryshev98913d02018-05-18 18:41:34 -07001609 public boolean scrollLeft() {
1610 if (getNextPage() > 0) {
1611 snapToPage(getNextPage() - 1);
1612 return true;
1613 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001614 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001615 }
1616
Vadim Tryshev98913d02018-05-18 18:41:34 -07001617 public boolean scrollRight() {
1618 if (getNextPage() < getChildCount() - 1) {
1619 snapToPage(getNextPage() + 1);
1620 return true;
1621 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001622 return onOverscroll(getMeasuredWidth());
1623 }
1624
1625 protected boolean onOverscroll(int amount) {
1626 if (!mAllowOverScroll) return false;
1627 onScrollInteractionBegin();
1628 overScroll(amount);
1629 onScrollInteractionEnd();
1630 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001631 }
1632
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001633 @Override
1634 public CharSequence getAccessibilityClassName() {
1635 // Some accessibility services have special logic for ScrollView. Since we provide same
1636 // accessibility info as ScrollView, inform the service to handle use the same way.
1637 return ScrollView.class.getName();
1638 }
1639
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001640 protected boolean isPageOrderFlipped() {
1641 return false;
1642 }
1643
Winson Chung6a0f57d2011-06-29 20:10:49 -07001644 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001645 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001646 @Override
1647 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1648 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001649 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001650 int offset = (mAllowOverScroll ? 0 : 1);
1651 info.setScrollable(getPageCount() > offset);
1652 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001653 info.addAction(pagesFlipped ?
1654 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1655 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1656 info.addAction(mIsRtl ?
1657 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1658 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001659 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001660 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001661 info.addAction(pagesFlipped ?
1662 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1663 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1664 info.addAction(mIsRtl ?
1665 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1666 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001667 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001668 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1669 // Besides disabling the accessibility long-click, this also prevents this view from getting
1670 // accessibility focus.
1671 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001672 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001673 }
1674
1675 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001676 public void sendAccessibilityEvent(int eventType) {
1677 // Don't let the view send real scroll events.
1678 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1679 super.sendAccessibilityEvent(eventType);
1680 }
1681 }
1682
1683 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001684 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1685 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001686 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001687 }
1688
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001689 @Override
1690 public boolean performAccessibilityAction(int action, Bundle arguments) {
1691 if (super.performAccessibilityAction(action, arguments)) {
1692 return true;
1693 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001694 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001695 switch (action) {
1696 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001697 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001698 return true;
1699 }
1700 } break;
1701 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001702 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001703 return true;
1704 }
yingleiw02cc8482019-08-02 16:14:27 -07001705 } break;
1706 case android.R.id.accessibilityActionPageRight: {
1707 if (!mIsRtl) {
1708 return scrollRight();
1709 } else {
1710 return scrollLeft();
1711 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001712 }
yingleiw02cc8482019-08-02 16:14:27 -07001713 case android.R.id.accessibilityActionPageLeft: {
1714 if (!mIsRtl) {
1715 return scrollLeft();
1716 } else {
1717 return scrollRight();
1718 }
1719 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001720 }
1721 return false;
1722 }
1723
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001724 protected boolean canAnnouncePageDescription() {
1725 return true;
1726 }
1727
Adam Cohen0ffac432013-07-10 11:19:26 -07001728 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001729 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001730 getNextPage() + 1, getChildCount());
1731 }
1732
Tony Mak2e564402018-02-27 17:19:34 +00001733 protected float getDownMotionX() {
1734 return mDownMotionX;
1735 }
1736
1737 protected float getDownMotionY() {
1738 return mDownMotionY;
1739 }
1740
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001741 protected interface ComputePageScrollsLogic {
1742
1743 boolean shouldIncludeView(View view);
1744 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001745
1746 public int[] getVisibleChildrenRange() {
1747 float visibleLeft = 0;
1748 float visibleRight = visibleLeft + getMeasuredWidth();
1749 float scaleX = getScaleX();
1750 if (scaleX < 1 && scaleX > 0) {
1751 float mid = getMeasuredWidth() / 2;
1752 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1753 visibleRight = mid + ((visibleRight - mid) / scaleX);
1754 }
1755
1756 int leftChild = -1;
1757 int rightChild = -1;
1758 final int childCount = getChildCount();
1759 for (int i = 0; i < childCount; i++) {
1760 final View child = getPageAt(i);
1761
1762 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1763 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1764 if (leftChild == -1) {
1765 leftChild = i;
1766 }
1767 rightChild = i;
1768 }
1769 }
1770 mTmpIntPair[0] = leftChild;
1771 mTmpIntPair[1] = rightChild;
1772 return mTmpIntPair;
1773 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001774}