blob: a6180a63606150dd04bda1625159304ed3381762 [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
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070045import com.android.launcher3.anim.Interpolators;
vadimt4d93df52019-07-10 17:32:58 -070046import com.android.launcher3.compat.AccessibilityManagerCompat;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070047import com.android.launcher3.config.FeatureFlags;
Vinit Nayaka406f722019-12-19 11:50:55 -080048import com.android.launcher3.model.PagedViewOrientedState;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049import com.android.launcher3.pageindicators.PageIndicator;
Vinit Nayak5fcf0612020-03-03 12:22:25 -080050import com.android.launcher3.states.RotationHelper;
Vinit Nayaka406f722019-12-19 11:50:55 -080051import com.android.launcher3.touch.PortraitPagedViewHandler;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070052import com.android.launcher3.touch.OverScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -080053import com.android.launcher3.touch.PagedOrientationHandler;
54import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalb7b01352018-08-09 12:31:20 -070055import com.android.launcher3.util.OverScroller;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070057
Winson Chung6a0f57d2011-06-29 20:10:49 -070058import java.util.ArrayList;
59
Vinit Nayaka406f722019-12-19 11:50:55 -080060import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
61import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
62import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
63import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
64import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
65import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
66import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
67
Winson Chung321e9ee2010-08-09 13:37:56 -070068/**
69 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070070 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070071 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080072public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070073 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070074 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070075
Vinit Nayaka406f722019-12-19 11:50:55 -080076 public static boolean sFlagForcedRotation = false;
77
Sunny Goyala7a5bf32020-01-05 15:35:29 +053078 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070079 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Vadim Tryshevfedca432015-08-19 17:55:02 -070081 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070082
Sunny Goyalb72d8b22017-07-14 00:02:27 -070083 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070084 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070085
Adam Cohenb64cb5a2011-02-15 13:53:42 -080086 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080087 // The page is moved more than halfway, automatically move to the next page on touch up.
88 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080089
Sunny Goyal8e2133b2015-05-06 13:39:07 -070090 private static final float MAX_SCROLL_PROGRESS = 1.0f;
91
Adam Cohen265b9a62011-12-07 14:37:18 -080092 // The following constants need to be scaled based on density. The scaled versions will be
93 // assigned to the corresponding member variables below.
94 private static final int FLING_THRESHOLD_VELOCITY = 500;
95 private static final int MIN_SNAP_VELOCITY = 1500;
96 private static final int MIN_FLING_VELOCITY = 250;
97
Adam Cohenf358a4b2013-07-23 16:47:31 -070098 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070099
Adam Cohen265b9a62011-12-07 14:37:18 -0800100 protected int mFlingThresholdVelocity;
101 protected int mMinFlingVelocity;
102 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected boolean mFirstLayout = true;
105
Sunny Goyal4ffec482016-02-09 11:28:52 -0800106 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700108
Sunny Goyal4ffec482016-02-09 11:28:52 -0800109 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800111 protected int mMaxScroll;
112 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700113 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800114 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800116 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
118 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700119 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800120 private float mDownMotionPrimary;
121 private float mLastMotion;
122 private float mLastMotionRemainder;
123 private float mTotalMotion;
124 protected PagedOrientationHandler mOrientationHandler = new PortraitPagedViewHandler();
125 protected final PagedViewOrientedState mOrientationState = new PagedViewOrientedState();
Adam Cohenedb40762013-07-18 16:45:45 -0700126
Vadim Trysheve628c472018-05-16 14:28:18 -0700127 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700128 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800129
Michael Jurka7426c422010-11-11 15:23:47 -0800130 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800132 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Michael Jurka5f1c5092010-09-03 14:15:02 -0700134 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka5f1c5092010-09-03 14:15:02 -0700136 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700138 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700139
Vinit Nayaka406f722019-12-19 11:50:55 -0800140 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800141
Sunny Goyal061380a2016-02-29 15:15:03 -0800142 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700143
Vinit Nayaka406f722019-12-19 11:50:55 -0800144 protected int mUnboundedScroll;
145
146 protected int mLayoutRotation = Surface.ROTATION_0;
147 protected int mDisplayRotation = Surface.ROTATION_0;
Adam Cohen8d769d62017-06-23 17:27:38 -0700148
Winson Chungd2be3812013-07-16 11:11:32 -0700149 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700150 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800151 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700152
John Spurlock77e1f472013-09-11 10:09:51 -0400153 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800154 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400155
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800156 // Similar to the platform implementation of isLayoutValid();
157 protected boolean mIsLayoutValid;
158
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700159 private int[] mTmpIntPair = new int[2];
160
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 public PagedView(Context context) {
162 this(context, null);
163 }
164
165 public PagedView(Context context, AttributeSet attrs) {
166 this(context, attrs, 0);
167 }
168
169 public PagedView(Context context, AttributeSet attrs, int defStyle) {
170 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700171
Adam Cohen9c4949e2010-10-05 12:27:22 -0700172 TypedArray a = context.obtainStyledAttributes(attrs,
173 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700174 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 a.recycle();
176
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700178 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700179 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 }
181
182 /**
183 * Initializes various states for this workspace.
184 */
Michael Jurka0142d492010-08-25 17:46:15 -0700185 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800186 Context context = getContext();
187 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700188 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700189 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700190
Vinit Nayaka406f722019-12-19 11:50:55 -0800191 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700192 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800194
Sunny Goyalcf25b522015-07-09 00:01:18 -0700195 float density = getResources().getDisplayMetrics().density;
196 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
197 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
198 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700199
200 if (Utilities.ATLEAST_OREO) {
201 setDefaultFocusHighlightEnabled(false);
202 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800203
204 sFlagForcedRotation = Utilities.isForcedRotation(context);
Winson Chung321e9ee2010-08-09 13:37:56 -0700205 }
206
Adam Cohenf9618852013-11-08 06:45:03 -0800207 protected void setDefaultInterpolator(Interpolator interpolator) {
208 mDefaultInterpolator = interpolator;
209 mScroller.setInterpolator(mDefaultInterpolator);
210 }
211
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700212 public void initParentViews(View parent) {
213 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800214 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700215 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700216 }
217 }
218
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800219 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700220 return mPageIndicator;
221 }
222
Adam Cohen674531f2013-12-13 15:07:14 -0800223 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700224 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
225 * that the user was on before entering free scroll mode (e.g. the home screen page they
226 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
227 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700229 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700230 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700232
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700233 /**
234 * Returns the index of page to be shown immediately afterwards.
235 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700236 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700237 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
238 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700239
Adam Cohenf9c184a2016-01-15 16:47:43 -0800240 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 return getChildCount();
242 }
243
Sunny Goyal83a8f042015-05-19 12:52:12 -0700244 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 return getChildAt(index);
246 }
247
Adam Cohenae4f1552011-10-20 00:15:42 -0700248 protected int indexToPage(int index) {
249 return index;
250 }
251
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800253 * Updates the scroll of the current page immediately to its final scroll position. We use this
254 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
255 * the previous tab page.
256 */
257 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700258 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800259 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700260 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800261 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700262 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800263 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
264 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700265 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800266 }
267
Sreyase3e7e632020-01-29 10:42:10 -0800268 /**
269 * Returns left offset of a page. This is the gap between pages and prevents overlap.
270 */
271 public int scrollOffsetLeft() {
272 return mInsets.left + getPaddingLeft();
273 }
274
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700275 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700276 mScroller.abortAnimation();
277 // We need to clean up the next page here to avoid computeScrollHelper from
278 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700279 if (resetNextPage) {
280 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800281 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700282 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700283 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700284
Tony Wickham8f7ead32016-04-07 18:46:44 -0700285 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700286 mScroller.forceFinished(true);
287 // We need to clean up the next page here to avoid computeScrollHelper from
288 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700289 if (resetNextPage) {
290 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800291 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700292 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700293 }
294
Adam Cohen6f127a62014-06-12 14:54:41 -0700295 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700296 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700297 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700298 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700299 }
300
Chet Haasebc2f0822012-10-26 17:59:53 -0700301 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700302 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
303 */
304 private int ensureWithinScrollBounds(int page) {
305 int dir = !mIsRtl ? 1 : - 1;
306 int currScroll = getScrollForPage(page);
307 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800308 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700309 page += dir;
310 prevScroll = currScroll;
311 currScroll = getScrollForPage(page);
312 if (currScroll <= prevScroll) {
313 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
314 break;
315 }
316 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800317 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700318 page -= dir;
319 prevScroll = currScroll;
320 currScroll = getScrollForPage(page);
321 if (currScroll >= prevScroll) {
322 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
323 break;
324 }
325 }
326 return page;
327 }
328
Tony Wickham03f27012019-04-25 14:22:54 -0700329 public void setCurrentPage(int currentPage) {
330 setCurrentPage(currentPage, INVALID_PAGE);
331 }
332
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700333 /**
Winson Chung86f77532010-08-24 11:08:22 -0700334 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 */
Tony Wickham03f27012019-04-25 14:22:54 -0700336 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800337 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700338 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800339 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800340 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
341 // the default
342 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800343 return;
344 }
Tony Wickham03f27012019-04-25 14:22:54 -0700345 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700346 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700347 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700348 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800349 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 }
351
Winson Chung8c87cd82013-07-23 16:20:10 -0700352 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800353 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
354 * has settled.
355 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700356 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800357 updatePageIndicator();
358 }
359
360 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700361 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000362 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700363 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700365 protected void pageBeginTransition() {
366 if (!mIsPageInTransition) {
367 mIsPageInTransition = true;
368 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700369 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700370 }
371
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700372 protected void pageEndTransition() {
373 if (mIsPageInTransition) {
374 mIsPageInTransition = false;
375 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700376 }
Michael Jurka0142d492010-08-25 17:46:15 -0700377 }
378
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700379 protected boolean isPageInTransition() {
380 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700381 }
382
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700383 /**
384 * Called when the page starts moving as part of the scroll. Subclasses can override this
385 * to provide custom behavior during animation.
386 */
387 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700388 }
389
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700390 /**
391 * Called when the page ends moving as part of the scroll. Subclasses can override this
392 * to provide custom behavior during animation.
393 */
394 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700395 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700396 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800397 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
398 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Michael Jurka0142d492010-08-25 17:46:15 -0700399 }
400
Vinit Nayaka406f722019-12-19 11:50:55 -0800401 protected int getUnboundedScroll() {
402 return mUnboundedScroll;
403 }
404
405 protected void updateLayoutRotation(int touchRotation) {
406 setLayoutRotation(touchRotation, mDisplayRotation);
407 }
408
409 /** @param touchRotation Must be one of {@link android.view.Surface.ROTATION_0/90/180/270} */
410 public void setLayoutRotation(int touchRotation, int displayRotation) {
411 if (mLayoutRotation == touchRotation && mDisplayRotation == displayRotation) {
412 return;
413 }
414
415 mOrientationState.update(touchRotation, displayRotation);
416 mOrientationHandler = mOrientationState.getOrientationHandler();
417 mLayoutRotation = touchRotation;
418 mDisplayRotation = displayRotation;
419 requestLayout();
420 }
421
422 public PagedViewOrientedState getPagedViewOrientedState() {
423 return mOrientationState;
424 }
425
426 public PagedOrientationHandler getPagedOrientationHandler() {
427 return getPagedViewOrientedState().getOrientationHandler();
428 }
429
430 public void disableMultipleLayoutRotations(boolean disable) {
431 mOrientationState.disableMultipleOrientations(disable);
432 mOrientationHandler = mOrientationState.getOrientationHandler();
433 requestLayout();
Sunny Goyalc86df472016-02-25 09:19:38 -0800434 }
435
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800437 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800438 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800439 }
440
441 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700442 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800443 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
444 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
445 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700446
Vinit Nayaka406f722019-12-19 11:50:55 -0800447 boolean isBeforeFirstPage = mIsRtl ?
448 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
449 boolean isAfterLastPage = mIsRtl ?
450 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
451 if (!isBeforeFirstPage && !isAfterLastPage) {
452 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800453 }
454
Vinit Nayaka406f722019-12-19 11:50:55 -0800455 if (isBeforeFirstPage) {
456 mOrientationHandler.delegateScrollTo(this,
457 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800458 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700459 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800460 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800461 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800462 } else if (isAfterLastPage) {
463 mOrientationHandler.delegateScrollTo(this,
464 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800465 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700466 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800467 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800468 }
469 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700470 if (mWasInOverscroll) {
471 overScroll(0);
472 mWasInOverscroll = false;
473 }
Adam Cohen68d73932010-11-15 10:50:58 -0800474 super.scrollTo(x, y);
475 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800476 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700477
Vinit Nayaka406f722019-12-19 11:50:55 -0800478 /**
479 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
480 */
481 public void superScrollTo(int x, int y) {
482 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700483 }
484
Adam Cohen53805212013-10-01 10:39:23 -0700485 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800486 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700487 if (mCurrentPage != getNextPage()) {
488 AccessibilityEvent ev =
489 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700490 ev.setScrollable(true);
491 ev.setScrollX(getScrollX());
492 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800493 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700494 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700495 }
Adam Cohen53805212013-10-01 10:39:23 -0700496 }
497 }
498
Michael Jurka0142d492010-08-25 17:46:15 -0700499 // we moved this functionality to a helper function so SmoothPagedView can reuse it
500 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800501 return computeScrollHelper(true);
502 }
503
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700504 protected void announcePageForAccessibility() {
505 if (isAccessibilityEnabled(getContext())) {
506 // Notify the user when the page changes
507 announceForAccessibility(getCurrentPageDescription());
508 }
509 }
510
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800511 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700513 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800514 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
515 if (mUnboundedScroll != mScroller.getCurrPos()
516 || currentScroll != mScroller.getCurrPos()) {
517 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700518 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800519 if (shouldInvalidate) {
520 invalidate();
521 }
Michael Jurka0142d492010-08-25 17:46:15 -0700522 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800523 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700524 sendScrollAccessibilityEvent();
525
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700526 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700527 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700528 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700529 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700530
Adam Cohen73aa9752010-11-24 16:26:58 -0800531 // We don't want to trigger a page end moving unless the page has settled
532 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700533 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700534 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800535 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700536
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700537 if (canAnnouncePageDescription()) {
538 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700539 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700540 }
Michael Jurka0142d492010-08-25 17:46:15 -0700541 return false;
542 }
543
544 @Override
545 public void computeScroll() {
546 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 }
548
Sunny Goyalce8809a2018-01-18 14:02:47 -0800549 public int getExpectedHeight() {
550 return getMeasuredHeight();
551 }
552
Adam Cohen410f3cd2013-09-22 12:09:32 -0700553 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800554 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800555 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700556 }
557
Sunny Goyalce8809a2018-01-18 14:02:47 -0800558 public int getExpectedWidth() {
559 return getMeasuredWidth();
560 }
561
Sunny Goyalbc683e92017-12-06 10:25:07 -0800562 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800563 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800564 - mInsets.left - mInsets.right;
565 }
566
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800568 public void requestLayout() {
569 mIsLayoutValid = false;
570 super.requestLayout();
571 }
572
573 @Override
574 public void forceLayout() {
575 mIsLayoutValid = false;
576 super.forceLayout();
577 }
578
579 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700581 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700582 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
583 return;
584 }
585
Adam Cohen7d30a372013-07-01 17:03:59 -0700586 // We measure the dimensions of the PagedView to be larger than the pages so that when we
587 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700588 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
589 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
590 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700591 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700592
Adam Cohen7d30a372013-07-01 17:03:59 -0700593 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
594 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
595 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700596 }
597
Winson Chung8aad6102012-05-11 16:27:49 -0700598 // Return early if we aren't given a proper dimension
599 if (widthSize <= 0 || heightSize <= 0) {
600 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
601 return;
602 }
603
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700605 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700606 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700607
Sunny Goyalac00cba2017-11-13 15:58:01 -0800608 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800609 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800610 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800611 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700612
Sunny Goyalac00cba2017-11-13 15:58:01 -0800613 // measureChildren takes accounts for content padding, we only need to care about extra
614 // space due to insets.
615 measureChildren(myWidthSpec, myHeightSpec);
616 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800617 }
618
Sunny Goyalcf25b522015-07-09 00:01:18 -0700619 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700621 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800622 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700623 final int childCount = getChildCount();
624 boolean pageScrollChanged = false;
625 if (mPageScrolls == null || childCount != mPageScrolls.length) {
626 mPageScrolls = new int[childCount];
627 pageScrollChanged = true;
628 }
629
630 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700631 return;
632 }
633
Winson Chung785d2eb2011-04-14 16:08:02 -0700634 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700635
Vinit Nayaka406f722019-12-19 11:50:55 -0800636 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
637 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700638 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700640
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700641 final LayoutTransition transition = getLayoutTransition();
642 // If the transition is running defer updating max scroll, as some empty pages could
643 // still be present, and a max scroll change could cause sudden jumps in scroll.
644 if (transition != null && transition.isRunning()) {
645 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
646
647 @Override
648 public void startTransition(LayoutTransition transition, ViewGroup container,
649 View view, int transitionType) { }
650
651 @Override
652 public void endTransition(LayoutTransition transition, ViewGroup container,
653 View view, int transitionType) {
654 // Wait until all transitions are complete.
655 if (!transition.isRunning()) {
656 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700657 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700658 }
659 }
660 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700661 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700662 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700663 }
664
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700665 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
666 updateCurrentPageScroll();
667 mFirstLayout = false;
668 }
669
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800670 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700671 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700672 }
Winson Chunge3193b92010-09-10 11:44:42 -0700673 }
674
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700675 /**
676 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
677 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700678 */
679 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
680 ComputePageScrollsLogic scrollLogic) {
681 final int childCount = getChildCount();
682
683 final int startIndex = mIsRtl ? childCount - 1 : 0;
684 final int endIndex = mIsRtl ? -1 : childCount;
685 final int delta = mIsRtl ? -1 : 1;
686
Vinit Nayaka406f722019-12-19 11:50:55 -0800687 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700688
Vinit Nayaka406f722019-12-19 11:50:55 -0800689 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
690 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700691 boolean pageScrollChanged = false;
692
Vinit Nayaka406f722019-12-19 11:50:55 -0800693 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700694 final View child = getPageAt(i);
695 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800696 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
697 pageCenter, layoutChildren);
698 final int primaryDimension = bounds.primaryDimension;
699 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700700
Sunny Goyalc82c6392018-06-06 15:39:13 -0700701 // In case the pages are of different width, align the page to left or right edge
702 // based on the orientation.
703 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800704 ? (childStart - scrollOffsetStart)
705 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700706 if (outPageScrolls[i] != pageScroll) {
707 pageScrollChanged = true;
708 outPageScrolls[i] = pageScroll;
709 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800710 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700711 }
712 }
713 return pageScrollChanged;
714 }
715
Sunny Goyala1fbd842015-05-20 13:40:27 -0700716 protected int getChildGap() {
717 return 0;
718 }
719
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700720 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800721 mMinScroll = computeMinScroll();
722 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700723 }
724
Vinit Nayaka406f722019-12-19 11:50:55 -0800725 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700726 return 0;
Tony50876bf2018-09-19 23:09:14 -0400727 }
728
Vinit Nayaka406f722019-12-19 11:50:55 -0800729 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700730 int childCount = getChildCount();
731 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700732 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700733 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700734 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700735 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700736 }
737 }
738
Adam Cohen3b185e22013-10-29 14:45:58 -0700739 public void setPageSpacing(int pageSpacing) {
740 mPageSpacing = pageSpacing;
741 requestLayout();
742 }
743
Tony50876bf2018-09-19 23:09:14 -0400744 public int getPageSpacing() {
745 return mPageSpacing;
746 }
747
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800748 private void dispatchPageCountChanged() {
749 if (mPageIndicator != null) {
750 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700751 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700752 // This ensures that when children are added, they get the correct transforms / alphas
753 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700754 invalidate();
755 }
756
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800758 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800759 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800760 dispatchPageCountChanged();
761 }
762
763 @Override
764 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800765 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700766 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800767 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700768 }
769
Adam Cohen73894962011-10-31 13:17:17 -0700770 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700771 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800772 View pageAtIndex = getPageAt(index);
773 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700774 }
775
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 @Override
777 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700778 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700779 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700780 if (immediate) {
781 setCurrentPage(page);
782 } else {
783 snapToPage(page);
784 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 return true;
786 }
787 return false;
788 }
789
790 @Override
791 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700792 int focusablePage;
793 if (mNextPage != INVALID_PAGE) {
794 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700796 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 }
Winson Chung86f77532010-08-24 11:08:22 -0700798 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700800 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 }
802 return false;
803 }
804
805 @Override
806 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800807 if (super.dispatchUnhandledMove(focused, direction)) {
808 return true;
809 }
810
811 if (mIsRtl) {
812 if (direction == View.FOCUS_LEFT) {
813 direction = View.FOCUS_RIGHT;
814 } else if (direction == View.FOCUS_RIGHT) {
815 direction = View.FOCUS_LEFT;
816 }
817 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700819 if (getCurrentPage() > 0) {
820 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700821 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 return true;
823 }
824 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700825 if (getCurrentPage() < getPageCount() - 1) {
826 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700827 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 return true;
829 }
830 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800831 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 }
833
834 @Override
835 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700836 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
837 return;
838 }
839
Adam Cohen0ffac432013-07-10 11:19:26 -0700840 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700841 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700842 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700845 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700846 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700849 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700850 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 }
852 }
853 }
854
855 /**
856 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700857 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 *
Winson Chung86f77532010-08-24 11:08:22 -0700859 * This happens when live folders requery, and if they're off page, they
860 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 */
862 @Override
863 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700864 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 View v = focused;
866 while (true) {
867 if (v == current) {
868 super.focusableViewAvailable(focused);
869 return;
870 }
871 if (v == this) {
872 return;
873 }
874 ViewParent parent = v.getParent();
875 if (parent instanceof View) {
876 v = (View)v.getParent();
877 } else {
878 return;
879 }
880 }
881 }
882
883 /**
884 * {@inheritDoc}
885 */
886 @Override
887 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
888 if (disallowIntercept) {
889 // We need to make sure to cancel our long press if
890 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700891 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700892 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700893 }
894 super.requestDisallowInterceptTouchEvent(disallowIntercept);
895 }
896
897 @Override
898 public boolean onInterceptTouchEvent(MotionEvent ev) {
899 /*
900 * This method JUST determines whether we want to intercept the motion.
901 * If we return true, onTouchEvent will be called and we do the actual
902 * scrolling there.
903 */
904
Winson Chung45e1d6e2010-11-09 17:19:49 -0800905 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700906 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700907
908 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800909
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 /*
911 * Shortcut the most recurring case: the user is in the dragging
912 * state and he is moving his finger. We want to intercept this
913 * motion.
914 */
915 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700916 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 return true;
918 }
919
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 switch (action & MotionEvent.ACTION_MASK) {
921 case MotionEvent.ACTION_MOVE: {
922 /*
923 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800924 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700926 if (mActivePointerId != INVALID_POINTER) {
927 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700928 }
929 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800930 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700931 // i.e. fall through to the next case (don't break)
932 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
933 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700934 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 }
936
937 case MotionEvent.ACTION_DOWN: {
938 final float x = ev.getX();
939 final float y = ev.getY();
940 // Remember location of down touch
941 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700942 mDownMotionY = y;
Vinit Nayaka406f722019-12-19 11:50:55 -0800943 mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
944 mLastMotionRemainder = 0;
945 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700947
Tony Wickhama0aee212019-09-18 09:24:03 -0700948 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700949
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 break;
951 }
952
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800954 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700955 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 break;
957
958 case MotionEvent.ACTION_POINTER_UP:
959 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800960 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 break;
962 }
963
964 /*
965 * The only time we want to intercept motion events is if we are in the
966 * drag mode.
967 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700968 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 }
970
Tony Wickhama0aee212019-09-18 09:24:03 -0700971 /**
972 * If being flinged and user touches the screen, initiate drag; otherwise don't.
973 */
974 private void updateIsBeingDraggedOnTouchDown() {
975 // mScroller.isFinished should be false when being flinged.
976 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
977 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
978
979 if (finishedScrolling) {
980 mIsBeingDragged = false;
981 if (!mScroller.isFinished() && !mFreeScroll) {
982 setCurrentPage(getNextPage());
983 pageEndTransition();
984 }
985 } else {
986 mIsBeingDragged = true;
987 }
988 }
989
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800990 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700991 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800992 }
993
Adam Cohenf8d28232011-02-01 21:47:00 -0800994 protected void determineScrollingStart(MotionEvent ev) {
995 determineScrollingStart(ev, 1.0f);
996 }
997
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 /*
999 * Determines if we should change the touch state to start scrolling after the
1000 * user moves their touch point too far.
1001 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001002 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001003 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001005 if (pointerIndex == -1) return;
1006
Vinit Nayaka406f722019-12-19 11:50:55 -08001007 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1008 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001009 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -08001010 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001011
Vinit Nayaka406f722019-12-19 11:50:55 -08001012 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001013 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001014 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001015 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1016 mLastMotion = primaryDirection;
1017 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001018 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001019 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001020 // Stop listening for things like pinches.
1021 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001022 }
1023 }
1024
1025 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001026 // Try canceling the long press. It could also have been scheduled
1027 // by a distant descendant, so use the mAllowLongPress flag to block
1028 // everything
1029 final View currentPage = getPageAt(mCurrentPage);
1030 if (currentPage != null) {
1031 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 }
1033 }
1034
Adam Cohenb5ba0972011-09-07 18:02:31 -07001035 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001036 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001037
Adam Cohenedb40762013-07-18 16:45:45 -07001038 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001039 int count = getChildCount();
1040
1041 final int totalDistance;
1042
1043 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001044 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001045 adjacentPage = page - 1;
1046 }
1047
1048 if (adjacentPage < 0 || adjacentPage > count - 1) {
1049 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1050 } else {
1051 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1052 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001053
1054 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001055 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1056 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001057 return scrollProgress;
1058 }
1059
Adam Cohenedb40762013-07-18 16:45:45 -07001060 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001061 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001062 return 0;
1063 } else {
1064 return mPageScrolls[index];
1065 }
1066 }
1067
Adam Cohen564a2e72013-10-09 14:47:32 -07001068 // While layout transitions are occurring, a child's position may stray from its baseline
1069 // position. This method returns the magnitude of this stray at any given time.
1070 public int getLayoutTransitionOffsetForPage(int index) {
1071 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1072 return 0;
1073 } else {
1074 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001075
Sunny Goyal228153d2018-01-04 15:35:22 -08001076 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1077 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001078 return (int) (child.getX() - baselineX);
1079 }
1080 }
1081
Jon Miranda71cb80c2019-01-24 21:25:13 -08001082 @Override
1083 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001084 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001085 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001086 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001087 super.dispatchDraw(canvas);
1088
1089 canvas.restoreToCount(saveCount);
1090 } else {
1091 super.dispatchDraw(canvas);
1092 }
1093 }
1094
Sunny Goyalb7b01352018-08-09 12:31:20 -07001095 protected void dampedOverScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001096 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001097 if (amount == 0) {
1098 return;
1099 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001100
Vinit Nayaka406f722019-12-19 11:50:55 -08001101 int size = mOrientationHandler.getMeasuredSize(this);
1102 int overScrollAmount = OverScroll.dampedScroll(amount, size);
1103 mSpringOverScroll = overScrollAmount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001104 if (mScroller.isSpringing()) {
1105 invalidate();
1106 return;
1107 }
1108
Vinit Nayaka406f722019-12-19 11:50:55 -08001109 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1110 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1111 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001112 invalidate();
1113 }
1114
Sunny Goyalb7b01352018-08-09 12:31:20 -07001115 protected void overScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001116 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001117 if (mScroller.isSpringing()) {
1118 invalidate();
1119 return;
1120 }
1121
Sunny Goyalb7b01352018-08-09 12:31:20 -07001122 if (amount == 0) return;
1123
1124 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001125 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1126 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001127 } else {
1128 dampedOverScroll(amount);
1129 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001130 }
1131
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001132
Tony Wickham023f4042019-01-29 10:52:31 -08001133 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001134 if (mFreeScroll == freeScroll) {
1135 return;
1136 }
1137
Tony Wickham8f7ead32016-04-07 18:46:44 -07001138 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001139 mFreeScroll = freeScroll;
1140
Adam Cohenf9618852013-11-08 06:45:03 -08001141 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001142 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001143 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001144 if (getScrollForPage(getNextPage()) != getScrollX()) {
1145 snapToPage(getNextPage());
1146 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001147 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001148 }
1149
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001150 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001151 mAllowOverScroll = enable;
1152 }
1153
Winson Chung321e9ee2010-08-09 13:37:56 -07001154 @Override
1155 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001156 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001157 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001158
Michael Jurkab8f06722010-10-10 15:58:46 -07001159 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001160
1161 final int action = ev.getAction();
1162
1163 switch (action & MotionEvent.ACTION_MASK) {
1164 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001165 updateIsBeingDraggedOnTouchDown();
1166
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 /*
1168 * If being flinged and user touches, stop the fling. isFinished
1169 * will be false if being flinged.
1170 */
1171 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001172 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 }
1174
1175 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001176 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001177 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001178 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1179 mLastMotionRemainder = 0;
1180 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001182 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001183 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001184 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001185 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 break;
1187
Vinit Nayaka406f722019-12-19 11:50:55 -08001188 case MotionEvent.ACTION_MOVE:
1189 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 // Scroll to follow the motion event
1191 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001192
1193 if (pointerIndex == -1) return true;
1194
Vinit Nayaka406f722019-12-19 11:50:55 -08001195 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1196 float delta = mLastMotion + mLastMotionRemainder - direction;
1197 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001198
Winson Chungc0844aa2011-02-02 15:25:58 -08001199 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1200 // keep the remainder because we are actually testing if we've moved from the last
1201 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001202 if (Math.abs(delta) >= 1.0f) {
1203 mLastMotion = direction;
1204 mLastMotionRemainder = delta - (int) delta;
1205
1206 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 } else {
1208 awakenScrollBars();
1209 }
Adam Cohen564976a2010-10-13 18:52:07 -07001210 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 determineScrollingStart(ev);
1212 }
1213 break;
1214
1215 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001216 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 final int activePointerId = mActivePointerId;
1218 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001219 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1220 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001221 final VelocityTracker velocityTracker = mVelocityTracker;
1222 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001223
Vinit Nayaka406f722019-12-19 11:50:55 -08001224 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1225 mActivePointerId);
1226 int delta = (int) (primaryDirection - mDownMotionPrimary);
1227 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1228
1229 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1230 SIGNIFICANT_MOVE_THRESHOLD;
1231
1232 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1233 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1234 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1235 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001236
Adam Cohenf358a4b2013-07-23 16:47:31 -07001237 if (!mFreeScroll) {
1238 // In the case that the page is moved far to one direction and then is flung
1239 // in the opposite direction, we use a threshold to determine whether we should
1240 // just return to the starting page, or if we should skip one further.
1241 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001242 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1243 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001244 returnToOriginalPage = true;
1245 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001246
Adam Cohenf358a4b2013-07-23 16:47:31 -07001247 int finalPage;
1248 // We give flings precedence over large moves, which is why we short-circuit our
1249 // test for a large move if a fling has been registered. That is, a large
1250 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001251
Vinit Nayaka406f722019-12-19 11:50:55 -08001252 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1253 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001254 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001255 snapToPageWithVelocity(finalPage, velocity);
1256 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1257 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001258 mCurrentPage < getChildCount() - 1) {
1259 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001260 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001261 } else {
1262 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001263 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001265 if (!mScroller.isFinished()) {
1266 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001267 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001268
Vinit Nayaka406f722019-12-19 11:50:55 -08001269 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1270 int maxScroll = mMaxScroll;
1271 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001272
Vinit Nayaka406f722019-12-19 11:50:55 -08001273 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1274 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1275 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001276 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001277 } else {
1278 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001279 mScroller.fling(initialScroll, -velocity,
1280 minScroll, maxScroll,
1281 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001282
Vinit Nayaka406f722019-12-19 11:50:55 -08001283 int finalPos = mScroller.getFinalPos();
1284 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001285
1286 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1287 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001288 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001289 // If scrolling ends in the half of the added space that is closer to
1290 // the end, settle to the end. Otherwise snap to the nearest page.
1291 // If flinging past one of the ends, don't change the velocity as it
1292 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001293 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1294 ? minScroll
1295 : finalPos > (lastPageScroll + maxScroll) / 2
1296 ? maxScroll
1297 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001298
Vinit Nayaka406f722019-12-19 11:50:55 -08001299 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001300 // Ensure the scroll/snap doesn't happen too fast;
1301 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001302 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001303 if (extraScrollDuration > 0) {
1304 mScroller.extendDuration(extraScrollDuration);
1305 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001306 }
1307 }
1308 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001310 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001312
Adam Cohen7d30a372013-07-01 17:03:59 -07001313 // End any intermediate reordering states
1314 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 break;
1316
1317 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001318 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001319 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001320 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001321 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001323 break;
1324
1325 case MotionEvent.ACTION_POINTER_UP:
1326 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001327 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 break;
1329 }
1330
1331 return true;
1332 }
1333
Vinit Nayaka406f722019-12-19 11:50:55 -08001334 protected boolean shouldFlingForVelocity(int velocity) {
1335 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001336 }
1337
Adam Cohen7d30a372013-07-01 17:03:59 -07001338 private void resetTouchState() {
1339 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001340 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001341 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001342 }
1343
Adam Cohenc2d6e892014-10-16 09:49:24 -07001344 /**
1345 * Triggered by scrolling via touch
1346 */
1347 protected void onScrollInteractionBegin() {
1348 }
1349
1350 protected void onScrollInteractionEnd() {
1351 }
1352
Winson Chung185d7162011-02-28 13:47:29 -08001353 @Override
1354 public boolean onGenericMotionEvent(MotionEvent event) {
1355 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1356 switch (event.getAction()) {
1357 case MotionEvent.ACTION_SCROLL: {
Samuel Fufa9fd21052019-08-28 17:06:51 -07001358 Launcher launcher = Launcher.getLauncher(getContext());
1359 if (launcher != null) {
1360 AbstractFloatingView.closeAllOpenViews(launcher);
1361 }
Winson Chung185d7162011-02-28 13:47:29 -08001362 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1363 final float vscroll;
1364 final float hscroll;
1365 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1366 vscroll = 0;
1367 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1368 } else {
1369 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1370 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1371 }
Samuel Fufa59cba192019-08-22 18:31:34 -07001372 if (Math.abs(vscroll) > Math.abs(hscroll) && !isVerticalScrollable()) {
1373 return true;
1374 }
Winson Chung185d7162011-02-28 13:47:29 -08001375 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001376 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001377 : (hscroll > 0 || vscroll > 0);
1378 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001379 scrollRight();
1380 } else {
1381 scrollLeft();
1382 }
1383 return true;
1384 }
1385 }
1386 }
1387 }
1388 return super.onGenericMotionEvent(event);
1389 }
1390
Samuel Fufa59cba192019-08-22 18:31:34 -07001391 protected boolean isVerticalScrollable() {
1392 return true;
1393 }
1394
Michael Jurkab8f06722010-10-10 15:58:46 -07001395 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1396 if (mVelocityTracker == null) {
1397 mVelocityTracker = VelocityTracker.obtain();
1398 }
1399 mVelocityTracker.addMovement(ev);
1400 }
1401
1402 private void releaseVelocityTracker() {
1403 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001404 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001405 mVelocityTracker.recycle();
1406 mVelocityTracker = null;
1407 }
1408 }
1409
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 private void onSecondaryPointerUp(MotionEvent ev) {
1411 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1412 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1413 final int pointerId = ev.getPointerId(pointerIndex);
1414 if (pointerId == mActivePointerId) {
1415 // This was our active pointer going up. Choose a new
1416 // active pointer and adjust accordingly.
1417 // TODO: Make this decision more intelligent.
1418 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001419 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1420 newPointerIndex);
1421 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 mActivePointerId = ev.getPointerId(newPointerIndex);
1423 if (mVelocityTracker != null) {
1424 mVelocityTracker.clear();
1425 }
1426 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001427 }
1428
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 @Override
1430 public void requestChildFocus(View child, View focused) {
1431 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001432 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001433 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001434 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 }
1436 }
1437
Sunny Goyal228153d2018-01-04 15:35:22 -08001438 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001439 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001440 }
1441
Vinit Nayaka406f722019-12-19 11:50:55 -08001442 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1443 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1444 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001445 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001447 final int childCount = getChildCount();
1448 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001449 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001450 int childSize = mOrientationHandler.getMeasuredSize(layout);
1451 int halfChildSize = (childSize / 2);
1452 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1454 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1455 minDistanceFromScreenCenter = distanceFromScreenCenter;
1456 minDistanceFromScreenCenterIndex = i;
1457 }
1458 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001459 return minDistanceFromScreenCenterIndex;
1460 }
1461
1462 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001463 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1464 }
1465
1466 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001467 int scroll = mOrientationHandler.getPrimaryScroll(this);
1468 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001469 }
1470
1471 protected int getPageSnapDuration() {
1472 if (isInOverScroll()) {
1473 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1474 }
1475 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 }
1477
Adam Cohene0f66b52010-11-23 15:06:07 -08001478 // We want the duration of the page snap animation to be influenced by the distance that
1479 // the screen has to travel, however, we don't want this duration to be effected in a
1480 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1481 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001482 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001483 f -= 0.5f; // center the values about 0.
1484 f *= 0.3f * Math.PI / 2.0f;
1485 return (float) Math.sin(f);
1486 }
1487
Winson Chung05a31ed2018-02-08 17:08:43 -08001488 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001489 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001490 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001491
Vinit Nayaka406f722019-12-19 11:50:55 -08001492 final int newLoc = getScrollForPage(whichPage);
1493 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001494 int duration = 0;
1495
Sunny Goyal4d113a52015-05-27 10:05:28 -07001496 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001497 // If the velocity is low enough, then treat this more as an automatic page advance
1498 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001499 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001500 }
1501
1502 // Here we compute a "distance" that will be used in the computation of the overall
1503 // snap duration. This is a function of the actual distance that needs to be traveled;
1504 // we keep this value close to half screen size in order to reduce the variance in snap
1505 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001506 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001507 float distance = halfScreenSize + halfScreenSize *
1508 distanceInfluenceForSnapDuration(distanceRatio);
1509
1510 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001511 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001512
1513 // we want the page's snap velocity to approximately match the velocity at which the
1514 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001515 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1516 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001517
Jon Miranda71cb80c2019-01-24 21:25:13 -08001518 if (QUICKSTEP_SPRINGS.get()) {
1519 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001520 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001521 } else {
1522 return snapToPage(whichPage, delta, duration);
1523 }
Michael Jurka0142d492010-08-25 17:46:15 -07001524 }
1525
Winson Chung05a31ed2018-02-08 17:08:43 -08001526 public boolean snapToPage(int whichPage) {
1527 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 }
1529
Winson Chung05a31ed2018-02-08 17:08:43 -08001530 public boolean snapToPageImmediately(int whichPage) {
1531 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001532 }
1533
Winson Chung05a31ed2018-02-08 17:08:43 -08001534 public boolean snapToPage(int whichPage, int duration) {
1535 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001536 }
1537
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001538 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001539 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001540 }
1541
Winson Chung05a31ed2018-02-08 17:08:43 -08001542 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001543 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001544 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001545
Vinit Nayaka406f722019-12-19 11:50:55 -08001546 int newLoc = getScrollForPage(whichPage);
1547 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001548 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001549 }
1550
Winson Chung05a31ed2018-02-08 17:08:43 -08001551 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001552 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001553 }
Michael Jurka0142d492010-08-25 17:46:15 -07001554
Winson Chung05a31ed2018-02-08 17:08:43 -08001555 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001556 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001557 if (mFirstLayout) {
1558 setCurrentPage(whichPage);
1559 return false;
1560 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001561
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001562 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001563 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1564 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001565 }
1566
Adam Cohen6f127a62014-06-12 14:54:41 -07001567 whichPage = validateNewPage(whichPage);
1568
Adam Cohen7d30a372013-07-01 17:03:59 -07001569 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001570
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001572 if (immediate) {
1573 duration = 0;
1574 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 duration = Math.abs(delta);
1576 }
1577
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001578 if (duration != 0) {
1579 pageBeginTransition();
1580 }
1581
Adam Cohenf358a4b2013-07-23 16:47:31 -07001582 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001583 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001584 }
Adam Cohenf9618852013-11-08 06:45:03 -08001585
1586 if (interpolator != null) {
1587 mScroller.setInterpolator(interpolator);
1588 } else {
1589 mScroller.setInterpolator(mDefaultInterpolator);
1590 }
1591
Jon Miranda71cb80c2019-01-24 21:25:13 -08001592 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001593 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001594 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001595 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001596 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001597
Adam Cohen674531f2013-12-13 15:07:14 -08001598 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001599
1600 // Trigger a compute() to finish switching pages if necessary
1601 if (immediate) {
1602 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001603 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001604 }
1605
Winson Chung321e9ee2010-08-09 13:37:56 -07001606 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001607 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001608 }
1609
Vadim Tryshev98913d02018-05-18 18:41:34 -07001610 public boolean scrollLeft() {
1611 if (getNextPage() > 0) {
1612 snapToPage(getNextPage() - 1);
1613 return true;
1614 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001615 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001616 }
1617
Vadim Tryshev98913d02018-05-18 18:41:34 -07001618 public boolean scrollRight() {
1619 if (getNextPage() < getChildCount() - 1) {
1620 snapToPage(getNextPage() + 1);
1621 return true;
1622 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001623 return onOverscroll(getMeasuredWidth());
1624 }
1625
1626 protected boolean onOverscroll(int amount) {
1627 if (!mAllowOverScroll) return false;
1628 onScrollInteractionBegin();
1629 overScroll(amount);
1630 onScrollInteractionEnd();
1631 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001632 }
1633
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001634 @Override
1635 public CharSequence getAccessibilityClassName() {
1636 // Some accessibility services have special logic for ScrollView. Since we provide same
1637 // accessibility info as ScrollView, inform the service to handle use the same way.
1638 return ScrollView.class.getName();
1639 }
1640
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001641 protected boolean isPageOrderFlipped() {
1642 return false;
1643 }
1644
Winson Chung6a0f57d2011-06-29 20:10:49 -07001645 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001646 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001647 @Override
1648 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1649 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001650 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001651 int offset = (mAllowOverScroll ? 0 : 1);
1652 info.setScrollable(getPageCount() > offset);
1653 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001654 info.addAction(pagesFlipped ?
1655 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1656 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1657 info.addAction(mIsRtl ?
1658 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1659 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001660 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001661 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001662 info.addAction(pagesFlipped ?
1663 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1664 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1665 info.addAction(mIsRtl ?
1666 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1667 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001668 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001669 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1670 // Besides disabling the accessibility long-click, this also prevents this view from getting
1671 // accessibility focus.
1672 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001673 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001674 }
1675
1676 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001677 public void sendAccessibilityEvent(int eventType) {
1678 // Don't let the view send real scroll events.
1679 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1680 super.sendAccessibilityEvent(eventType);
1681 }
1682 }
1683
1684 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001685 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1686 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001687 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001688 }
1689
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001690 @Override
1691 public boolean performAccessibilityAction(int action, Bundle arguments) {
1692 if (super.performAccessibilityAction(action, arguments)) {
1693 return true;
1694 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001695 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001696 switch (action) {
1697 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001698 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001699 return true;
1700 }
1701 } break;
1702 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001703 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001704 return true;
1705 }
yingleiw02cc8482019-08-02 16:14:27 -07001706 } break;
1707 case android.R.id.accessibilityActionPageRight: {
1708 if (!mIsRtl) {
1709 return scrollRight();
1710 } else {
1711 return scrollLeft();
1712 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001713 }
yingleiw02cc8482019-08-02 16:14:27 -07001714 case android.R.id.accessibilityActionPageLeft: {
1715 if (!mIsRtl) {
1716 return scrollLeft();
1717 } else {
1718 return scrollRight();
1719 }
1720 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001721 }
1722 return false;
1723 }
1724
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001725 protected boolean canAnnouncePageDescription() {
1726 return true;
1727 }
1728
Adam Cohen0ffac432013-07-10 11:19:26 -07001729 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001730 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001731 getNextPage() + 1, getChildCount());
1732 }
1733
Tony Mak2e564402018-02-27 17:19:34 +00001734 protected float getDownMotionX() {
1735 return mDownMotionX;
1736 }
1737
1738 protected float getDownMotionY() {
1739 return mDownMotionY;
1740 }
1741
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001742 protected interface ComputePageScrollsLogic {
1743
1744 boolean shouldIncludeView(View view);
1745 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001746
1747 public int[] getVisibleChildrenRange() {
1748 float visibleLeft = 0;
1749 float visibleRight = visibleLeft + getMeasuredWidth();
1750 float scaleX = getScaleX();
1751 if (scaleX < 1 && scaleX > 0) {
1752 float mid = getMeasuredWidth() / 2;
1753 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1754 visibleRight = mid + ((visibleRight - mid) / scaleX);
1755 }
1756
1757 int leftChild = -1;
1758 int rightChild = -1;
1759 final int childCount = getChildCount();
1760 for (int i = 0; i < childCount; i++) {
1761 final View child = getPageAt(i);
1762
1763 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1764 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1765 if (leftChild == -1) {
1766 leftChild = i;
1767 }
1768 rightChild = i;
1769 }
1770 }
1771 mTmpIntPair[0] = leftChild;
1772 mTmpIntPair[1] = rightChild;
1773 return mTmpIntPair;
1774 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001775}