blob: 7496703f888458328304967db9670ca992936146 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyal8b37c572020-03-31 12:12:14 -070019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
22import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
23import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE;
24import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
25import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
26
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070027import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070031import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080032import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070034import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070035import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
48import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070050import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
Tony Wickham62117d72020-03-26 16:56:26 -070052import androidx.annotation.Nullable;
53
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.anim.Interpolators;
55import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
58import com.android.launcher3.touch.OverScroll;
59import com.android.launcher3.touch.PagedOrientationHandler;
60import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Jon Miranda9e198662020-03-11 14:42:02 -070061import com.android.launcher3.util.OverScroller;
62import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070063import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070064
65import java.util.ArrayList;
Andras Kloczl2bb51342021-03-20 01:13:45 +010066import java.util.List;
Jon Miranda9e198662020-03-11 14:42:02 -070067
Winson Chung321e9ee2010-08-09 13:37:56 -070068/**
69 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070070 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070071 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080072public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070073 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070074 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070075 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070076
Winson Chung4941f652020-06-18 15:11:53 -070077 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053078 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070079 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Vadim Tryshevfedca432015-08-19 17:55:02 -070081 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070082
Sunny Goyalb72d8b22017-07-14 00:02:27 -070083 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070084 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070085
Adam Cohenb64cb5a2011-02-15 13:53:42 -080086 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080087 // The page is moved more than halfway, automatically move to the next page on touch up.
88 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080089
Sunny Goyal8e2133b2015-05-06 13:39:07 -070090 private static final float MAX_SCROLL_PROGRESS = 1.0f;
91
Adam Cohen265b9a62011-12-07 14:37:18 -080092 // The following constants need to be scaled based on density. The scaled versions will be
93 // assigned to the corresponding member variables below.
94 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070095 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080096 private static final int MIN_SNAP_VELOCITY = 1500;
97 private static final int MIN_FLING_VELOCITY = 250;
98
Adam Cohenf358a4b2013-07-23 16:47:31 -070099 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700100
Winson Chung4941f652020-06-18 15:11:53 -0700101 protected final int mFlingThresholdVelocity;
102 protected final int mEasyFlingThresholdVelocity;
103 protected final int mMinFlingVelocity;
104 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700105
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected boolean mFirstLayout = true;
107
Sunny Goyal4ffec482016-02-09 11:28:52 -0800108 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700109 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700110
Sunny Goyal4ffec482016-02-09 11:28:52 -0800111 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800113 protected int mMaxScroll;
114 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700115 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800116 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800118 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
120 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700121 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800122 private float mDownMotionPrimary;
123 private float mLastMotion;
124 private float mLastMotionRemainder;
125 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700126 // Used in special cases where the fling checks can be relaxed for an intentional gesture
127 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700128 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700129
Vadim Trysheve628c472018-05-16 14:28:18 -0700130 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700131 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800132
Winson Chung4941f652020-06-18 15:11:53 -0700133 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700135 // The amount of movement to begin paging
136 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800138 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700144 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700145 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700146
Vinit Nayaka406f722019-12-19 11:50:55 -0800147 protected float mSpringOverScroll;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800148
Sunny Goyal061380a2016-02-29 15:15:03 -0800149 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700150
Vinit Nayaka406f722019-12-19 11:50:55 -0800151 protected int mUnboundedScroll;
152
Winson Chungd2be3812013-07-16 11:11:32 -0700153 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800155 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700156
John Spurlock77e1f472013-09-11 10:09:51 -0400157 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800158 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400159
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800160 // Similar to the platform implementation of isLayoutValid();
161 protected boolean mIsLayoutValid;
162
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700163 private int[] mTmpIntPair = new int[2];
164
Winson Chung321e9ee2010-08-09 13:37:56 -0700165 public PagedView(Context context) {
166 this(context, null);
167 }
168
169 public PagedView(Context context, AttributeSet attrs) {
170 this(context, attrs, 0);
171 }
172
173 public PagedView(Context context, AttributeSet attrs, int defStyle) {
174 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700175
Adam Cohen9c4949e2010-10-05 12:27:22 -0700176 TypedArray a = context.obtainStyledAttributes(attrs,
177 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700178 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700179 a.recycle();
180
Winson Chung321e9ee2010-08-09 13:37:56 -0700181 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700182 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Vinit Nayaka406f722019-12-19 11:50:55 -0800184 mScroller = new OverScroller(context);
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700185 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700186 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700187
Vinit Nayaka406f722019-12-19 11:50:55 -0800188 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700189 mTouchSlop = configuration.getScaledTouchSlop();
190 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800192
Sunny Goyalcf25b522015-07-09 00:01:18 -0700193 float density = getResources().getDisplayMetrics().density;
194 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700195 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700196 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
197 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700198
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700199 setDefaultFocusHighlightEnabled(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
Adam Cohenf9618852013-11-08 06:45:03 -0800202 protected void setDefaultInterpolator(Interpolator interpolator) {
203 mDefaultInterpolator = interpolator;
204 mScroller.setInterpolator(mDefaultInterpolator);
205 }
206
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700207 public void initParentViews(View parent) {
208 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800209 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700210 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700211 }
212 }
213
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800214 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700215 return mPageIndicator;
216 }
217
Adam Cohen674531f2013-12-13 15:07:14 -0800218 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700219 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
220 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000221 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700222 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700224 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700225 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700227
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700228 /**
229 * Returns the index of page to be shown immediately afterwards.
230 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700231 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700232 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
233 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700234
Adam Cohenf9c184a2016-01-15 16:47:43 -0800235 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Sunny Goyal83a8f042015-05-19 12:52:12 -0700239 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
Adam Cohenae4f1552011-10-20 00:15:42 -0700243 protected int indexToPage(int index) {
244 return index;
245 }
246
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 * Updates the scroll of the current page immediately to its final scroll position. We use this
249 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
250 * the previous tab page.
251 */
252 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700253 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800254 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700255 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800256 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700257 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800258 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
259 mOrientationHandler.scrollerStartScroll(mScroller, newPosition);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700260 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800261 }
262
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700263 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700264 mScroller.abortAnimation();
265 // We need to clean up the next page here to avoid computeScrollHelper from
266 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700267 if (resetNextPage) {
268 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800269 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700270 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700271 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700272
Tony Wickham8f7ead32016-04-07 18:46:44 -0700273 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700274 mScroller.forceFinished(true);
275 // We need to clean up the next page here to avoid computeScrollHelper from
276 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700277 if (resetNextPage) {
278 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800279 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700280 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700281 }
282
Adam Cohen6f127a62014-06-12 14:54:41 -0700283 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700284 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700285 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100286 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
287
288 if (getPanelCount() > 1) {
289 // Always return left panel as new page
290 newPage = getLeftmostVisiblePageForIndex(newPage);
291 }
292 return newPage;
293 }
294
295 private int getLeftmostVisiblePageForIndex(int pageIndex) {
296 int panelCount = getPanelCount();
297 return (pageIndex / panelCount) * panelCount;
298 }
299
300 /**
301 * Returns the number of pages that are shown at the same time.
302 */
303 protected int getPanelCount() {
304 return 1;
305 }
306
307 /**
Andras Kloczl2bb51342021-03-20 01:13:45 +0100308 * Returns the currently visible pages.
309 */
Andras Kloczla4cb0f02021-03-21 00:23:07 +0100310 public Iterable<View> getVisiblePages() {
Andras Kloczl2bb51342021-03-20 01:13:45 +0100311 int panelCount = getPanelCount();
312 List<View> visiblePages = new ArrayList<>(panelCount);
313 for (int i = mCurrentPage; i < mCurrentPage + panelCount; i++) {
314 View page = getPageAt(i);
315 if (page != null) {
316 visiblePages.add(page);
317 }
318 }
319 return visiblePages;
320 }
321
322 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100323 * Returns true if the view is on one of the current pages, false otherwise.
324 */
325 public boolean isVisible(View child) {
326 return getLeftmostVisiblePageForIndex(indexOfChild(child)) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700327 }
328
Chet Haasebc2f0822012-10-26 17:59:53 -0700329 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700330 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
331 */
332 private int ensureWithinScrollBounds(int page) {
333 int dir = !mIsRtl ? 1 : - 1;
334 int currScroll = getScrollForPage(page);
335 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800336 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700337 page += dir;
338 prevScroll = currScroll;
339 currScroll = getScrollForPage(page);
340 if (currScroll <= prevScroll) {
341 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
342 break;
343 }
344 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800345 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700346 page -= dir;
347 prevScroll = currScroll;
348 currScroll = getScrollForPage(page);
349 if (currScroll >= prevScroll) {
350 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
351 break;
352 }
353 }
354 return page;
355 }
356
Tony Wickham03f27012019-04-25 14:22:54 -0700357 public void setCurrentPage(int currentPage) {
358 setCurrentPage(currentPage, INVALID_PAGE);
359 }
360
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700361 /**
Winson Chung86f77532010-08-24 11:08:22 -0700362 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 */
Tony Wickham03f27012019-04-25 14:22:54 -0700364 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800365 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700366 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800367 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800368 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
369 // the default
370 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800371 return;
372 }
Tony Wickham03f27012019-04-25 14:22:54 -0700373 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700374 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700375 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700376 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800377 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700378 }
379
Winson Chung8c87cd82013-07-23 16:20:10 -0700380 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800381 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
382 * has settled.
383 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700384 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800385 updatePageIndicator();
386 }
387
388 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700389 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000390 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700391 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700393 protected void pageBeginTransition() {
394 if (!mIsPageInTransition) {
395 mIsPageInTransition = true;
396 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700397 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700398 }
399
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700400 protected void pageEndTransition() {
401 if (mIsPageInTransition) {
402 mIsPageInTransition = false;
403 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700404 }
Michael Jurka0142d492010-08-25 17:46:15 -0700405 }
406
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700407 protected boolean isPageInTransition() {
408 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700409 }
410
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700411 /**
412 * Called when the page starts moving as part of the scroll. Subclasses can override this
413 * to provide custom behavior during animation.
414 */
415 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700416 }
417
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700418 /**
419 * Called when the page ends moving as part of the scroll. Subclasses can override this
420 * to provide custom behavior during animation.
421 */
422 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700423 mWasInOverscroll = false;
vadimt4d93df52019-07-10 17:32:58 -0700424 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800425 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
426 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700427 if (mOnPageTransitionEndCallback != null) {
428 mOnPageTransitionEndCallback.run();
429 mOnPageTransitionEndCallback = null;
430 }
431 }
432
433 /**
434 * Sets a callback to run once when the scrolling finishes. If there is currently
435 * no page in transition, then the callback is called immediately.
436 */
437 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
438 if (mIsPageInTransition || callback == null) {
439 mOnPageTransitionEndCallback = callback;
440 } else {
441 callback.run();
442 }
Michael Jurka0142d492010-08-25 17:46:15 -0700443 }
444
Vinit Nayaka406f722019-12-19 11:50:55 -0800445 protected int getUnboundedScroll() {
446 return mUnboundedScroll;
447 }
448
Winson Chung321e9ee2010-08-09 13:37:56 -0700449 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800450 public void scrollBy(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800451 mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800452 }
453
454 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700455 public void scrollTo(int x, int y) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800456 int primaryScroll = mOrientationHandler.getPrimaryValue(x, y);
457 int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y);
458 mUnboundedScroll = primaryScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -0700459
Vinit Nayaka406f722019-12-19 11:50:55 -0800460 boolean isBeforeFirstPage = mIsRtl ?
461 (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll);
462 boolean isAfterLastPage = mIsRtl ?
463 (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll);
464 if (!isBeforeFirstPage && !isAfterLastPage) {
465 mSpringOverScroll = 0;
Jon Miranda71cb80c2019-01-24 21:25:13 -0800466 }
467
Vinit Nayaka406f722019-12-19 11:50:55 -0800468 if (isBeforeFirstPage) {
469 mOrientationHandler.delegateScrollTo(this,
470 secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800471 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700472 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800473 overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800474 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800475 } else if (isAfterLastPage) {
476 mOrientationHandler.delegateScrollTo(this,
477 secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll);
Adam Cohen68d73932010-11-15 10:50:58 -0800478 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700479 mWasInOverscroll = true;
Vinit Nayaka406f722019-12-19 11:50:55 -0800480 overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll));
Adam Cohen68d73932010-11-15 10:50:58 -0800481 }
482 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700483 if (mWasInOverscroll) {
484 overScroll(0);
485 mWasInOverscroll = false;
486 }
Adam Cohen68d73932010-11-15 10:50:58 -0800487 super.scrollTo(x, y);
488 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800489 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700490
Vinit Nayaka406f722019-12-19 11:50:55 -0800491 /**
492 * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method
493 */
494 public void superScrollTo(int x, int y) {
495 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700496 }
497
Adam Cohen53805212013-10-01 10:39:23 -0700498 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800499 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700500 if (mCurrentPage != getNextPage()) {
501 AccessibilityEvent ev =
502 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700503 ev.setScrollable(true);
504 ev.setScrollX(getScrollX());
505 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800506 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700507 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700508 }
Adam Cohen53805212013-10-01 10:39:23 -0700509 }
510 }
511
Michael Jurka0142d492010-08-25 17:46:15 -0700512 // we moved this functionality to a helper function so SmoothPagedView can reuse it
513 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800514 return computeScrollHelper(true);
515 }
516
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700517 protected void announcePageForAccessibility() {
518 if (isAccessibilityEnabled(getContext())) {
519 // Notify the user when the page changes
520 announceForAccessibility(getCurrentPageDescription());
521 }
522 }
523
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800524 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700526 // Don't bother scrolling if the page does not need to be moved
Vinit Nayaka406f722019-12-19 11:50:55 -0800527 int currentScroll = mOrientationHandler.getPrimaryScroll(this);
528 if (mUnboundedScroll != mScroller.getCurrPos()
529 || currentScroll != mScroller.getCurrPos()) {
530 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos());
Winson Chung557d6ed2011-07-08 15:34:52 -0700531 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800532 if (shouldInvalidate) {
533 invalidate();
534 }
Michael Jurka0142d492010-08-25 17:46:15 -0700535 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800536 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700537 sendScrollAccessibilityEvent();
538
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700539 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700540 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700541 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700542 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700543
Adam Cohen73aa9752010-11-24 16:26:58 -0800544 // We don't want to trigger a page end moving unless the page has settled
545 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700546 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700547 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800548 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700549
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700550 if (canAnnouncePageDescription()) {
551 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700552 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 }
Michael Jurka0142d492010-08-25 17:46:15 -0700554 return false;
555 }
556
557 @Override
558 public void computeScroll() {
559 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 }
561
Sunny Goyalce8809a2018-01-18 14:02:47 -0800562 public int getExpectedHeight() {
563 return getMeasuredHeight();
564 }
565
Adam Cohen410f3cd2013-09-22 12:09:32 -0700566 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800567 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800568 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700569 }
570
Sunny Goyalce8809a2018-01-18 14:02:47 -0800571 public int getExpectedWidth() {
572 return getMeasuredWidth();
573 }
574
Sunny Goyalbc683e92017-12-06 10:25:07 -0800575 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800576 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800577 - mInsets.left - mInsets.right;
578 }
579
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800581 public void requestLayout() {
582 mIsLayoutValid = false;
583 super.requestLayout();
584 }
585
586 @Override
587 public void forceLayout() {
588 mIsLayoutValid = false;
589 super.forceLayout();
590 }
591
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100592 private int getPageWidthSize(int widthSize) {
593 return (widthSize - mInsets.left - mInsets.right) / getPanelCount();
594 }
595
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800596 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700598 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700599 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
600 return;
601 }
602
Adam Cohen7d30a372013-07-01 17:03:59 -0700603 // We measure the dimensions of the PagedView to be larger than the pages so that when we
604 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700605 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
606 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
607 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700608 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700609
Adam Cohen7d30a372013-07-01 17:03:59 -0700610 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
611 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
612 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
614
Winson Chung8aad6102012-05-11 16:27:49 -0700615 // Return early if we aren't given a proper dimension
616 if (widthSize <= 0 || heightSize <= 0) {
617 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
618 return;
619 }
620
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700622 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700623 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700624
Sunny Goyalac00cba2017-11-13 15:58:01 -0800625 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100626 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800627 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800628 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700629
Sunny Goyalac00cba2017-11-13 15:58:01 -0800630 // measureChildren takes accounts for content padding, we only need to care about extra
631 // space due to insets.
632 measureChildren(myWidthSpec, myHeightSpec);
633 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800634 }
635
Sunny Goyalcf25b522015-07-09 00:01:18 -0700636 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700638 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800639 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700640 final int childCount = getChildCount();
641 boolean pageScrollChanged = false;
642 if (mPageScrolls == null || childCount != mPageScrolls.length) {
643 mPageScrolls = new int[childCount];
644 pageScrollChanged = true;
645 }
646
647 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700648 return;
649 }
650
Winson Chung785d2eb2011-04-14 16:08:02 -0700651 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700652
Vinit Nayaka406f722019-12-19 11:50:55 -0800653 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
654 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700655 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700657
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700658 final LayoutTransition transition = getLayoutTransition();
659 // If the transition is running defer updating max scroll, as some empty pages could
660 // still be present, and a max scroll change could cause sudden jumps in scroll.
661 if (transition != null && transition.isRunning()) {
662 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
663
664 @Override
665 public void startTransition(LayoutTransition transition, ViewGroup container,
666 View view, int transitionType) { }
667
668 @Override
669 public void endTransition(LayoutTransition transition, ViewGroup container,
670 View view, int transitionType) {
671 // Wait until all transitions are complete.
672 if (!transition.isRunning()) {
673 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700674 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700675 }
676 }
677 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700678 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700679 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700680 }
681
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700682 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
683 updateCurrentPageScroll();
684 mFirstLayout = false;
685 }
686
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800687 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700688 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700689 }
Winson Chunge3193b92010-09-10 11:44:42 -0700690 }
691
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700692 /**
693 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
694 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700695 */
696 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
697 ComputePageScrollsLogic scrollLogic) {
698 final int childCount = getChildCount();
699
700 final int startIndex = mIsRtl ? childCount - 1 : 0;
701 final int endIndex = mIsRtl ? -1 : childCount;
702 final int delta = mIsRtl ? -1 : 1;
703
Vinit Nayaka406f722019-12-19 11:50:55 -0800704 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700705
Vinit Nayaka406f722019-12-19 11:50:55 -0800706 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
707 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700708 boolean pageScrollChanged = false;
709
Vinit Nayaka406f722019-12-19 11:50:55 -0800710 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700711 final View child = getPageAt(i);
712 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800713 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
714 pageCenter, layoutChildren);
715 final int primaryDimension = bounds.primaryDimension;
716 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700717
Alex Chaub5c4d112021-03-29 17:37:53 +0100718 // In case the pages are of different width, align the page to left edge for non-RTL
719 // or right edge for RTL.
720 final int pageScroll =
721 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700722 if (outPageScrolls[i] != pageScroll) {
723 pageScrollChanged = true;
724 outPageScrolls[i] = pageScroll;
725 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800726 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700727 }
728 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100729
730 int panelCount = getPanelCount();
731 if (panelCount > 1) {
732 for (int i = 0; i < childCount; i++) {
733 // In case we have multiple panels, always use left panel's page scroll for all
734 // panels on the screen.
735 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
736 if (outPageScrolls[i] != adjustedScroll) {
737 outPageScrolls[i] = adjustedScroll;
738 pageScrollChanged = true;
739 }
740 }
741 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700742 return pageScrollChanged;
743 }
744
Sunny Goyala1fbd842015-05-20 13:40:27 -0700745 protected int getChildGap() {
746 return 0;
747 }
748
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700749 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800750 mMinScroll = computeMinScroll();
751 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700752 }
753
Vinit Nayaka406f722019-12-19 11:50:55 -0800754 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700755 return 0;
Tony50876bf2018-09-19 23:09:14 -0400756 }
757
Vinit Nayaka406f722019-12-19 11:50:55 -0800758 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700759 int childCount = getChildCount();
760 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700761 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700762 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700763 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700764 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700765 }
766 }
767
Adam Cohen3b185e22013-10-29 14:45:58 -0700768 public void setPageSpacing(int pageSpacing) {
769 mPageSpacing = pageSpacing;
770 requestLayout();
771 }
772
Tony50876bf2018-09-19 23:09:14 -0400773 public int getPageSpacing() {
774 return mPageSpacing;
775 }
776
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800777 private void dispatchPageCountChanged() {
778 if (mPageIndicator != null) {
779 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700780 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700781 // This ensures that when children are added, they get the correct transforms / alphas
782 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700783 invalidate();
784 }
785
Michael Jurka8b805b12012-04-18 14:23:14 -0700786 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800787 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800788 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800789 dispatchPageCountChanged();
790 }
791
792 @Override
793 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800794 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700795 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800796 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700797 }
798
Adam Cohen73894962011-10-31 13:17:17 -0700799 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700800 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800801 View pageAtIndex = getPageAt(index);
802 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700803 }
804
Alex Chau7944ee52021-01-08 19:07:25 +0000805 protected int getChildVisibleSize(int index) {
806 View layout = getPageAt(index);
807 return mOrientationHandler.getMeasuredSize(layout);
808 }
809
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 @Override
811 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700812 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700813 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700814 if (immediate) {
815 setCurrentPage(page);
816 } else {
817 snapToPage(page);
818 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 return true;
820 }
821 return false;
822 }
823
824 @Override
825 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700826 int focusablePage;
827 if (mNextPage != INVALID_PAGE) {
828 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700830 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 }
Winson Chung86f77532010-08-24 11:08:22 -0700832 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700834 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
836 return false;
837 }
838
839 @Override
840 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800841 if (super.dispatchUnhandledMove(focused, direction)) {
842 return true;
843 }
844
845 if (mIsRtl) {
846 if (direction == View.FOCUS_LEFT) {
847 direction = View.FOCUS_RIGHT;
848 } else if (direction == View.FOCUS_RIGHT) {
849 direction = View.FOCUS_LEFT;
850 }
851 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700853 if (getCurrentPage() > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100854 int nextPage = validateNewPage(getCurrentPage() - 1);
855 snapToPage(nextPage);
856 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 return true;
858 }
859 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700860 if (getCurrentPage() < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100861 int nextPage = validateNewPage(getCurrentPage() + 1);
862 snapToPage(nextPage);
863 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 return true;
865 }
866 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800867 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 }
869
870 @Override
871 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700872 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
873 return;
874 }
875
Adam Cohen0ffac432013-07-10 11:19:26 -0700876 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700877 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700878 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 }
880 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700881 if (mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100882 int nextPage = validateNewPage(mCurrentPage - 1);
883 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100885 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700886 if (mCurrentPage < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100887 int nextPage = validateNewPage(mCurrentPage + 1);
888 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 }
890 }
891 }
892
893 /**
894 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700895 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 *
Winson Chung86f77532010-08-24 11:08:22 -0700897 * This happens when live folders requery, and if they're off page, they
898 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 */
900 @Override
901 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700902 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 View v = focused;
904 while (true) {
905 if (v == current) {
906 super.focusableViewAvailable(focused);
907 return;
908 }
909 if (v == this) {
910 return;
911 }
912 ViewParent parent = v.getParent();
913 if (parent instanceof View) {
914 v = (View)v.getParent();
915 } else {
916 return;
917 }
918 }
919 }
920
921 /**
922 * {@inheritDoc}
923 */
924 @Override
925 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
926 if (disallowIntercept) {
927 // We need to make sure to cancel our long press if
928 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +0200929 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 }
931 super.requestDisallowInterceptTouchEvent(disallowIntercept);
932 }
933
934 @Override
935 public boolean onInterceptTouchEvent(MotionEvent ev) {
936 /*
937 * This method JUST determines whether we want to intercept the motion.
938 * If we return true, onTouchEvent will be called and we do the actual
939 * scrolling there.
940 */
941
Winson Chung45e1d6e2010-11-09 17:19:49 -0800942 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700943 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700944
945 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800946
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 /*
948 * Shortcut the most recurring case: the user is in the dragging
949 * state and he is moving his finger. We want to intercept this
950 * motion.
951 */
952 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700953 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 return true;
955 }
956
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 switch (action & MotionEvent.ACTION_MASK) {
958 case MotionEvent.ACTION_MOVE: {
959 /*
960 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800961 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700963 if (mActivePointerId != INVALID_POINTER) {
964 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700965 }
966 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800967 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700968 // i.e. fall through to the next case (don't break)
969 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
970 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700971 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 }
973
974 case MotionEvent.ACTION_DOWN: {
975 final float x = ev.getX();
976 final float y = ev.getY();
977 // Remember location of down touch
978 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700979 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700980 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800981 mLastMotionRemainder = 0;
982 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700983 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700985
Tony Wickhama0aee212019-09-18 09:24:03 -0700986 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700987
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 break;
989 }
990
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800992 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700993 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 break;
995
996 case MotionEvent.ACTION_POINTER_UP:
997 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800998 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 break;
1000 }
1001
1002 /*
1003 * The only time we want to intercept motion events is if we are in the
1004 * drag mode.
1005 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001006 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 }
1008
Tony Wickhama0aee212019-09-18 09:24:03 -07001009 /**
1010 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1011 */
1012 private void updateIsBeingDraggedOnTouchDown() {
1013 // mScroller.isFinished should be false when being flinged.
1014 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -07001015 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001016
1017 if (finishedScrolling) {
1018 mIsBeingDragged = false;
1019 if (!mScroller.isFinished() && !mFreeScroll) {
1020 setCurrentPage(getNextPage());
1021 pageEndTransition();
1022 }
1023 } else {
1024 mIsBeingDragged = true;
1025 }
1026 }
1027
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001028 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001029 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001030 }
1031
Adam Cohenf8d28232011-02-01 21:47:00 -08001032 protected void determineScrollingStart(MotionEvent ev) {
1033 determineScrollingStart(ev, 1.0f);
1034 }
1035
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 /*
1037 * Determines if we should change the touch state to start scrolling after the
1038 * user moves their touch point too far.
1039 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001040 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001041 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001043 if (pointerIndex == -1) return;
1044
Vinit Nayaka406f722019-12-19 11:50:55 -08001045 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1046 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001047 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001048 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001049
Vinit Nayaka406f722019-12-19 11:50:55 -08001050 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001051 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001052 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001053 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1054 mLastMotion = primaryDirection;
1055 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001056 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001057 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001058 // Stop listening for things like pinches.
1059 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001060 }
1061 }
1062
1063 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001064 // Try canceling the long press. It could also have been scheduled
1065 // by a distant descendant, so use the mAllowLongPress flag to block
1066 // everything
Andras Kloczl2bb51342021-03-20 01:13:45 +01001067 getVisiblePages().forEach(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001068 }
1069
Adam Cohenb5ba0972011-09-07 18:02:31 -07001070 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001071 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001072
Adam Cohenedb40762013-07-18 16:45:45 -07001073 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001074 int count = getChildCount();
1075
1076 final int totalDistance;
1077
1078 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001079 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001080 adjacentPage = page - 1;
1081 }
1082
1083 if (adjacentPage < 0 || adjacentPage > count - 1) {
1084 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1085 } else {
1086 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1087 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001088
1089 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001090 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1091 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001092 return scrollProgress;
1093 }
1094
Adam Cohenedb40762013-07-18 16:45:45 -07001095 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001096 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001097 return 0;
1098 } else {
1099 return mPageScrolls[index];
1100 }
1101 }
1102
Adam Cohen564a2e72013-10-09 14:47:32 -07001103 // While layout transitions are occurring, a child's position may stray from its baseline
1104 // position. This method returns the magnitude of this stray at any given time.
1105 public int getLayoutTransitionOffsetForPage(int index) {
1106 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1107 return 0;
1108 } else {
1109 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001110
Sunny Goyal228153d2018-01-04 15:35:22 -08001111 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1112 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001113 return (int) (child.getX() - baselineX);
1114 }
1115 }
1116
Jon Miranda71cb80c2019-01-24 21:25:13 -08001117 @Override
1118 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001119 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001120 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001121 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001122 super.dispatchDraw(canvas);
1123
1124 canvas.restoreToCount(saveCount);
1125 } else {
1126 super.dispatchDraw(canvas);
1127 }
1128 }
1129
Jon Miranda57dab6f2020-04-10 13:38:14 -07001130 /**
1131 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1132 */
1133 private int getSpringOverScroll(int amount) {
1134 if (mScroller.isSpringing()) {
1135 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001136 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001137 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1138 } else {
1139 return 0;
1140 }
1141 }
1142
Sunny Goyalb7b01352018-08-09 12:31:20 -07001143 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001144 if (amount == 0) {
1145 return;
1146 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001147
Vinit Nayaka406f722019-12-19 11:50:55 -08001148 int size = mOrientationHandler.getMeasuredSize(this);
1149 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001150 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001151 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001152 invalidate();
1153 return;
1154 }
1155
Vinit Nayaka406f722019-12-19 11:50:55 -08001156 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1157 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1158 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001159 invalidate();
1160 }
1161
Sunny Goyalb7b01352018-08-09 12:31:20 -07001162 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001163 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001164 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001165 invalidate();
1166 return;
1167 }
1168
Sunny Goyalb7b01352018-08-09 12:31:20 -07001169 if (amount == 0) return;
1170
1171 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001172 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1173 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001174 } else {
1175 dampedOverScroll(amount);
1176 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001177 }
1178
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001179
Tony Wickham023f4042019-01-29 10:52:31 -08001180 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001181 if (mFreeScroll == freeScroll) {
1182 return;
1183 }
1184
Tony Wickham8f7ead32016-04-07 18:46:44 -07001185 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001186 mFreeScroll = freeScroll;
1187
Adam Cohenf9618852013-11-08 06:45:03 -08001188 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001189 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001190 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001191 if (getScrollForPage(getNextPage()) != getScrollX()) {
1192 snapToPage(getNextPage());
1193 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001194 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001195 }
1196
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001197 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001198 mAllowOverScroll = enable;
1199 }
1200
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 @Override
1202 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001203 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001204 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001205
Michael Jurkab8f06722010-10-10 15:58:46 -07001206 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001207
1208 final int action = ev.getAction();
1209
1210 switch (action & MotionEvent.ACTION_MASK) {
1211 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001212 updateIsBeingDraggedOnTouchDown();
1213
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 /*
1215 * If being flinged and user touches, stop the fling. isFinished
1216 * will be false if being flinged.
1217 */
1218 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001219 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221
1222 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001223 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001224 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001225 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1226 mLastMotionRemainder = 0;
1227 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001228 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001230 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001231 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001232 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001233 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 break;
1235
Winson Chung4941f652020-06-18 15:11:53 -07001236 case ACTION_MOVE_ALLOW_EASY_FLING:
1237 // Start scrolling immediately
1238 determineScrollingStart(ev);
1239 mAllowEasyFling = true;
1240 break;
1241
1242 case MotionEvent.ACTION_MOVE:
1243 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 // Scroll to follow the motion event
1245 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001246
1247 if (pointerIndex == -1) return true;
1248
Vinit Nayaka406f722019-12-19 11:50:55 -08001249 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1250 float delta = mLastMotion + mLastMotionRemainder - direction;
1251 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001252
Winson Chungc0844aa2011-02-02 15:25:58 -08001253 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1254 // keep the remainder because we are actually testing if we've moved from the last
1255 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001256 if (Math.abs(delta) >= 1.0f) {
1257 mLastMotion = direction;
1258 mLastMotionRemainder = delta - (int) delta;
1259
1260 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 } else {
1262 awakenScrollBars();
1263 }
Adam Cohen564976a2010-10-13 18:52:07 -07001264 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 determineScrollingStart(ev);
1266 }
1267 break;
1268
1269 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001270 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 final int activePointerId = mActivePointerId;
1272 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001273 if (pointerIndex == -1) return true;
1274
Vinit Nayaka406f722019-12-19 11:50:55 -08001275 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1276 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001277 final VelocityTracker velocityTracker = mVelocityTracker;
1278 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001279
Vinit Nayaka406f722019-12-19 11:50:55 -08001280 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1281 mActivePointerId);
1282 int delta = (int) (primaryDirection - mDownMotionPrimary);
1283 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1284
1285 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1286 SIGNIFICANT_MOVE_THRESHOLD;
1287
1288 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001289 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1290 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001291 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1292 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001293 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1294 Log.d("Quickswitch", "isFling=false vel=" + velocity
1295 + " threshold=" + mEasyFlingThresholdVelocity);
1296 }
Adam Cohen00481b32011-11-18 12:03:48 -08001297
Adam Cohenf358a4b2013-07-23 16:47:31 -07001298 if (!mFreeScroll) {
1299 // In the case that the page is moved far to one direction and then is flung
1300 // in the opposite direction, we use a threshold to determine whether we should
1301 // just return to the starting page, or if we should skip one further.
1302 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001303 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1304 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001305 returnToOriginalPage = true;
1306 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001307
Adam Cohenf358a4b2013-07-23 16:47:31 -07001308 int finalPage;
1309 // We give flings precedence over large moves, which is why we short-circuit our
1310 // test for a large move if a fling has been registered. That is, a large
1311 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001312
Vinit Nayaka406f722019-12-19 11:50:55 -08001313 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1314 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001315 finalPage = returnToOriginalPage
1316 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001317 snapToPageWithVelocity(finalPage, velocity);
1318 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1319 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001320 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001321 finalPage = returnToOriginalPage
1322 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001323 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001324 } else {
1325 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001326 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001328 if (!mScroller.isFinished()) {
1329 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001330 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001331
Vinit Nayaka406f722019-12-19 11:50:55 -08001332 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1333 int maxScroll = mMaxScroll;
1334 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001335
Vinit Nayaka406f722019-12-19 11:50:55 -08001336 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1337 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1338 mScroller.springBack(initialScroll, minScroll, maxScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001339 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001340 } else {
1341 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001342 mScroller.fling(initialScroll, -velocity,
1343 minScroll, maxScroll,
1344 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001345
Vinit Nayaka406f722019-12-19 11:50:55 -08001346 int finalPos = mScroller.getFinalPos();
Alex Chaub794ea62021-02-03 18:00:30 +00001347 mNextPage = getDestinationPage(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001348
1349 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1350 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001351 if (snapToPageInFreeScroll() && finalPos > minScroll
1352 && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001353 // If scrolling ends in the half of the added space that is closer to
1354 // the end, settle to the end. Otherwise snap to the nearest page.
1355 // If flinging past one of the ends, don't change the velocity as it
1356 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001357 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1358 ? minScroll
1359 : finalPos > (lastPageScroll + maxScroll) / 2
1360 ? maxScroll
1361 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001362
Vinit Nayaka406f722019-12-19 11:50:55 -08001363 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001364 // Ensure the scroll/snap doesn't happen too fast;
1365 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001366 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001367 if (extraScrollDuration > 0) {
1368 mScroller.extendDuration(extraScrollDuration);
1369 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001370 }
1371 }
1372 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001373 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001374 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001375 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001376
Adam Cohen7d30a372013-07-01 17:03:59 -07001377 // End any intermediate reordering states
1378 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 break;
1380
1381 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001382 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001383 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001384 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001385 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001386 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 break;
1388
1389 case MotionEvent.ACTION_POINTER_UP:
1390 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001391 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001392 break;
1393 }
1394
1395 return true;
1396 }
1397
Alex Chaub794ea62021-02-03 18:00:30 +00001398 protected boolean snapToPageInFreeScroll() {
1399 return true;
1400 }
1401
Vinit Nayaka406f722019-12-19 11:50:55 -08001402 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001403 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1404 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001405 }
1406
Adam Cohen7d30a372013-07-01 17:03:59 -07001407 private void resetTouchState() {
1408 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001409 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001410 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001411 }
1412
Adam Cohenc2d6e892014-10-16 09:49:24 -07001413 /**
1414 * Triggered by scrolling via touch
1415 */
1416 protected void onScrollInteractionBegin() {
1417 }
1418
1419 protected void onScrollInteractionEnd() {
1420 }
1421
Winson Chung185d7162011-02-28 13:47:29 -08001422 @Override
1423 public boolean onGenericMotionEvent(MotionEvent event) {
1424 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1425 switch (event.getAction()) {
1426 case MotionEvent.ACTION_SCROLL: {
1427 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1428 final float vscroll;
1429 final float hscroll;
1430 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1431 vscroll = 0;
1432 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1433 } else {
1434 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1435 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1436 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001437 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1438 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001439 }
Winson Chung185d7162011-02-28 13:47:29 -08001440 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001441 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001442 : (hscroll > 0 || vscroll > 0);
1443 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001444 scrollRight();
1445 } else {
1446 scrollLeft();
1447 }
1448 return true;
1449 }
1450 }
1451 }
1452 }
1453 return super.onGenericMotionEvent(event);
1454 }
1455
Sunny Goyal8b37c572020-03-31 12:12:14 -07001456 /**
1457 * Returns true if the paged view can scroll for the provided vertical and horizontal
1458 * scroll values
1459 */
1460 protected boolean canScroll(float absVScroll, float absHScroll) {
1461 ActivityContext ac = ActivityContext.lookupContext(getContext());
1462 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001463 }
1464
Michael Jurkab8f06722010-10-10 15:58:46 -07001465 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1466 if (mVelocityTracker == null) {
1467 mVelocityTracker = VelocityTracker.obtain();
1468 }
1469 mVelocityTracker.addMovement(ev);
1470 }
1471
1472 private void releaseVelocityTracker() {
1473 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001474 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001475 mVelocityTracker.recycle();
1476 mVelocityTracker = null;
1477 }
1478 }
1479
Winson Chung321e9ee2010-08-09 13:37:56 -07001480 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001481 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 final int pointerId = ev.getPointerId(pointerIndex);
1483 if (pointerId == mActivePointerId) {
1484 // This was our active pointer going up. Choose a new
1485 // active pointer and adjust accordingly.
1486 // TODO: Make this decision more intelligent.
1487 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001488 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1489 newPointerIndex);
1490 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 mActivePointerId = ev.getPointerId(newPointerIndex);
1492 if (mVelocityTracker != null) {
1493 mVelocityTracker.clear();
1494 }
1495 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001496 }
1497
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 @Override
1499 public void requestChildFocus(View child, View focused) {
1500 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001501 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001502 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001503 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 }
1505 }
1506
Alex Chaub794ea62021-02-03 18:00:30 +00001507 public int getDestinationPage() {
1508 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1509 }
1510
Alex Chaudedbc8a2021-03-17 16:53:26 +00001511 protected int getDestinationPage(int primaryScroll) {
1512 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001513 }
1514
Sunny Goyal228153d2018-01-04 15:35:22 -08001515 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001516 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001517 }
1518
Alex Chaudedbc8a2021-03-17 16:53:26 +00001519 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1520 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001521 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001522 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001523 final int childCount = getChildCount();
1524 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001525 int distanceFromScreenCenter = Math.abs(
1526 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1528 minDistanceFromScreenCenter = distanceFromScreenCenter;
1529 minDistanceFromScreenCenterIndex = i;
1530 }
1531 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001532 return minDistanceFromScreenCenterIndex;
1533 }
1534
Tony Wickham7383d4e2020-07-07 19:25:25 -07001535 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001536 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001537 int halfChildSize = (childSize / 2);
1538 int childCenter = getChildOffset(childIndex) + halfChildSize;
1539 return childCenter - screenCenter;
1540 }
1541
1542 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001543 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1544 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001545 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1546 }
1547
Alex Chaudedbc8a2021-03-17 16:53:26 +00001548 private int getScreenCenter(int primaryScroll) {
1549 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1550 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1551 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1552 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1553 + primaryPivot);
1554 }
1555
Adam Cohend19d3ca2010-09-15 14:43:42 -07001556 protected void snapToDestination() {
Alex Chaub794ea62021-02-03 18:00:30 +00001557 snapToPage(getDestinationPage(), getPageSnapDuration());
Adam Cohen8d769d62017-06-23 17:27:38 -07001558 }
1559
1560 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001561 int scroll = mOrientationHandler.getPrimaryScroll(this);
1562 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001563 }
1564
1565 protected int getPageSnapDuration() {
1566 if (isInOverScroll()) {
1567 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1568 }
1569 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 }
1571
Adam Cohene0f66b52010-11-23 15:06:07 -08001572 // We want the duration of the page snap animation to be influenced by the distance that
1573 // the screen has to travel, however, we don't want this duration to be effected in a
1574 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1575 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001576 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001577 f -= 0.5f; // center the values about 0.
1578 f *= 0.3f * Math.PI / 2.0f;
1579 return (float) Math.sin(f);
1580 }
1581
Winson Chung05a31ed2018-02-08 17:08:43 -08001582 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001583 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001584 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001585
Vinit Nayaka406f722019-12-19 11:50:55 -08001586 final int newLoc = getScrollForPage(whichPage);
1587 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001588 int duration = 0;
1589
Sunny Goyal4d113a52015-05-27 10:05:28 -07001590 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001591 // If the velocity is low enough, then treat this more as an automatic page advance
1592 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001593 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001594 }
1595
1596 // Here we compute a "distance" that will be used in the computation of the overall
1597 // snap duration. This is a function of the actual distance that needs to be traveled;
1598 // we keep this value close to half screen size in order to reduce the variance in snap
1599 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001600 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001601 float distance = halfScreenSize + halfScreenSize *
1602 distanceInfluenceForSnapDuration(distanceRatio);
1603
1604 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001605 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001606
1607 // we want the page's snap velocity to approximately match the velocity at which the
1608 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001609 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1610 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001611
Jon Miranda9e198662020-03-11 14:42:02 -07001612 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001613 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001614 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001615 } else {
1616 return snapToPage(whichPage, delta, duration);
1617 }
Michael Jurka0142d492010-08-25 17:46:15 -07001618 }
1619
Winson Chung05a31ed2018-02-08 17:08:43 -08001620 public boolean snapToPage(int whichPage) {
1621 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001622 }
1623
Winson Chung05a31ed2018-02-08 17:08:43 -08001624 public boolean snapToPageImmediately(int whichPage) {
1625 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001626 }
1627
Winson Chung05a31ed2018-02-08 17:08:43 -08001628 public boolean snapToPage(int whichPage, int duration) {
1629 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001630 }
1631
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001632 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001633 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001634 }
1635
Winson Chung05a31ed2018-02-08 17:08:43 -08001636 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001637 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001638 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001639
Vinit Nayaka406f722019-12-19 11:50:55 -08001640 int newLoc = getScrollForPage(whichPage);
1641 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001642 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001643 }
1644
Winson Chung05a31ed2018-02-08 17:08:43 -08001645 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001646 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001647 }
Michael Jurka0142d492010-08-25 17:46:15 -07001648
Winson Chung05a31ed2018-02-08 17:08:43 -08001649 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001650 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001651 if (mFirstLayout) {
1652 setCurrentPage(whichPage);
1653 return false;
1654 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001655
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001656 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001657 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1658 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001659 }
1660
Adam Cohen6f127a62014-06-12 14:54:41 -07001661 whichPage = validateNewPage(whichPage);
1662
Adam Cohen7d30a372013-07-01 17:03:59 -07001663 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001664
Winson Chung321e9ee2010-08-09 13:37:56 -07001665 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001666 if (immediate) {
1667 duration = 0;
1668 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001669 duration = Math.abs(delta);
1670 }
1671
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001672 if (duration != 0) {
1673 pageBeginTransition();
1674 }
1675
Adam Cohenf358a4b2013-07-23 16:47:31 -07001676 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001677 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001678 }
Adam Cohenf9618852013-11-08 06:45:03 -08001679
1680 if (interpolator != null) {
1681 mScroller.setInterpolator(interpolator);
1682 } else {
1683 mScroller.setInterpolator(mDefaultInterpolator);
1684 }
1685
Jon Miranda71cb80c2019-01-24 21:25:13 -08001686 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001687 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001688 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001689 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001690 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001691
Adam Cohen674531f2013-12-13 15:07:14 -08001692 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001693
1694 // Trigger a compute() to finish switching pages if necessary
1695 if (immediate) {
1696 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001697 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001698 }
1699
Winson Chung321e9ee2010-08-09 13:37:56 -07001700 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001701 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001702 }
1703
Vadim Tryshev98913d02018-05-18 18:41:34 -07001704 public boolean scrollLeft() {
1705 if (getNextPage() > 0) {
1706 snapToPage(getNextPage() - 1);
1707 return true;
1708 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001709 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001710 }
1711
Vadim Tryshev98913d02018-05-18 18:41:34 -07001712 public boolean scrollRight() {
1713 if (getNextPage() < getChildCount() - 1) {
1714 snapToPage(getNextPage() + 1);
1715 return true;
1716 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001717 return onOverscroll(getMeasuredWidth());
1718 }
1719
1720 protected boolean onOverscroll(int amount) {
1721 if (!mAllowOverScroll) return false;
1722 onScrollInteractionBegin();
1723 overScroll(amount);
1724 onScrollInteractionEnd();
1725 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001726 }
1727
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001728 @Override
1729 public CharSequence getAccessibilityClassName() {
1730 // Some accessibility services have special logic for ScrollView. Since we provide same
1731 // accessibility info as ScrollView, inform the service to handle use the same way.
1732 return ScrollView.class.getName();
1733 }
1734
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001735 protected boolean isPageOrderFlipped() {
1736 return false;
1737 }
1738
Winson Chung6a0f57d2011-06-29 20:10:49 -07001739 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001740 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001741 @Override
1742 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1743 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001744 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001745 int offset = (mAllowOverScroll ? 0 : 1);
1746 info.setScrollable(getPageCount() > offset);
1747 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001748 info.addAction(pagesFlipped ?
1749 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1750 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1751 info.addAction(mIsRtl ?
1752 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1753 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001754 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001755 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001756 info.addAction(pagesFlipped ?
1757 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1758 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1759 info.addAction(mIsRtl ?
1760 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1761 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001762 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001763 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1764 // Besides disabling the accessibility long-click, this also prevents this view from getting
1765 // accessibility focus.
1766 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001767 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001768 }
1769
1770 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001771 public void sendAccessibilityEvent(int eventType) {
1772 // Don't let the view send real scroll events.
1773 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1774 super.sendAccessibilityEvent(eventType);
1775 }
1776 }
1777
1778 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001779 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1780 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001781 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001782 }
1783
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001784 @Override
1785 public boolean performAccessibilityAction(int action, Bundle arguments) {
1786 if (super.performAccessibilityAction(action, arguments)) {
1787 return true;
1788 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001789 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001790 switch (action) {
1791 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001792 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001793 return true;
1794 }
1795 } break;
1796 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001797 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001798 return true;
1799 }
yingleiw02cc8482019-08-02 16:14:27 -07001800 } break;
1801 case android.R.id.accessibilityActionPageRight: {
1802 if (!mIsRtl) {
1803 return scrollRight();
1804 } else {
1805 return scrollLeft();
1806 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001807 }
yingleiw02cc8482019-08-02 16:14:27 -07001808 case android.R.id.accessibilityActionPageLeft: {
1809 if (!mIsRtl) {
1810 return scrollLeft();
1811 } else {
1812 return scrollRight();
1813 }
1814 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001815 }
1816 return false;
1817 }
1818
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001819 protected boolean canAnnouncePageDescription() {
1820 return true;
1821 }
1822
Adam Cohen0ffac432013-07-10 11:19:26 -07001823 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001824 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001825 getNextPage() + 1, getChildCount());
1826 }
1827
Tony Mak2e564402018-02-27 17:19:34 +00001828 protected float getDownMotionX() {
1829 return mDownMotionX;
1830 }
1831
1832 protected float getDownMotionY() {
1833 return mDownMotionY;
1834 }
1835
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001836 protected interface ComputePageScrollsLogic {
1837
1838 boolean shouldIncludeView(View view);
1839 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001840
1841 public int[] getVisibleChildrenRange() {
1842 float visibleLeft = 0;
1843 float visibleRight = visibleLeft + getMeasuredWidth();
1844 float scaleX = getScaleX();
1845 if (scaleX < 1 && scaleX > 0) {
1846 float mid = getMeasuredWidth() / 2;
1847 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1848 visibleRight = mid + ((visibleRight - mid) / scaleX);
1849 }
1850
1851 int leftChild = -1;
1852 int rightChild = -1;
1853 final int childCount = getChildCount();
1854 for (int i = 0; i < childCount; i++) {
1855 final View child = getPageAt(i);
1856
1857 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1858 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1859 if (leftChild == -1) {
1860 leftChild = i;
1861 }
1862 rightChild = i;
1863 }
1864 }
1865 mTmpIntPair[0] = leftChild;
1866 mTmpIntPair[1] = rightChild;
1867 return mTmpIntPair;
1868 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001869}