blob: 5e47e2ff73504a40fa47f4214e113841f7d84540 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyal8b37c572020-03-31 12:12:14 -070019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
22import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
23import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
24import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
25import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
26
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070027import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070031import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080032import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070034import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070035import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
Vinit Nayaka406f722019-12-19 11:50:55 -080041import android.view.Surface;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
49import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080050import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070051import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
Tony Wickham62117d72020-03-26 16:56:26 -070053import androidx.annotation.Nullable;
54
Jon Miranda9e198662020-03-11 14:42:02 -070055import com.android.launcher3.anim.Interpolators;
56import com.android.launcher3.compat.AccessibilityManagerCompat;
57import com.android.launcher3.config.FeatureFlags;
58import com.android.launcher3.model.PagedViewOrientedState;
59import com.android.launcher3.pageindicators.PageIndicator;
60import com.android.launcher3.touch.OverScroll;
61import com.android.launcher3.touch.PagedOrientationHandler;
62import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
63import com.android.launcher3.touch.PortraitPagedViewHandler;
64import com.android.launcher3.util.OverScroller;
65import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070066import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070067
68import java.util.ArrayList;
69
Winson Chung321e9ee2010-08-09 13:37:56 -070070/**
71 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070072 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070073 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080074public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070075 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070076 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070077
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;
Tony Wickham62117d72020-03-26 16:56:26 -0700139 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700140
Vinit Nayaka406f722019-12-19 11:50:55 -0800141 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800142
Sunny Goyal061380a2016-02-29 15:15:03 -0800143 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700144
Vinit Nayaka406f722019-12-19 11:50:55 -0800145 protected int mUnboundedScroll;
146
147 protected int mLayoutRotation = Surface.ROTATION_0;
148 protected int mDisplayRotation = Surface.ROTATION_0;
Adam Cohen8d769d62017-06-23 17:27:38 -0700149
Winson Chungd2be3812013-07-16 11:11:32 -0700150 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700151 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800152 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700153
John Spurlock77e1f472013-09-11 10:09:51 -0400154 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800155 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400156
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800157 // Similar to the platform implementation of isLayoutValid();
158 protected boolean mIsLayoutValid;
159
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700160 private int[] mTmpIntPair = new int[2];
161
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 public PagedView(Context context) {
163 this(context, null);
164 }
165
166 public PagedView(Context context, AttributeSet attrs) {
167 this(context, attrs, 0);
168 }
169
170 public PagedView(Context context, AttributeSet attrs, int defStyle) {
171 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700172
Adam Cohen9c4949e2010-10-05 12:27:22 -0700173 TypedArray a = context.obtainStyledAttributes(attrs,
174 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700175 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700176 a.recycle();
177
Winson Chung321e9ee2010-08-09 13:37:56 -0700178 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700179 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700180 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700181 }
182
183 /**
184 * Initializes various states for this workspace.
185 */
Michael Jurka0142d492010-08-25 17:46:15 -0700186 protected void init() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800187 Context context = getContext();
188 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700189 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700190 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700191
Vinit Nayaka406f722019-12-19 11:50:55 -0800192 final ViewConfiguration configuration = ViewConfiguration.get(context);
Adam Cohen7d30a372013-07-01 17:03:59 -0700193 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800195
Sunny Goyalcf25b522015-07-09 00:01:18 -0700196 float density = getResources().getDisplayMetrics().density;
197 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
198 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
199 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700200
201 if (Utilities.ATLEAST_OREO) {
202 setDefaultFocusHighlightEnabled(false);
203 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 }
205
Adam Cohenf9618852013-11-08 06:45:03 -0800206 protected void setDefaultInterpolator(Interpolator interpolator) {
207 mDefaultInterpolator = interpolator;
208 mScroller.setInterpolator(mDefaultInterpolator);
209 }
210
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700211 public void initParentViews(View parent) {
212 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800213 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700214 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700215 }
216 }
217
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800218 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700219 return mPageIndicator;
220 }
221
Adam Cohen674531f2013-12-13 15:07:14 -0800222 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700223 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
224 * that the user was on before entering free scroll mode (e.g. the home screen page they
225 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
226 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700228 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700229 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700231
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700232 /**
233 * Returns the index of page to be shown immediately afterwards.
234 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700235 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700236 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
237 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700238
Adam Cohenf9c184a2016-01-15 16:47:43 -0800239 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildCount();
241 }
242
Sunny Goyal83a8f042015-05-19 12:52:12 -0700243 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 return getChildAt(index);
245 }
246
Adam Cohenae4f1552011-10-20 00:15:42 -0700247 protected int indexToPage(int index) {
248 return index;
249 }
250
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800252 * Updates the scroll of the current page immediately to its final scroll position. We use this
253 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
254 * the previous tab page.
255 */
256 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700257 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800258 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700259 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800260 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700261 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800262 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
263 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700264 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800265 }
266
Sreyase3e7e632020-01-29 10:42:10 -0800267 /**
268 * Returns left offset of a page. This is the gap between pages and prevents overlap.
269 */
270 public int scrollOffsetLeft() {
271 return mInsets.left + getPaddingLeft();
272 }
273
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700274 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700275 mScroller.abortAnimation();
276 // We need to clean up the next page here to avoid computeScrollHelper from
277 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700278 if (resetNextPage) {
279 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800280 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700281 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700282 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700283
Tony Wickham8f7ead32016-04-07 18:46:44 -0700284 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700285 mScroller.forceFinished(true);
286 // We need to clean up the next page here to avoid computeScrollHelper from
287 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700288 if (resetNextPage) {
289 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800290 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700291 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700292 }
293
Adam Cohen6f127a62014-06-12 14:54:41 -0700294 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700295 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700296 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700297 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700298 }
299
Chet Haasebc2f0822012-10-26 17:59:53 -0700300 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700301 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
302 */
303 private int ensureWithinScrollBounds(int page) {
304 int dir = !mIsRtl ? 1 : - 1;
305 int currScroll = getScrollForPage(page);
306 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800307 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700308 page += dir;
309 prevScroll = currScroll;
310 currScroll = getScrollForPage(page);
311 if (currScroll <= prevScroll) {
312 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
313 break;
314 }
315 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800316 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700317 page -= dir;
318 prevScroll = currScroll;
319 currScroll = getScrollForPage(page);
320 if (currScroll >= prevScroll) {
321 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
322 break;
323 }
324 }
325 return page;
326 }
327
Tony Wickham03f27012019-04-25 14:22:54 -0700328 public void setCurrentPage(int currentPage) {
329 setCurrentPage(currentPage, INVALID_PAGE);
330 }
331
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700332 /**
Winson Chung86f77532010-08-24 11:08:22 -0700333 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 */
Tony Wickham03f27012019-04-25 14:22:54 -0700335 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800336 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700337 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800338 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800339 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
340 // the default
341 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800342 return;
343 }
Tony Wickham03f27012019-04-25 14:22:54 -0700344 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700345 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700346 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700347 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800348 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 }
350
Winson Chung8c87cd82013-07-23 16:20:10 -0700351 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800352 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
353 * has settled.
354 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700355 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800356 updatePageIndicator();
357 }
358
359 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700360 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000361 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700362 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700364 protected void pageBeginTransition() {
365 if (!mIsPageInTransition) {
366 mIsPageInTransition = true;
367 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700368 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700369 }
370
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700371 protected void pageEndTransition() {
372 if (mIsPageInTransition) {
373 mIsPageInTransition = false;
374 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700375 }
Michael Jurka0142d492010-08-25 17:46:15 -0700376 }
377
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700378 protected boolean isPageInTransition() {
379 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700380 }
381
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700382 /**
383 * Called when the page starts moving as part of the scroll. Subclasses can override this
384 * to provide custom behavior during animation.
385 */
386 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700387 }
388
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700389 /**
390 * Called when the page ends moving as part of the scroll. Subclasses can override this
391 * to provide custom behavior during animation.
392 */
393 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700394 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700395 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800396 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
397 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700398 if (mOnPageTransitionEndCallback != null) {
399 mOnPageTransitionEndCallback.run();
400 mOnPageTransitionEndCallback = null;
401 }
402 }
403
404 /**
405 * Sets a callback to run once when the scrolling finishes. If there is currently
406 * no page in transition, then the callback is called immediately.
407 */
408 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
409 if (mIsPageInTransition || callback == null) {
410 mOnPageTransitionEndCallback = callback;
411 } else {
412 callback.run();
413 }
Michael Jurka0142d492010-08-25 17:46:15 -0700414 }
415
Vinit Nayaka406f722019-12-19 11:50:55 -0800416 protected int getUnboundedScroll() {
417 return mUnboundedScroll;
418 }
419
420 protected void updateLayoutRotation(int touchRotation) {
421 setLayoutRotation(touchRotation, mDisplayRotation);
422 }
423
424 /** @param touchRotation Must be one of {@link android.view.Surface.ROTATION_0/90/180/270} */
425 public void setLayoutRotation(int touchRotation, int displayRotation) {
426 if (mLayoutRotation == touchRotation && mDisplayRotation == displayRotation) {
427 return;
428 }
429
430 mOrientationState.update(touchRotation, displayRotation);
431 mOrientationHandler = mOrientationState.getOrientationHandler();
432 mLayoutRotation = touchRotation;
433 mDisplayRotation = displayRotation;
434 requestLayout();
435 }
436
437 public PagedViewOrientedState getPagedViewOrientedState() {
438 return mOrientationState;
439 }
440
441 public PagedOrientationHandler getPagedOrientationHandler() {
442 return getPagedViewOrientedState().getOrientationHandler();
443 }
444
445 public void disableMultipleLayoutRotations(boolean disable) {
446 mOrientationState.disableMultipleOrientations(disable);
447 mOrientationHandler = mOrientationState.getOrientationHandler();
448 requestLayout();
Sunny Goyalc86df472016-02-25 09:19:38 -0800449 }
450
Winson Chung321e9ee2010-08-09 13:37:56 -0700451 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800452 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800453 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800454 }
455
456 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700457 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800458 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
459 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
460 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700461
Vinit Nayaka406f722019-12-19 11:50:55 -0800462 boolean isBeforeFirstPage = mIsRtl ?
463 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
464 boolean isAfterLastPage = mIsRtl ?
465 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
466 if (!isBeforeFirstPage && !isAfterLastPage) {
467 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800468 }
469
Vinit Nayaka406f722019-12-19 11:50:55 -0800470 if (isBeforeFirstPage) {
471 mOrientationHandler.delegateScrollTo(this,
472 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800473 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700474 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800475 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800476 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800477 } else if (isAfterLastPage) {
478 mOrientationHandler.delegateScrollTo(this,
479 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800480 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700481 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800482 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800483 }
484 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700485 if (mWasInOverscroll) {
486 overScroll(0);
487 mWasInOverscroll = false;
488 }
Adam Cohen68d73932010-11-15 10:50:58 -0800489 super.scrollTo(x, y);
490 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800491 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700492
Vinit Nayaka406f722019-12-19 11:50:55 -0800493 /**
494 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
495 */
496 public void superScrollTo(int x, int y) {
497 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700498 }
499
Adam Cohen53805212013-10-01 10:39:23 -0700500 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800501 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700502 if (mCurrentPage != getNextPage()) {
503 AccessibilityEvent ev =
504 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700505 ev.setScrollable(true);
506 ev.setScrollX(getScrollX());
507 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800508 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700509 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700510 }
Adam Cohen53805212013-10-01 10:39:23 -0700511 }
512 }
513
Michael Jurka0142d492010-08-25 17:46:15 -0700514 // we moved this functionality to a helper function so SmoothPagedView can reuse it
515 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800516 return computeScrollHelper(true);
517 }
518
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700519 protected void announcePageForAccessibility() {
520 if (isAccessibilityEnabled(getContext())) {
521 // Notify the user when the page changes
522 announceForAccessibility(getCurrentPageDescription());
523 }
524 }
525
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800526 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700528 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800529 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
530 if (mUnboundedScroll != mScroller.getCurrPos()
531 || currentScroll != mScroller.getCurrPos()) {
532 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700533 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800534 if (shouldInvalidate) {
535 invalidate();
536 }
Michael Jurka0142d492010-08-25 17:46:15 -0700537 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800538 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700539 sendScrollAccessibilityEvent();
540
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700541 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700542 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700543 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700544 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700545
Adam Cohen73aa9752010-11-24 16:26:58 -0800546 // We don't want to trigger a page end moving unless the page has settled
547 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700548 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700549 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800550 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700551
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700552 if (canAnnouncePageDescription()) {
553 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700554 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 }
Michael Jurka0142d492010-08-25 17:46:15 -0700556 return false;
557 }
558
559 @Override
560 public void computeScroll() {
561 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
563
Sunny Goyalce8809a2018-01-18 14:02:47 -0800564 public int getExpectedHeight() {
565 return getMeasuredHeight();
566 }
567
Adam Cohen410f3cd2013-09-22 12:09:32 -0700568 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800569 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800570 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700571 }
572
Sunny Goyalce8809a2018-01-18 14:02:47 -0800573 public int getExpectedWidth() {
574 return getMeasuredWidth();
575 }
576
Sunny Goyalbc683e92017-12-06 10:25:07 -0800577 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800578 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800579 - mInsets.left - mInsets.right;
580 }
581
Winson Chung321e9ee2010-08-09 13:37:56 -0700582 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800583 public void requestLayout() {
584 mIsLayoutValid = false;
585 super.requestLayout();
586 }
587
588 @Override
589 public void forceLayout() {
590 mIsLayoutValid = false;
591 super.forceLayout();
592 }
593
594 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700596 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700597 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
598 return;
599 }
600
Adam Cohen7d30a372013-07-01 17:03:59 -0700601 // We measure the dimensions of the PagedView to be larger than the pages so that when we
602 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700603 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
604 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
605 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700606 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700607
Adam Cohen7d30a372013-07-01 17:03:59 -0700608 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
609 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
610 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 }
612
Winson Chung8aad6102012-05-11 16:27:49 -0700613 // Return early if we aren't given a proper dimension
614 if (widthSize <= 0 || heightSize <= 0) {
615 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
616 return;
617 }
618
Winson Chung321e9ee2010-08-09 13:37:56 -0700619 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700620 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700621 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700622
Sunny Goyalac00cba2017-11-13 15:58:01 -0800623 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800624 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800625 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800626 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700627
Sunny Goyalac00cba2017-11-13 15:58:01 -0800628 // measureChildren takes accounts for content padding, we only need to care about extra
629 // space due to insets.
630 measureChildren(myWidthSpec, myHeightSpec);
631 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800632 }
633
Sunny Goyalcf25b522015-07-09 00:01:18 -0700634 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700636 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800637 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700638 final int childCount = getChildCount();
639 boolean pageScrollChanged = false;
640 if (mPageScrolls == null || childCount != mPageScrolls.length) {
641 mPageScrolls = new int[childCount];
642 pageScrollChanged = true;
643 }
644
645 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700646 return;
647 }
648
Winson Chung785d2eb2011-04-14 16:08:02 -0700649 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700650
Vinit Nayaka406f722019-12-19 11:50:55 -0800651 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
652 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700653 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700654 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700655
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700656 final LayoutTransition transition = getLayoutTransition();
657 // If the transition is running defer updating max scroll, as some empty pages could
658 // still be present, and a max scroll change could cause sudden jumps in scroll.
659 if (transition != null && transition.isRunning()) {
660 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
661
662 @Override
663 public void startTransition(LayoutTransition transition, ViewGroup container,
664 View view, int transitionType) { }
665
666 @Override
667 public void endTransition(LayoutTransition transition, ViewGroup container,
668 View view, int transitionType) {
669 // Wait until all transitions are complete.
670 if (!transition.isRunning()) {
671 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700672 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700673 }
674 }
675 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700676 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700677 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700678 }
679
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700680 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
681 updateCurrentPageScroll();
682 mFirstLayout = false;
683 }
684
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800685 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700686 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700687 }
Winson Chunge3193b92010-09-10 11:44:42 -0700688 }
689
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700690 /**
691 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
692 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700693 */
694 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
695 ComputePageScrollsLogic scrollLogic) {
696 final int childCount = getChildCount();
697
698 final int startIndex = mIsRtl ? childCount - 1 : 0;
699 final int endIndex = mIsRtl ? -1 : childCount;
700 final int delta = mIsRtl ? -1 : 1;
701
Vinit Nayaka406f722019-12-19 11:50:55 -0800702 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700703
Vinit Nayaka406f722019-12-19 11:50:55 -0800704 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
705 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700706 boolean pageScrollChanged = false;
707
Vinit Nayaka406f722019-12-19 11:50:55 -0800708 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700709 final View child = getPageAt(i);
710 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800711 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
712 pageCenter, layoutChildren);
713 final int primaryDimension = bounds.primaryDimension;
714 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700715
Sunny Goyalc82c6392018-06-06 15:39:13 -0700716 // In case the pages are of different width, align the page to left or right edge
717 // based on the orientation.
718 final int pageScroll = mIsRtl
Vinit Nayaka406f722019-12-19 11:50:55 -0800719 ? (childStart - scrollOffsetStart)
720 : Math.max(0, childPrimaryEnd - scrollOffsetEnd);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700721 if (outPageScrolls[i] != pageScroll) {
722 pageScrollChanged = true;
723 outPageScrolls[i] = pageScroll;
724 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800725 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700726 }
727 }
728 return pageScrollChanged;
729 }
730
Sunny Goyala1fbd842015-05-20 13:40:27 -0700731 protected int getChildGap() {
732 return 0;
733 }
734
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700735 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800736 mMinScroll = computeMinScroll();
737 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700738 }
739
Vinit Nayaka406f722019-12-19 11:50:55 -0800740 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700741 return 0;
Tony50876bf2018-09-19 23:09:14 -0400742 }
743
Vinit Nayaka406f722019-12-19 11:50:55 -0800744 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700745 int childCount = getChildCount();
746 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700747 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700748 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700749 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700750 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700751 }
752 }
753
Adam Cohen3b185e22013-10-29 14:45:58 -0700754 public void setPageSpacing(int pageSpacing) {
755 mPageSpacing = pageSpacing;
756 requestLayout();
757 }
758
Tony50876bf2018-09-19 23:09:14 -0400759 public int getPageSpacing() {
760 return mPageSpacing;
761 }
762
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800763 private void dispatchPageCountChanged() {
764 if (mPageIndicator != null) {
765 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700766 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700767 // This ensures that when children are added, they get the correct transforms / alphas
768 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700769 invalidate();
770 }
771
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800773 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800774 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800775 dispatchPageCountChanged();
776 }
777
778 @Override
779 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800780 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700781 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800782 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700783 }
784
Adam Cohen73894962011-10-31 13:17:17 -0700785 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700786 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800787 View pageAtIndex = getPageAt(index);
788 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700789 }
790
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 @Override
792 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700793 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700794 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700795 if (immediate) {
796 setCurrentPage(page);
797 } else {
798 snapToPage(page);
799 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 return true;
801 }
802 return false;
803 }
804
805 @Override
806 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700807 int focusablePage;
808 if (mNextPage != INVALID_PAGE) {
809 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700811 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 }
Winson Chung86f77532010-08-24 11:08:22 -0700813 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700815 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 }
817 return false;
818 }
819
820 @Override
821 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800822 if (super.dispatchUnhandledMove(focused, direction)) {
823 return true;
824 }
825
826 if (mIsRtl) {
827 if (direction == View.FOCUS_LEFT) {
828 direction = View.FOCUS_RIGHT;
829 } else if (direction == View.FOCUS_RIGHT) {
830 direction = View.FOCUS_LEFT;
831 }
832 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700834 if (getCurrentPage() > 0) {
835 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700836 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 return true;
838 }
839 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (getCurrentPage() < getPageCount() - 1) {
841 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700842 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 return true;
844 }
845 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800846 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848
849 @Override
850 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700851 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
852 return;
853 }
854
Adam Cohen0ffac432013-07-10 11:19:26 -0700855 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700856 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700857 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 }
859 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700860 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700861 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 }
863 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700864 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700865 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 }
867 }
868 }
869
870 /**
871 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700872 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 *
Winson Chung86f77532010-08-24 11:08:22 -0700874 * This happens when live folders requery, and if they're off page, they
875 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 */
877 @Override
878 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700879 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 View v = focused;
881 while (true) {
882 if (v == current) {
883 super.focusableViewAvailable(focused);
884 return;
885 }
886 if (v == this) {
887 return;
888 }
889 ViewParent parent = v.getParent();
890 if (parent instanceof View) {
891 v = (View)v.getParent();
892 } else {
893 return;
894 }
895 }
896 }
897
898 /**
899 * {@inheritDoc}
900 */
901 @Override
902 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
903 if (disallowIntercept) {
904 // We need to make sure to cancel our long press if
905 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700906 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700907 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700908 }
909 super.requestDisallowInterceptTouchEvent(disallowIntercept);
910 }
911
912 @Override
913 public boolean onInterceptTouchEvent(MotionEvent ev) {
914 /*
915 * This method JUST determines whether we want to intercept the motion.
916 * If we return true, onTouchEvent will be called and we do the actual
917 * scrolling there.
918 */
919
Winson Chung45e1d6e2010-11-09 17:19:49 -0800920 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700921 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700922
923 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 /*
926 * Shortcut the most recurring case: the user is in the dragging
927 * state and he is moving his finger. We want to intercept this
928 * motion.
929 */
930 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700931 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 return true;
933 }
934
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 switch (action & MotionEvent.ACTION_MASK) {
936 case MotionEvent.ACTION_MOVE: {
937 /*
938 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800939 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700941 if (mActivePointerId != INVALID_POINTER) {
942 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700943 }
944 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800945 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700946 // i.e. fall through to the next case (don't break)
947 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
948 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700949 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 }
951
952 case MotionEvent.ACTION_DOWN: {
953 final float x = ev.getX();
954 final float y = ev.getY();
955 // Remember location of down touch
956 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700957 mDownMotionY = y;
Vinit Nayaka406f722019-12-19 11:50:55 -0800958 mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
959 mLastMotionRemainder = 0;
960 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700962
Tony Wickhama0aee212019-09-18 09:24:03 -0700963 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700964
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 break;
966 }
967
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800969 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700970 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 break;
972
973 case MotionEvent.ACTION_POINTER_UP:
974 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800975 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 break;
977 }
978
979 /*
980 * The only time we want to intercept motion events is if we are in the
981 * drag mode.
982 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700983 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 }
985
Tony Wickhama0aee212019-09-18 09:24:03 -0700986 /**
987 * If being flinged and user touches the screen, initiate drag; otherwise don't.
988 */
989 private void updateIsBeingDraggedOnTouchDown() {
990 // mScroller.isFinished should be false when being flinged.
991 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
992 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
993
994 if (finishedScrolling) {
995 mIsBeingDragged = false;
996 if (!mScroller.isFinished() && !mFreeScroll) {
997 setCurrentPage(getNextPage());
998 pageEndTransition();
999 }
1000 } else {
1001 mIsBeingDragged = true;
1002 }
1003 }
1004
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001005 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001006 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001007 }
1008
Adam Cohenf8d28232011-02-01 21:47:00 -08001009 protected void determineScrollingStart(MotionEvent ev) {
1010 determineScrollingStart(ev, 1.0f);
1011 }
1012
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 /*
1014 * Determines if we should change the touch state to start scrolling after the
1015 * user moves their touch point too far.
1016 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001017 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001018 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001020 if (pointerIndex == -1) return;
1021
Vinit Nayaka406f722019-12-19 11:50:55 -08001022 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1023 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001024 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Vinit Nayaka406f722019-12-19 11:50:55 -08001025 boolean moved = diff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001026
Vinit Nayaka406f722019-12-19 11:50:55 -08001027 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001028 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001029 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001030 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1031 mLastMotion = primaryDirection;
1032 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001033 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001034 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001035 // Stop listening for things like pinches.
1036 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001037 }
1038 }
1039
1040 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001041 // Try canceling the long press. It could also have been scheduled
1042 // by a distant descendant, so use the mAllowLongPress flag to block
1043 // everything
1044 final View currentPage = getPageAt(mCurrentPage);
1045 if (currentPage != null) {
1046 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001047 }
1048 }
1049
Adam Cohenb5ba0972011-09-07 18:02:31 -07001050 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001051 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001052
Adam Cohenedb40762013-07-18 16:45:45 -07001053 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001054 int count = getChildCount();
1055
1056 final int totalDistance;
1057
1058 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001059 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001060 adjacentPage = page - 1;
1061 }
1062
1063 if (adjacentPage < 0 || adjacentPage > count - 1) {
1064 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1065 } else {
1066 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1067 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001068
1069 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001070 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1071 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001072 return scrollProgress;
1073 }
1074
Adam Cohenedb40762013-07-18 16:45:45 -07001075 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001076 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001077 return 0;
1078 } else {
1079 return mPageScrolls[index];
1080 }
1081 }
1082
Adam Cohen564a2e72013-10-09 14:47:32 -07001083 // While layout transitions are occurring, a child's position may stray from its baseline
1084 // position. This method returns the magnitude of this stray at any given time.
1085 public int getLayoutTransitionOffsetForPage(int index) {
1086 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1087 return 0;
1088 } else {
1089 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001090
Sunny Goyal228153d2018-01-04 15:35:22 -08001091 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1092 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001093 return (int) (child.getX() - baselineX);
1094 }
1095 }
1096
Jon Miranda71cb80c2019-01-24 21:25:13 -08001097 @Override
1098 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001099 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001100 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001101 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001102 super.dispatchDraw(canvas);
1103
1104 canvas.restoreToCount(saveCount);
1105 } else {
1106 super.dispatchDraw(canvas);
1107 }
1108 }
1109
Sunny Goyalb7b01352018-08-09 12:31:20 -07001110 protected void dampedOverScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001111 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001112 if (amount == 0) {
1113 return;
1114 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001115
Vinit Nayaka406f722019-12-19 11:50:55 -08001116 int size = mOrientationHandler.getMeasuredSize(this);
1117 int overScrollAmount = OverScroll.dampedScroll(amount, size);
1118 mSpringOverScroll = overScrollAmount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001119 if (mScroller.isSpringing()) {
1120 invalidate();
1121 return;
1122 }
1123
Vinit Nayaka406f722019-12-19 11:50:55 -08001124 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1125 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1126 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001127 invalidate();
1128 }
1129
Sunny Goyalb7b01352018-08-09 12:31:20 -07001130 protected void overScroll(int amount) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001131 mSpringOverScroll = amount;
Jon Miranda71cb80c2019-01-24 21:25:13 -08001132 if (mScroller.isSpringing()) {
1133 invalidate();
1134 return;
1135 }
1136
Sunny Goyalb7b01352018-08-09 12:31:20 -07001137 if (amount == 0) return;
1138
1139 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001140 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1141 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001142 } else {
1143 dampedOverScroll(amount);
1144 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001145 }
1146
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001147
Tony Wickham023f4042019-01-29 10:52:31 -08001148 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001149 if (mFreeScroll == freeScroll) {
1150 return;
1151 }
1152
Tony Wickham8f7ead32016-04-07 18:46:44 -07001153 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001154 mFreeScroll = freeScroll;
1155
Adam Cohenf9618852013-11-08 06:45:03 -08001156 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001157 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001158 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001159 if (getScrollForPage(getNextPage()) != getScrollX()) {
1160 snapToPage(getNextPage());
1161 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001162 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001163 }
1164
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001165 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001166 mAllowOverScroll = enable;
1167 }
1168
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 @Override
1170 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001171 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001172 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001173
Michael Jurkab8f06722010-10-10 15:58:46 -07001174 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001175
1176 final int action = ev.getAction();
1177
1178 switch (action & MotionEvent.ACTION_MASK) {
1179 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001180 updateIsBeingDraggedOnTouchDown();
1181
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 /*
1183 * If being flinged and user touches, stop the fling. isFinished
1184 * will be false if being flinged.
1185 */
1186 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001187 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 }
1189
1190 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001191 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001192 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001193 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1194 mLastMotionRemainder = 0;
1195 mTotalMotion = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001197 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001198 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001199 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001200 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 break;
1202
Vinit Nayaka406f722019-12-19 11:50:55 -08001203 case MotionEvent.ACTION_MOVE:
1204 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 // Scroll to follow the motion event
1206 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001207
1208 if (pointerIndex == -1) return true;
1209
Vinit Nayaka406f722019-12-19 11:50:55 -08001210 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1211 float delta = mLastMotion + mLastMotionRemainder - direction;
1212 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001213
Winson Chungc0844aa2011-02-02 15:25:58 -08001214 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1215 // keep the remainder because we are actually testing if we've moved from the last
1216 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001217 if (Math.abs(delta) >= 1.0f) {
1218 mLastMotion = direction;
1219 mLastMotionRemainder = delta - (int) delta;
1220
1221 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 } else {
1223 awakenScrollBars();
1224 }
Adam Cohen564976a2010-10-13 18:52:07 -07001225 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 determineScrollingStart(ev);
1227 }
1228 break;
1229
1230 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001231 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 final int activePointerId = mActivePointerId;
1233 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayaka406f722019-12-19 11:50:55 -08001234 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1235 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001236 final VelocityTracker velocityTracker = mVelocityTracker;
1237 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001238
Vinit Nayaka406f722019-12-19 11:50:55 -08001239 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1240 mActivePointerId);
1241 int delta = (int) (primaryDirection - mDownMotionPrimary);
1242 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1243
1244 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1245 SIGNIFICANT_MOVE_THRESHOLD;
1246
1247 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
1248 boolean isFling = mTotalMotion > mTouchSlop && shouldFlingForVelocity(velocity);
1249 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1250 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001251
Adam Cohenf358a4b2013-07-23 16:47:31 -07001252 if (!mFreeScroll) {
1253 // In the case that the page is moved far to one direction and then is flung
1254 // in the opposite direction, we use a threshold to determine whether we should
1255 // just return to the starting page, or if we should skip one further.
1256 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001257 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1258 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001259 returnToOriginalPage = true;
1260 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001261
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 int finalPage;
1263 // We give flings precedence over large moves, which is why we short-circuit our
1264 // test for a large move if a fling has been registered. That is, a large
1265 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001266
Vinit Nayaka406f722019-12-19 11:50:55 -08001267 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1268 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001269 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001270 snapToPageWithVelocity(finalPage, velocity);
1271 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1272 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001273 mCurrentPage < getChildCount() - 1) {
1274 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
Vinit Nayaka406f722019-12-19 11:50:55 -08001275 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001276 } else {
1277 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001278 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001280 if (!mScroller.isFinished()) {
1281 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001282 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001283
Vinit Nayaka406f722019-12-19 11:50:55 -08001284 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1285 int maxScroll = mMaxScroll;
1286 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001287
Vinit Nayaka406f722019-12-19 11:50:55 -08001288 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1289 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1290 mScroller.springBack(initialScroll, minScroll, maxScroll);
Tony Wickham312209b2019-06-18 15:54:29 -07001291 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001292 } else {
1293 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001294 mScroller.fling(initialScroll, -velocity,
1295 minScroll, maxScroll,
1296 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001297
Vinit Nayaka406f722019-12-19 11:50:55 -08001298 int finalPos = mScroller.getFinalPos();
1299 mNextPage = getPageNearestToCenterOfScreen(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001300
1301 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1302 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001303 if (finalPos > minScroll && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001304 // If scrolling ends in the half of the added space that is closer to
1305 // the end, settle to the end. Otherwise snap to the nearest page.
1306 // If flinging past one of the ends, don't change the velocity as it
1307 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001308 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1309 ? minScroll
1310 : finalPos > (lastPageScroll + maxScroll) / 2
1311 ? maxScroll
1312 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001313
Vinit Nayaka406f722019-12-19 11:50:55 -08001314 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001315 // Ensure the scroll/snap doesn't happen too fast;
1316 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001317 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001318 if (extraScrollDuration > 0) {
1319 mScroller.extendDuration(extraScrollDuration);
1320 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001321 }
1322 }
1323 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001325 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001327
Adam Cohen7d30a372013-07-01 17:03:59 -07001328 // End any intermediate reordering states
1329 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 break;
1331
1332 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001333 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001334 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001335 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001336 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001337 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 break;
1339
1340 case MotionEvent.ACTION_POINTER_UP:
1341 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001342 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 break;
1344 }
1345
1346 return true;
1347 }
1348
Vinit Nayaka406f722019-12-19 11:50:55 -08001349 protected boolean shouldFlingForVelocity(int velocity) {
1350 return Math.abs(velocity) > mFlingThresholdVelocity;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001351 }
1352
Adam Cohen7d30a372013-07-01 17:03:59 -07001353 private void resetTouchState() {
1354 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001355 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001356 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 }
1358
Adam Cohenc2d6e892014-10-16 09:49:24 -07001359 /**
1360 * Triggered by scrolling via touch
1361 */
1362 protected void onScrollInteractionBegin() {
1363 }
1364
1365 protected void onScrollInteractionEnd() {
1366 }
1367
Winson Chung185d7162011-02-28 13:47:29 -08001368 @Override
1369 public boolean onGenericMotionEvent(MotionEvent event) {
1370 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1371 switch (event.getAction()) {
1372 case MotionEvent.ACTION_SCROLL: {
1373 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1374 final float vscroll;
1375 final float hscroll;
1376 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1377 vscroll = 0;
1378 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1379 } else {
1380 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1381 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1382 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001383 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1384 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001385 }
Winson Chung185d7162011-02-28 13:47:29 -08001386 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001387 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001388 : (hscroll > 0 || vscroll > 0);
1389 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001390 scrollRight();
1391 } else {
1392 scrollLeft();
1393 }
1394 return true;
1395 }
1396 }
1397 }
1398 }
1399 return super.onGenericMotionEvent(event);
1400 }
1401
Sunny Goyal8b37c572020-03-31 12:12:14 -07001402 /**
1403 * Returns true if the paged view can scroll for the provided vertical and horizontal
1404 * scroll values
1405 */
1406 protected boolean canScroll(float absVScroll, float absHScroll) {
1407 ActivityContext ac = ActivityContext.lookupContext(getContext());
1408 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001409 }
1410
Michael Jurkab8f06722010-10-10 15:58:46 -07001411 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1412 if (mVelocityTracker == null) {
1413 mVelocityTracker = VelocityTracker.obtain();
1414 }
1415 mVelocityTracker.addMovement(ev);
1416 }
1417
1418 private void releaseVelocityTracker() {
1419 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001421 mVelocityTracker.recycle();
1422 mVelocityTracker = null;
1423 }
1424 }
1425
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 private void onSecondaryPointerUp(MotionEvent ev) {
1427 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1428 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1429 final int pointerId = ev.getPointerId(pointerIndex);
1430 if (pointerId == mActivePointerId) {
1431 // This was our active pointer going up. Choose a new
1432 // active pointer and adjust accordingly.
1433 // TODO: Make this decision more intelligent.
1434 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001435 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1436 newPointerIndex);
1437 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 mActivePointerId = ev.getPointerId(newPointerIndex);
1439 if (mVelocityTracker != null) {
1440 mVelocityTracker.clear();
1441 }
1442 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001443 }
1444
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 @Override
1446 public void requestChildFocus(View child, View focused) {
1447 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001448 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001449 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001450 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 }
1452 }
1453
Sunny Goyal228153d2018-01-04 15:35:22 -08001454 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001455 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001456 }
1457
Vinit Nayaka406f722019-12-19 11:50:55 -08001458 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1459 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1460 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001461 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 final int childCount = getChildCount();
1464 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001465 View layout = getPageAt(i);
Vinit Nayaka406f722019-12-19 11:50:55 -08001466 int childSize = mOrientationHandler.getMeasuredSize(layout);
1467 int halfChildSize = (childSize / 2);
1468 int childCenter = getChildOffset(i) + halfChildSize;
Winson Chung321e9ee2010-08-09 13:37:56 -07001469 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1470 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1471 minDistanceFromScreenCenter = distanceFromScreenCenter;
1472 minDistanceFromScreenCenterIndex = i;
1473 }
1474 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001475 return minDistanceFromScreenCenterIndex;
1476 }
1477
1478 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001479 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1480 }
1481
1482 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001483 int scroll = mOrientationHandler.getPrimaryScroll(this);
1484 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001485 }
1486
1487 protected int getPageSnapDuration() {
1488 if (isInOverScroll()) {
1489 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1490 }
1491 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001492 }
1493
Adam Cohene0f66b52010-11-23 15:06:07 -08001494 // We want the duration of the page snap animation to be influenced by the distance that
1495 // the screen has to travel, however, we don't want this duration to be effected in a
1496 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1497 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001498 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001499 f -= 0.5f; // center the values about 0.
1500 f *= 0.3f * Math.PI / 2.0f;
1501 return (float) Math.sin(f);
1502 }
1503
Winson Chung05a31ed2018-02-08 17:08:43 -08001504 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001505 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001506 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001507
Vinit Nayaka406f722019-12-19 11:50:55 -08001508 final int newLoc = getScrollForPage(whichPage);
1509 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001510 int duration = 0;
1511
Sunny Goyal4d113a52015-05-27 10:05:28 -07001512 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001513 // If the velocity is low enough, then treat this more as an automatic page advance
1514 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001515 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001516 }
1517
1518 // Here we compute a "distance" that will be used in the computation of the overall
1519 // snap duration. This is a function of the actual distance that needs to be traveled;
1520 // we keep this value close to half screen size in order to reduce the variance in snap
1521 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001522 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001523 float distance = halfScreenSize + halfScreenSize *
1524 distanceInfluenceForSnapDuration(distanceRatio);
1525
1526 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001527 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001528
1529 // we want the page's snap velocity to approximately match the velocity at which the
1530 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001531 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1532 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001533
Jon Miranda9e198662020-03-11 14:42:02 -07001534 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001535 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001536 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001537 } else {
1538 return snapToPage(whichPage, delta, duration);
1539 }
Michael Jurka0142d492010-08-25 17:46:15 -07001540 }
1541
Winson Chung05a31ed2018-02-08 17:08:43 -08001542 public boolean snapToPage(int whichPage) {
1543 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 }
1545
Winson Chung05a31ed2018-02-08 17:08:43 -08001546 public boolean snapToPageImmediately(int whichPage) {
1547 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001548 }
1549
Winson Chung05a31ed2018-02-08 17:08:43 -08001550 public boolean snapToPage(int whichPage, int duration) {
1551 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001552 }
1553
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001554 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001555 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001556 }
1557
Winson Chung05a31ed2018-02-08 17:08:43 -08001558 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001559 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001560 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001561
Vinit Nayaka406f722019-12-19 11:50:55 -08001562 int newLoc = getScrollForPage(whichPage);
1563 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001564 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001565 }
1566
Winson Chung05a31ed2018-02-08 17:08:43 -08001567 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001568 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001569 }
Michael Jurka0142d492010-08-25 17:46:15 -07001570
Winson Chung05a31ed2018-02-08 17:08:43 -08001571 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001572 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001573 if (mFirstLayout) {
1574 setCurrentPage(whichPage);
1575 return false;
1576 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001577
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001578 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001579 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1580 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001581 }
1582
Adam Cohen6f127a62014-06-12 14:54:41 -07001583 whichPage = validateNewPage(whichPage);
1584
Adam Cohen7d30a372013-07-01 17:03:59 -07001585 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001586
Winson Chung321e9ee2010-08-09 13:37:56 -07001587 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001588 if (immediate) {
1589 duration = 0;
1590 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001591 duration = Math.abs(delta);
1592 }
1593
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001594 if (duration != 0) {
1595 pageBeginTransition();
1596 }
1597
Adam Cohenf358a4b2013-07-23 16:47:31 -07001598 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001599 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001600 }
Adam Cohenf9618852013-11-08 06:45:03 -08001601
1602 if (interpolator != null) {
1603 mScroller.setInterpolator(interpolator);
1604 } else {
1605 mScroller.setInterpolator(mDefaultInterpolator);
1606 }
1607
Jon Miranda71cb80c2019-01-24 21:25:13 -08001608 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001609 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001610 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001611 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001612 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001613
Adam Cohen674531f2013-12-13 15:07:14 -08001614 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001615
1616 // Trigger a compute() to finish switching pages if necessary
1617 if (immediate) {
1618 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001619 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001620 }
1621
Winson Chung321e9ee2010-08-09 13:37:56 -07001622 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001623 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001624 }
1625
Vadim Tryshev98913d02018-05-18 18:41:34 -07001626 public boolean scrollLeft() {
1627 if (getNextPage() > 0) {
1628 snapToPage(getNextPage() - 1);
1629 return true;
1630 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001631 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001632 }
1633
Vadim Tryshev98913d02018-05-18 18:41:34 -07001634 public boolean scrollRight() {
1635 if (getNextPage() < getChildCount() - 1) {
1636 snapToPage(getNextPage() + 1);
1637 return true;
1638 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001639 return onOverscroll(getMeasuredWidth());
1640 }
1641
1642 protected boolean onOverscroll(int amount) {
1643 if (!mAllowOverScroll) return false;
1644 onScrollInteractionBegin();
1645 overScroll(amount);
1646 onScrollInteractionEnd();
1647 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001648 }
1649
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001650 @Override
1651 public CharSequence getAccessibilityClassName() {
1652 // Some accessibility services have special logic for ScrollView. Since we provide same
1653 // accessibility info as ScrollView, inform the service to handle use the same way.
1654 return ScrollView.class.getName();
1655 }
1656
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001657 protected boolean isPageOrderFlipped() {
1658 return false;
1659 }
1660
Winson Chung6a0f57d2011-06-29 20:10:49 -07001661 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001662 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001663 @Override
1664 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1665 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001666 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001667 int offset = (mAllowOverScroll ? 0 : 1);
1668 info.setScrollable(getPageCount() > offset);
1669 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001670 info.addAction(pagesFlipped ?
1671 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1672 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1673 info.addAction(mIsRtl ?
1674 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1675 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001676 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001677 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001678 info.addAction(pagesFlipped ?
1679 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1680 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1681 info.addAction(mIsRtl ?
1682 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1683 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001684 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001685 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1686 // Besides disabling the accessibility long-click, this also prevents this view from getting
1687 // accessibility focus.
1688 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001689 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001690 }
1691
1692 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001693 public void sendAccessibilityEvent(int eventType) {
1694 // Don't let the view send real scroll events.
1695 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1696 super.sendAccessibilityEvent(eventType);
1697 }
1698 }
1699
1700 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001701 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1702 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001703 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001704 }
1705
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001706 @Override
1707 public boolean performAccessibilityAction(int action, Bundle arguments) {
1708 if (super.performAccessibilityAction(action, arguments)) {
1709 return true;
1710 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001711 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001712 switch (action) {
1713 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001714 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001715 return true;
1716 }
1717 } break;
1718 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001719 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001720 return true;
1721 }
yingleiw02cc8482019-08-02 16:14:27 -07001722 } break;
1723 case android.R.id.accessibilityActionPageRight: {
1724 if (!mIsRtl) {
1725 return scrollRight();
1726 } else {
1727 return scrollLeft();
1728 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001729 }
yingleiw02cc8482019-08-02 16:14:27 -07001730 case android.R.id.accessibilityActionPageLeft: {
1731 if (!mIsRtl) {
1732 return scrollLeft();
1733 } else {
1734 return scrollRight();
1735 }
1736 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001737 }
1738 return false;
1739 }
1740
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001741 protected boolean canAnnouncePageDescription() {
1742 return true;
1743 }
1744
Adam Cohen0ffac432013-07-10 11:19:26 -07001745 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001746 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001747 getNextPage() + 1, getChildCount());
1748 }
1749
Tony Mak2e564402018-02-27 17:19:34 +00001750 protected float getDownMotionX() {
1751 return mDownMotionX;
1752 }
1753
1754 protected float getDownMotionY() {
1755 return mDownMotionY;
1756 }
1757
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001758 protected interface ComputePageScrollsLogic {
1759
1760 boolean shouldIncludeView(View view);
1761 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001762
1763 public int[] getVisibleChildrenRange() {
1764 float visibleLeft = 0;
1765 float visibleRight = visibleLeft + getMeasuredWidth();
1766 float scaleX = getScaleX();
1767 if (scaleX < 1 && scaleX > 0) {
1768 float mid = getMeasuredWidth() / 2;
1769 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1770 visibleRight = mid + ((visibleRight - mid) / scaleX);
1771 }
1772
1773 int leftChild = -1;
1774 int rightChild = -1;
1775 final int childCount = getChildCount();
1776 for (int i = 0; i < childCount; i++) {
1777 final View child = getPageAt(i);
1778
1779 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1780 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1781 if (leftChild == -1) {
1782 leftChild = i;
1783 }
1784 rightChild = i;
1785 }
1786 }
1787 mTmpIntPair[0] = leftChild;
1788 mTmpIntPair[1] = rightChild;
1789 return mTmpIntPair;
1790 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001791}