blob: 4c6e1f3ebc936a86848f4b4b566804266f854adb [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 Goyald0030b02017-12-08 15:07:24 -080019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Jon Miranda71cb80c2019-01-24 21:25:13 -080021import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
Sunny Goyalb7b01352018-08-09 12:31:20 -070022import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyald0030b02017-12-08 15:07:24 -080023
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070024import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070026import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070028import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080029import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070031import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070032import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080035import android.view.InputDevice;
36import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070037import android.view.MotionEvent;
38import android.view.VelocityTracker;
39import android.view.View;
40import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080041import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.view.ViewGroup;
43import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080046import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070047import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070048
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070049import com.android.launcher3.anim.Interpolators;
vadimt4d93df52019-07-10 17:32:58 -070050import com.android.launcher3.compat.AccessibilityManagerCompat;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070051import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070053import com.android.launcher3.touch.OverScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -070054import com.android.launcher3.util.OverScroller;
Adam Cohen091440a2015-03-18 14:16:05 -070055import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070056
Winson Chung6a0f57d2011-06-29 20:10:49 -070057import java.util.ArrayList;
58
Winson Chung321e9ee2010-08-09 13:37:56 -070059/**
60 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070061 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070062 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080063public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070064 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070065 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070066
Sunny Goyala7a5bf32020-01-05 15:35:29 +053067 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070068 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Vadim Tryshevfedca432015-08-19 17:55:02 -070070 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070071
Sunny Goyalb72d8b22017-07-14 00:02:27 -070072 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070073 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070074
Adam Cohenb64cb5a2011-02-15 13:53:42 -080075 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080076 // The page is moved more than halfway, automatically move to the next page on touch up.
77 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080078
Sunny Goyal8e2133b2015-05-06 13:39:07 -070079 private static final float MAX_SCROLL_PROGRESS = 1.0f;
80
Adam Cohen265b9a62011-12-07 14:37:18 -080081 // The following constants need to be scaled based on density. The scaled versions will be
82 // assigned to the corresponding member variables below.
83 private static final int FLING_THRESHOLD_VELOCITY = 500;
84 private static final int MIN_SNAP_VELOCITY = 1500;
85 private static final int MIN_FLING_VELOCITY = 250;
86
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070088
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
94
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -070097
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mNextPage = INVALID_PAGE;
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700100 protected int mMinScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800101 protected int mMaxScrollX;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700102 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800103 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800105 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
107 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700108 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700109 private float mLastMotionX;
110 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700111 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700112
Vadim Trysheve628c472018-05-16 14:28:18 -0700113 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700114 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800115
Michael Jurka7426c422010-11-11 15:23:47 -0800116 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800118 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka5f1c5092010-09-03 14:15:02 -0700120 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka5f1c5092010-09-03 14:15:02 -0700122 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700124 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700125
Jon Miranda71cb80c2019-01-24 21:25:13 -0800126 protected float mSpringOverScrollX;
127
Sunny Goyal061380a2016-02-29 15:15:03 -0800128 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700129
Adam Cohen8d769d62017-06-23 17:27:38 -0700130 protected int mUnboundedScrollX;
131
Winson Chungd2be3812013-07-16 11:11:32 -0700132 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700133 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800134 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700135
John Spurlock77e1f472013-09-11 10:09:51 -0400136 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800137 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400138
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800139 // Similar to the platform implementation of isLayoutValid();
140 protected boolean mIsLayoutValid;
141
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700142 private int[] mTmpIntPair = new int[2];
143
Winson Chung321e9ee2010-08-09 13:37:56 -0700144 public PagedView(Context context) {
145 this(context, null);
146 }
147
148 public PagedView(Context context, AttributeSet attrs) {
149 this(context, attrs, 0);
150 }
151
152 public PagedView(Context context, AttributeSet attrs, int defStyle) {
153 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700154
Adam Cohen9c4949e2010-10-05 12:27:22 -0700155 TypedArray a = context.obtainStyledAttributes(attrs,
156 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700157 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700158 a.recycle();
159
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700161 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700162 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700163 }
164
165 /**
166 * Initializes various states for this workspace.
167 */
Michael Jurka0142d492010-08-25 17:46:15 -0700168 protected void init() {
Sunny Goyalb7b01352018-08-09 12:31:20 -0700169 mScroller = new OverScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700170 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700171 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700172
173 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700174 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800176
Sunny Goyalcf25b522015-07-09 00:01:18 -0700177 float density = getResources().getDisplayMetrics().density;
178 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
179 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
180 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700181
182 if (Utilities.ATLEAST_OREO) {
183 setDefaultFocusHighlightEnabled(false);
184 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700185 }
186
Adam Cohenf9618852013-11-08 06:45:03 -0800187 protected void setDefaultInterpolator(Interpolator interpolator) {
188 mDefaultInterpolator = interpolator;
189 mScroller.setInterpolator(mDefaultInterpolator);
190 }
191
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700192 public void initParentViews(View parent) {
193 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800194 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700195 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700196 }
197 }
198
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800199 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700200 return mPageIndicator;
201 }
202
Adam Cohen674531f2013-12-13 15:07:14 -0800203 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700204 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
205 * that the user was on before entering free scroll mode (e.g. the home screen page they
206 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
207 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700209 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700210 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700212
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700213 /**
214 * Returns the index of page to be shown immediately afterwards.
215 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700216 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700217 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
218 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700219
Adam Cohenf9c184a2016-01-15 16:47:43 -0800220 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 return getChildCount();
222 }
223
Sunny Goyal83a8f042015-05-19 12:52:12 -0700224 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 return getChildAt(index);
226 }
227
Adam Cohenae4f1552011-10-20 00:15:42 -0700228 protected int indexToPage(int index) {
229 return index;
230 }
231
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800233 * Updates the scroll of the current page immediately to its final scroll position. We use this
234 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
235 * the previous tab page.
236 */
237 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700238 // If the current page is invalid, just reset the scroll position to zero
239 int newX = 0;
240 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700241 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700242 }
Sunny Goyalc82c6392018-06-06 15:39:13 -0700243 scrollTo(newX, 0);
Sunny Goyalb7b01352018-08-09 12:31:20 -0700244 mScroller.startScroll(mScroller.getCurrPos(), newX - mScroller.getCurrPos());
Sunny Goyalc82c6392018-06-06 15:39:13 -0700245 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800246 }
247
Sreyase3e7e632020-01-29 10:42:10 -0800248 /**
249 * Returns left offset of a page. This is the gap between pages and prevents overlap.
250 */
251 public int scrollOffsetLeft() {
252 return mInsets.left + getPaddingLeft();
253 }
254
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700255 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700256 mScroller.abortAnimation();
257 // We need to clean up the next page here to avoid computeScrollHelper from
258 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700259 if (resetNextPage) {
260 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800261 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700262 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700263 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700264
Tony Wickham8f7ead32016-04-07 18:46:44 -0700265 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700266 mScroller.forceFinished(true);
267 // We need to clean up the next page here to avoid computeScrollHelper from
268 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700269 if (resetNextPage) {
270 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800271 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700272 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273 }
274
Adam Cohen6f127a62014-06-12 14:54:41 -0700275 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700276 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700277 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700278 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700279 }
280
Chet Haasebc2f0822012-10-26 17:59:53 -0700281 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700282 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
283 */
284 private int ensureWithinScrollBounds(int page) {
285 int dir = !mIsRtl ? 1 : - 1;
286 int currScroll = getScrollForPage(page);
287 int prevScroll;
288 while (currScroll < mMinScrollX) {
289 page += dir;
290 prevScroll = currScroll;
291 currScroll = getScrollForPage(page);
292 if (currScroll <= prevScroll) {
293 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
294 break;
295 }
296 }
297 while (currScroll > mMaxScrollX) {
298 page -= dir;
299 prevScroll = currScroll;
300 currScroll = getScrollForPage(page);
301 if (currScroll >= prevScroll) {
302 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
303 break;
304 }
305 }
306 return page;
307 }
308
Tony Wickham03f27012019-04-25 14:22:54 -0700309 public void setCurrentPage(int currentPage) {
310 setCurrentPage(currentPage, INVALID_PAGE);
311 }
312
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700313 /**
Winson Chung86f77532010-08-24 11:08:22 -0700314 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 */
Tony Wickham03f27012019-04-25 14:22:54 -0700316 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800317 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700318 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800319 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800320 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
321 // the default
322 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800323 return;
324 }
Tony Wickham03f27012019-04-25 14:22:54 -0700325 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700326 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700327 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700328 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800329 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 }
331
Winson Chung8c87cd82013-07-23 16:20:10 -0700332 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800333 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
334 * has settled.
335 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700336 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800337 updatePageIndicator();
338 }
339
340 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700341 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000342 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700343 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700345 protected void pageBeginTransition() {
346 if (!mIsPageInTransition) {
347 mIsPageInTransition = true;
348 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700349 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700350 }
351
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700352 protected void pageEndTransition() {
353 if (mIsPageInTransition) {
354 mIsPageInTransition = false;
355 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700356 }
Michael Jurka0142d492010-08-25 17:46:15 -0700357 }
358
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700359 protected boolean isPageInTransition() {
360 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700361 }
362
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700363 /**
364 * Called when the page starts moving as part of the scroll. Subclasses can override this
365 * to provide custom behavior during animation.
366 */
367 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700368 }
369
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700370 /**
371 * Called when the page ends moving as part of the scroll. Subclasses can override this
372 * to provide custom behavior during animation.
373 */
374 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700375 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700376 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Michael Jurka0142d492010-08-25 17:46:15 -0700377 }
378
Sunny Goyalc86df472016-02-25 09:19:38 -0800379 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700380 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800381 }
382
Winson Chung321e9ee2010-08-09 13:37:56 -0700383 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800384 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800385 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800386 }
387
388 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700389 public void scrollTo(int x, int y) {
Adam Cohen8d769d62017-06-23 17:27:38 -0700390 mUnboundedScrollX = x;
391
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700392 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < mMinScrollX);
393 boolean isXAfterLastPage = mIsRtl ? (x < mMinScrollX) : (x > mMaxScrollX);
Jon Miranda71cb80c2019-01-24 21:25:13 -0800394
395 if (!isXBeforeFirstPage && !isXAfterLastPage) {
396 mSpringOverScrollX = 0;
397 }
398
Adam Cohen0ffac432013-07-10 11:19:26 -0700399 if (isXBeforeFirstPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700400 super.scrollTo(mIsRtl ? mMaxScrollX : mMinScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800401 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700402 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700403 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700404 overScroll(x - mMaxScrollX);
405 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700406 overScroll(x - mMinScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700407 }
Adam Cohen68d73932010-11-15 10:50:58 -0800408 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700409 } else if (isXAfterLastPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700410 super.scrollTo(mIsRtl ? mMinScrollX : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800411 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700412 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700413 if (mIsRtl) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700414 overScroll(x - mMinScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700415 } else {
416 overScroll(x - mMaxScrollX);
417 }
Adam Cohen68d73932010-11-15 10:50:58 -0800418 }
419 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700420 if (mWasInOverscroll) {
421 overScroll(0);
422 mWasInOverscroll = false;
423 }
Adam Cohen68d73932010-11-15 10:50:58 -0800424 super.scrollTo(x, y);
425 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700426
Michael Jurka0142d492010-08-25 17:46:15 -0700427 }
428
Adam Cohen53805212013-10-01 10:39:23 -0700429 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800430 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700431 if (mCurrentPage != getNextPage()) {
432 AccessibilityEvent ev =
433 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700434 ev.setScrollable(true);
435 ev.setScrollX(getScrollX());
436 ev.setScrollY(getScrollY());
437 ev.setMaxScrollX(mMaxScrollX);
438 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700439
Vadim Tryshevf4715972015-05-08 17:21:03 -0700440 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700441 }
Adam Cohen53805212013-10-01 10:39:23 -0700442 }
443 }
444
Michael Jurka0142d492010-08-25 17:46:15 -0700445 // we moved this functionality to a helper function so SmoothPagedView can reuse it
446 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800447 return computeScrollHelper(true);
448 }
449
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700450 protected void announcePageForAccessibility() {
451 if (isAccessibilityEnabled(getContext())) {
452 // Notify the user when the page changes
453 announceForAccessibility(getCurrentPageDescription());
454 }
455 }
456
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800457 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700459 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalb7b01352018-08-09 12:31:20 -0700460 if (getUnboundedScrollX() != mScroller.getCurrPos()
461 || getScrollX() != mScroller.getCurrPos()) {
462 scrollTo(mScroller.getCurrPos(), 0);
Winson Chung557d6ed2011-07-08 15:34:52 -0700463 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800464 if (shouldInvalidate) {
465 invalidate();
466 }
Michael Jurka0142d492010-08-25 17:46:15 -0700467 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800468 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700469 sendScrollAccessibilityEvent();
470
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700471 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700472 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700473 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700474 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700475
Adam Cohen73aa9752010-11-24 16:26:58 -0800476 // We don't want to trigger a page end moving unless the page has settled
477 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700478 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700479 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800480 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700481
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700482 if (canAnnouncePageDescription()) {
483 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700484 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700485 }
Michael Jurka0142d492010-08-25 17:46:15 -0700486 return false;
487 }
488
489 @Override
490 public void computeScroll() {
491 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 }
493
Sunny Goyalce8809a2018-01-18 14:02:47 -0800494 public int getExpectedHeight() {
495 return getMeasuredHeight();
496 }
497
Adam Cohen410f3cd2013-09-22 12:09:32 -0700498 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800499 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800500 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700501 }
502
Sunny Goyalce8809a2018-01-18 14:02:47 -0800503 public int getExpectedWidth() {
504 return getMeasuredWidth();
505 }
506
Sunny Goyalbc683e92017-12-06 10:25:07 -0800507 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800508 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800509 - mInsets.left - mInsets.right;
510 }
511
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800513 public void requestLayout() {
514 mIsLayoutValid = false;
515 super.requestLayout();
516 }
517
518 @Override
519 public void forceLayout() {
520 mIsLayoutValid = false;
521 super.forceLayout();
522 }
523
524 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700526 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700527 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
528 return;
529 }
530
Adam Cohen7d30a372013-07-01 17:03:59 -0700531 // We measure the dimensions of the PagedView to be larger than the pages so that when we
532 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700533 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
534 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
535 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700536 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700537
Adam Cohen7d30a372013-07-01 17:03:59 -0700538 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
539 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
540 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 }
542
Winson Chung8aad6102012-05-11 16:27:49 -0700543 // Return early if we aren't given a proper dimension
544 if (widthSize <= 0 || heightSize <= 0) {
545 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
546 return;
547 }
548
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700550 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700551 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700552
Sunny Goyalac00cba2017-11-13 15:58:01 -0800553 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800554 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800555 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800556 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700557
Sunny Goyalac00cba2017-11-13 15:58:01 -0800558 // measureChildren takes accounts for content padding, we only need to care about extra
559 // space due to insets.
560 measureChildren(myWidthSpec, myHeightSpec);
561 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800562 }
563
Sunny Goyalcf25b522015-07-09 00:01:18 -0700564 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700566 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800567 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700568 final int childCount = getChildCount();
569 boolean pageScrollChanged = false;
570 if (mPageScrolls == null || childCount != mPageScrolls.length) {
571 mPageScrolls = new int[childCount];
572 pageScrollChanged = true;
573 }
574
575 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700576 return;
577 }
578
Winson Chung785d2eb2011-04-14 16:08:02 -0700579 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700580
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700581 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
582 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700584
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700585 final LayoutTransition transition = getLayoutTransition();
586 // If the transition is running defer updating max scroll, as some empty pages could
587 // still be present, and a max scroll change could cause sudden jumps in scroll.
588 if (transition != null && transition.isRunning()) {
589 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
590
591 @Override
592 public void startTransition(LayoutTransition transition, ViewGroup container,
593 View view, int transitionType) { }
594
595 @Override
596 public void endTransition(LayoutTransition transition, ViewGroup container,
597 View view, int transitionType) {
598 // Wait until all transitions are complete.
599 if (!transition.isRunning()) {
600 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700601 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700602 }
603 }
604 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700605 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700606 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700607 }
608
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700609 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
610 updateCurrentPageScroll();
611 mFirstLayout = false;
612 }
613
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800614 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700615 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700616 }
Winson Chunge3193b92010-09-10 11:44:42 -0700617 }
618
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700619 /**
620 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
621 * of {@code outPageScrolls} should be same as the the childCount
622 *
623 */
624 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
625 ComputePageScrollsLogic scrollLogic) {
626 final int childCount = getChildCount();
627
628 final int startIndex = mIsRtl ? childCount - 1 : 0;
629 final int endIndex = mIsRtl ? -1 : childCount;
630 final int delta = mIsRtl ? -1 : 1;
631
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700632 final int verticalCenter = (getPaddingTop() + getMeasuredHeight() + mInsets.top
633 - mInsets.bottom - getPaddingBottom()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700634
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700635 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700636 final int scrollOffsetRight = getWidth() - getPaddingRight() - mInsets.right;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700637 boolean pageScrollChanged = false;
638
Sunny Goyalc82c6392018-06-06 15:39:13 -0700639 for (int i = startIndex, childLeft = scrollOffsetLeft; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700640 final View child = getPageAt(i);
641 if (scrollLogic.shouldIncludeView(child)) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700642 final int childWidth = child.getMeasuredWidth();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700643 final int childRight = childLeft + childWidth;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700644
645 if (layoutChildren) {
646 final int childHeight = child.getMeasuredHeight();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700647 final int childTop = verticalCenter - childHeight / 2;
648 child.layout(childLeft, childTop, childRight, childTop + childHeight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700649 }
650
Sunny Goyalc82c6392018-06-06 15:39:13 -0700651 // In case the pages are of different width, align the page to left or right edge
652 // based on the orientation.
653 final int pageScroll = mIsRtl
654 ? (childLeft - scrollOffsetLeft)
655 : Math.max(0, childRight - scrollOffsetRight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700656 if (outPageScrolls[i] != pageScroll) {
657 pageScrollChanged = true;
658 outPageScrolls[i] = pageScroll;
659 }
660
661 childLeft += childWidth + mPageSpacing + getChildGap();
662 }
663 }
664 return pageScrollChanged;
665 }
666
Sunny Goyala1fbd842015-05-20 13:40:27 -0700667 protected int getChildGap() {
668 return 0;
669 }
670
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700671 protected void updateMinAndMaxScrollX() {
672 mMinScrollX = computeMinScrollX();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700673 mMaxScrollX = computeMaxScrollX();
674 }
675
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700676 protected int computeMinScrollX() {
677 return 0;
Tony50876bf2018-09-19 23:09:14 -0400678 }
679
Tony Wickhamf549dab2016-05-16 09:54:06 -0700680 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700681 int childCount = getChildCount();
682 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700683 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700684 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700685 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700686 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700687 }
688 }
689
Adam Cohen3b185e22013-10-29 14:45:58 -0700690 public void setPageSpacing(int pageSpacing) {
691 mPageSpacing = pageSpacing;
692 requestLayout();
693 }
694
Tony50876bf2018-09-19 23:09:14 -0400695 public int getPageSpacing() {
696 return mPageSpacing;
697 }
698
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800699 private void dispatchPageCountChanged() {
700 if (mPageIndicator != null) {
701 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700702 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700703 // This ensures that when children are added, they get the correct transforms / alphas
704 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700705 invalidate();
706 }
707
Michael Jurka8b805b12012-04-18 14:23:14 -0700708 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800709 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800710 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800711 dispatchPageCountChanged();
712 }
713
714 @Override
715 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800716 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700717 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800718 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700719 }
720
Adam Cohen73894962011-10-31 13:17:17 -0700721 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700722 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800723 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700724 }
725
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 @Override
727 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700728 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700729 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700730 if (immediate) {
731 setCurrentPage(page);
732 } else {
733 snapToPage(page);
734 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 return true;
736 }
737 return false;
738 }
739
740 @Override
741 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700742 int focusablePage;
743 if (mNextPage != INVALID_PAGE) {
744 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700746 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 }
Winson Chung86f77532010-08-24 11:08:22 -0700748 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700749 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700750 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752 return false;
753 }
754
755 @Override
756 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800757 if (super.dispatchUnhandledMove(focused, direction)) {
758 return true;
759 }
760
761 if (mIsRtl) {
762 if (direction == View.FOCUS_LEFT) {
763 direction = View.FOCUS_RIGHT;
764 } else if (direction == View.FOCUS_RIGHT) {
765 direction = View.FOCUS_LEFT;
766 }
767 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700769 if (getCurrentPage() > 0) {
770 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700771 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 return true;
773 }
774 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700775 if (getCurrentPage() < getPageCount() - 1) {
776 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700777 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 return true;
779 }
780 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800781 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 }
783
784 @Override
785 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700786 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
787 return;
788 }
789
Adam Cohen0ffac432013-07-10 11:19:26 -0700790 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700791 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700792 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 }
794 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700795 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700796 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 }
798 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700799 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700800 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 }
802 }
803 }
804
805 /**
806 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700807 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 *
Winson Chung86f77532010-08-24 11:08:22 -0700809 * This happens when live folders requery, and if they're off page, they
810 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 */
812 @Override
813 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700814 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 View v = focused;
816 while (true) {
817 if (v == current) {
818 super.focusableViewAvailable(focused);
819 return;
820 }
821 if (v == this) {
822 return;
823 }
824 ViewParent parent = v.getParent();
825 if (parent instanceof View) {
826 v = (View)v.getParent();
827 } else {
828 return;
829 }
830 }
831 }
832
833 /**
834 * {@inheritDoc}
835 */
836 @Override
837 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
838 if (disallowIntercept) {
839 // We need to make sure to cancel our long press if
840 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700841 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700842 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844 super.requestDisallowInterceptTouchEvent(disallowIntercept);
845 }
846
847 @Override
848 public boolean onInterceptTouchEvent(MotionEvent ev) {
849 /*
850 * This method JUST determines whether we want to intercept the motion.
851 * If we return true, onTouchEvent will be called and we do the actual
852 * scrolling there.
853 */
854
Winson Chung45e1d6e2010-11-09 17:19:49 -0800855 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700856 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700857
858 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800859
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 /*
861 * Shortcut the most recurring case: the user is in the dragging
862 * state and he is moving his finger. We want to intercept this
863 * motion.
864 */
865 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700866 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 return true;
868 }
869
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 switch (action & MotionEvent.ACTION_MASK) {
871 case MotionEvent.ACTION_MOVE: {
872 /*
873 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
874 * whether the user has moved far enough from his original down touch.
875 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700876 if (mActivePointerId != INVALID_POINTER) {
877 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700878 }
879 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
880 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
881 // i.e. fall through to the next case (don't break)
882 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
883 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700884 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 }
886
887 case MotionEvent.ACTION_DOWN: {
888 final float x = ev.getX();
889 final float y = ev.getY();
890 // Remember location of down touch
891 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700892 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700893 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800894 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800895 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700897
Tony Wickhama0aee212019-09-18 09:24:03 -0700898 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700899
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 break;
901 }
902
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800904 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700905 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 break;
907
908 case MotionEvent.ACTION_POINTER_UP:
909 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800910 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 break;
912 }
913
914 /*
915 * The only time we want to intercept motion events is if we are in the
916 * drag mode.
917 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700918 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 }
920
Tony Wickhama0aee212019-09-18 09:24:03 -0700921 /**
922 * If being flinged and user touches the screen, initiate drag; otherwise don't.
923 */
924 private void updateIsBeingDraggedOnTouchDown() {
925 // mScroller.isFinished should be false when being flinged.
926 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
927 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
928
929 if (finishedScrolling) {
930 mIsBeingDragged = false;
931 if (!mScroller.isFinished() && !mFreeScroll) {
932 setCurrentPage(getNextPage());
933 pageEndTransition();
934 }
935 } else {
936 mIsBeingDragged = true;
937 }
938 }
939
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800940 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700941 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800942 }
943
Adam Cohenf8d28232011-02-01 21:47:00 -0800944 protected void determineScrollingStart(MotionEvent ev) {
945 determineScrollingStart(ev, 1.0f);
946 }
947
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 /*
949 * Determines if we should change the touch state to start scrolling after the
950 * user moves their touch point too far.
951 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800952 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700953 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700955 if (pointerIndex == -1) return;
956
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 final float x = ev.getX(pointerIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 final int xDiff = (int) Math.abs(x - mLastMotionX);
Adam Cohenf8d28232011-02-01 21:47:00 -0800959 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700961
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700962 if (xMoved) {
963 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700964 mIsBeingDragged = true;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700965 mTotalMotionX += Math.abs(mLastMotionX - x);
966 mLastMotionX = x;
967 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700968 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700969 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800970 // Stop listening for things like pinches.
971 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800972 }
973 }
974
975 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700976 // Try canceling the long press. It could also have been scheduled
977 // by a distant descendant, so use the mAllowLongPress flag to block
978 // everything
979 final View currentPage = getPageAt(mCurrentPage);
980 if (currentPage != null) {
981 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 }
983 }
984
Adam Cohenb5ba0972011-09-07 18:02:31 -0700985 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800986 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700987
Adam Cohenedb40762013-07-18 16:45:45 -0700988 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700989 int count = getChildCount();
990
991 final int totalDistance;
992
993 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700994 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700995 adjacentPage = page - 1;
996 }
997
998 if (adjacentPage < 0 || adjacentPage > count - 1) {
999 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1000 } else {
1001 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1002 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001003
1004 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001005 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1006 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001007 return scrollProgress;
1008 }
1009
Adam Cohenedb40762013-07-18 16:45:45 -07001010 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001011 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001012 return 0;
1013 } else {
1014 return mPageScrolls[index];
1015 }
1016 }
1017
Adam Cohen564a2e72013-10-09 14:47:32 -07001018 // While layout transitions are occurring, a child's position may stray from its baseline
1019 // position. This method returns the magnitude of this stray at any given time.
1020 public int getLayoutTransitionOffsetForPage(int index) {
1021 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1022 return 0;
1023 } else {
1024 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001025
Sunny Goyal228153d2018-01-04 15:35:22 -08001026 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1027 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001028 return (int) (child.getX() - baselineX);
1029 }
1030 }
1031
Jon Miranda71cb80c2019-01-24 21:25:13 -08001032 @Override
1033 protected void dispatchDraw(Canvas canvas) {
1034 if (mScroller.isSpringing() && mSpringOverScrollX != 0) {
1035 int saveCount = canvas.save();
1036
1037 canvas.translate(-mSpringOverScrollX, 0);
1038 super.dispatchDraw(canvas);
1039
1040 canvas.restoreToCount(saveCount);
1041 } else {
1042 super.dispatchDraw(canvas);
1043 }
1044 }
1045
Sunny Goyalb7b01352018-08-09 12:31:20 -07001046 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001047 mSpringOverScrollX = amount;
1048 if (amount == 0) {
1049 return;
1050 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001051
Sunny Goyal228153d2018-01-04 15:35:22 -08001052 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Jon Miranda71cb80c2019-01-24 21:25:13 -08001053 mSpringOverScrollX = overScrollAmount;
1054 if (mScroller.isSpringing()) {
1055 invalidate();
1056 return;
1057 }
1058
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001059 int x = Utilities.boundToRange(getScrollX(), mMinScrollX, mMaxScrollX);
1060 super.scrollTo(x + overScrollAmount, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001061 invalidate();
1062 }
1063
Sunny Goyalb7b01352018-08-09 12:31:20 -07001064 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001065 mSpringOverScrollX = amount;
1066 if (mScroller.isSpringing()) {
1067 invalidate();
1068 return;
1069 }
1070
Sunny Goyalb7b01352018-08-09 12:31:20 -07001071 if (amount == 0) return;
1072
1073 if (mFreeScroll && !mScroller.isFinished()) {
1074 if (amount < 0) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001075 super.scrollTo(mMinScrollX + amount, getScrollY());
Sunny Goyalb7b01352018-08-09 12:31:20 -07001076 } else {
1077 super.scrollTo(mMaxScrollX + amount, getScrollY());
1078 }
1079 } else {
1080 dampedOverScroll(amount);
1081 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001082 }
1083
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001084
Tony Wickham023f4042019-01-29 10:52:31 -08001085 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001086 if (mFreeScroll == freeScroll) {
1087 return;
1088 }
1089
Tony Wickham8f7ead32016-04-07 18:46:44 -07001090 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001091 mFreeScroll = freeScroll;
1092
Adam Cohenf9618852013-11-08 06:45:03 -08001093 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001094 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001095 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001096 if (getScrollForPage(getNextPage()) != getScrollX()) {
1097 snapToPage(getNextPage());
1098 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001099 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001100 }
1101
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001102 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001103 mAllowOverScroll = enable;
1104 }
1105
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 @Override
1107 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001108 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001109 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001110
Michael Jurkab8f06722010-10-10 15:58:46 -07001111 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001112
1113 final int action = ev.getAction();
1114
1115 switch (action & MotionEvent.ACTION_MASK) {
1116 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001117 updateIsBeingDraggedOnTouchDown();
1118
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 /*
1120 * If being flinged and user touches, stop the fling. isFinished
1121 * will be false if being flinged.
1122 */
1123 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001124 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 }
1126
1127 // Remember where the motion event started
1128 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001129 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001130 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001131 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001132 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001133
Sunny Goyalaad33592018-08-07 17:20:35 -07001134 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001136 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001137 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001138 break;
1139
1140 case MotionEvent.ACTION_MOVE:
Sunny Goyalaad33592018-08-07 17:20:35 -07001141 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 // Scroll to follow the motion event
1143 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001144
1145 if (pointerIndex == -1) return true;
1146
Winson Chung321e9ee2010-08-09 13:37:56 -07001147 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001148 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001149
Adam Cohenaefd4e12011-02-14 16:39:38 -08001150 mTotalMotionX += Math.abs(deltaX);
1151
Winson Chungc0844aa2011-02-02 15:25:58 -08001152 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1153 // keep the remainder because we are actually testing if we've moved from the last
1154 // scrolled position (which is discrete).
1155 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001156 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001157 mLastMotionX = x;
1158 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 } else {
1160 awakenScrollBars();
1161 }
Adam Cohen564976a2010-10-13 18:52:07 -07001162 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 determineScrollingStart(ev);
1164 }
1165 break;
1166
1167 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001168 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 final int activePointerId = mActivePointerId;
1170 final int pointerIndex = ev.findPointerIndex(activePointerId);
1171 final float x = ev.getX(pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001172 final VelocityTracker velocityTracker = mVelocityTracker;
1173 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1174 int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001175 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001176 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001177 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1178 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001179
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001180 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001181 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001182 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1183 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001184
Adam Cohenf358a4b2013-07-23 16:47:31 -07001185 if (!mFreeScroll) {
1186 // In the case that the page is moved far to one direction and then is flung
1187 // in the opposite direction, we use a threshold to determine whether we should
1188 // just return to the starting page, or if we should skip one further.
1189 boolean returnToOriginalPage = false;
1190 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1191 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1192 returnToOriginalPage = true;
1193 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001194
Adam Cohenf358a4b2013-07-23 16:47:31 -07001195 int finalPage;
1196 // We give flings precedence over large moves, which is why we short-circuit our
1197 // test for a large move if a fling has been registered. That is, a large
1198 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001199
Adam Cohenf358a4b2013-07-23 16:47:31 -07001200 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1201 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1202 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1203 snapToPageWithVelocity(finalPage, velocityX);
1204 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1205 (isFling && isVelocityXLeft)) &&
1206 mCurrentPage < getChildCount() - 1) {
1207 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1208 snapToPageWithVelocity(finalPage, velocityX);
1209 } else {
1210 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001211 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001213 if (!mScroller.isFinished()) {
1214 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001215 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001216
Sunny Goyalb7b01352018-08-09 12:31:20 -07001217 int initialScrollX = getScrollX();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001218
Sunny Goyalb7b01352018-08-09 12:31:20 -07001219 if (((initialScrollX >= mMaxScrollX) && (isVelocityXLeft || !isFling)) ||
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001220 ((initialScrollX <= mMinScrollX) && (!isVelocityXLeft || !isFling))) {
1221 mScroller.springBack(getScrollX(), mMinScrollX, mMaxScrollX);
Tony Wickham312209b2019-06-18 15:54:29 -07001222 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001223 } else {
1224 mScroller.setInterpolator(mDefaultInterpolator);
1225 mScroller.fling(initialScrollX, -velocityX,
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001226 mMinScrollX, mMaxScrollX,
Sunny Goyalb7b01352018-08-09 12:31:20 -07001227 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001228
Sunny Goyalb7b01352018-08-09 12:31:20 -07001229 int finalX = mScroller.getFinalPos();
1230 mNextPage = getPageNearestToCenterOfScreen(finalX);
1231
1232 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1233 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001234 if (finalX > mMinScrollX && finalX < mMaxScrollX) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001235 // If scrolling ends in the half of the added space that is closer to
1236 // the end, settle to the end. Otherwise snap to the nearest page.
1237 // If flinging past one of the ends, don't change the velocity as it
1238 // will get stopped at the end anyway.
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001239 int pageSnappedX = finalX < (firstPageScroll + mMinScrollX) / 2
1240 ? mMinScrollX
Sunny Goyalb7b01352018-08-09 12:31:20 -07001241 : finalX > (lastPageScroll + mMaxScrollX) / 2
1242 ? mMaxScrollX
1243 : getScrollForPage(mNextPage);
1244
1245 mScroller.setFinalPos(pageSnappedX);
1246 // Ensure the scroll/snap doesn't happen too fast;
1247 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1248 - mScroller.getDuration();
1249 if (extraScrollDuration > 0) {
1250 mScroller.extendDuration(extraScrollDuration);
1251 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001252 }
1253 }
1254 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001256 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001258
Adam Cohen7d30a372013-07-01 17:03:59 -07001259 // End any intermediate reordering states
1260 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 break;
1262
1263 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001264 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001265 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001266 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001267 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001268 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 break;
1270
1271 case MotionEvent.ACTION_POINTER_UP:
1272 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001273 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 break;
1275 }
1276
1277 return true;
1278 }
1279
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001280 protected boolean shouldFlingForVelocity(int velocityX) {
1281 return Math.abs(velocityX) > mFlingThresholdVelocity;
1282 }
1283
Adam Cohen7d30a372013-07-01 17:03:59 -07001284 private void resetTouchState() {
1285 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001286 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001287 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001288 }
1289
Adam Cohenc2d6e892014-10-16 09:49:24 -07001290 /**
1291 * Triggered by scrolling via touch
1292 */
1293 protected void onScrollInteractionBegin() {
1294 }
1295
1296 protected void onScrollInteractionEnd() {
1297 }
1298
Winson Chung185d7162011-02-28 13:47:29 -08001299 @Override
1300 public boolean onGenericMotionEvent(MotionEvent event) {
1301 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1302 switch (event.getAction()) {
1303 case MotionEvent.ACTION_SCROLL: {
Samuel Fufa9fd21052019-08-28 17:06:51 -07001304 Launcher launcher = Launcher.getLauncher(getContext());
1305 if (launcher != null) {
1306 AbstractFloatingView.closeAllOpenViews(launcher);
1307 }
Winson Chung185d7162011-02-28 13:47:29 -08001308 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1309 final float vscroll;
1310 final float hscroll;
1311 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1312 vscroll = 0;
1313 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1314 } else {
1315 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1316 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1317 }
Samuel Fufa59cba192019-08-22 18:31:34 -07001318 if (Math.abs(vscroll) > Math.abs(hscroll) && !isVerticalScrollable()) {
1319 return true;
1320 }
Winson Chung185d7162011-02-28 13:47:29 -08001321 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001322 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001323 : (hscroll > 0 || vscroll > 0);
1324 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001325 scrollRight();
1326 } else {
1327 scrollLeft();
1328 }
1329 return true;
1330 }
1331 }
1332 }
1333 }
1334 return super.onGenericMotionEvent(event);
1335 }
1336
Samuel Fufa59cba192019-08-22 18:31:34 -07001337 protected boolean isVerticalScrollable() {
1338 return true;
1339 }
1340
Michael Jurkab8f06722010-10-10 15:58:46 -07001341 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1342 if (mVelocityTracker == null) {
1343 mVelocityTracker = VelocityTracker.obtain();
1344 }
1345 mVelocityTracker.addMovement(ev);
1346 }
1347
1348 private void releaseVelocityTracker() {
1349 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001351 mVelocityTracker.recycle();
1352 mVelocityTracker = null;
1353 }
1354 }
1355
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 private void onSecondaryPointerUp(MotionEvent ev) {
1357 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1358 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1359 final int pointerId = ev.getPointerId(pointerIndex);
1360 if (pointerId == mActivePointerId) {
1361 // This was our active pointer going up. Choose a new
1362 // active pointer and adjust accordingly.
1363 // TODO: Make this decision more intelligent.
1364 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1365 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001366 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 mActivePointerId = ev.getPointerId(newPointerIndex);
1368 if (mVelocityTracker != null) {
1369 mVelocityTracker.clear();
1370 }
1371 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001372 }
1373
Winson Chung321e9ee2010-08-09 13:37:56 -07001374 @Override
1375 public void requestChildFocus(View child, View focused) {
1376 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001377 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001378 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001379 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 }
1381 }
1382
Sunny Goyal228153d2018-01-04 15:35:22 -08001383 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001384 return getPageNearestToCenterOfScreen(getScrollX());
1385 }
1386
1387 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001388 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001389 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 final int childCount = getChildCount();
1392 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001393 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001394 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001396 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1398 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1399 minDistanceFromScreenCenter = distanceFromScreenCenter;
1400 minDistanceFromScreenCenterIndex = i;
1401 }
1402 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001403 return minDistanceFromScreenCenterIndex;
1404 }
1405
1406 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001407 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1408 }
1409
1410 protected boolean isInOverScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001411 return (getScrollX() > mMaxScrollX || getScrollX() < mMinScrollX);
Adam Cohen8d769d62017-06-23 17:27:38 -07001412 }
1413
1414 protected int getPageSnapDuration() {
1415 if (isInOverScroll()) {
1416 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1417 }
1418 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 }
1420
Adam Cohene0f66b52010-11-23 15:06:07 -08001421 // We want the duration of the page snap animation to be influenced by the distance that
1422 // the screen has to travel, however, we don't want this duration to be effected in a
1423 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1424 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001425 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001426 f -= 0.5f; // center the values about 0.
1427 f *= 0.3f * Math.PI / 2.0f;
1428 return (float) Math.sin(f);
1429 }
1430
Winson Chung05a31ed2018-02-08 17:08:43 -08001431 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001432 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001433 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001434
Adam Cohenedb40762013-07-18 16:45:45 -07001435 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001436 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001437 int duration = 0;
1438
Sunny Goyal4d113a52015-05-27 10:05:28 -07001439 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001440 // If the velocity is low enough, then treat this more as an automatic page advance
1441 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001442 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001443 }
1444
1445 // Here we compute a "distance" that will be used in the computation of the overall
1446 // snap duration. This is a function of the actual distance that needs to be traveled;
1447 // we keep this value close to half screen size in order to reduce the variance in snap
1448 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001449 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001450 float distance = halfScreenSize + halfScreenSize *
1451 distanceInfluenceForSnapDuration(distanceRatio);
1452
1453 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001454 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001455
1456 // we want the page's snap velocity to approximately match the velocity at which the
1457 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001458 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1459 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001460
Jon Miranda71cb80c2019-01-24 21:25:13 -08001461 if (QUICKSTEP_SPRINGS.get()) {
1462 return snapToPage(whichPage, delta, duration, false, null,
1463 velocity * Math.signum(newX - getUnboundedScrollX()), true);
1464 } else {
1465 return snapToPage(whichPage, delta, duration);
1466 }
Michael Jurka0142d492010-08-25 17:46:15 -07001467 }
1468
Winson Chung05a31ed2018-02-08 17:08:43 -08001469 public boolean snapToPage(int whichPage) {
1470 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001471 }
1472
Winson Chung05a31ed2018-02-08 17:08:43 -08001473 public boolean snapToPageImmediately(int whichPage) {
1474 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001475 }
1476
Winson Chung05a31ed2018-02-08 17:08:43 -08001477 public boolean snapToPage(int whichPage, int duration) {
1478 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001479 }
1480
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001481 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001482 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001483 }
1484
Winson Chung05a31ed2018-02-08 17:08:43 -08001485 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001486 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001487 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001488
Adam Cohenedb40762013-07-18 16:45:45 -07001489 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001490 final int delta = newX - getUnboundedScrollX();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001491 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001492 }
1493
Winson Chung05a31ed2018-02-08 17:08:43 -08001494 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001495 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001496 }
Michael Jurka0142d492010-08-25 17:46:15 -07001497
Winson Chung05a31ed2018-02-08 17:08:43 -08001498 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001499 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001500 if (mFirstLayout) {
1501 setCurrentPage(whichPage);
1502 return false;
1503 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001504
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001505 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001506 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1507 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001508 }
1509
Adam Cohen6f127a62014-06-12 14:54:41 -07001510 whichPage = validateNewPage(whichPage);
1511
Adam Cohen7d30a372013-07-01 17:03:59 -07001512 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001513
Winson Chung321e9ee2010-08-09 13:37:56 -07001514 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001515 if (immediate) {
1516 duration = 0;
1517 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 duration = Math.abs(delta);
1519 }
1520
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001521 if (duration != 0) {
1522 pageBeginTransition();
1523 }
1524
Adam Cohenf358a4b2013-07-23 16:47:31 -07001525 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001526 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001527 }
Adam Cohenf9618852013-11-08 06:45:03 -08001528
1529 if (interpolator != null) {
1530 mScroller.setInterpolator(interpolator);
1531 } else {
1532 mScroller.setInterpolator(mDefaultInterpolator);
1533 }
1534
Jon Miranda71cb80c2019-01-24 21:25:13 -08001535 if (spring && QUICKSTEP_SPRINGS.get()) {
1536 mScroller.startScrollSpring(getUnboundedScrollX(), delta, duration, velocity);
1537 } else {
1538 mScroller.startScroll(getUnboundedScrollX(), delta, duration);
1539 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001540
Adam Cohen674531f2013-12-13 15:07:14 -08001541 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001542
1543 // Trigger a compute() to finish switching pages if necessary
1544 if (immediate) {
1545 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001546 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001547 }
1548
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001550 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001551 }
1552
Vadim Tryshev98913d02018-05-18 18:41:34 -07001553 public boolean scrollLeft() {
1554 if (getNextPage() > 0) {
1555 snapToPage(getNextPage() - 1);
1556 return true;
1557 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001558 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001559 }
1560
Vadim Tryshev98913d02018-05-18 18:41:34 -07001561 public boolean scrollRight() {
1562 if (getNextPage() < getChildCount() - 1) {
1563 snapToPage(getNextPage() + 1);
1564 return true;
1565 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001566 return onOverscroll(getMeasuredWidth());
1567 }
1568
1569 protected boolean onOverscroll(int amount) {
1570 if (!mAllowOverScroll) return false;
1571 onScrollInteractionBegin();
1572 overScroll(amount);
1573 onScrollInteractionEnd();
1574 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 }
1576
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001577 @Override
1578 public CharSequence getAccessibilityClassName() {
1579 // Some accessibility services have special logic for ScrollView. Since we provide same
1580 // accessibility info as ScrollView, inform the service to handle use the same way.
1581 return ScrollView.class.getName();
1582 }
1583
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001584 protected boolean isPageOrderFlipped() {
1585 return false;
1586 }
1587
Winson Chung6a0f57d2011-06-29 20:10:49 -07001588 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001589 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001590 @Override
1591 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1592 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001593 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001594 int offset = (mAllowOverScroll ? 0 : 1);
1595 info.setScrollable(getPageCount() > offset);
1596 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001597 info.addAction(pagesFlipped ?
1598 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1599 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1600 info.addAction(mIsRtl ?
1601 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1602 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001603 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001604 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001605 info.addAction(pagesFlipped ?
1606 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1607 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1608 info.addAction(mIsRtl ?
1609 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1610 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001611 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001612 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1613 // Besides disabling the accessibility long-click, this also prevents this view from getting
1614 // accessibility focus.
1615 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001616 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001617 }
1618
1619 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001620 public void sendAccessibilityEvent(int eventType) {
1621 // Don't let the view send real scroll events.
1622 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1623 super.sendAccessibilityEvent(eventType);
1624 }
1625 }
1626
1627 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001628 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1629 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001630 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001631 }
1632
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001633 @Override
1634 public boolean performAccessibilityAction(int action, Bundle arguments) {
1635 if (super.performAccessibilityAction(action, arguments)) {
1636 return true;
1637 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001638 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001639 switch (action) {
1640 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001641 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001642 return true;
1643 }
1644 } break;
1645 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001646 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001647 return true;
1648 }
yingleiw02cc8482019-08-02 16:14:27 -07001649 } break;
1650 case android.R.id.accessibilityActionPageRight: {
1651 if (!mIsRtl) {
1652 return scrollRight();
1653 } else {
1654 return scrollLeft();
1655 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001656 }
yingleiw02cc8482019-08-02 16:14:27 -07001657 case android.R.id.accessibilityActionPageLeft: {
1658 if (!mIsRtl) {
1659 return scrollLeft();
1660 } else {
1661 return scrollRight();
1662 }
1663 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001664 }
1665 return false;
1666 }
1667
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001668 protected boolean canAnnouncePageDescription() {
1669 return true;
1670 }
1671
Adam Cohen0ffac432013-07-10 11:19:26 -07001672 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001673 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001674 getNextPage() + 1, getChildCount());
1675 }
1676
Tony Mak2e564402018-02-27 17:19:34 +00001677 protected float getDownMotionX() {
1678 return mDownMotionX;
1679 }
1680
1681 protected float getDownMotionY() {
1682 return mDownMotionY;
1683 }
1684
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001685 protected interface ComputePageScrollsLogic {
1686
1687 boolean shouldIncludeView(View view);
1688 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001689
1690 public int[] getVisibleChildrenRange() {
1691 float visibleLeft = 0;
1692 float visibleRight = visibleLeft + getMeasuredWidth();
1693 float scaleX = getScaleX();
1694 if (scaleX < 1 && scaleX > 0) {
1695 float mid = getMeasuredWidth() / 2;
1696 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1697 visibleRight = mid + ((visibleRight - mid) / scaleX);
1698 }
1699
1700 int leftChild = -1;
1701 int rightChild = -1;
1702 final int childCount = getChildCount();
1703 for (int i = 0; i < childCount; i++) {
1704 final View child = getPageAt(i);
1705
1706 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1707 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1708 if (leftChild == -1) {
1709 leftChild = i;
1710 }
1711 rightChild = i;
1712 }
1713 }
1714 mTmpIntPair[0] = leftChild;
1715 mTmpIntPair[1] = rightChild;
1716 return mTmpIntPair;
1717 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001718}