blob: abb45e51ddb5f6b4d5b32f0007bc140feadf20b2 [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;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070050import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070052import com.android.launcher3.touch.OverScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -070053import com.android.launcher3.util.OverScroller;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070055
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chung321e9ee2010-08-09 13:37:56 -070058/**
59 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070060 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070061 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080062public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070063 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070064 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070065
Michael Jurka0142d492010-08-25 17:46:15 -070066 protected static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070067 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Vadim Tryshevfedca432015-08-19 17:55:02 -070069 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Sunny Goyalb72d8b22017-07-14 00:02:27 -070071 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070072 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070073
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080075 // The page is moved more than halfway, automatically move to the next page on touch up.
76 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080077
Sunny Goyal8e2133b2015-05-06 13:39:07 -070078 private static final float MAX_SCROLL_PROGRESS = 1.0f;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 // The following constants need to be scaled based on density. The scaled versions will be
81 // assigned to the corresponding member variables below.
82 private static final int FLING_THRESHOLD_VELOCITY = 500;
83 private static final int MIN_SNAP_VELOCITY = 1500;
84 private static final int MIN_FLING_VELOCITY = 250;
85
Adam Cohen21cd0022013-10-09 18:57:02 -070086 public static final int INVALID_RESTORE_PAGE = -1001;
87
Adam Cohenf358a4b2013-07-23 16:47:31 -070088 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070089
Adam Cohen265b9a62011-12-07 14:37:18 -080090 protected int mFlingThresholdVelocity;
91 protected int mMinFlingVelocity;
92 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected boolean mFirstLayout = true;
95
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -070098
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mNextPage = INVALID_PAGE;
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700101 protected int mMinScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800102 protected int mMaxScrollX;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700103 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800104 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800106 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
108 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700110 private float mLastMotionX;
111 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700112 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700113
Vadim Trysheve628c472018-05-16 14:28:18 -0700114 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700115 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800119 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka5f1c5092010-09-03 14:15:02 -0700121 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Michael Jurka5f1c5092010-09-03 14:15:02 -0700123 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700125 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700126
Jon Miranda71cb80c2019-01-24 21:25:13 -0800127 protected float mSpringOverScrollX;
128
Sunny Goyal061380a2016-02-29 15:15:03 -0800129 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700130
Adam Cohen8d769d62017-06-23 17:27:38 -0700131 protected int mUnboundedScrollX;
132
Winson Chungd2be3812013-07-16 11:11:32 -0700133 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700134 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800135 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700136
John Spurlock77e1f472013-09-11 10:09:51 -0400137 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800138 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400139
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800140 // Similar to the platform implementation of isLayoutValid();
141 protected boolean mIsLayoutValid;
142
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700143 private int[] mTmpIntPair = new int[2];
144
Winson Chung321e9ee2010-08-09 13:37:56 -0700145 public PagedView(Context context) {
146 this(context, null);
147 }
148
149 public PagedView(Context context, AttributeSet attrs) {
150 this(context, attrs, 0);
151 }
152
153 public PagedView(Context context, AttributeSet attrs, int defStyle) {
154 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700155
Adam Cohen9c4949e2010-10-05 12:27:22 -0700156 TypedArray a = context.obtainStyledAttributes(attrs,
157 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700158 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700159 a.recycle();
160
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700162 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700163 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 }
165
166 /**
167 * Initializes various states for this workspace.
168 */
Michael Jurka0142d492010-08-25 17:46:15 -0700169 protected void init() {
Sunny Goyalb7b01352018-08-09 12:31:20 -0700170 mScroller = new OverScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700171 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700172 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700173
174 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700176 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800177
Sunny Goyalcf25b522015-07-09 00:01:18 -0700178 float density = getResources().getDisplayMetrics().density;
179 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
180 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
181 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700182
183 if (Utilities.ATLEAST_OREO) {
184 setDefaultFocusHighlightEnabled(false);
185 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700186 }
187
Adam Cohenf9618852013-11-08 06:45:03 -0800188 protected void setDefaultInterpolator(Interpolator interpolator) {
189 mDefaultInterpolator = interpolator;
190 mScroller.setInterpolator(mDefaultInterpolator);
191 }
192
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700193 public void initParentViews(View parent) {
194 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800195 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700196 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700197 }
198 }
199
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800200 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700201 return mPageIndicator;
202 }
203
Adam Cohen674531f2013-12-13 15:07:14 -0800204 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700205 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
206 * that the user was on before entering free scroll mode (e.g. the home screen page they
207 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
208 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700210 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700211 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700213
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700214 /**
215 * Returns the index of page to be shown immediately afterwards.
216 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700217 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700218 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
219 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700220
Adam Cohenf9c184a2016-01-15 16:47:43 -0800221 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 return getChildCount();
223 }
224
Sunny Goyal83a8f042015-05-19 12:52:12 -0700225 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 return getChildAt(index);
227 }
228
Adam Cohenae4f1552011-10-20 00:15:42 -0700229 protected int indexToPage(int index) {
230 return index;
231 }
232
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800234 * Updates the scroll of the current page immediately to its final scroll position. We use this
235 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
236 * the previous tab page.
237 */
238 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700239 // If the current page is invalid, just reset the scroll position to zero
240 int newX = 0;
241 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700242 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700243 }
Sunny Goyalc82c6392018-06-06 15:39:13 -0700244 scrollTo(newX, 0);
Sunny Goyalb7b01352018-08-09 12:31:20 -0700245 mScroller.startScroll(mScroller.getCurrPos(), newX - mScroller.getCurrPos());
Sunny Goyalc82c6392018-06-06 15:39:13 -0700246 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800247 }
248
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700249 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700250 mScroller.abortAnimation();
251 // We need to clean up the next page here to avoid computeScrollHelper from
252 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700253 if (resetNextPage) {
254 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800255 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700256 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700257 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700258
Tony Wickham8f7ead32016-04-07 18:46:44 -0700259 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700260 mScroller.forceFinished(true);
261 // We need to clean up the next page here to avoid computeScrollHelper from
262 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700263 if (resetNextPage) {
264 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800265 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700266 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700267 }
268
Adam Cohen6f127a62014-06-12 14:54:41 -0700269 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700270 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700271 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700272 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700273 }
274
Chet Haasebc2f0822012-10-26 17:59:53 -0700275 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700276 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
277 */
278 private int ensureWithinScrollBounds(int page) {
279 int dir = !mIsRtl ? 1 : - 1;
280 int currScroll = getScrollForPage(page);
281 int prevScroll;
282 while (currScroll < mMinScrollX) {
283 page += dir;
284 prevScroll = currScroll;
285 currScroll = getScrollForPage(page);
286 if (currScroll <= prevScroll) {
287 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
288 break;
289 }
290 }
291 while (currScroll > mMaxScrollX) {
292 page -= dir;
293 prevScroll = currScroll;
294 currScroll = getScrollForPage(page);
295 if (currScroll >= prevScroll) {
296 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
297 break;
298 }
299 }
300 return page;
301 }
302
303 /**
Winson Chung86f77532010-08-24 11:08:22 -0700304 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700306 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800307 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700308 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800309 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800310 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
311 // the default
312 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800313 return;
314 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700315 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700316 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700317 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700318 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800319 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
321
Winson Chung8c87cd82013-07-23 16:20:10 -0700322 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800323 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
324 * has settled.
325 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700326 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800327 updatePageIndicator();
328 }
329
330 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700331 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000332 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700333 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700335 protected void pageBeginTransition() {
336 if (!mIsPageInTransition) {
337 mIsPageInTransition = true;
338 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700339 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700340 }
341
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700342 protected void pageEndTransition() {
343 if (mIsPageInTransition) {
344 mIsPageInTransition = false;
345 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700346 }
Michael Jurka0142d492010-08-25 17:46:15 -0700347 }
348
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700349 protected boolean isPageInTransition() {
350 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700351 }
352
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700353 /**
354 * Called when the page starts moving as part of the scroll. Subclasses can override this
355 * to provide custom behavior during animation.
356 */
357 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700358 }
359
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700360 /**
361 * Called when the page ends moving as part of the scroll. Subclasses can override this
362 * to provide custom behavior during animation.
363 */
364 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700365 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700366 }
367
Sunny Goyalc86df472016-02-25 09:19:38 -0800368 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700369 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800370 }
371
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800373 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800374 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800375 }
376
377 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700378 public void scrollTo(int x, int y) {
Adam Cohen8d769d62017-06-23 17:27:38 -0700379 mUnboundedScrollX = x;
380
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700381 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < mMinScrollX);
382 boolean isXAfterLastPage = mIsRtl ? (x < mMinScrollX) : (x > mMaxScrollX);
Jon Miranda71cb80c2019-01-24 21:25:13 -0800383
384 if (!isXBeforeFirstPage && !isXAfterLastPage) {
385 mSpringOverScrollX = 0;
386 }
387
Adam Cohen0ffac432013-07-10 11:19:26 -0700388 if (isXBeforeFirstPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700389 super.scrollTo(mIsRtl ? mMaxScrollX : mMinScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800390 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700391 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700392 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700393 overScroll(x - mMaxScrollX);
394 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700395 overScroll(x - mMinScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700396 }
Adam Cohen68d73932010-11-15 10:50:58 -0800397 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700398 } else if (isXAfterLastPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700399 super.scrollTo(mIsRtl ? mMinScrollX : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800400 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700401 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700402 if (mIsRtl) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700403 overScroll(x - mMinScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700404 } else {
405 overScroll(x - mMaxScrollX);
406 }
Adam Cohen68d73932010-11-15 10:50:58 -0800407 }
408 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700409 if (mWasInOverscroll) {
410 overScroll(0);
411 mWasInOverscroll = false;
412 }
Adam Cohen68d73932010-11-15 10:50:58 -0800413 super.scrollTo(x, y);
414 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700415
Michael Jurka0142d492010-08-25 17:46:15 -0700416 }
417
Adam Cohen53805212013-10-01 10:39:23 -0700418 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800419 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700420 if (mCurrentPage != getNextPage()) {
421 AccessibilityEvent ev =
422 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700423 ev.setScrollable(true);
424 ev.setScrollX(getScrollX());
425 ev.setScrollY(getScrollY());
426 ev.setMaxScrollX(mMaxScrollX);
427 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700428
Vadim Tryshevf4715972015-05-08 17:21:03 -0700429 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700430 }
Adam Cohen53805212013-10-01 10:39:23 -0700431 }
432 }
433
Michael Jurka0142d492010-08-25 17:46:15 -0700434 // we moved this functionality to a helper function so SmoothPagedView can reuse it
435 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800436 return computeScrollHelper(true);
437 }
438
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700439 protected void announcePageForAccessibility() {
440 if (isAccessibilityEnabled(getContext())) {
441 // Notify the user when the page changes
442 announceForAccessibility(getCurrentPageDescription());
443 }
444 }
445
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800446 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700448 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalb7b01352018-08-09 12:31:20 -0700449 if (getUnboundedScrollX() != mScroller.getCurrPos()
450 || getScrollX() != mScroller.getCurrPos()) {
451 scrollTo(mScroller.getCurrPos(), 0);
Winson Chung557d6ed2011-07-08 15:34:52 -0700452 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800453 if (shouldInvalidate) {
454 invalidate();
455 }
Michael Jurka0142d492010-08-25 17:46:15 -0700456 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800457 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700458 sendScrollAccessibilityEvent();
459
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700460 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700461 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700462 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700463 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700464
Adam Cohen73aa9752010-11-24 16:26:58 -0800465 // We don't want to trigger a page end moving unless the page has settled
466 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700467 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700468 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800469 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700470
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700471 if (canAnnouncePageDescription()) {
472 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700473 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
Michael Jurka0142d492010-08-25 17:46:15 -0700475 return false;
476 }
477
478 @Override
479 public void computeScroll() {
480 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 }
482
Sunny Goyalce8809a2018-01-18 14:02:47 -0800483 public int getExpectedHeight() {
484 return getMeasuredHeight();
485 }
486
Adam Cohen410f3cd2013-09-22 12:09:32 -0700487 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800488 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800489 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700490 }
491
Sunny Goyalce8809a2018-01-18 14:02:47 -0800492 public int getExpectedWidth() {
493 return getMeasuredWidth();
494 }
495
Sunny Goyalbc683e92017-12-06 10:25:07 -0800496 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800497 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800498 - mInsets.left - mInsets.right;
499 }
500
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800502 public void requestLayout() {
503 mIsLayoutValid = false;
504 super.requestLayout();
505 }
506
507 @Override
508 public void forceLayout() {
509 mIsLayoutValid = false;
510 super.forceLayout();
511 }
512
513 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700515 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700516 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
517 return;
518 }
519
Adam Cohen7d30a372013-07-01 17:03:59 -0700520 // We measure the dimensions of the PagedView to be larger than the pages so that when we
521 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700522 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
523 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
524 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700525 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700526
Adam Cohen7d30a372013-07-01 17:03:59 -0700527 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
528 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
529 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 }
531
Winson Chung8aad6102012-05-11 16:27:49 -0700532 // Return early if we aren't given a proper dimension
533 if (widthSize <= 0 || heightSize <= 0) {
534 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
535 return;
536 }
537
Winson Chung321e9ee2010-08-09 13:37:56 -0700538 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700539 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700540 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700541
Sunny Goyalac00cba2017-11-13 15:58:01 -0800542 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800543 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800544 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800545 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700546
Sunny Goyalac00cba2017-11-13 15:58:01 -0800547 // measureChildren takes accounts for content padding, we only need to care about extra
548 // space due to insets.
549 measureChildren(myWidthSpec, myHeightSpec);
550 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800551 }
552
Sunny Goyalcf25b522015-07-09 00:01:18 -0700553 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700555 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800556 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700557 final int childCount = getChildCount();
558 boolean pageScrollChanged = false;
559 if (mPageScrolls == null || childCount != mPageScrolls.length) {
560 mPageScrolls = new int[childCount];
561 pageScrollChanged = true;
562 }
563
564 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 return;
566 }
567
Winson Chung785d2eb2011-04-14 16:08:02 -0700568 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700569
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700570 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
571 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700573
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700574 final LayoutTransition transition = getLayoutTransition();
575 // If the transition is running defer updating max scroll, as some empty pages could
576 // still be present, and a max scroll change could cause sudden jumps in scroll.
577 if (transition != null && transition.isRunning()) {
578 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
579
580 @Override
581 public void startTransition(LayoutTransition transition, ViewGroup container,
582 View view, int transitionType) { }
583
584 @Override
585 public void endTransition(LayoutTransition transition, ViewGroup container,
586 View view, int transitionType) {
587 // Wait until all transitions are complete.
588 if (!transition.isRunning()) {
589 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700590 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700591 }
592 }
593 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700594 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700595 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700596 }
597
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700598 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
599 updateCurrentPageScroll();
600 mFirstLayout = false;
601 }
602
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800603 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700604 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700605 }
Winson Chunge3193b92010-09-10 11:44:42 -0700606 }
607
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700608 /**
609 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
610 * of {@code outPageScrolls} should be same as the the childCount
611 *
612 */
613 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
614 ComputePageScrollsLogic scrollLogic) {
615 final int childCount = getChildCount();
616
617 final int startIndex = mIsRtl ? childCount - 1 : 0;
618 final int endIndex = mIsRtl ? -1 : childCount;
619 final int delta = mIsRtl ? -1 : 1;
620
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700621 final int verticalCenter = (getPaddingTop() + getMeasuredHeight() + mInsets.top
622 - mInsets.bottom - getPaddingBottom()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700623
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700624 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700625 final int scrollOffsetRight = getWidth() - getPaddingRight() - mInsets.right;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700626 boolean pageScrollChanged = false;
627
Sunny Goyalc82c6392018-06-06 15:39:13 -0700628 for (int i = startIndex, childLeft = scrollOffsetLeft; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700629 final View child = getPageAt(i);
630 if (scrollLogic.shouldIncludeView(child)) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700631 final int childWidth = child.getMeasuredWidth();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700632 final int childRight = childLeft + childWidth;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700633
634 if (layoutChildren) {
635 final int childHeight = child.getMeasuredHeight();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700636 final int childTop = verticalCenter - childHeight / 2;
637 child.layout(childLeft, childTop, childRight, childTop + childHeight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700638 }
639
Sunny Goyalc82c6392018-06-06 15:39:13 -0700640 // In case the pages are of different width, align the page to left or right edge
641 // based on the orientation.
642 final int pageScroll = mIsRtl
643 ? (childLeft - scrollOffsetLeft)
644 : Math.max(0, childRight - scrollOffsetRight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700645 if (outPageScrolls[i] != pageScroll) {
646 pageScrollChanged = true;
647 outPageScrolls[i] = pageScroll;
648 }
649
650 childLeft += childWidth + mPageSpacing + getChildGap();
651 }
652 }
653 return pageScrollChanged;
654 }
655
Sunny Goyala1fbd842015-05-20 13:40:27 -0700656 protected int getChildGap() {
657 return 0;
658 }
659
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700660 protected void updateMinAndMaxScrollX() {
661 mMinScrollX = computeMinScrollX();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700662 mMaxScrollX = computeMaxScrollX();
663 }
664
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700665 protected int computeMinScrollX() {
666 return 0;
Tony50876bf2018-09-19 23:09:14 -0400667 }
668
Tony Wickhamf549dab2016-05-16 09:54:06 -0700669 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700670 int childCount = getChildCount();
671 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700672 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700673 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700674 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700675 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700676 }
677 }
678
Adam Cohen3b185e22013-10-29 14:45:58 -0700679 public void setPageSpacing(int pageSpacing) {
680 mPageSpacing = pageSpacing;
681 requestLayout();
682 }
683
Tony50876bf2018-09-19 23:09:14 -0400684 public int getPageSpacing() {
685 return mPageSpacing;
686 }
687
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800688 private void dispatchPageCountChanged() {
689 if (mPageIndicator != null) {
690 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700691 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700692 // This ensures that when children are added, they get the correct transforms / alphas
693 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700694 invalidate();
695 }
696
Michael Jurka8b805b12012-04-18 14:23:14 -0700697 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800698 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800699 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800700 dispatchPageCountChanged();
701 }
702
703 @Override
704 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800705 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700706 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800707 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700708 }
709
Adam Cohen73894962011-10-31 13:17:17 -0700710 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700711 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800712 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700713 }
714
Winson Chung321e9ee2010-08-09 13:37:56 -0700715 @Override
716 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700717 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700718 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700719 if (immediate) {
720 setCurrentPage(page);
721 } else {
722 snapToPage(page);
723 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 return true;
725 }
726 return false;
727 }
728
729 @Override
730 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700731 int focusablePage;
732 if (mNextPage != INVALID_PAGE) {
733 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700735 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 }
Winson Chung86f77532010-08-24 11:08:22 -0700737 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700739 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741 return false;
742 }
743
744 @Override
745 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800746 if (super.dispatchUnhandledMove(focused, direction)) {
747 return true;
748 }
749
750 if (mIsRtl) {
751 if (direction == View.FOCUS_LEFT) {
752 direction = View.FOCUS_RIGHT;
753 } else if (direction == View.FOCUS_RIGHT) {
754 direction = View.FOCUS_LEFT;
755 }
756 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700758 if (getCurrentPage() > 0) {
759 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700760 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700761 return true;
762 }
763 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700764 if (getCurrentPage() < getPageCount() - 1) {
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 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800770 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 }
772
773 @Override
774 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700775 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
776 return;
777 }
778
Adam Cohen0ffac432013-07-10 11:19:26 -0700779 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700780 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700781 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 }
783 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700784 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700785 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 }
787 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700788 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700789 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 }
791 }
792 }
793
794 /**
795 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700796 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 *
Winson Chung86f77532010-08-24 11:08:22 -0700798 * This happens when live folders requery, and if they're off page, they
799 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 */
801 @Override
802 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700803 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 View v = focused;
805 while (true) {
806 if (v == current) {
807 super.focusableViewAvailable(focused);
808 return;
809 }
810 if (v == this) {
811 return;
812 }
813 ViewParent parent = v.getParent();
814 if (parent instanceof View) {
815 v = (View)v.getParent();
816 } else {
817 return;
818 }
819 }
820 }
821
822 /**
823 * {@inheritDoc}
824 */
825 @Override
826 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
827 if (disallowIntercept) {
828 // We need to make sure to cancel our long press if
829 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700830 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700831 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 }
833 super.requestDisallowInterceptTouchEvent(disallowIntercept);
834 }
835
836 @Override
837 public boolean onInterceptTouchEvent(MotionEvent ev) {
838 /*
839 * This method JUST determines whether we want to intercept the motion.
840 * If we return true, onTouchEvent will be called and we do the actual
841 * scrolling there.
842 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800843 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700844
Winson Chung45e1d6e2010-11-09 17:19:49 -0800845 // Skip touch handling if there are no pages to swipe
846 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
847
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 /*
849 * Shortcut the most recurring case: the user is in the dragging
850 * state and he is moving his finger. We want to intercept this
851 * motion.
852 */
853 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700854 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 return true;
856 }
857
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 switch (action & MotionEvent.ACTION_MASK) {
859 case MotionEvent.ACTION_MOVE: {
860 /*
861 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
862 * whether the user has moved far enough from his original down touch.
863 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700864 if (mActivePointerId != INVALID_POINTER) {
865 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700866 }
867 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
868 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
869 // i.e. fall through to the next case (don't break)
870 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
871 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700872 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 }
874
875 case MotionEvent.ACTION_DOWN: {
876 final float x = ev.getX();
877 final float y = ev.getY();
878 // Remember location of down touch
879 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700880 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800882 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800883 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700885
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 /*
887 * If being flinged and user touches the screen, initiate drag;
888 * otherwise don't. mScroller.isFinished should be false when
889 * being flinged.
890 */
Sunny Goyalb7b01352018-08-09 12:31:20 -0700891 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Adam Cohena7652152013-11-18 20:06:55 +0000892 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800893
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700894 if (finishedScrolling) {
Sunny Goyalaad33592018-08-07 17:20:35 -0700895 mIsBeingDragged = false;
Adam Cohen59b5c792013-12-04 16:09:07 -0800896 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000897 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700898 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800899 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700900 } else {
Tony Wickham44a58882019-04-09 14:24:33 -0700901 mIsBeingDragged = true;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700902 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700903
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 break;
905 }
906
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800908 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700909 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 break;
911
912 case MotionEvent.ACTION_POINTER_UP:
913 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800914 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 break;
916 }
917
918 /*
919 * The only time we want to intercept motion events is if we are in the
920 * drag mode.
921 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700922 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 }
924
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800925 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700926 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800927 }
928
Adam Cohenf8d28232011-02-01 21:47:00 -0800929 protected void determineScrollingStart(MotionEvent ev) {
930 determineScrollingStart(ev, 1.0f);
931 }
932
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 /*
934 * Determines if we should change the touch state to start scrolling after the
935 * user moves their touch point too far.
936 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800937 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700938 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700940 if (pointerIndex == -1) return;
941
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 final float x = ev.getX(pointerIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 final int xDiff = (int) Math.abs(x - mLastMotionX);
Adam Cohenf8d28232011-02-01 21:47:00 -0800944 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700946
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700947 if (xMoved) {
948 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700949 mIsBeingDragged = true;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700950 mTotalMotionX += Math.abs(mLastMotionX - x);
951 mLastMotionX = x;
952 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700953 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700954 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800955 // Stop listening for things like pinches.
956 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800957 }
958 }
959
960 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700961 // Try canceling the long press. It could also have been scheduled
962 // by a distant descendant, so use the mAllowLongPress flag to block
963 // everything
964 final View currentPage = getPageAt(mCurrentPage);
965 if (currentPage != null) {
966 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 }
968 }
969
Adam Cohenb5ba0972011-09-07 18:02:31 -0700970 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800971 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700972
Adam Cohenedb40762013-07-18 16:45:45 -0700973 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700974 int count = getChildCount();
975
976 final int totalDistance;
977
978 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700979 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700980 adjacentPage = page - 1;
981 }
982
983 if (adjacentPage < 0 || adjacentPage > count - 1) {
984 totalDistance = v.getMeasuredWidth() + mPageSpacing;
985 } else {
986 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
987 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700988
989 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700990 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
991 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700992 return scrollProgress;
993 }
994
Adam Cohenedb40762013-07-18 16:45:45 -0700995 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700996 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -0700997 return 0;
998 } else {
999 return mPageScrolls[index];
1000 }
1001 }
1002
Adam Cohen564a2e72013-10-09 14:47:32 -07001003 // While layout transitions are occurring, a child's position may stray from its baseline
1004 // position. This method returns the magnitude of this stray at any given time.
1005 public int getLayoutTransitionOffsetForPage(int index) {
1006 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1007 return 0;
1008 } else {
1009 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001010
Sunny Goyal228153d2018-01-04 15:35:22 -08001011 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1012 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001013 return (int) (child.getX() - baselineX);
1014 }
1015 }
1016
Jon Miranda71cb80c2019-01-24 21:25:13 -08001017 @Override
1018 protected void dispatchDraw(Canvas canvas) {
1019 if (mScroller.isSpringing() && mSpringOverScrollX != 0) {
1020 int saveCount = canvas.save();
1021
1022 canvas.translate(-mSpringOverScrollX, 0);
1023 super.dispatchDraw(canvas);
1024
1025 canvas.restoreToCount(saveCount);
1026 } else {
1027 super.dispatchDraw(canvas);
1028 }
1029 }
1030
Sunny Goyalb7b01352018-08-09 12:31:20 -07001031 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001032 mSpringOverScrollX = amount;
1033 if (amount == 0) {
1034 return;
1035 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001036
Sunny Goyal228153d2018-01-04 15:35:22 -08001037 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Jon Miranda71cb80c2019-01-24 21:25:13 -08001038 mSpringOverScrollX = overScrollAmount;
1039 if (mScroller.isSpringing()) {
1040 invalidate();
1041 return;
1042 }
1043
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001044 int x = Utilities.boundToRange(getScrollX(), mMinScrollX, mMaxScrollX);
1045 super.scrollTo(x + overScrollAmount, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001046 invalidate();
1047 }
1048
Sunny Goyalb7b01352018-08-09 12:31:20 -07001049 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001050 mSpringOverScrollX = amount;
1051 if (mScroller.isSpringing()) {
1052 invalidate();
1053 return;
1054 }
1055
Sunny Goyalb7b01352018-08-09 12:31:20 -07001056 if (amount == 0) return;
1057
1058 if (mFreeScroll && !mScroller.isFinished()) {
1059 if (amount < 0) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001060 super.scrollTo(mMinScrollX + amount, getScrollY());
Sunny Goyalb7b01352018-08-09 12:31:20 -07001061 } else {
1062 super.scrollTo(mMaxScrollX + amount, getScrollY());
1063 }
1064 } else {
1065 dampedOverScroll(amount);
1066 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001067 }
1068
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001069
Tony Wickham023f4042019-01-29 10:52:31 -08001070 public void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001071 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001072 mFreeScroll = freeScroll;
1073
Adam Cohenf9618852013-11-08 06:45:03 -08001074 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001075 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001076 } else if (wasFreeScroll) {
1077 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001078 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001079 }
1080
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001081 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001082 mAllowOverScroll = enable;
1083 }
1084
Winson Chung321e9ee2010-08-09 13:37:56 -07001085 @Override
1086 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001087 super.onTouchEvent(ev);
1088
Winson Chung45e1d6e2010-11-09 17:19:49 -08001089 // Skip touch handling if there are no pages to swipe
1090 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1091
Michael Jurkab8f06722010-10-10 15:58:46 -07001092 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001093
1094 final int action = ev.getAction();
1095
1096 switch (action & MotionEvent.ACTION_MASK) {
1097 case MotionEvent.ACTION_DOWN:
1098 /*
1099 * If being flinged and user touches, stop the fling. isFinished
1100 * will be false if being flinged.
1101 */
1102 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001103 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001104 }
1105
1106 // Remember where the motion event started
1107 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001108 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001109 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001110 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001112
Sunny Goyalaad33592018-08-07 17:20:35 -07001113 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001115 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001116 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 break;
1118
1119 case MotionEvent.ACTION_MOVE:
Sunny Goyalaad33592018-08-07 17:20:35 -07001120 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 // Scroll to follow the motion event
1122 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001123
1124 if (pointerIndex == -1) return true;
1125
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001127 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001128
Adam Cohenaefd4e12011-02-14 16:39:38 -08001129 mTotalMotionX += Math.abs(deltaX);
1130
Winson Chungc0844aa2011-02-02 15:25:58 -08001131 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1132 // keep the remainder because we are actually testing if we've moved from the last
1133 // scrolled position (which is discrete).
1134 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001135 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001136 mLastMotionX = x;
1137 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001138 } else {
1139 awakenScrollBars();
1140 }
Adam Cohen564976a2010-10-13 18:52:07 -07001141 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 determineScrollingStart(ev);
1143 }
1144 break;
1145
1146 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001147 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 final int activePointerId = mActivePointerId;
1149 final int pointerIndex = ev.findPointerIndex(activePointerId);
1150 final float x = ev.getX(pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001151 final VelocityTracker velocityTracker = mVelocityTracker;
1152 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1153 int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001154 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001155 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001156 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1157 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001158
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001159 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001160 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001161 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1162 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001163
Adam Cohenf358a4b2013-07-23 16:47:31 -07001164 if (!mFreeScroll) {
1165 // In the case that the page is moved far to one direction and then is flung
1166 // in the opposite direction, we use a threshold to determine whether we should
1167 // just return to the starting page, or if we should skip one further.
1168 boolean returnToOriginalPage = false;
1169 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1170 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1171 returnToOriginalPage = true;
1172 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001173
Adam Cohenf358a4b2013-07-23 16:47:31 -07001174 int finalPage;
1175 // We give flings precedence over large moves, which is why we short-circuit our
1176 // test for a large move if a fling has been registered. That is, a large
1177 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001178
Adam Cohenf358a4b2013-07-23 16:47:31 -07001179 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1180 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1181 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1182 snapToPageWithVelocity(finalPage, velocityX);
1183 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1184 (isFling && isVelocityXLeft)) &&
1185 mCurrentPage < getChildCount() - 1) {
1186 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1187 snapToPageWithVelocity(finalPage, velocityX);
1188 } else {
1189 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001190 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001192 if (!mScroller.isFinished()) {
1193 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001194 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001195
Sunny Goyalb7b01352018-08-09 12:31:20 -07001196 int initialScrollX = getScrollX();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001197
Sunny Goyalb7b01352018-08-09 12:31:20 -07001198 if (((initialScrollX >= mMaxScrollX) && (isVelocityXLeft || !isFling)) ||
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001199 ((initialScrollX <= mMinScrollX) && (!isVelocityXLeft || !isFling))) {
1200 mScroller.springBack(getScrollX(), mMinScrollX, mMaxScrollX);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001201 } else {
1202 mScroller.setInterpolator(mDefaultInterpolator);
1203 mScroller.fling(initialScrollX, -velocityX,
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001204 mMinScrollX, mMaxScrollX,
Sunny Goyalb7b01352018-08-09 12:31:20 -07001205 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001206
Sunny Goyalb7b01352018-08-09 12:31:20 -07001207 int finalX = mScroller.getFinalPos();
1208 mNextPage = getPageNearestToCenterOfScreen(finalX);
1209
1210 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1211 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001212 if (finalX > mMinScrollX && finalX < mMaxScrollX) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001213 // If scrolling ends in the half of the added space that is closer to
1214 // the end, settle to the end. Otherwise snap to the nearest page.
1215 // If flinging past one of the ends, don't change the velocity as it
1216 // will get stopped at the end anyway.
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001217 int pageSnappedX = finalX < (firstPageScroll + mMinScrollX) / 2
1218 ? mMinScrollX
Sunny Goyalb7b01352018-08-09 12:31:20 -07001219 : finalX > (lastPageScroll + mMaxScrollX) / 2
1220 ? mMaxScrollX
1221 : getScrollForPage(mNextPage);
1222
1223 mScroller.setFinalPos(pageSnappedX);
1224 // Ensure the scroll/snap doesn't happen too fast;
1225 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1226 - mScroller.getDuration();
1227 if (extraScrollDuration > 0) {
1228 mScroller.extendDuration(extraScrollDuration);
1229 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001230 }
1231 }
1232 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001234 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001236
Adam Cohen7d30a372013-07-01 17:03:59 -07001237 // End any intermediate reordering states
1238 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 break;
1240
1241 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001242 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001243 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001244 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001245 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001246 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 break;
1248
1249 case MotionEvent.ACTION_POINTER_UP:
1250 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 break;
1253 }
1254
1255 return true;
1256 }
1257
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001258 protected boolean shouldFlingForVelocity(int velocityX) {
1259 return Math.abs(velocityX) > mFlingThresholdVelocity;
1260 }
1261
Adam Cohen7d30a372013-07-01 17:03:59 -07001262 private void resetTouchState() {
1263 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001264 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001265 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001266 }
1267
Adam Cohenc2d6e892014-10-16 09:49:24 -07001268 /**
1269 * Triggered by scrolling via touch
1270 */
1271 protected void onScrollInteractionBegin() {
1272 }
1273
1274 protected void onScrollInteractionEnd() {
1275 }
1276
Winson Chung185d7162011-02-28 13:47:29 -08001277 @Override
1278 public boolean onGenericMotionEvent(MotionEvent event) {
1279 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1280 switch (event.getAction()) {
1281 case MotionEvent.ACTION_SCROLL: {
1282 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1283 final float vscroll;
1284 final float hscroll;
1285 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1286 vscroll = 0;
1287 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1288 } else {
1289 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1290 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1291 }
1292 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001293 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001294 : (hscroll > 0 || vscroll > 0);
1295 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001296 scrollRight();
1297 } else {
1298 scrollLeft();
1299 }
1300 return true;
1301 }
1302 }
1303 }
1304 }
1305 return super.onGenericMotionEvent(event);
1306 }
1307
Michael Jurkab8f06722010-10-10 15:58:46 -07001308 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1309 if (mVelocityTracker == null) {
1310 mVelocityTracker = VelocityTracker.obtain();
1311 }
1312 mVelocityTracker.addMovement(ev);
1313 }
1314
1315 private void releaseVelocityTracker() {
1316 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001317 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001318 mVelocityTracker.recycle();
1319 mVelocityTracker = null;
1320 }
1321 }
1322
Winson Chung321e9ee2010-08-09 13:37:56 -07001323 private void onSecondaryPointerUp(MotionEvent ev) {
1324 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1325 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1326 final int pointerId = ev.getPointerId(pointerIndex);
1327 if (pointerId == mActivePointerId) {
1328 // This was our active pointer going up. Choose a new
1329 // active pointer and adjust accordingly.
1330 // TODO: Make this decision more intelligent.
1331 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1332 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001333 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001334 mActivePointerId = ev.getPointerId(newPointerIndex);
1335 if (mVelocityTracker != null) {
1336 mVelocityTracker.clear();
1337 }
1338 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001339 }
1340
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 @Override
1342 public void requestChildFocus(View child, View focused) {
1343 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001344 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001345 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001346 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 }
1348 }
1349
Sunny Goyal228153d2018-01-04 15:35:22 -08001350 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001351 return getPageNearestToCenterOfScreen(getScrollX());
1352 }
1353
1354 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001355 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001356 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 final int childCount = getChildCount();
1359 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001360 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001361 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001363 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1365 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1366 minDistanceFromScreenCenter = distanceFromScreenCenter;
1367 minDistanceFromScreenCenterIndex = i;
1368 }
1369 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001370 return minDistanceFromScreenCenterIndex;
1371 }
1372
1373 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001374 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1375 }
1376
1377 protected boolean isInOverScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -07001378 return (getScrollX() > mMaxScrollX || getScrollX() < mMinScrollX);
Adam Cohen8d769d62017-06-23 17:27:38 -07001379 }
1380
1381 protected int getPageSnapDuration() {
1382 if (isInOverScroll()) {
1383 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1384 }
1385 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 }
1387
Adam Cohene0f66b52010-11-23 15:06:07 -08001388 // We want the duration of the page snap animation to be influenced by the distance that
1389 // the screen has to travel, however, we don't want this duration to be effected in a
1390 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1391 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001392 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001393 f -= 0.5f; // center the values about 0.
1394 f *= 0.3f * Math.PI / 2.0f;
1395 return (float) Math.sin(f);
1396 }
1397
Winson Chung05a31ed2018-02-08 17:08:43 -08001398 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001399 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001400 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001401
Adam Cohenedb40762013-07-18 16:45:45 -07001402 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001403 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001404 int duration = 0;
1405
Sunny Goyal4d113a52015-05-27 10:05:28 -07001406 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001407 // If the velocity is low enough, then treat this more as an automatic page advance
1408 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001409 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001410 }
1411
1412 // Here we compute a "distance" that will be used in the computation of the overall
1413 // snap duration. This is a function of the actual distance that needs to be traveled;
1414 // we keep this value close to half screen size in order to reduce the variance in snap
1415 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001416 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001417 float distance = halfScreenSize + halfScreenSize *
1418 distanceInfluenceForSnapDuration(distanceRatio);
1419
1420 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001421 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001422
1423 // we want the page's snap velocity to approximately match the velocity at which the
1424 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001425 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1426 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001427
Jon Miranda71cb80c2019-01-24 21:25:13 -08001428 if (QUICKSTEP_SPRINGS.get()) {
1429 return snapToPage(whichPage, delta, duration, false, null,
1430 velocity * Math.signum(newX - getUnboundedScrollX()), true);
1431 } else {
1432 return snapToPage(whichPage, delta, duration);
1433 }
Michael Jurka0142d492010-08-25 17:46:15 -07001434 }
1435
Winson Chung05a31ed2018-02-08 17:08:43 -08001436 public boolean snapToPage(int whichPage) {
1437 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 }
1439
Winson Chung05a31ed2018-02-08 17:08:43 -08001440 public boolean snapToPageImmediately(int whichPage) {
1441 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 }
1443
Winson Chung05a31ed2018-02-08 17:08:43 -08001444 public boolean snapToPage(int whichPage, int duration) {
1445 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001446 }
1447
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001448 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001449 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001450 }
1451
Winson Chung05a31ed2018-02-08 17:08:43 -08001452 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001453 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001454 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001455
Adam Cohenedb40762013-07-18 16:45:45 -07001456 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001457 final int delta = newX - getUnboundedScrollX();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001458 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001459 }
1460
Winson Chung05a31ed2018-02-08 17:08:43 -08001461 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001462 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001463 }
Michael Jurka0142d492010-08-25 17:46:15 -07001464
Winson Chung05a31ed2018-02-08 17:08:43 -08001465 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001466 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001467 if (mFirstLayout) {
1468 setCurrentPage(whichPage);
1469 return false;
1470 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001471
1472 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001473 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1474 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001475 }
1476
Adam Cohen6f127a62014-06-12 14:54:41 -07001477 whichPage = validateNewPage(whichPage);
1478
Adam Cohen7d30a372013-07-01 17:03:59 -07001479 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001480
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001482 if (immediate) {
1483 duration = 0;
1484 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 duration = Math.abs(delta);
1486 }
1487
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001488 if (duration != 0) {
1489 pageBeginTransition();
1490 }
1491
Adam Cohenf358a4b2013-07-23 16:47:31 -07001492 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001493 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001494 }
Adam Cohenf9618852013-11-08 06:45:03 -08001495
1496 if (interpolator != null) {
1497 mScroller.setInterpolator(interpolator);
1498 } else {
1499 mScroller.setInterpolator(mDefaultInterpolator);
1500 }
1501
Jon Miranda71cb80c2019-01-24 21:25:13 -08001502 if (spring && QUICKSTEP_SPRINGS.get()) {
1503 mScroller.startScrollSpring(getUnboundedScrollX(), delta, duration, velocity);
1504 } else {
1505 mScroller.startScroll(getUnboundedScrollX(), delta, duration);
1506 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001507
Adam Cohen674531f2013-12-13 15:07:14 -08001508 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001509
1510 // Trigger a compute() to finish switching pages if necessary
1511 if (immediate) {
1512 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001513 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 }
1515
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001517 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 }
1519
Vadim Tryshev98913d02018-05-18 18:41:34 -07001520 public boolean scrollLeft() {
1521 if (getNextPage() > 0) {
1522 snapToPage(getNextPage() - 1);
1523 return true;
1524 }
1525 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001526 }
1527
Vadim Tryshev98913d02018-05-18 18:41:34 -07001528 public boolean scrollRight() {
1529 if (getNextPage() < getChildCount() - 1) {
1530 snapToPage(getNextPage() + 1);
1531 return true;
1532 }
1533 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 }
1535
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001536 @Override
1537 public CharSequence getAccessibilityClassName() {
1538 // Some accessibility services have special logic for ScrollView. Since we provide same
1539 // accessibility info as ScrollView, inform the service to handle use the same way.
1540 return ScrollView.class.getName();
1541 }
1542
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001543 protected boolean isPageOrderFlipped() {
1544 return false;
1545 }
1546
Winson Chung6a0f57d2011-06-29 20:10:49 -07001547 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001548 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001549 @Override
1550 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1551 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001552 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001553 info.setScrollable(getPageCount() > 1);
1554 if (getCurrentPage() < getPageCount() - 1) {
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001555 info.addAction(pagesFlipped ? AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
1556 : AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001557 }
1558 if (getCurrentPage() > 0) {
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001559 info.addAction(pagesFlipped ? AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
1560 : AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001561 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001562
1563 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1564 // Besides disabling the accessibility long-click, this also prevents this view from getting
1565 // accessibility focus.
1566 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001567 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001568 }
1569
1570 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001571 public void sendAccessibilityEvent(int eventType) {
1572 // Don't let the view send real scroll events.
1573 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1574 super.sendAccessibilityEvent(eventType);
1575 }
1576 }
1577
1578 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001579 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1580 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001581 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001582 }
1583
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001584 @Override
1585 public boolean performAccessibilityAction(int action, Bundle arguments) {
1586 if (super.performAccessibilityAction(action, arguments)) {
1587 return true;
1588 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001589 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001590 switch (action) {
1591 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001592 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001593 return true;
1594 }
1595 } break;
1596 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001597 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001598 return true;
1599 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001600 }
1601 break;
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001602 }
1603 return false;
1604 }
1605
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001606 protected boolean canAnnouncePageDescription() {
1607 return true;
1608 }
1609
Adam Cohen0ffac432013-07-10 11:19:26 -07001610 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001611 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001612 getNextPage() + 1, getChildCount());
1613 }
1614
Tony Mak2e564402018-02-27 17:19:34 +00001615 protected float getDownMotionX() {
1616 return mDownMotionX;
1617 }
1618
1619 protected float getDownMotionY() {
1620 return mDownMotionY;
1621 }
1622
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001623 protected interface ComputePageScrollsLogic {
1624
1625 boolean shouldIncludeView(View view);
1626 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001627
1628 public int[] getVisibleChildrenRange() {
1629 float visibleLeft = 0;
1630 float visibleRight = visibleLeft + getMeasuredWidth();
1631 float scaleX = getScaleX();
1632 if (scaleX < 1 && scaleX > 0) {
1633 float mid = getMeasuredWidth() / 2;
1634 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1635 visibleRight = mid + ((visibleRight - mid) / scaleX);
1636 }
1637
1638 int leftChild = -1;
1639 int rightChild = -1;
1640 final int childCount = getChildCount();
1641 for (int i = 0; i < childCount; i++) {
1642 final View child = getPageAt(i);
1643
1644 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1645 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1646 if (leftChild == -1) {
1647 leftChild = i;
1648 }
1649 rightChild = i;
1650 }
1651 }
1652 mTmpIntPair[0] = leftChild;
1653 mTmpIntPair[1] = rightChild;
1654 return mTmpIntPair;
1655 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001656}