blob: 01f7c71daf9e787fcd3c9ef44c6ed8ebe5163472 [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
Adam Cohen22f823d2011-09-01 17:22:18 -0700929 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700930 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 }
932 super.requestDisallowInterceptTouchEvent(disallowIntercept);
933 }
934
935 @Override
936 public boolean onInterceptTouchEvent(MotionEvent ev) {
937 /*
938 * This method JUST determines whether we want to intercept the motion.
939 * If we return true, onTouchEvent will be called and we do the actual
940 * scrolling there.
941 */
942
Winson Chung45e1d6e2010-11-09 17:19:49 -0800943 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700944 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700945
946 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800947
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 /*
949 * Shortcut the most recurring case: the user is in the dragging
950 * state and he is moving his finger. We want to intercept this
951 * motion.
952 */
953 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700954 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 return true;
956 }
957
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 switch (action & MotionEvent.ACTION_MASK) {
959 case MotionEvent.ACTION_MOVE: {
960 /*
961 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800962 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700964 if (mActivePointerId != INVALID_POINTER) {
965 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700966 }
967 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800968 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700969 // i.e. fall through to the next case (don't break)
970 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
971 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700972 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 }
974
975 case MotionEvent.ACTION_DOWN: {
976 final float x = ev.getX();
977 final float y = ev.getY();
978 // Remember location of down touch
979 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700981 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800982 mLastMotionRemainder = 0;
983 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700984 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700986
Tony Wickhama0aee212019-09-18 09:24:03 -0700987 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700988
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 break;
990 }
991
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800993 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700994 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 break;
996
997 case MotionEvent.ACTION_POINTER_UP:
998 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800999 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 break;
1001 }
1002
1003 /*
1004 * The only time we want to intercept motion events is if we are in the
1005 * drag mode.
1006 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001007 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 }
1009
Tony Wickhama0aee212019-09-18 09:24:03 -07001010 /**
1011 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1012 */
1013 private void updateIsBeingDraggedOnTouchDown() {
1014 // mScroller.isFinished should be false when being flinged.
1015 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -07001016 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001017
1018 if (finishedScrolling) {
1019 mIsBeingDragged = false;
1020 if (!mScroller.isFinished() && !mFreeScroll) {
1021 setCurrentPage(getNextPage());
1022 pageEndTransition();
1023 }
1024 } else {
1025 mIsBeingDragged = true;
1026 }
1027 }
1028
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001029 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001030 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001031 }
1032
Adam Cohenf8d28232011-02-01 21:47:00 -08001033 protected void determineScrollingStart(MotionEvent ev) {
1034 determineScrollingStart(ev, 1.0f);
1035 }
1036
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 /*
1038 * Determines if we should change the touch state to start scrolling after the
1039 * user moves their touch point too far.
1040 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001041 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001042 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001044 if (pointerIndex == -1) return;
1045
Vinit Nayaka406f722019-12-19 11:50:55 -08001046 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1047 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001048 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001049 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001050
Vinit Nayaka406f722019-12-19 11:50:55 -08001051 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001052 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001053 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001054 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1055 mLastMotion = primaryDirection;
1056 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001057 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001058 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001059 // Stop listening for things like pinches.
1060 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001061 }
1062 }
1063
1064 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001065 // Try canceling the long press. It could also have been scheduled
1066 // by a distant descendant, so use the mAllowLongPress flag to block
1067 // everything
Andras Kloczl2bb51342021-03-20 01:13:45 +01001068 getVisiblePages().forEach(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 }
1070
Adam Cohenb5ba0972011-09-07 18:02:31 -07001071 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001072 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001073
Adam Cohenedb40762013-07-18 16:45:45 -07001074 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001075 int count = getChildCount();
1076
1077 final int totalDistance;
1078
1079 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001080 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001081 adjacentPage = page - 1;
1082 }
1083
1084 if (adjacentPage < 0 || adjacentPage > count - 1) {
1085 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1086 } else {
1087 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1088 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001089
1090 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001091 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1092 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001093 return scrollProgress;
1094 }
1095
Adam Cohenedb40762013-07-18 16:45:45 -07001096 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001097 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001098 return 0;
1099 } else {
1100 return mPageScrolls[index];
1101 }
1102 }
1103
Adam Cohen564a2e72013-10-09 14:47:32 -07001104 // While layout transitions are occurring, a child's position may stray from its baseline
1105 // position. This method returns the magnitude of this stray at any given time.
1106 public int getLayoutTransitionOffsetForPage(int index) {
1107 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1108 return 0;
1109 } else {
1110 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001111
Sunny Goyal228153d2018-01-04 15:35:22 -08001112 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1113 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001114 return (int) (child.getX() - baselineX);
1115 }
1116 }
1117
Jon Miranda71cb80c2019-01-24 21:25:13 -08001118 @Override
1119 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001120 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001121 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001122 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001123 super.dispatchDraw(canvas);
1124
1125 canvas.restoreToCount(saveCount);
1126 } else {
1127 super.dispatchDraw(canvas);
1128 }
1129 }
1130
Jon Miranda57dab6f2020-04-10 13:38:14 -07001131 /**
1132 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1133 */
1134 private int getSpringOverScroll(int amount) {
1135 if (mScroller.isSpringing()) {
1136 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001137 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001138 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1139 } else {
1140 return 0;
1141 }
1142 }
1143
Sunny Goyalb7b01352018-08-09 12:31:20 -07001144 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001145 if (amount == 0) {
1146 return;
1147 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001148
Vinit Nayaka406f722019-12-19 11:50:55 -08001149 int size = mOrientationHandler.getMeasuredSize(this);
1150 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001151 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001152 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001153 invalidate();
1154 return;
1155 }
1156
Vinit Nayaka406f722019-12-19 11:50:55 -08001157 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1158 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1159 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001160 invalidate();
1161 }
1162
Sunny Goyalb7b01352018-08-09 12:31:20 -07001163 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001164 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001165 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001166 invalidate();
1167 return;
1168 }
1169
Sunny Goyalb7b01352018-08-09 12:31:20 -07001170 if (amount == 0) return;
1171
1172 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001173 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1174 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001175 } else {
1176 dampedOverScroll(amount);
1177 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001178 }
1179
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001180
Tony Wickham023f4042019-01-29 10:52:31 -08001181 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001182 if (mFreeScroll == freeScroll) {
1183 return;
1184 }
1185
Tony Wickham8f7ead32016-04-07 18:46:44 -07001186 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001187 mFreeScroll = freeScroll;
1188
Adam Cohenf9618852013-11-08 06:45:03 -08001189 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001190 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001191 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001192 if (getScrollForPage(getNextPage()) != getScrollX()) {
1193 snapToPage(getNextPage());
1194 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001195 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001196 }
1197
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001198 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001199 mAllowOverScroll = enable;
1200 }
1201
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 @Override
1203 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001204 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001205 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001206
Michael Jurkab8f06722010-10-10 15:58:46 -07001207 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208
1209 final int action = ev.getAction();
1210
1211 switch (action & MotionEvent.ACTION_MASK) {
1212 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001213 updateIsBeingDraggedOnTouchDown();
1214
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 /*
1216 * If being flinged and user touches, stop the fling. isFinished
1217 * will be false if being flinged.
1218 */
1219 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001220 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 }
1222
1223 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001224 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001225 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001226 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1227 mLastMotionRemainder = 0;
1228 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001229 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001231 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001232 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001233 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001234 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 break;
1236
Winson Chung4941f652020-06-18 15:11:53 -07001237 case ACTION_MOVE_ALLOW_EASY_FLING:
1238 // Start scrolling immediately
1239 determineScrollingStart(ev);
1240 mAllowEasyFling = true;
1241 break;
1242
1243 case MotionEvent.ACTION_MOVE:
1244 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 // Scroll to follow the motion event
1246 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001247
1248 if (pointerIndex == -1) return true;
1249
Vinit Nayaka406f722019-12-19 11:50:55 -08001250 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1251 float delta = mLastMotion + mLastMotionRemainder - direction;
1252 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001253
Winson Chungc0844aa2011-02-02 15:25:58 -08001254 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1255 // keep the remainder because we are actually testing if we've moved from the last
1256 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001257 if (Math.abs(delta) >= 1.0f) {
1258 mLastMotion = direction;
1259 mLastMotionRemainder = delta - (int) delta;
1260
1261 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 } else {
1263 awakenScrollBars();
1264 }
Adam Cohen564976a2010-10-13 18:52:07 -07001265 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 determineScrollingStart(ev);
1267 }
1268 break;
1269
1270 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001271 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 final int activePointerId = mActivePointerId;
1273 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001274 if (pointerIndex == -1) return true;
1275
Vinit Nayaka406f722019-12-19 11:50:55 -08001276 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1277 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001278 final VelocityTracker velocityTracker = mVelocityTracker;
1279 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001280
Vinit Nayaka406f722019-12-19 11:50:55 -08001281 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1282 mActivePointerId);
1283 int delta = (int) (primaryDirection - mDownMotionPrimary);
1284 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1285
1286 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1287 SIGNIFICANT_MOVE_THRESHOLD;
1288
1289 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001290 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1291 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001292 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1293 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001294 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1295 Log.d("Quickswitch", "isFling=false vel=" + velocity
1296 + " threshold=" + mEasyFlingThresholdVelocity);
1297 }
Adam Cohen00481b32011-11-18 12:03:48 -08001298
Adam Cohenf358a4b2013-07-23 16:47:31 -07001299 if (!mFreeScroll) {
1300 // In the case that the page is moved far to one direction and then is flung
1301 // in the opposite direction, we use a threshold to determine whether we should
1302 // just return to the starting page, or if we should skip one further.
1303 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001304 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1305 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001306 returnToOriginalPage = true;
1307 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001308
Adam Cohenf358a4b2013-07-23 16:47:31 -07001309 int finalPage;
1310 // We give flings precedence over large moves, which is why we short-circuit our
1311 // test for a large move if a fling has been registered. That is, a large
1312 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001313
Vinit Nayaka406f722019-12-19 11:50:55 -08001314 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1315 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001316 finalPage = returnToOriginalPage
1317 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001318 snapToPageWithVelocity(finalPage, velocity);
1319 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1320 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001321 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001322 finalPage = returnToOriginalPage
1323 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001324 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001325 } else {
1326 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001327 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001329 if (!mScroller.isFinished()) {
1330 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001331 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001332
Vinit Nayaka406f722019-12-19 11:50:55 -08001333 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1334 int maxScroll = mMaxScroll;
1335 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001336
Vinit Nayaka406f722019-12-19 11:50:55 -08001337 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1338 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1339 mScroller.springBack(initialScroll, minScroll, maxScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001340 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001341 } else {
1342 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001343 mScroller.fling(initialScroll, -velocity,
1344 minScroll, maxScroll,
1345 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001346
Vinit Nayaka406f722019-12-19 11:50:55 -08001347 int finalPos = mScroller.getFinalPos();
Alex Chaub794ea62021-02-03 18:00:30 +00001348 mNextPage = getDestinationPage(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001349
1350 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1351 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001352 if (snapToPageInFreeScroll() && finalPos > minScroll
1353 && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001354 // If scrolling ends in the half of the added space that is closer to
1355 // the end, settle to the end. Otherwise snap to the nearest page.
1356 // If flinging past one of the ends, don't change the velocity as it
1357 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001358 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1359 ? minScroll
1360 : finalPos > (lastPageScroll + maxScroll) / 2
1361 ? maxScroll
1362 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001363
Vinit Nayaka406f722019-12-19 11:50:55 -08001364 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001365 // Ensure the scroll/snap doesn't happen too fast;
1366 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001367 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001368 if (extraScrollDuration > 0) {
1369 mScroller.extendDuration(extraScrollDuration);
1370 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001371 }
1372 }
1373 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001374 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001375 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001377
Adam Cohen7d30a372013-07-01 17:03:59 -07001378 // End any intermediate reordering states
1379 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 break;
1381
1382 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001383 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001384 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001385 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001386 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001387 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 break;
1389
1390 case MotionEvent.ACTION_POINTER_UP:
1391 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 break;
1394 }
1395
1396 return true;
1397 }
1398
Alex Chaub794ea62021-02-03 18:00:30 +00001399 protected boolean snapToPageInFreeScroll() {
1400 return true;
1401 }
1402
Vinit Nayaka406f722019-12-19 11:50:55 -08001403 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001404 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1405 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001406 }
1407
Adam Cohen7d30a372013-07-01 17:03:59 -07001408 private void resetTouchState() {
1409 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001410 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001411 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001412 }
1413
Adam Cohenc2d6e892014-10-16 09:49:24 -07001414 /**
1415 * Triggered by scrolling via touch
1416 */
1417 protected void onScrollInteractionBegin() {
1418 }
1419
1420 protected void onScrollInteractionEnd() {
1421 }
1422
Winson Chung185d7162011-02-28 13:47:29 -08001423 @Override
1424 public boolean onGenericMotionEvent(MotionEvent event) {
1425 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1426 switch (event.getAction()) {
1427 case MotionEvent.ACTION_SCROLL: {
1428 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1429 final float vscroll;
1430 final float hscroll;
1431 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1432 vscroll = 0;
1433 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1434 } else {
1435 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1436 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1437 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001438 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1439 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001440 }
Winson Chung185d7162011-02-28 13:47:29 -08001441 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001442 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001443 : (hscroll > 0 || vscroll > 0);
1444 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001445 scrollRight();
1446 } else {
1447 scrollLeft();
1448 }
1449 return true;
1450 }
1451 }
1452 }
1453 }
1454 return super.onGenericMotionEvent(event);
1455 }
1456
Sunny Goyal8b37c572020-03-31 12:12:14 -07001457 /**
1458 * Returns true if the paged view can scroll for the provided vertical and horizontal
1459 * scroll values
1460 */
1461 protected boolean canScroll(float absVScroll, float absHScroll) {
1462 ActivityContext ac = ActivityContext.lookupContext(getContext());
1463 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001464 }
1465
Michael Jurkab8f06722010-10-10 15:58:46 -07001466 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1467 if (mVelocityTracker == null) {
1468 mVelocityTracker = VelocityTracker.obtain();
1469 }
1470 mVelocityTracker.addMovement(ev);
1471 }
1472
1473 private void releaseVelocityTracker() {
1474 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001475 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001476 mVelocityTracker.recycle();
1477 mVelocityTracker = null;
1478 }
1479 }
1480
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001482 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 final int pointerId = ev.getPointerId(pointerIndex);
1484 if (pointerId == mActivePointerId) {
1485 // This was our active pointer going up. Choose a new
1486 // active pointer and adjust accordingly.
1487 // TODO: Make this decision more intelligent.
1488 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001489 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1490 newPointerIndex);
1491 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001492 mActivePointerId = ev.getPointerId(newPointerIndex);
1493 if (mVelocityTracker != null) {
1494 mVelocityTracker.clear();
1495 }
1496 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001497 }
1498
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 @Override
1500 public void requestChildFocus(View child, View focused) {
1501 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001502 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001503 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001504 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 }
1506 }
1507
Alex Chaub794ea62021-02-03 18:00:30 +00001508 public int getDestinationPage() {
1509 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1510 }
1511
Alex Chaudedbc8a2021-03-17 16:53:26 +00001512 protected int getDestinationPage(int primaryScroll) {
1513 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001514 }
1515
Sunny Goyal228153d2018-01-04 15:35:22 -08001516 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001517 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001518 }
1519
Alex Chaudedbc8a2021-03-17 16:53:26 +00001520 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1521 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001522 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001523 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 final int childCount = getChildCount();
1525 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001526 int distanceFromScreenCenter = Math.abs(
1527 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1529 minDistanceFromScreenCenter = distanceFromScreenCenter;
1530 minDistanceFromScreenCenterIndex = i;
1531 }
1532 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001533 return minDistanceFromScreenCenterIndex;
1534 }
1535
Tony Wickham7383d4e2020-07-07 19:25:25 -07001536 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001537 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001538 int halfChildSize = (childSize / 2);
1539 int childCenter = getChildOffset(childIndex) + halfChildSize;
1540 return childCenter - screenCenter;
1541 }
1542
1543 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001544 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1545 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001546 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1547 }
1548
Alex Chaudedbc8a2021-03-17 16:53:26 +00001549 private int getScreenCenter(int primaryScroll) {
1550 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1551 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1552 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1553 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1554 + primaryPivot);
1555 }
1556
Adam Cohend19d3ca2010-09-15 14:43:42 -07001557 protected void snapToDestination() {
Alex Chaub794ea62021-02-03 18:00:30 +00001558 snapToPage(getDestinationPage(), getPageSnapDuration());
Adam Cohen8d769d62017-06-23 17:27:38 -07001559 }
1560
1561 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001562 int scroll = mOrientationHandler.getPrimaryScroll(this);
1563 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001564 }
1565
1566 protected int getPageSnapDuration() {
1567 if (isInOverScroll()) {
1568 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1569 }
1570 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 }
1572
Adam Cohene0f66b52010-11-23 15:06:07 -08001573 // We want the duration of the page snap animation to be influenced by the distance that
1574 // the screen has to travel, however, we don't want this duration to be effected in a
1575 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1576 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001577 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001578 f -= 0.5f; // center the values about 0.
1579 f *= 0.3f * Math.PI / 2.0f;
1580 return (float) Math.sin(f);
1581 }
1582
Winson Chung05a31ed2018-02-08 17:08:43 -08001583 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001584 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001585 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001586
Vinit Nayaka406f722019-12-19 11:50:55 -08001587 final int newLoc = getScrollForPage(whichPage);
1588 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001589 int duration = 0;
1590
Sunny Goyal4d113a52015-05-27 10:05:28 -07001591 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001592 // If the velocity is low enough, then treat this more as an automatic page advance
1593 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001594 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001595 }
1596
1597 // Here we compute a "distance" that will be used in the computation of the overall
1598 // snap duration. This is a function of the actual distance that needs to be traveled;
1599 // we keep this value close to half screen size in order to reduce the variance in snap
1600 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001601 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001602 float distance = halfScreenSize + halfScreenSize *
1603 distanceInfluenceForSnapDuration(distanceRatio);
1604
1605 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001606 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001607
1608 // we want the page's snap velocity to approximately match the velocity at which the
1609 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001610 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1611 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001612
Jon Miranda9e198662020-03-11 14:42:02 -07001613 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001614 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001615 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001616 } else {
1617 return snapToPage(whichPage, delta, duration);
1618 }
Michael Jurka0142d492010-08-25 17:46:15 -07001619 }
1620
Winson Chung05a31ed2018-02-08 17:08:43 -08001621 public boolean snapToPage(int whichPage) {
1622 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001623 }
1624
Winson Chung05a31ed2018-02-08 17:08:43 -08001625 public boolean snapToPageImmediately(int whichPage) {
1626 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001627 }
1628
Winson Chung05a31ed2018-02-08 17:08:43 -08001629 public boolean snapToPage(int whichPage, int duration) {
1630 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001631 }
1632
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001633 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001634 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001635 }
1636
Winson Chung05a31ed2018-02-08 17:08:43 -08001637 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001638 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001639 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001640
Vinit Nayaka406f722019-12-19 11:50:55 -08001641 int newLoc = getScrollForPage(whichPage);
1642 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001643 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001644 }
1645
Winson Chung05a31ed2018-02-08 17:08:43 -08001646 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001647 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001648 }
Michael Jurka0142d492010-08-25 17:46:15 -07001649
Winson Chung05a31ed2018-02-08 17:08:43 -08001650 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001651 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001652 if (mFirstLayout) {
1653 setCurrentPage(whichPage);
1654 return false;
1655 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001656
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001657 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001658 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1659 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001660 }
1661
Adam Cohen6f127a62014-06-12 14:54:41 -07001662 whichPage = validateNewPage(whichPage);
1663
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001665
Winson Chung321e9ee2010-08-09 13:37:56 -07001666 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001667 if (immediate) {
1668 duration = 0;
1669 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001670 duration = Math.abs(delta);
1671 }
1672
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001673 if (duration != 0) {
1674 pageBeginTransition();
1675 }
1676
Adam Cohenf358a4b2013-07-23 16:47:31 -07001677 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001678 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001679 }
Adam Cohenf9618852013-11-08 06:45:03 -08001680
1681 if (interpolator != null) {
1682 mScroller.setInterpolator(interpolator);
1683 } else {
1684 mScroller.setInterpolator(mDefaultInterpolator);
1685 }
1686
Jon Miranda71cb80c2019-01-24 21:25:13 -08001687 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001688 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001689 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001690 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001691 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001692
Adam Cohen674531f2013-12-13 15:07:14 -08001693 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001694
1695 // Trigger a compute() to finish switching pages if necessary
1696 if (immediate) {
1697 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001698 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001699 }
1700
Winson Chung321e9ee2010-08-09 13:37:56 -07001701 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001702 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001703 }
1704
Vadim Tryshev98913d02018-05-18 18:41:34 -07001705 public boolean scrollLeft() {
1706 if (getNextPage() > 0) {
1707 snapToPage(getNextPage() - 1);
1708 return true;
1709 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001710 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001711 }
1712
Vadim Tryshev98913d02018-05-18 18:41:34 -07001713 public boolean scrollRight() {
1714 if (getNextPage() < getChildCount() - 1) {
1715 snapToPage(getNextPage() + 1);
1716 return true;
1717 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001718 return onOverscroll(getMeasuredWidth());
1719 }
1720
1721 protected boolean onOverscroll(int amount) {
1722 if (!mAllowOverScroll) return false;
1723 onScrollInteractionBegin();
1724 overScroll(amount);
1725 onScrollInteractionEnd();
1726 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001727 }
1728
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001729 @Override
1730 public CharSequence getAccessibilityClassName() {
1731 // Some accessibility services have special logic for ScrollView. Since we provide same
1732 // accessibility info as ScrollView, inform the service to handle use the same way.
1733 return ScrollView.class.getName();
1734 }
1735
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001736 protected boolean isPageOrderFlipped() {
1737 return false;
1738 }
1739
Winson Chung6a0f57d2011-06-29 20:10:49 -07001740 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001741 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001742 @Override
1743 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1744 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001745 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001746 int offset = (mAllowOverScroll ? 0 : 1);
1747 info.setScrollable(getPageCount() > offset);
1748 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001749 info.addAction(pagesFlipped ?
1750 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1751 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1752 info.addAction(mIsRtl ?
1753 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1754 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001755 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001756 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001757 info.addAction(pagesFlipped ?
1758 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1759 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1760 info.addAction(mIsRtl ?
1761 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1762 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001763 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001764 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1765 // Besides disabling the accessibility long-click, this also prevents this view from getting
1766 // accessibility focus.
1767 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001768 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001769 }
1770
1771 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001772 public void sendAccessibilityEvent(int eventType) {
1773 // Don't let the view send real scroll events.
1774 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1775 super.sendAccessibilityEvent(eventType);
1776 }
1777 }
1778
1779 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001780 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1781 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001782 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001783 }
1784
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001785 @Override
1786 public boolean performAccessibilityAction(int action, Bundle arguments) {
1787 if (super.performAccessibilityAction(action, arguments)) {
1788 return true;
1789 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001790 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001791 switch (action) {
1792 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001793 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001794 return true;
1795 }
1796 } break;
1797 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001798 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001799 return true;
1800 }
yingleiw02cc8482019-08-02 16:14:27 -07001801 } break;
1802 case android.R.id.accessibilityActionPageRight: {
1803 if (!mIsRtl) {
1804 return scrollRight();
1805 } else {
1806 return scrollLeft();
1807 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001808 }
yingleiw02cc8482019-08-02 16:14:27 -07001809 case android.R.id.accessibilityActionPageLeft: {
1810 if (!mIsRtl) {
1811 return scrollLeft();
1812 } else {
1813 return scrollRight();
1814 }
1815 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001816 }
1817 return false;
1818 }
1819
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001820 protected boolean canAnnouncePageDescription() {
1821 return true;
1822 }
1823
Adam Cohen0ffac432013-07-10 11:19:26 -07001824 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001825 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001826 getNextPage() + 1, getChildCount());
1827 }
1828
Tony Mak2e564402018-02-27 17:19:34 +00001829 protected float getDownMotionX() {
1830 return mDownMotionX;
1831 }
1832
1833 protected float getDownMotionY() {
1834 return mDownMotionY;
1835 }
1836
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001837 protected interface ComputePageScrollsLogic {
1838
1839 boolean shouldIncludeView(View view);
1840 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001841
1842 public int[] getVisibleChildrenRange() {
1843 float visibleLeft = 0;
1844 float visibleRight = visibleLeft + getMeasuredWidth();
1845 float scaleX = getScaleX();
1846 if (scaleX < 1 && scaleX > 0) {
1847 float mid = getMeasuredWidth() / 2;
1848 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1849 visibleRight = mid + ((visibleRight - mid) / scaleX);
1850 }
1851
1852 int leftChild = -1;
1853 int rightChild = -1;
1854 final int childCount = getChildCount();
1855 for (int i = 0; i < childCount; i++) {
1856 final View child = getPageAt(i);
1857
1858 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1859 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1860 if (leftChild == -1) {
1861 leftChild = i;
1862 }
1863 rightChild = i;
1864 }
1865 }
1866 mTmpIntPair[0] = leftChild;
1867 mTmpIntPair[1] = rightChild;
1868 return mTmpIntPair;
1869 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001870}