blob: 65fde8659a7c7d7e6b8823c3ca3143c3addc7ddb [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
Sunny Goyalc82c6392018-06-06 15:39:13 -0700718 // In case the pages are of different width, align the page to left or right edge
719 // based on the orientation.
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100720 // In case we have multiple panels on the screen, scrollOffsetEnd is the scroll
721 // needed for the whole visible area, so we have to divide it by panelCount.
Sunny Goyalc82c6392018-06-06 15:39:13 -0700722 final int pageScroll = mIsRtl
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100723 ? (childStart - scrollOffsetStart)
724 : Math.max(0, childPrimaryEnd - scrollOffsetEnd / getPanelCount());
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700725 if (outPageScrolls[i] != pageScroll) {
726 pageScrollChanged = true;
727 outPageScrolls[i] = pageScroll;
728 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800729 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700730 }
731 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100732
733 int panelCount = getPanelCount();
734 if (panelCount > 1) {
735 for (int i = 0; i < childCount; i++) {
736 // In case we have multiple panels, always use left panel's page scroll for all
737 // panels on the screen.
738 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
739 if (outPageScrolls[i] != adjustedScroll) {
740 outPageScrolls[i] = adjustedScroll;
741 pageScrollChanged = true;
742 }
743 }
744 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700745 return pageScrollChanged;
746 }
747
Sunny Goyala1fbd842015-05-20 13:40:27 -0700748 protected int getChildGap() {
749 return 0;
750 }
751
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700752 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800753 mMinScroll = computeMinScroll();
754 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700755 }
756
Vinit Nayaka406f722019-12-19 11:50:55 -0800757 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700758 return 0;
Tony50876bf2018-09-19 23:09:14 -0400759 }
760
Vinit Nayaka406f722019-12-19 11:50:55 -0800761 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700762 int childCount = getChildCount();
763 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700764 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700765 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700766 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700767 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700768 }
769 }
770
Adam Cohen3b185e22013-10-29 14:45:58 -0700771 public void setPageSpacing(int pageSpacing) {
772 mPageSpacing = pageSpacing;
773 requestLayout();
774 }
775
Tony50876bf2018-09-19 23:09:14 -0400776 public int getPageSpacing() {
777 return mPageSpacing;
778 }
779
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800780 private void dispatchPageCountChanged() {
781 if (mPageIndicator != null) {
782 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700783 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700784 // This ensures that when children are added, they get the correct transforms / alphas
785 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700786 invalidate();
787 }
788
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800790 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800791 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800792 dispatchPageCountChanged();
793 }
794
795 @Override
796 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800797 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700798 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800799 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700800 }
801
Adam Cohen73894962011-10-31 13:17:17 -0700802 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700803 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800804 View pageAtIndex = getPageAt(index);
805 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700806 }
807
Alex Chau7944ee52021-01-08 19:07:25 +0000808 protected int getChildVisibleSize(int index) {
809 View layout = getPageAt(index);
810 return mOrientationHandler.getMeasuredSize(layout);
811 }
812
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 @Override
814 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700815 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700816 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700817 if (immediate) {
818 setCurrentPage(page);
819 } else {
820 snapToPage(page);
821 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 return true;
823 }
824 return false;
825 }
826
827 @Override
828 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700829 int focusablePage;
830 if (mNextPage != INVALID_PAGE) {
831 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700833 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 }
Winson Chung86f77532010-08-24 11:08:22 -0700835 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700837 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 return false;
840 }
841
842 @Override
843 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800844 if (super.dispatchUnhandledMove(focused, direction)) {
845 return true;
846 }
847
848 if (mIsRtl) {
849 if (direction == View.FOCUS_LEFT) {
850 direction = View.FOCUS_RIGHT;
851 } else if (direction == View.FOCUS_RIGHT) {
852 direction = View.FOCUS_LEFT;
853 }
854 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700856 if (getCurrentPage() > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100857 int nextPage = validateNewPage(getCurrentPage() - 1);
858 snapToPage(nextPage);
859 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 return true;
861 }
862 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700863 if (getCurrentPage() < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100864 int nextPage = validateNewPage(getCurrentPage() + 1);
865 snapToPage(nextPage);
866 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 return true;
868 }
869 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800870 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 }
872
873 @Override
874 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700875 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
876 return;
877 }
878
Adam Cohen0ffac432013-07-10 11:19:26 -0700879 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700880 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700881 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 }
883 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700884 if (mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100885 int nextPage = validateNewPage(mCurrentPage - 1);
886 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100888 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700889 if (mCurrentPage < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100890 int nextPage = validateNewPage(mCurrentPage + 1);
891 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 }
893 }
894 }
895
896 /**
897 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700898 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 *
Winson Chung86f77532010-08-24 11:08:22 -0700900 * This happens when live folders requery, and if they're off page, they
901 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 */
903 @Override
904 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700905 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 View v = focused;
907 while (true) {
908 if (v == current) {
909 super.focusableViewAvailable(focused);
910 return;
911 }
912 if (v == this) {
913 return;
914 }
915 ViewParent parent = v.getParent();
916 if (parent instanceof View) {
917 v = (View)v.getParent();
918 } else {
919 return;
920 }
921 }
922 }
923
924 /**
925 * {@inheritDoc}
926 */
927 @Override
928 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
929 if (disallowIntercept) {
930 // We need to make sure to cancel our long press if
931 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700932 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700933 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 }
935 super.requestDisallowInterceptTouchEvent(disallowIntercept);
936 }
937
938 @Override
939 public boolean onInterceptTouchEvent(MotionEvent ev) {
940 /*
941 * This method JUST determines whether we want to intercept the motion.
942 * If we return true, onTouchEvent will be called and we do the actual
943 * scrolling there.
944 */
945
Winson Chung45e1d6e2010-11-09 17:19:49 -0800946 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700947 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700948
949 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800950
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 /*
952 * Shortcut the most recurring case: the user is in the dragging
953 * state and he is moving his finger. We want to intercept this
954 * motion.
955 */
956 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700957 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 return true;
959 }
960
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 switch (action & MotionEvent.ACTION_MASK) {
962 case MotionEvent.ACTION_MOVE: {
963 /*
964 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800965 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700967 if (mActivePointerId != INVALID_POINTER) {
968 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700969 }
970 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800971 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700972 // i.e. fall through to the next case (don't break)
973 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
974 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700975 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 }
977
978 case MotionEvent.ACTION_DOWN: {
979 final float x = ev.getX();
980 final float y = ev.getY();
981 // Remember location of down touch
982 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700983 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700984 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800985 mLastMotionRemainder = 0;
986 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700987 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700989
Tony Wickhama0aee212019-09-18 09:24:03 -0700990 updateIsBeingDraggedOnTouchDown();
Winson Chung321e9ee2010-08-09 13:37:56 -0700991
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 break;
993 }
994
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800996 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700997 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 break;
999
1000 case MotionEvent.ACTION_POINTER_UP:
1001 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001002 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 break;
1004 }
1005
1006 /*
1007 * The only time we want to intercept motion events is if we are in the
1008 * drag mode.
1009 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001010 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 }
1012
Tony Wickhama0aee212019-09-18 09:24:03 -07001013 /**
1014 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1015 */
1016 private void updateIsBeingDraggedOnTouchDown() {
1017 // mScroller.isFinished should be false when being flinged.
1018 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Winson Chung4941f652020-06-18 15:11:53 -07001019 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001020
1021 if (finishedScrolling) {
1022 mIsBeingDragged = false;
1023 if (!mScroller.isFinished() && !mFreeScroll) {
1024 setCurrentPage(getNextPage());
1025 pageEndTransition();
1026 }
1027 } else {
1028 mIsBeingDragged = true;
1029 }
1030 }
1031
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001032 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001033 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001034 }
1035
Adam Cohenf8d28232011-02-01 21:47:00 -08001036 protected void determineScrollingStart(MotionEvent ev) {
1037 determineScrollingStart(ev, 1.0f);
1038 }
1039
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 /*
1041 * Determines if we should change the touch state to start scrolling after the
1042 * user moves their touch point too far.
1043 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001044 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001045 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001047 if (pointerIndex == -1) return;
1048
Vinit Nayaka406f722019-12-19 11:50:55 -08001049 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1050 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001051 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001052 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001053
Vinit Nayaka406f722019-12-19 11:50:55 -08001054 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001055 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001056 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001057 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1058 mLastMotion = primaryDirection;
1059 mLastMotionRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001060 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001061 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001062 // Stop listening for things like pinches.
1063 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001064 }
1065 }
1066
1067 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001068 // Try canceling the long press. It could also have been scheduled
1069 // by a distant descendant, so use the mAllowLongPress flag to block
1070 // everything
Andras Kloczl2bb51342021-03-20 01:13:45 +01001071 getVisiblePages().forEach(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 }
1073
Adam Cohenb5ba0972011-09-07 18:02:31 -07001074 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001075 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001076
Adam Cohenedb40762013-07-18 16:45:45 -07001077 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001078 int count = getChildCount();
1079
1080 final int totalDistance;
1081
1082 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001083 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001084 adjacentPage = page - 1;
1085 }
1086
1087 if (adjacentPage < 0 || adjacentPage > count - 1) {
1088 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1089 } else {
1090 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1091 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001092
1093 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001094 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1095 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001096 return scrollProgress;
1097 }
1098
Adam Cohenedb40762013-07-18 16:45:45 -07001099 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001100 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001101 return 0;
1102 } else {
1103 return mPageScrolls[index];
1104 }
1105 }
1106
Adam Cohen564a2e72013-10-09 14:47:32 -07001107 // While layout transitions are occurring, a child's position may stray from its baseline
1108 // position. This method returns the magnitude of this stray at any given time.
1109 public int getLayoutTransitionOffsetForPage(int index) {
1110 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1111 return 0;
1112 } else {
1113 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001114
Sunny Goyal228153d2018-01-04 15:35:22 -08001115 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1116 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001117 return (int) (child.getX() - baselineX);
1118 }
1119 }
1120
Jon Miranda71cb80c2019-01-24 21:25:13 -08001121 @Override
1122 protected void dispatchDraw(Canvas canvas) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001123 if (mScroller.isSpringing() && mSpringOverScroll != 0) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001124 int saveCount = canvas.save();
Vinit Nayaka406f722019-12-19 11:50:55 -08001125 mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001126 super.dispatchDraw(canvas);
1127
1128 canvas.restoreToCount(saveCount);
1129 } else {
1130 super.dispatchDraw(canvas);
1131 }
1132 }
1133
Jon Miranda57dab6f2020-04-10 13:38:14 -07001134 /**
1135 * Returns the amount of overscroll caused by the spring in {@link OverScroller}.
1136 */
1137 private int getSpringOverScroll(int amount) {
1138 if (mScroller.isSpringing()) {
1139 return amount < 0
Tony Wickhameb885022020-04-30 23:27:39 -05001140 ? mScroller.getCurrPos() - mMinScroll
Jon Miranda57dab6f2020-04-10 13:38:14 -07001141 : Math.max(0, mScroller.getCurrPos() - mMaxScroll);
1142 } else {
1143 return 0;
1144 }
1145 }
1146
Sunny Goyalb7b01352018-08-09 12:31:20 -07001147 protected void dampedOverScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001148 if (amount == 0) {
1149 return;
1150 }
Adam Cohen8d769d62017-06-23 17:27:38 -07001151
Vinit Nayaka406f722019-12-19 11:50:55 -08001152 int size = mOrientationHandler.getMeasuredSize(this);
1153 int overScrollAmount = OverScroll.dampedScroll(amount, size);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001154 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001155 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001156 invalidate();
1157 return;
1158 }
1159
Vinit Nayaka406f722019-12-19 11:50:55 -08001160 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1161 int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll);
1162 mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount);
Adam Cohen68d73932010-11-15 10:50:58 -08001163 invalidate();
1164 }
1165
Sunny Goyalb7b01352018-08-09 12:31:20 -07001166 protected void overScroll(int amount) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001167 if (mScroller.isSpringing()) {
Jon Miranda57dab6f2020-04-10 13:38:14 -07001168 mSpringOverScroll = getSpringOverScroll(amount);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001169 invalidate();
1170 return;
1171 }
1172
Sunny Goyalb7b01352018-08-09 12:31:20 -07001173 if (amount == 0) return;
1174
1175 if (mFreeScroll && !mScroller.isFinished()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001176 int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount;
1177 mOrientationHandler.delegateScrollTo(this, scrollAmount);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001178 } else {
1179 dampedOverScroll(amount);
1180 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001181 }
1182
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001183
Tony Wickham023f4042019-01-29 10:52:31 -08001184 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001185 if (mFreeScroll == freeScroll) {
1186 return;
1187 }
1188
Tony Wickham8f7ead32016-04-07 18:46:44 -07001189 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001190 mFreeScroll = freeScroll;
1191
Adam Cohenf9618852013-11-08 06:45:03 -08001192 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001193 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001194 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001195 if (getScrollForPage(getNextPage()) != getScrollX()) {
1196 snapToPage(getNextPage());
1197 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001198 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001199 }
1200
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001201 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001202 mAllowOverScroll = enable;
1203 }
1204
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 @Override
1206 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001207 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001208 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001209
Michael Jurkab8f06722010-10-10 15:58:46 -07001210 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001211
1212 final int action = ev.getAction();
1213
1214 switch (action & MotionEvent.ACTION_MASK) {
1215 case MotionEvent.ACTION_DOWN:
Tony Wickhama0aee212019-09-18 09:24:03 -07001216 updateIsBeingDraggedOnTouchDown();
1217
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 /*
1219 * If being flinged and user touches, stop the fling. isFinished
1220 * will be false if being flinged.
1221 */
1222 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001223 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 }
1225
1226 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001227 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001228 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001229 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1230 mLastMotionRemainder = 0;
1231 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001232 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001234 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001235 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001236 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001237 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 break;
1239
Winson Chung4941f652020-06-18 15:11:53 -07001240 case ACTION_MOVE_ALLOW_EASY_FLING:
1241 // Start scrolling immediately
1242 determineScrollingStart(ev);
1243 mAllowEasyFling = true;
1244 break;
1245
1246 case MotionEvent.ACTION_MOVE:
1247 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 // Scroll to follow the motion event
1249 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001250
1251 if (pointerIndex == -1) return true;
1252
Vinit Nayaka406f722019-12-19 11:50:55 -08001253 float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1254 float delta = mLastMotion + mLastMotionRemainder - direction;
1255 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001256
Winson Chungc0844aa2011-02-02 15:25:58 -08001257 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1258 // keep the remainder because we are actually testing if we've moved from the last
1259 // scrolled position (which is discrete).
Vinit Nayaka406f722019-12-19 11:50:55 -08001260 if (Math.abs(delta) >= 1.0f) {
1261 mLastMotion = direction;
1262 mLastMotionRemainder = delta - (int) delta;
1263
1264 mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 } else {
1266 awakenScrollBars();
1267 }
Adam Cohen564976a2010-10-13 18:52:07 -07001268 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 determineScrollingStart(ev);
1270 }
1271 break;
1272
1273 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001274 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 final int activePointerId = mActivePointerId;
1276 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001277 if (pointerIndex == -1) return true;
1278
Vinit Nayaka406f722019-12-19 11:50:55 -08001279 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1280 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001281 final VelocityTracker velocityTracker = mVelocityTracker;
1282 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001283
Vinit Nayaka406f722019-12-19 11:50:55 -08001284 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1285 mActivePointerId);
1286 int delta = (int) (primaryDirection - mDownMotionPrimary);
1287 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1288
1289 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1290 SIGNIFICANT_MOVE_THRESHOLD;
1291
1292 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001293 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1294 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001295 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1296 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001297 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1298 Log.d("Quickswitch", "isFling=false vel=" + velocity
1299 + " threshold=" + mEasyFlingThresholdVelocity);
1300 }
Adam Cohen00481b32011-11-18 12:03:48 -08001301
Adam Cohenf358a4b2013-07-23 16:47:31 -07001302 if (!mFreeScroll) {
1303 // In the case that the page is moved far to one direction and then is flung
1304 // in the opposite direction, we use a threshold to determine whether we should
1305 // just return to the starting page, or if we should skip one further.
1306 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001307 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1308 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001309 returnToOriginalPage = true;
1310 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001311
Adam Cohenf358a4b2013-07-23 16:47:31 -07001312 int finalPage;
1313 // We give flings precedence over large moves, which is why we short-circuit our
1314 // test for a large move if a fling has been registered. That is, a large
1315 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001316
Vinit Nayaka406f722019-12-19 11:50:55 -08001317 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1318 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001319 finalPage = returnToOriginalPage
1320 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001321 snapToPageWithVelocity(finalPage, velocity);
1322 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1323 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001324 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001325 finalPage = returnToOriginalPage
1326 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001327 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001328 } else {
1329 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001330 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001332 if (!mScroller.isFinished()) {
1333 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001334 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001335
Vinit Nayaka406f722019-12-19 11:50:55 -08001336 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1337 int maxScroll = mMaxScroll;
1338 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001339
Vinit Nayaka406f722019-12-19 11:50:55 -08001340 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1341 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
1342 mScroller.springBack(initialScroll, minScroll, maxScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001343 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001344 } else {
1345 mScroller.setInterpolator(mDefaultInterpolator);
Vinit Nayaka406f722019-12-19 11:50:55 -08001346 mScroller.fling(initialScroll, -velocity,
1347 minScroll, maxScroll,
1348 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001349
Vinit Nayaka406f722019-12-19 11:50:55 -08001350 int finalPos = mScroller.getFinalPos();
Alex Chaub794ea62021-02-03 18:00:30 +00001351 mNextPage = getDestinationPage(finalPos);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001352
1353 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1354 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001355 if (snapToPageInFreeScroll() && finalPos > minScroll
1356 && finalPos < maxScroll) {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001357 // If scrolling ends in the half of the added space that is closer to
1358 // the end, settle to the end. Otherwise snap to the nearest page.
1359 // If flinging past one of the ends, don't change the velocity as it
1360 // will get stopped at the end anyway.
Vinit Nayaka406f722019-12-19 11:50:55 -08001361 int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2
1362 ? minScroll
1363 : finalPos > (lastPageScroll + maxScroll) / 2
1364 ? maxScroll
1365 : getScrollForPage(mNextPage);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001366
Vinit Nayaka406f722019-12-19 11:50:55 -08001367 mScroller.setFinalPos(pageSnapped);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001368 // Ensure the scroll/snap doesn't happen too fast;
1369 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
Vinit Nayaka406f722019-12-19 11:50:55 -08001370 - mScroller.getDuration();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001371 if (extraScrollDuration > 0) {
1372 mScroller.extendDuration(extraScrollDuration);
1373 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001374 }
1375 }
1376 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001378 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001380
Adam Cohen7d30a372013-07-01 17:03:59 -07001381 // End any intermediate reordering states
1382 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 break;
1384
1385 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001386 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001387 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001388 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001389 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001390 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 break;
1392
1393 case MotionEvent.ACTION_POINTER_UP:
1394 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001395 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 break;
1397 }
1398
1399 return true;
1400 }
1401
Alex Chaub794ea62021-02-03 18:00:30 +00001402 protected boolean snapToPageInFreeScroll() {
1403 return true;
1404 }
1405
Vinit Nayaka406f722019-12-19 11:50:55 -08001406 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001407 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1408 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001409 }
1410
Adam Cohen7d30a372013-07-01 17:03:59 -07001411 private void resetTouchState() {
1412 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001413 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001414 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001415 }
1416
Adam Cohenc2d6e892014-10-16 09:49:24 -07001417 /**
1418 * Triggered by scrolling via touch
1419 */
1420 protected void onScrollInteractionBegin() {
1421 }
1422
1423 protected void onScrollInteractionEnd() {
1424 }
1425
Winson Chung185d7162011-02-28 13:47:29 -08001426 @Override
1427 public boolean onGenericMotionEvent(MotionEvent event) {
1428 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1429 switch (event.getAction()) {
1430 case MotionEvent.ACTION_SCROLL: {
1431 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1432 final float vscroll;
1433 final float hscroll;
1434 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1435 vscroll = 0;
1436 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1437 } else {
1438 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1439 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1440 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001441 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1442 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001443 }
Winson Chung185d7162011-02-28 13:47:29 -08001444 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001445 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001446 : (hscroll > 0 || vscroll > 0);
1447 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001448 scrollRight();
1449 } else {
1450 scrollLeft();
1451 }
1452 return true;
1453 }
1454 }
1455 }
1456 }
1457 return super.onGenericMotionEvent(event);
1458 }
1459
Sunny Goyal8b37c572020-03-31 12:12:14 -07001460 /**
1461 * Returns true if the paged view can scroll for the provided vertical and horizontal
1462 * scroll values
1463 */
1464 protected boolean canScroll(float absVScroll, float absHScroll) {
1465 ActivityContext ac = ActivityContext.lookupContext(getContext());
1466 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001467 }
1468
Michael Jurkab8f06722010-10-10 15:58:46 -07001469 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1470 if (mVelocityTracker == null) {
1471 mVelocityTracker = VelocityTracker.obtain();
1472 }
1473 mVelocityTracker.addMovement(ev);
1474 }
1475
1476 private void releaseVelocityTracker() {
1477 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001478 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001479 mVelocityTracker.recycle();
1480 mVelocityTracker = null;
1481 }
1482 }
1483
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001485 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 final int pointerId = ev.getPointerId(pointerIndex);
1487 if (pointerId == mActivePointerId) {
1488 // This was our active pointer going up. Choose a new
1489 // active pointer and adjust accordingly.
1490 // TODO: Make this decision more intelligent.
1491 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001492 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1493 newPointerIndex);
1494 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 mActivePointerId = ev.getPointerId(newPointerIndex);
1496 if (mVelocityTracker != null) {
1497 mVelocityTracker.clear();
1498 }
1499 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001500 }
1501
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 @Override
1503 public void requestChildFocus(View child, View focused) {
1504 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001505 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001506 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001507 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 }
1509 }
1510
Alex Chaub794ea62021-02-03 18:00:30 +00001511 public int getDestinationPage() {
1512 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1513 }
1514
1515 protected int getDestinationPage(int scaledScroll) {
1516 return getPageNearestToCenterOfScreen(scaledScroll);
1517 }
1518
Sunny Goyal228153d2018-01-04 15:35:22 -08001519 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001520 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001521 }
1522
Vinit Nayaka406f722019-12-19 11:50:55 -08001523 private int getPageNearestToCenterOfScreen(int scaledScroll) {
1524 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1525 int screenCenter = scaledScroll + (pageOrientationSize / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001526 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 final int childCount = getChildCount();
1529 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001530 int distanceFromScreenCenter = Math.abs(
1531 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001532 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1533 minDistanceFromScreenCenter = distanceFromScreenCenter;
1534 minDistanceFromScreenCenterIndex = i;
1535 }
1536 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001537 return minDistanceFromScreenCenterIndex;
1538 }
1539
Tony Wickham7383d4e2020-07-07 19:25:25 -07001540 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chau7944ee52021-01-08 19:07:25 +00001541 int childSize = getChildVisibleSize(childIndex);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001542 int halfChildSize = (childSize / 2);
1543 int childCenter = getChildOffset(childIndex) + halfChildSize;
1544 return childCenter - screenCenter;
1545 }
1546
1547 protected int getDisplacementFromScreenCenter(int childIndex) {
1548 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1549 int screenCenter = mOrientationHandler.getPrimaryScroll(this) + (pageOrientationSize / 2);
1550 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1551 }
1552
Adam Cohend19d3ca2010-09-15 14:43:42 -07001553 protected void snapToDestination() {
Alex Chaub794ea62021-02-03 18:00:30 +00001554 snapToPage(getDestinationPage(), getPageSnapDuration());
Adam Cohen8d769d62017-06-23 17:27:38 -07001555 }
1556
1557 protected boolean isInOverScroll() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001558 int scroll = mOrientationHandler.getPrimaryScroll(this);
1559 return scroll > mMaxScroll || scroll < mMinScroll;
Adam Cohen8d769d62017-06-23 17:27:38 -07001560 }
1561
1562 protected int getPageSnapDuration() {
1563 if (isInOverScroll()) {
1564 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1565 }
1566 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001567 }
1568
Adam Cohene0f66b52010-11-23 15:06:07 -08001569 // We want the duration of the page snap animation to be influenced by the distance that
1570 // the screen has to travel, however, we don't want this duration to be effected in a
1571 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1572 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001573 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001574 f -= 0.5f; // center the values about 0.
1575 f *= 0.3f * Math.PI / 2.0f;
1576 return (float) Math.sin(f);
1577 }
1578
Winson Chung05a31ed2018-02-08 17:08:43 -08001579 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001580 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001581 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001582
Vinit Nayaka406f722019-12-19 11:50:55 -08001583 final int newLoc = getScrollForPage(whichPage);
1584 int delta = newLoc - getUnboundedScroll();
Adam Cohene0f66b52010-11-23 15:06:07 -08001585 int duration = 0;
1586
Sunny Goyal4d113a52015-05-27 10:05:28 -07001587 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001588 // If the velocity is low enough, then treat this more as an automatic page advance
1589 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001590 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001591 }
1592
1593 // Here we compute a "distance" that will be used in the computation of the overall
1594 // snap duration. This is a function of the actual distance that needs to be traveled;
1595 // we keep this value close to half screen size in order to reduce the variance in snap
1596 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001597 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001598 float distance = halfScreenSize + halfScreenSize *
1599 distanceInfluenceForSnapDuration(distanceRatio);
1600
1601 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001602 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001603
1604 // we want the page's snap velocity to approximately match the velocity at which the
1605 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001606 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1607 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001608
Jon Miranda9e198662020-03-11 14:42:02 -07001609 if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001610 return snapToPage(whichPage, delta, duration, false, null,
Vinit Nayaka406f722019-12-19 11:50:55 -08001611 velocity * Math.signum(delta), true);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001612 } else {
1613 return snapToPage(whichPage, delta, duration);
1614 }
Michael Jurka0142d492010-08-25 17:46:15 -07001615 }
1616
Winson Chung05a31ed2018-02-08 17:08:43 -08001617 public boolean snapToPage(int whichPage) {
1618 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001619 }
1620
Winson Chung05a31ed2018-02-08 17:08:43 -08001621 public boolean snapToPageImmediately(int whichPage) {
1622 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001623 }
1624
Winson Chung05a31ed2018-02-08 17:08:43 -08001625 public boolean snapToPage(int whichPage, int duration) {
1626 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001627 }
1628
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001629 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001630 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001631 }
1632
Winson Chung05a31ed2018-02-08 17:08:43 -08001633 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001634 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001635 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001636
Vinit Nayaka406f722019-12-19 11:50:55 -08001637 int newLoc = getScrollForPage(whichPage);
1638 final int delta = newLoc - getUnboundedScroll();
Jon Miranda71cb80c2019-01-24 21:25:13 -08001639 return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false);
Michael Jurka0142d492010-08-25 17:46:15 -07001640 }
1641
Winson Chung05a31ed2018-02-08 17:08:43 -08001642 protected boolean snapToPage(int whichPage, int delta, int duration) {
Jon Miranda71cb80c2019-01-24 21:25:13 -08001643 return snapToPage(whichPage, delta, duration, false, null, 0, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001644 }
Michael Jurka0142d492010-08-25 17:46:15 -07001645
Winson Chung05a31ed2018-02-08 17:08:43 -08001646 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Jon Miranda71cb80c2019-01-24 21:25:13 -08001647 TimeInterpolator interpolator, float velocity, boolean spring) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001648 if (mFirstLayout) {
1649 setCurrentPage(whichPage);
1650 return false;
1651 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001652
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001653 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001654 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1655 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001656 }
1657
Adam Cohen6f127a62014-06-12 14:54:41 -07001658 whichPage = validateNewPage(whichPage);
1659
Adam Cohen7d30a372013-07-01 17:03:59 -07001660 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001661
Winson Chung321e9ee2010-08-09 13:37:56 -07001662 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001663 if (immediate) {
1664 duration = 0;
1665 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001666 duration = Math.abs(delta);
1667 }
1668
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001669 if (duration != 0) {
1670 pageBeginTransition();
1671 }
1672
Adam Cohenf358a4b2013-07-23 16:47:31 -07001673 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001674 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001675 }
Adam Cohenf9618852013-11-08 06:45:03 -08001676
1677 if (interpolator != null) {
1678 mScroller.setInterpolator(interpolator);
1679 } else {
1680 mScroller.setInterpolator(mDefaultInterpolator);
1681 }
1682
Jon Miranda71cb80c2019-01-24 21:25:13 -08001683 if (spring && QUICKSTEP_SPRINGS.get()) {
Vinit Nayaka406f722019-12-19 11:50:55 -08001684 mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001685 } else {
Vinit Nayaka406f722019-12-19 11:50:55 -08001686 mScroller.startScroll(getUnboundedScroll(), delta, duration);
Jon Miranda71cb80c2019-01-24 21:25:13 -08001687 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001688
Adam Cohen674531f2013-12-13 15:07:14 -08001689 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001690
1691 // Trigger a compute() to finish switching pages if necessary
1692 if (immediate) {
1693 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001694 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001695 }
1696
Winson Chung321e9ee2010-08-09 13:37:56 -07001697 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001698 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001699 }
1700
Vadim Tryshev98913d02018-05-18 18:41:34 -07001701 public boolean scrollLeft() {
1702 if (getNextPage() > 0) {
1703 snapToPage(getNextPage() - 1);
1704 return true;
1705 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001706 return onOverscroll(-getMeasuredWidth());
Winson Chung321e9ee2010-08-09 13:37:56 -07001707 }
1708
Vadim Tryshev98913d02018-05-18 18:41:34 -07001709 public boolean scrollRight() {
1710 if (getNextPage() < getChildCount() - 1) {
1711 snapToPage(getNextPage() + 1);
1712 return true;
1713 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001714 return onOverscroll(getMeasuredWidth());
1715 }
1716
1717 protected boolean onOverscroll(int amount) {
1718 if (!mAllowOverScroll) return false;
1719 onScrollInteractionBegin();
1720 overScroll(amount);
1721 onScrollInteractionEnd();
1722 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -07001723 }
1724
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001725 @Override
1726 public CharSequence getAccessibilityClassName() {
1727 // Some accessibility services have special logic for ScrollView. Since we provide same
1728 // accessibility info as ScrollView, inform the service to handle use the same way.
1729 return ScrollView.class.getName();
1730 }
1731
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001732 protected boolean isPageOrderFlipped() {
1733 return false;
1734 }
1735
Winson Chung6a0f57d2011-06-29 20:10:49 -07001736 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001737 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001738 @Override
1739 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1740 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001741 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001742 int offset = (mAllowOverScroll ? 0 : 1);
1743 info.setScrollable(getPageCount() > offset);
1744 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001745 info.addAction(pagesFlipped ?
1746 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1747 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1748 info.addAction(mIsRtl ?
1749 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1750 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001751 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001752 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001753 info.addAction(pagesFlipped ?
1754 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1755 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1756 info.addAction(mIsRtl ?
1757 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1758 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001759 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001760 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1761 // Besides disabling the accessibility long-click, this also prevents this view from getting
1762 // accessibility focus.
1763 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001764 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001765 }
1766
1767 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001768 public void sendAccessibilityEvent(int eventType) {
1769 // Don't let the view send real scroll events.
1770 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1771 super.sendAccessibilityEvent(eventType);
1772 }
1773 }
1774
1775 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001776 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1777 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001778 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001779 }
1780
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001781 @Override
1782 public boolean performAccessibilityAction(int action, Bundle arguments) {
1783 if (super.performAccessibilityAction(action, arguments)) {
1784 return true;
1785 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001786 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001787 switch (action) {
1788 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001789 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001790 return true;
1791 }
1792 } break;
1793 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001794 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001795 return true;
1796 }
yingleiw02cc8482019-08-02 16:14:27 -07001797 } break;
1798 case android.R.id.accessibilityActionPageRight: {
1799 if (!mIsRtl) {
1800 return scrollRight();
1801 } else {
1802 return scrollLeft();
1803 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001804 }
yingleiw02cc8482019-08-02 16:14:27 -07001805 case android.R.id.accessibilityActionPageLeft: {
1806 if (!mIsRtl) {
1807 return scrollLeft();
1808 } else {
1809 return scrollRight();
1810 }
1811 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001812 }
1813 return false;
1814 }
1815
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001816 protected boolean canAnnouncePageDescription() {
1817 return true;
1818 }
1819
Adam Cohen0ffac432013-07-10 11:19:26 -07001820 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001821 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001822 getNextPage() + 1, getChildCount());
1823 }
1824
Tony Mak2e564402018-02-27 17:19:34 +00001825 protected float getDownMotionX() {
1826 return mDownMotionX;
1827 }
1828
1829 protected float getDownMotionY() {
1830 return mDownMotionY;
1831 }
1832
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001833 protected interface ComputePageScrollsLogic {
1834
1835 boolean shouldIncludeView(View view);
1836 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001837
1838 public int[] getVisibleChildrenRange() {
1839 float visibleLeft = 0;
1840 float visibleRight = visibleLeft + getMeasuredWidth();
1841 float scaleX = getScaleX();
1842 if (scaleX < 1 && scaleX > 0) {
1843 float mid = getMeasuredWidth() / 2;
1844 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1845 visibleRight = mid + ((visibleRight - mid) / scaleX);
1846 }
1847
1848 int leftChild = -1;
1849 int rightChild = -1;
1850 final int childCount = getChildCount();
1851 for (int i = 0; i < childCount; i++) {
1852 final View child = getPageAt(i);
1853
1854 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1855 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1856 if (leftChild == -1) {
1857 leftChild = i;
1858 }
1859 rightChild = i;
1860 }
1861 }
1862 mTmpIntPair[0] = leftChild;
1863 mTmpIntPair[1] = rightChild;
1864 return mTmpIntPair;
1865 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001866}