blob: a99c7c28f96c9deb1de010c3d1eadd5979a44888 [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
Michael Jurka0142d492010-08-25 17:46:15 -070067 protected 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 Cohen21cd0022013-10-09 18:57:02 -070087 public static final int INVALID_RESTORE_PAGE = -1001;
88
Adam Cohenf358a4b2013-07-23 16:47:31 -070089 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070090
Adam Cohen265b9a62011-12-07 14:37:18 -080091 protected int mFlingThresholdVelocity;
92 protected int mMinFlingVelocity;
93 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected boolean mFirstLayout = true;
96
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -070099
Sunny Goyal4ffec482016-02-09 11:28:52 -0800100 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected int mNextPage = INVALID_PAGE;
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700102 protected int mMinScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800103 protected int mMaxScrollX;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700104 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800105 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800107 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
109 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700110 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700111 private float mLastMotionX;
112 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700113 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700114
Vadim Trysheve628c472018-05-16 14:28:18 -0700115 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700116 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800117
Michael Jurka7426c422010-11-11 15:23:47 -0800118 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800120 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka5f1c5092010-09-03 14:15:02 -0700122 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Michael Jurka5f1c5092010-09-03 14:15:02 -0700124 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700126 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700127
Jon Miranda71cb80c2019-01-24 21:25:13 -0800128 protected float mSpringOverScrollX;
129
Sunny Goyal061380a2016-02-29 15:15:03 -0800130 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700131
Adam Cohen8d769d62017-06-23 17:27:38 -0700132 protected int mUnboundedScrollX;
133
Winson Chungd2be3812013-07-16 11:11:32 -0700134 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800136 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700137
John Spurlock77e1f472013-09-11 10:09:51 -0400138 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800139 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400140
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800141 // Similar to the platform implementation of isLayoutValid();
142 protected boolean mIsLayoutValid;
143
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700144 private int[] mTmpIntPair = new int[2];
145
Winson Chung321e9ee2010-08-09 13:37:56 -0700146 public PagedView(Context context) {
147 this(context, null);
148 }
149
150 public PagedView(Context context, AttributeSet attrs) {
151 this(context, attrs, 0);
152 }
153
154 public PagedView(Context context, AttributeSet attrs, int defStyle) {
155 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700156
Adam Cohen9c4949e2010-10-05 12:27:22 -0700157 TypedArray a = context.obtainStyledAttributes(attrs,
158 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700159 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700160 a.recycle();
161
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700163 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700164 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700165 }
166
167 /**
168 * Initializes various states for this workspace.
169 */
Michael Jurka0142d492010-08-25 17:46:15 -0700170 protected void init() {
Sunny Goyalb7b01352018-08-09 12:31:20 -0700171 mScroller = new OverScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700172 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700173 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700174
175 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700176 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800178
Sunny Goyalcf25b522015-07-09 00:01:18 -0700179 float density = getResources().getDisplayMetrics().density;
180 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
181 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
182 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700183
184 if (Utilities.ATLEAST_OREO) {
185 setDefaultFocusHighlightEnabled(false);
186 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 }
188
Adam Cohenf9618852013-11-08 06:45:03 -0800189 protected void setDefaultInterpolator(Interpolator interpolator) {
190 mDefaultInterpolator = interpolator;
191 mScroller.setInterpolator(mDefaultInterpolator);
192 }
193
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700194 public void initParentViews(View parent) {
195 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800196 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700197 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700198 }
199 }
200
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800201 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700202 return mPageIndicator;
203 }
204
Adam Cohen674531f2013-12-13 15:07:14 -0800205 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700206 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
207 * that the user was on before entering free scroll mode (e.g. the home screen page they
208 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
209 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700211 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700212 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700214
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700215 /**
216 * Returns the index of page to be shown immediately afterwards.
217 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700218 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700219 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
220 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700221
Adam Cohenf9c184a2016-01-15 16:47:43 -0800222 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 return getChildCount();
224 }
225
Sunny Goyal83a8f042015-05-19 12:52:12 -0700226 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 return getChildAt(index);
228 }
229
Adam Cohenae4f1552011-10-20 00:15:42 -0700230 protected int indexToPage(int index) {
231 return index;
232 }
233
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800235 * Updates the scroll of the current page immediately to its final scroll position. We use this
236 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
237 * the previous tab page.
238 */
239 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700240 // If the current page is invalid, just reset the scroll position to zero
241 int newX = 0;
242 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700243 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700244 }
Sunny Goyalc82c6392018-06-06 15:39:13 -0700245 scrollTo(newX, 0);
Sunny Goyalb7b01352018-08-09 12:31:20 -0700246 mScroller.startScroll(mScroller.getCurrPos(), newX - mScroller.getCurrPos());
Sunny Goyalc82c6392018-06-06 15:39:13 -0700247 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 }
249
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700250 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700251 mScroller.abortAnimation();
252 // We need to clean up the next page here to avoid computeScrollHelper from
253 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700254 if (resetNextPage) {
255 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800256 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700257 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700258 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700259
Tony Wickham8f7ead32016-04-07 18:46:44 -0700260 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700261 mScroller.forceFinished(true);
262 // We need to clean up the next page here to avoid computeScrollHelper from
263 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700264 if (resetNextPage) {
265 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800266 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700267 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700268 }
269
Adam Cohen6f127a62014-06-12 14:54:41 -0700270 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700271 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700272 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700273 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700274 }
275
Chet Haasebc2f0822012-10-26 17:59:53 -0700276 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700277 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
278 */
279 private int ensureWithinScrollBounds(int page) {
280 int dir = !mIsRtl ? 1 : - 1;
281 int currScroll = getScrollForPage(page);
282 int prevScroll;
283 while (currScroll < mMinScrollX) {
284 page += dir;
285 prevScroll = currScroll;
286 currScroll = getScrollForPage(page);
287 if (currScroll <= prevScroll) {
288 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
289 break;
290 }
291 }
292 while (currScroll > mMaxScrollX) {
293 page -= dir;
294 prevScroll = currScroll;
295 currScroll = getScrollForPage(page);
296 if (currScroll >= prevScroll) {
297 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
298 break;
299 }
300 }
301 return page;
302 }
303
Tony Wickham03f27012019-04-25 14:22:54 -0700304 public void setCurrentPage(int currentPage) {
305 setCurrentPage(currentPage, INVALID_PAGE);
306 }
307
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700308 /**
Winson Chung86f77532010-08-24 11:08:22 -0700309 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 */
Tony Wickham03f27012019-04-25 14:22:54 -0700311 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800312 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700313 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800314 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800315 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
316 // the default
317 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800318 return;
319 }
Tony Wickham03f27012019-04-25 14:22:54 -0700320 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700321 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700322 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700323 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800324 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 }
326
Winson Chung8c87cd82013-07-23 16:20:10 -0700327 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800328 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
329 * has settled.
330 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700331 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800332 updatePageIndicator();
333 }
334
335 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700336 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000337 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700338 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700340 protected void pageBeginTransition() {
341 if (!mIsPageInTransition) {
342 mIsPageInTransition = true;
343 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700344 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700345 }
346
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700347 protected void pageEndTransition() {
348 if (mIsPageInTransition) {
349 mIsPageInTransition = false;
350 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700351 }
Michael Jurka0142d492010-08-25 17:46:15 -0700352 }
353
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700354 protected boolean isPageInTransition() {
355 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700356 }
357
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700358 /**
359 * Called when the page starts moving as part of the scroll. Subclasses can override this
360 * to provide custom behavior during animation.
361 */
362 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700363 }
364
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700365 /**
366 * Called when the page ends moving as part of the scroll. Subclasses can override this
367 * to provide custom behavior during animation.
368 */
369 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700370 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700371 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Michael Jurka0142d492010-08-25 17:46:15 -0700372 }
373
Sunny Goyalc86df472016-02-25 09:19:38 -0800374 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700375 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800376 }
377
Winson Chung321e9ee2010-08-09 13:37:56 -0700378 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800379 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800380 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800381 }
382
383 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700384 public void scrollTo(int x, int y) {
Adam Cohen8d769d62017-06-23 17:27:38 -0700385 mUnboundedScrollX = x;
386
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700387 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < mMinScrollX);
388 boolean isXAfterLastPage = mIsRtl ? (x < mMinScrollX) : (x > mMaxScrollX);
Jon Miranda71cb80c2019-01-24 21:25:13 -0800389
390 if (!isXBeforeFirstPage && !isXAfterLastPage) {
391 mSpringOverScrollX = 0;
392 }
393
Adam Cohen0ffac432013-07-10 11:19:26 -0700394 if (isXBeforeFirstPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700395 super.scrollTo(mIsRtl ? mMaxScrollX : mMinScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800396 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700397 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700398 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700399 overScroll(x - mMaxScrollX);
400 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700401 overScroll(x - mMinScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700402 }
Adam Cohen68d73932010-11-15 10:50:58 -0800403 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700404 } else if (isXAfterLastPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700405 super.scrollTo(mIsRtl ? mMinScrollX : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800406 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700407 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700408 if (mIsRtl) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700409 overScroll(x - mMinScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700410 } else {
411 overScroll(x - mMaxScrollX);
412 }
Adam Cohen68d73932010-11-15 10:50:58 -0800413 }
414 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700415 if (mWasInOverscroll) {
416 overScroll(0);
417 mWasInOverscroll = false;
418 }
Adam Cohen68d73932010-11-15 10:50:58 -0800419 super.scrollTo(x, y);
420 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700421
Michael Jurka0142d492010-08-25 17:46:15 -0700422 }
423
Adam Cohen53805212013-10-01 10:39:23 -0700424 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800425 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700426 if (mCurrentPage != getNextPage()) {
427 AccessibilityEvent ev =
428 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700429 ev.setScrollable(true);
430 ev.setScrollX(getScrollX());
431 ev.setScrollY(getScrollY());
432 ev.setMaxScrollX(mMaxScrollX);
433 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700434
Vadim Tryshevf4715972015-05-08 17:21:03 -0700435 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700436 }
Adam Cohen53805212013-10-01 10:39:23 -0700437 }
438 }
439
Michael Jurka0142d492010-08-25 17:46:15 -0700440 // we moved this functionality to a helper function so SmoothPagedView can reuse it
441 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800442 return computeScrollHelper(true);
443 }
444
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700445 protected void announcePageForAccessibility() {
446 if (isAccessibilityEnabled(getContext())) {
447 // Notify the user when the page changes
448 announceForAccessibility(getCurrentPageDescription());
449 }
450 }
451
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800452 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700454 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalb7b01352018-08-09 12:31:20 -0700455 if (getUnboundedScrollX() != mScroller.getCurrPos()
456 || getScrollX() != mScroller.getCurrPos()) {
457 scrollTo(mScroller.getCurrPos(), 0);
Winson Chung557d6ed2011-07-08 15:34:52 -0700458 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800459 if (shouldInvalidate) {
460 invalidate();
461 }
Michael Jurka0142d492010-08-25 17:46:15 -0700462 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800463 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700464 sendScrollAccessibilityEvent();
465
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700466 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700467 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700468 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700469 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700470
Adam Cohen73aa9752010-11-24 16:26:58 -0800471 // We don't want to trigger a page end moving unless the page has settled
472 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700473 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700474 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800475 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700476
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700477 if (canAnnouncePageDescription()) {
478 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700479 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 }
Michael Jurka0142d492010-08-25 17:46:15 -0700481 return false;
482 }
483
484 @Override
485 public void computeScroll() {
486 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 }
488
Sunny Goyalce8809a2018-01-18 14:02:47 -0800489 public int getExpectedHeight() {
490 return getMeasuredHeight();
491 }
492
Adam Cohen410f3cd2013-09-22 12:09:32 -0700493 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800494 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800495 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700496 }
497
Sunny Goyalce8809a2018-01-18 14:02:47 -0800498 public int getExpectedWidth() {
499 return getMeasuredWidth();
500 }
501
Sunny Goyalbc683e92017-12-06 10:25:07 -0800502 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800503 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800504 - mInsets.left - mInsets.right;
505 }
506
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800508 public void requestLayout() {
509 mIsLayoutValid = false;
510 super.requestLayout();
511 }
512
513 @Override
514 public void forceLayout() {
515 mIsLayoutValid = false;
516 super.forceLayout();
517 }
518
519 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700520 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700521 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700522 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
523 return;
524 }
525
Adam Cohen7d30a372013-07-01 17:03:59 -0700526 // We measure the dimensions of the PagedView to be larger than the pages so that when we
527 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700528 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
529 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
530 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700531 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700532
Adam Cohen7d30a372013-07-01 17:03:59 -0700533 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
534 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
535 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 }
537
Winson Chung8aad6102012-05-11 16:27:49 -0700538 // Return early if we aren't given a proper dimension
539 if (widthSize <= 0 || heightSize <= 0) {
540 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
541 return;
542 }
543
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700545 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700546 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700547
Sunny Goyalac00cba2017-11-13 15:58:01 -0800548 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800549 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800550 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800551 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700552
Sunny Goyalac00cba2017-11-13 15:58:01 -0800553 // measureChildren takes accounts for content padding, we only need to care about extra
554 // space due to insets.
555 measureChildren(myWidthSpec, myHeightSpec);
556 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800557 }
558
Sunny Goyalcf25b522015-07-09 00:01:18 -0700559 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700561 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800562 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700563 final int childCount = getChildCount();
564 boolean pageScrollChanged = false;
565 if (mPageScrolls == null || childCount != mPageScrolls.length) {
566 mPageScrolls = new int[childCount];
567 pageScrollChanged = true;
568 }
569
570 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700571 return;
572 }
573
Winson Chung785d2eb2011-04-14 16:08:02 -0700574 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700575
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700576 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
577 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700579
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700580 final LayoutTransition transition = getLayoutTransition();
581 // If the transition is running defer updating max scroll, as some empty pages could
582 // still be present, and a max scroll change could cause sudden jumps in scroll.
583 if (transition != null && transition.isRunning()) {
584 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
585
586 @Override
587 public void startTransition(LayoutTransition transition, ViewGroup container,
588 View view, int transitionType) { }
589
590 @Override
591 public void endTransition(LayoutTransition transition, ViewGroup container,
592 View view, int transitionType) {
593 // Wait until all transitions are complete.
594 if (!transition.isRunning()) {
595 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700596 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700597 }
598 }
599 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700600 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700601 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700602 }
603
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700604 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
605 updateCurrentPageScroll();
606 mFirstLayout = false;
607 }
608
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800609 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700610 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700611 }
Winson Chunge3193b92010-09-10 11:44:42 -0700612 }
613
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700614 /**
615 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
616 * of {@code outPageScrolls} should be same as the the childCount
617 *
618 */
619 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
620 ComputePageScrollsLogic scrollLogic) {
621 final int childCount = getChildCount();
622
623 final int startIndex = mIsRtl ? childCount - 1 : 0;
624 final int endIndex = mIsRtl ? -1 : childCount;
625 final int delta = mIsRtl ? -1 : 1;
626
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700627 final int verticalCenter = (getPaddingTop() + getMeasuredHeight() + mInsets.top
628 - mInsets.bottom - getPaddingBottom()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700629
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700630 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700631 final int scrollOffsetRight = getWidth() - getPaddingRight() - mInsets.right;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700632 boolean pageScrollChanged = false;
633
Sunny Goyalc82c6392018-06-06 15:39:13 -0700634 for (int i = startIndex, childLeft = scrollOffsetLeft; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700635 final View child = getPageAt(i);
636 if (scrollLogic.shouldIncludeView(child)) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700637 final int childWidth = child.getMeasuredWidth();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700638 final int childRight = childLeft + childWidth;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700639
640 if (layoutChildren) {
641 final int childHeight = child.getMeasuredHeight();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700642 final int childTop = verticalCenter - childHeight / 2;
643 child.layout(childLeft, childTop, childRight, childTop + childHeight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700644 }
645
Sunny Goyalc82c6392018-06-06 15:39:13 -0700646 // In case the pages are of different width, align the page to left or right edge
647 // based on the orientation.
648 final int pageScroll = mIsRtl
649 ? (childLeft - scrollOffsetLeft)
650 : Math.max(0, childRight - scrollOffsetRight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700651 if (outPageScrolls[i] != pageScroll) {
652 pageScrollChanged = true;
653 outPageScrolls[i] = pageScroll;
654 }
655
656 childLeft += childWidth + mPageSpacing + getChildGap();
657 }
658 }
659 return pageScrollChanged;
660 }
661
Sunny Goyala1fbd842015-05-20 13:40:27 -0700662 protected int getChildGap() {
663 return 0;
664 }
665
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700666 protected void updateMinAndMaxScrollX() {
667 mMinScrollX = computeMinScrollX();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700668 mMaxScrollX = computeMaxScrollX();
669 }
670
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700671 protected int computeMinScrollX() {
672 return 0;
Tony50876bf2018-09-19 23:09:14 -0400673 }
674
Tony Wickhamf549dab2016-05-16 09:54:06 -0700675 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700676 int childCount = getChildCount();
677 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700678 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700679 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700680 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700681 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700682 }
683 }
684
Adam Cohen3b185e22013-10-29 14:45:58 -0700685 public void setPageSpacing(int pageSpacing) {
686 mPageSpacing = pageSpacing;
687 requestLayout();
688 }
689
Tony50876bf2018-09-19 23:09:14 -0400690 public int getPageSpacing() {
691 return mPageSpacing;
692 }
693
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800694 private void dispatchPageCountChanged() {
695 if (mPageIndicator != null) {
696 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700697 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700698 // This ensures that when children are added, they get the correct transforms / alphas
699 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700700 invalidate();
701 }
702
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800704 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800705 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800706 dispatchPageCountChanged();
707 }
708
709 @Override
710 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800711 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700712 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800713 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700714 }
715
Adam Cohen73894962011-10-31 13:17:17 -0700716 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700717 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800718 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700719 }
720
Winson Chung321e9ee2010-08-09 13:37:56 -0700721 @Override
722 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700723 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700724 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700725 if (immediate) {
726 setCurrentPage(page);
727 } else {
728 snapToPage(page);
729 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 return true;
731 }
732 return false;
733 }
734
735 @Override
736 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700737 int focusablePage;
738 if (mNextPage != INVALID_PAGE) {
739 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700741 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 }
Winson Chung86f77532010-08-24 11:08:22 -0700743 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700744 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700745 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 }
747 return false;
748 }
749
750 @Override
751 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800752 if (super.dispatchUnhandledMove(focused, direction)) {
753 return true;
754 }
755
756 if (mIsRtl) {
757 if (direction == View.FOCUS_LEFT) {
758 direction = View.FOCUS_RIGHT;
759 } else if (direction == View.FOCUS_RIGHT) {
760 direction = View.FOCUS_LEFT;
761 }
762 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700764 if (getCurrentPage() > 0) {
765 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700766 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 return true;
768 }
769 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700770 if (getCurrentPage() < getPageCount() - 1) {
771 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700772 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 return true;
774 }
775 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800776 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 }
778
779 @Override
780 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700781 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
782 return;
783 }
784
Adam Cohen0ffac432013-07-10 11:19:26 -0700785 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700786 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700787 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 }
789 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700790 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700791 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 }
793 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700794 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700795 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 }
797 }
798 }
799
800 /**
801 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700802 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 *
Winson Chung86f77532010-08-24 11:08:22 -0700804 * This happens when live folders requery, and if they're off page, they
805 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 */
807 @Override
808 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700809 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 View v = focused;
811 while (true) {
812 if (v == current) {
813 super.focusableViewAvailable(focused);
814 return;
815 }
816 if (v == this) {
817 return;
818 }
819 ViewParent parent = v.getParent();
820 if (parent instanceof View) {
821 v = (View)v.getParent();
822 } else {
823 return;
824 }
825 }
826 }
827
828 /**
829 * {@inheritDoc}
830 */
831 @Override
832 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
833 if (disallowIntercept) {
834 // We need to make sure to cancel our long press if
835 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700836 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700837 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 super.requestDisallowInterceptTouchEvent(disallowIntercept);
840 }
841
842 @Override
843 public boolean onInterceptTouchEvent(MotionEvent ev) {
844 /*
845 * This method JUST determines whether we want to intercept the motion.
846 * If we return true, onTouchEvent will be called and we do the actual
847 * scrolling there.
848 */
849
Winson Chung45e1d6e2010-11-09 17:19:49 -0800850 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700851 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700852
853 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800854
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 /*
856 * Shortcut the most recurring case: the user is in the dragging
857 * state and he is moving his finger. We want to intercept this
858 * motion.
859 */
860 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700861 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 return true;
863 }
864
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 switch (action & MotionEvent.ACTION_MASK) {
866 case MotionEvent.ACTION_MOVE: {
867 /*
868 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
869 * whether the user has moved far enough from his original down touch.
870 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700871 if (mActivePointerId != INVALID_POINTER) {
872 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700873 }
874 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
875 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
876 // i.e. fall through to the next case (don't break)
877 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
878 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700879 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 }
881
882 case MotionEvent.ACTION_DOWN: {
883 final float x = ev.getX();
884 final float y = ev.getY();
885 // Remember location of down touch
886 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700887 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800889 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800890 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700892
Tony Wickhama0aee212019-09-18 09:24:03 -0700893 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700894
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 break;
896 }
897
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800899 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700900 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 break;
902
903 case MotionEvent.ACTION_POINTER_UP:
904 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800905 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 break;
907 }
908
909 /*
910 * The only time we want to intercept motion events is if we are in the
911 * drag mode.
912 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700913 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 }
915
Tony Wickhama0aee212019-09-18 09:24:03 -0700916 /**
917 * If being flinged and user touches the screen, initiate drag; otherwise don't.
918 */
919 private void updateIsBeingDraggedOnTouchDown() {
920 // mScroller.isFinished should be false when being flinged.
921 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
922 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
923
924 if (finishedScrolling) {
925 mIsBeingDragged = false;
926 if (!mScroller.isFinished() && !mFreeScroll) {
927 setCurrentPage(getNextPage());
928 pageEndTransition();
929 }
930 } else {
931 mIsBeingDragged = true;
932 }
933 }
934
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800935 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700936 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800937 }
938
Adam Cohenf8d28232011-02-01 21:47:00 -0800939 protected void determineScrollingStart(MotionEvent ev) {
940 determineScrollingStart(ev, 1.0f);
941 }
942
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 /*
944 * Determines if we should change the touch state to start scrolling after the
945 * user moves their touch point too far.
946 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800947 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700948 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700950 if (pointerIndex == -1) return;
951
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 final float x = ev.getX(pointerIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 final int xDiff = (int) Math.abs(x - mLastMotionX);
Adam Cohenf8d28232011-02-01 21:47:00 -0800954 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700956
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700957 if (xMoved) {
958 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700959 mIsBeingDragged = true;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700960 mTotalMotionX += Math.abs(mLastMotionX - x);
961 mLastMotionX = x;
962 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700963 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700964 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800965 // Stop listening for things like pinches.
966 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800967 }
968 }
969
970 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700971 // Try canceling the long press. It could also have been scheduled
972 // by a distant descendant, so use the mAllowLongPress flag to block
973 // everything
974 final View currentPage = getPageAt(mCurrentPage);
975 if (currentPage != null) {
976 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 }
978 }
979
Adam Cohenb5ba0972011-09-07 18:02:31 -0700980 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800981 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700982
Adam Cohenedb40762013-07-18 16:45:45 -0700983 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700984 int count = getChildCount();
985
986 final int totalDistance;
987
988 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700989 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700990 adjacentPage = page - 1;
991 }
992
993 if (adjacentPage < 0 || adjacentPage > count - 1) {
994 totalDistance = v.getMeasuredWidth() + mPageSpacing;
995 } else {
996 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
997 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700998
999 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001000 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1001 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001002 return scrollProgress;
1003 }
1004
Adam Cohenedb40762013-07-18 16:45:45 -07001005 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001006 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001007 return 0;
1008 } else {
1009 return mPageScrolls[index];
1010 }
1011 }
1012
Adam Cohen564a2e72013-10-09 14:47:32 -07001013 // While layout transitions are occurring, a child's position may stray from its baseline
1014 // position. This method returns the magnitude of this stray at any given time.
1015 public int getLayoutTransitionOffsetForPage(int index) {
1016 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1017 return 0;
1018 } else {
1019 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001020
Sunny Goyal228153d2018-01-04 15:35:22 -08001021 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1022 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001023 return (int) (child.getX() - baselineX);
1024 }
1025 }
1026
Jon Miranda71cb80c2019-01-24 21:25:13 -08001027 @Override
1028 protected void dispatchDraw(Canvas canvas) {
1029 if (mScroller.isSpringing() && mSpringOverScrollX != 0) {
1030 int saveCount = canvas.save();
1031
1032 canvas.translate(-mSpringOverScrollX, 0);
1033 super.dispatchDraw(canvas);
1034
1035 canvas.restoreToCount(saveCount);
1036 } else {
1037 super.dispatchDraw(canvas);
1038 }
1039 }
1040
Sunny Goyalb7b01352018-08-09 12:31:20 -07001041 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001042 mSpringOverScrollX = amount;
1043 if (amount == 0) {
1044 return;
1045 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001046
Sunny Goyal228153d2018-01-04 15:35:22 -08001047 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Jon Miranda71cb80c2019-01-24 21:25:13 -08001048 mSpringOverScrollX = overScrollAmount;
1049 if (mScroller.isSpringing()) {
1050 invalidate();
1051 return;
1052 }
1053
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001054 int x = Utilities.boundToRange(getScrollX(), mMinScrollX, mMaxScrollX);
1055 super.scrollTo(x + overScrollAmount, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001056 invalidate();
1057 }
1058
Sunny Goyalb7b01352018-08-09 12:31:20 -07001059 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001060 mSpringOverScrollX = amount;
1061 if (mScroller.isSpringing()) {
1062 invalidate();
1063 return;
1064 }
1065
Sunny Goyalb7b01352018-08-09 12:31:20 -07001066 if (amount == 0) return;
1067
1068 if (mFreeScroll && !mScroller.isFinished()) {
1069 if (amount < 0) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001070 super.scrollTo(mMinScrollX + amount, getScrollY());
Sunny Goyalb7b01352018-08-09 12:31:20 -07001071 } else {
1072 super.scrollTo(mMaxScrollX + amount, getScrollY());
1073 }
1074 } else {
1075 dampedOverScroll(amount);
1076 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001077 }
1078
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001079
Tony Wickham023f4042019-01-29 10:52:31 -08001080 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001081 if (mFreeScroll == freeScroll) {
1082 return;
1083 }
1084
Tony Wickham8f7ead32016-04-07 18:46:44 -07001085 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001086 mFreeScroll = freeScroll;
1087
Adam Cohenf9618852013-11-08 06:45:03 -08001088 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001089 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001090 } else if (wasFreeScroll) {
1091 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001092 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001093 }
1094
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001095 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001096 mAllowOverScroll = enable;
1097 }
1098
Winson Chung321e9ee2010-08-09 13:37:56 -07001099 @Override
1100 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001101 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001102 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001103
Michael Jurkab8f06722010-10-10 15:58:46 -07001104 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001105
1106 final int action = ev.getAction();
1107
1108 switch (action & MotionEvent.ACTION_MASK) {
1109 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001110 updateIsBeingDraggedOnTouchDown();
1111
Winson Chung321e9ee2010-08-09 13:37:56 -07001112 /*
1113 * If being flinged and user touches, stop the fling. isFinished
1114 * will be false if being flinged.
1115 */
1116 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001117 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001118 }
1119
1120 // Remember where the motion event started
1121 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001122 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001123 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001124 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001126
Sunny Goyalaad33592018-08-07 17:20:35 -07001127 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001129 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001130 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 break;
1132
1133 case MotionEvent.ACTION_MOVE:
Sunny Goyalaad33592018-08-07 17:20:35 -07001134 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 // Scroll to follow the motion event
1136 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001137
1138 if (pointerIndex == -1) return true;
1139
Winson Chung321e9ee2010-08-09 13:37:56 -07001140 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001141 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001142
Adam Cohenaefd4e12011-02-14 16:39:38 -08001143 mTotalMotionX += Math.abs(deltaX);
1144
Winson Chungc0844aa2011-02-02 15:25:58 -08001145 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1146 // keep the remainder because we are actually testing if we've moved from the last
1147 // scrolled position (which is discrete).
1148 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001149 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001150 mLastMotionX = x;
1151 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 } else {
1153 awakenScrollBars();
1154 }
Adam Cohen564976a2010-10-13 18:52:07 -07001155 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 determineScrollingStart(ev);
1157 }
1158 break;
1159
1160 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001161 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 final int activePointerId = mActivePointerId;
1163 final int pointerIndex = ev.findPointerIndex(activePointerId);
1164 final float x = ev.getX(pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001165 final VelocityTracker velocityTracker = mVelocityTracker;
1166 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1167 int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001168 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001169 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001170 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1171 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001172
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001173 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001174 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001175 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1176 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001177
Adam Cohenf358a4b2013-07-23 16:47:31 -07001178 if (!mFreeScroll) {
1179 // In the case that the page is moved far to one direction and then is flung
1180 // in the opposite direction, we use a threshold to determine whether we should
1181 // just return to the starting page, or if we should skip one further.
1182 boolean returnToOriginalPage = false;
1183 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1184 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1185 returnToOriginalPage = true;
1186 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001187
Adam Cohenf358a4b2013-07-23 16:47:31 -07001188 int finalPage;
1189 // We give flings precedence over large moves, which is why we short-circuit our
1190 // test for a large move if a fling has been registered. That is, a large
1191 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001192
Adam Cohenf358a4b2013-07-23 16:47:31 -07001193 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1194 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1195 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1196 snapToPageWithVelocity(finalPage, velocityX);
1197 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1198 (isFling && isVelocityXLeft)) &&
1199 mCurrentPage < getChildCount() - 1) {
1200 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1201 snapToPageWithVelocity(finalPage, velocityX);
1202 } else {
1203 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001204 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001206 if (!mScroller.isFinished()) {
1207 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001208 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001209
Sunny Goyalb7b01352018-08-09 12:31:20 -07001210 int initialScrollX = getScrollX();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001211
Sunny Goyalb7b01352018-08-09 12:31:20 -07001212 if (((initialScrollX >= mMaxScrollX) && (isVelocityXLeft || !isFling)) ||
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001213 ((initialScrollX <= mMinScrollX) && (!isVelocityXLeft || !isFling))) {
1214 mScroller.springBack(getScrollX(), mMinScrollX, mMaxScrollX);
Tony Wickham312209b2019-06-18 15:54:29 -07001215 mNextPage = getPageNearestToCenterOfScreen();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001216 } else {
1217 mScroller.setInterpolator(mDefaultInterpolator);
1218 mScroller.fling(initialScrollX, -velocityX,
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001219 mMinScrollX, mMaxScrollX,
Sunny Goyalb7b01352018-08-09 12:31:20 -07001220 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001221
Sunny Goyalb7b01352018-08-09 12:31:20 -07001222 int finalX = mScroller.getFinalPos();
1223 mNextPage = getPageNearestToCenterOfScreen(finalX);
1224
1225 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1226 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001227 if (finalX > mMinScrollX && finalX < mMaxScrollX) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001228 // If scrolling ends in the half of the added space that is closer to
1229 // the end, settle to the end. Otherwise snap to the nearest page.
1230 // If flinging past one of the ends, don't change the velocity as it
1231 // will get stopped at the end anyway.
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001232 int pageSnappedX = finalX < (firstPageScroll + mMinScrollX) / 2
1233 ? mMinScrollX
Sunny Goyalb7b01352018-08-09 12:31:20 -07001234 : finalX > (lastPageScroll + mMaxScrollX) / 2
1235 ? mMaxScrollX
1236 : getScrollForPage(mNextPage);
1237
1238 mScroller.setFinalPos(pageSnappedX);
1239 // Ensure the scroll/snap doesn't happen too fast;
1240 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1241 - mScroller.getDuration();
1242 if (extraScrollDuration > 0) {
1243 mScroller.extendDuration(extraScrollDuration);
1244 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001245 }
1246 }
1247 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001249 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001251
Adam Cohen7d30a372013-07-01 17:03:59 -07001252 // End any intermediate reordering states
1253 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001254 break;
1255
1256 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001257 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001258 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001259 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001260 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001261 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 break;
1263
1264 case MotionEvent.ACTION_POINTER_UP:
1265 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001266 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 break;
1268 }
1269
1270 return true;
1271 }
1272
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001273 protected boolean shouldFlingForVelocity(int velocityX) {
1274 return Math.abs(velocityX) > mFlingThresholdVelocity;
1275 }
1276
Adam Cohen7d30a372013-07-01 17:03:59 -07001277 private void resetTouchState() {
1278 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001279 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001280 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001281 }
1282
Adam Cohenc2d6e892014-10-16 09:49:24 -07001283 /**
1284 * Triggered by scrolling via touch
1285 */
1286 protected void onScrollInteractionBegin() {
1287 }
1288
1289 protected void onScrollInteractionEnd() {
1290 }
1291
Winson Chung185d7162011-02-28 13:47:29 -08001292 @Override
1293 public boolean onGenericMotionEvent(MotionEvent event) {
1294 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1295 switch (event.getAction()) {
1296 case MotionEvent.ACTION_SCROLL: {
Samuel Fufa9fd21052019-08-28 17:06:51 -07001297 Launcher launcher = Launcher.getLauncher(getContext());
1298 if (launcher != null) {
1299 AbstractFloatingView.closeAllOpenViews(launcher);
1300 }
Winson Chung185d7162011-02-28 13:47:29 -08001301 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1302 final float vscroll;
1303 final float hscroll;
1304 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1305 vscroll = 0;
1306 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1307 } else {
1308 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1309 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1310 }
Samuel Fufa59cba192019-08-22 18:31:34 -07001311 if (Math.abs(vscroll) > Math.abs(hscroll) && !isVerticalScrollable()) {
1312 return true;
1313 }
Winson Chung185d7162011-02-28 13:47:29 -08001314 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001315 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001316 : (hscroll > 0 || vscroll > 0);
1317 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001318 scrollRight();
1319 } else {
1320 scrollLeft();
1321 }
1322 return true;
1323 }
1324 }
1325 }
1326 }
1327 return super.onGenericMotionEvent(event);
1328 }
1329
Samuel Fufa59cba192019-08-22 18:31:34 -07001330 protected boolean isVerticalScrollable() {
1331 return true;
1332 }
1333
Michael Jurkab8f06722010-10-10 15:58:46 -07001334 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1335 if (mVelocityTracker == null) {
1336 mVelocityTracker = VelocityTracker.obtain();
1337 }
1338 mVelocityTracker.addMovement(ev);
1339 }
1340
1341 private void releaseVelocityTracker() {
1342 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001343 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001344 mVelocityTracker.recycle();
1345 mVelocityTracker = null;
1346 }
1347 }
1348
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 private void onSecondaryPointerUp(MotionEvent ev) {
1350 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1351 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1352 final int pointerId = ev.getPointerId(pointerIndex);
1353 if (pointerId == mActivePointerId) {
1354 // This was our active pointer going up. Choose a new
1355 // active pointer and adjust accordingly.
1356 // TODO: Make this decision more intelligent.
1357 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1358 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001359 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 mActivePointerId = ev.getPointerId(newPointerIndex);
1361 if (mVelocityTracker != null) {
1362 mVelocityTracker.clear();
1363 }
1364 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001365 }
1366
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 @Override
1368 public void requestChildFocus(View child, View focused) {
1369 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001370 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001371 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001372 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001373 }
1374 }
1375
Sunny Goyal228153d2018-01-04 15:35:22 -08001376 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001377 return getPageNearestToCenterOfScreen(getScrollX());
1378 }
1379
1380 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001381 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001382 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001384 final int childCount = getChildCount();
1385 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001386 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001387 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001389 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1391 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1392 minDistanceFromScreenCenter = distanceFromScreenCenter;
1393 minDistanceFromScreenCenterIndex = i;
1394 }
1395 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001396 return minDistanceFromScreenCenterIndex;
1397 }
1398
1399 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001400 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1401 }
1402
1403 protected boolean isInOverScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001404 return (getScrollX() > mMaxScrollX || getScrollX() < mMinScrollX);
Adam Cohen8d769d62017-06-23 17:27:38 -07001405 }
1406
1407 protected int getPageSnapDuration() {
1408 if (isInOverScroll()) {
1409 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1410 }
1411 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 }
1413
Adam Cohene0f66b52010-11-23 15:06:07 -08001414 // We want the duration of the page snap animation to be influenced by the distance that
1415 // the screen has to travel, however, we don't want this duration to be effected in a
1416 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1417 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001418 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001419 f -= 0.5f; // center the values about 0.
1420 f *= 0.3f * Math.PI / 2.0f;
1421 return (float) Math.sin(f);
1422 }
1423
Winson Chung05a31ed2018-02-08 17:08:43 -08001424 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001425 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001426 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001427
Adam Cohenedb40762013-07-18 16:45:45 -07001428 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001429 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001430 int duration = 0;
1431
Sunny Goyal4d113a52015-05-27 10:05:28 -07001432 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001433 // If the velocity is low enough, then treat this more as an automatic page advance
1434 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001435 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001436 }
1437
1438 // Here we compute a "distance" that will be used in the computation of the overall
1439 // snap duration. This is a function of the actual distance that needs to be traveled;
1440 // we keep this value close to half screen size in order to reduce the variance in snap
1441 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001442 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001443 float distance = halfScreenSize + halfScreenSize *
1444 distanceInfluenceForSnapDuration(distanceRatio);
1445
1446 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001447 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001448
1449 // we want the page's snap velocity to approximately match the velocity at which the
1450 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001451 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1452 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001453
Jon Miranda71cb80c2019-01-24 21:25:13 -08001454 if (QUICKSTEP_SPRINGS.get()) {
1455 return snapToPage(whichPage, delta, duration, false, null,
1456 velocity * Math.signum(newX - getUnboundedScrollX()), true);
1457 } else {
1458 return snapToPage(whichPage, delta, duration);
1459 }
Michael Jurka0142d492010-08-25 17:46:15 -07001460 }
1461
Winson Chung05a31ed2018-02-08 17:08:43 -08001462 public boolean snapToPage(int whichPage) {
1463 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 }
1465
Winson Chung05a31ed2018-02-08 17:08:43 -08001466 public boolean snapToPageImmediately(int whichPage) {
1467 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001468 }
1469
Winson Chung05a31ed2018-02-08 17:08:43 -08001470 public boolean snapToPage(int whichPage, int duration) {
1471 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001472 }
1473
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001474 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001475 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001476 }
1477
Winson Chung05a31ed2018-02-08 17:08:43 -08001478 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001479 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001480 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001481
Adam Cohenedb40762013-07-18 16:45:45 -07001482 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001483 final int delta = newX - getUnboundedScrollX();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001484 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001485 }
1486
Winson Chung05a31ed2018-02-08 17:08:43 -08001487 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001488 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001489 }
Michael Jurka0142d492010-08-25 17:46:15 -07001490
Winson Chung05a31ed2018-02-08 17:08:43 -08001491 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001492 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001493 if (mFirstLayout) {
1494 setCurrentPage(whichPage);
1495 return false;
1496 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001497
1498 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001499 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1500 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001501 }
1502
Adam Cohen6f127a62014-06-12 14:54:41 -07001503 whichPage = validateNewPage(whichPage);
1504
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001506
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001508 if (immediate) {
1509 duration = 0;
1510 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 duration = Math.abs(delta);
1512 }
1513
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001514 if (duration != 0) {
1515 pageBeginTransition();
1516 }
1517
Adam Cohenf358a4b2013-07-23 16:47:31 -07001518 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001519 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001520 }
Adam Cohenf9618852013-11-08 06:45:03 -08001521
1522 if (interpolator != null) {
1523 mScroller.setInterpolator(interpolator);
1524 } else {
1525 mScroller.setInterpolator(mDefaultInterpolator);
1526 }
1527
Jon Miranda71cb80c2019-01-24 21:25:13 -08001528 if (spring && QUICKSTEP_SPRINGS.get()) {
1529 mScroller.startScrollSpring(getUnboundedScrollX(), delta, duration, velocity);
1530 } else {
1531 mScroller.startScroll(getUnboundedScrollX(), delta, duration);
1532 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001533
Adam Cohen674531f2013-12-13 15:07:14 -08001534 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001535
1536 // Trigger a compute() to finish switching pages if necessary
1537 if (immediate) {
1538 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001539 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001540 }
1541
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001543 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 }
1545
Vadim Tryshev98913d02018-05-18 18:41:34 -07001546 public boolean scrollLeft() {
1547 if (getNextPage() > 0) {
1548 snapToPage(getNextPage() - 1);
1549 return true;
1550 }
Pinyao Ting3ad4ace2019-09-17 16:58:07 +00001551 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 }
1553
Vadim Tryshev98913d02018-05-18 18:41:34 -07001554 public boolean scrollRight() {
1555 if (getNextPage() < getChildCount() - 1) {
1556 snapToPage(getNextPage() + 1);
1557 return true;
1558 }
Pinyao Ting3ad4ace2019-09-17 16:58:07 +00001559 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001560 }
1561
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001562 @Override
1563 public CharSequence getAccessibilityClassName() {
1564 // Some accessibility services have special logic for ScrollView. Since we provide same
1565 // accessibility info as ScrollView, inform the service to handle use the same way.
1566 return ScrollView.class.getName();
1567 }
1568
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001569 protected boolean isPageOrderFlipped() {
1570 return false;
1571 }
1572
Winson Chung6a0f57d2011-06-29 20:10:49 -07001573 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001574 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001575 @Override
1576 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1577 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001578 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Ting3ad4ace2019-09-17 16:58:07 +00001579 info.setScrollable(getPageCount() > 1);
1580 if (getCurrentPage() < getPageCount() - 1) {
yingleiw02cc8482019-08-02 16:14:27 -07001581 info.addAction(pagesFlipped ?
1582 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1583 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1584 info.addAction(mIsRtl ?
1585 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1586 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001587 }
Pinyao Ting3ad4ace2019-09-17 16:58:07 +00001588 if (getCurrentPage() > 0) {
yingleiw02cc8482019-08-02 16:14:27 -07001589 info.addAction(pagesFlipped ?
1590 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1591 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1592 info.addAction(mIsRtl ?
1593 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1594 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001595 }
Pinyao Ting3ad4ace2019-09-17 16:58:07 +00001596
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001597 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1598 // Besides disabling the accessibility long-click, this also prevents this view from getting
1599 // accessibility focus.
1600 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001601 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001602 }
1603
1604 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001605 public void sendAccessibilityEvent(int eventType) {
1606 // Don't let the view send real scroll events.
1607 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1608 super.sendAccessibilityEvent(eventType);
1609 }
1610 }
1611
1612 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001613 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1614 super.onInitializeAccessibilityEvent(event);
Pinyao Ting3ad4ace2019-09-17 16:58:07 +00001615 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001616 }
1617
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001618 @Override
1619 public boolean performAccessibilityAction(int action, Bundle arguments) {
1620 if (super.performAccessibilityAction(action, arguments)) {
1621 return true;
1622 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001623 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001624 switch (action) {
1625 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001626 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001627 return true;
1628 }
1629 } break;
1630 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001631 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001632 return true;
1633 }
yingleiw02cc8482019-08-02 16:14:27 -07001634 } break;
1635 case android.R.id.accessibilityActionPageRight: {
1636 if (!mIsRtl) {
1637 return scrollRight();
1638 } else {
1639 return scrollLeft();
1640 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001641 }
yingleiw02cc8482019-08-02 16:14:27 -07001642 case android.R.id.accessibilityActionPageLeft: {
1643 if (!mIsRtl) {
1644 return scrollLeft();
1645 } else {
1646 return scrollRight();
1647 }
1648 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001649 }
1650 return false;
1651 }
1652
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001653 protected boolean canAnnouncePageDescription() {
1654 return true;
1655 }
1656
Adam Cohen0ffac432013-07-10 11:19:26 -07001657 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001658 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001659 getNextPage() + 1, getChildCount());
1660 }
1661
Tony Mak2e564402018-02-27 17:19:34 +00001662 protected float getDownMotionX() {
1663 return mDownMotionX;
1664 }
1665
1666 protected float getDownMotionY() {
1667 return mDownMotionY;
1668 }
1669
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001670 protected interface ComputePageScrollsLogic {
1671
1672 boolean shouldIncludeView(View view);
1673 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001674
1675 public int[] getVisibleChildrenRange() {
1676 float visibleLeft = 0;
1677 float visibleRight = visibleLeft + getMeasuredWidth();
1678 float scaleX = getScaleX();
1679 if (scaleX < 1 && scaleX > 0) {
1680 float mid = getMeasuredWidth() / 2;
1681 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1682 visibleRight = mid + ((visibleRight - mid) / scaleX);
1683 }
1684
1685 int leftChild = -1;
1686 int rightChild = -1;
1687 final int childCount = getChildCount();
1688 for (int i = 0; i < childCount; i++) {
1689 final View child = getPageAt(i);
1690
1691 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1692 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1693 if (leftChild == -1) {
1694 leftChild = i;
1695 }
1696 rightChild = i;
1697 }
1698 }
1699 mTmpIntPair[0] = leftChild;
1700 mTmpIntPair[1] = rightChild;
1701 return mTmpIntPair;
1702 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001703}