blob: e4ee50a0b197385fc744dfbf8919a3b27034b6c8 [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
Andras Kloczl55edfe52021-05-14 12:21:30 +020019import static androidx.annotation.VisibleForTesting.PACKAGE_PRIVATE;
Andras Kloczldbf577a2021-05-26 15:02:52 +020020
Sunny Goyalc39db732021-04-03 01:20:21 -070021import static com.android.launcher3.anim.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070022import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
23import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyal8b37c572020-03-31 12:12:14 -070024import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070025import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
26import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
27
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070028import android.animation.LayoutTransition;
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;
Sunny Goyalc39db732021-04-03 01:20:21 -070049import android.widget.OverScroller;
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;
Andras Kloczl55edfe52021-05-14 12:21:30 +020053import androidx.annotation.VisibleForTesting;
Tony Wickham62117d72020-03-26 16:56:26 -070054
Jon Miranda9e198662020-03-11 14:42:02 -070055import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070058import com.android.launcher3.touch.PagedOrientationHandler;
59import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070060import com.android.launcher3.util.EdgeEffectCompat;
Andras Kloczl55edfe52021-05-14 12:21:30 +020061import com.android.launcher3.util.IntSet;
Jon Miranda9e198662020-03-11 14:42:02 -070062import 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;
Sunny Goyal59969372021-05-06 12:11:44 -070066import java.util.function.Consumer;
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
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Sunny Goyal8e2133b2015-05-06 13:39:07 -070087 private static final float MAX_SCROLL_PROGRESS = 1.0f;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 // The following constants need to be scaled based on density. The scaled versions will be
90 // assigned to the corresponding member variables below.
91 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070092 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080093 private static final int MIN_SNAP_VELOCITY = 1500;
94 private static final int MIN_FLING_VELOCITY = 250;
95
Adam Cohenf358a4b2013-07-23 16:47:31 -070096 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070097
Winson Chung4941f652020-06-18 15:11:53 -070098 protected final int mFlingThresholdVelocity;
99 protected final int mEasyFlingThresholdVelocity;
100 protected final int mMinFlingVelocity;
101 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700102
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected boolean mFirstLayout = true;
104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mCurrentPage;
Alex Chauba61b982021-06-15 16:29:19 +0100107 // Difference between current scroll position and mCurrentPage's page scroll. Used to maintain
108 // relative scroll position unchanged in updateCurrentPageScroll. Cleared when snapping to a
109 // page.
110 protected int mCurrentPageScrollDiff;
Lais Andradecc5c8842021-06-04 18:20:30 +0100111 // The current page the PagedView is scrolling over on it's way to the destination page.
112 protected int mCurrentScrollOverPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700113
Sunny Goyal4ffec482016-02-09 11:28:52 -0800114 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800116 protected int mMaxScroll;
117 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700118 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800120 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
122 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700123 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800124 private float mDownMotionPrimary;
125 private float mLastMotion;
126 private float mLastMotionRemainder;
127 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700128 // Used in special cases where the fling checks can be relaxed for an intentional gesture
129 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700130 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700131
Vadim Trysheve628c472018-05-16 14:28:18 -0700132 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700133 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800134
Winson Chung4941f652020-06-18 15:11:53 -0700135 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700137 // The amount of movement to begin paging
138 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800140 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka5f1c5092010-09-03 14:15:02 -0700144 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700146 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700147 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148
Winson Chungd2be3812013-07-16 11:11:32 -0700149 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700150 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800151 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700152
John Spurlock77e1f472013-09-11 10:09:51 -0400153 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800154 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400155
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800156 // Similar to the platform implementation of isLayoutValid();
157 protected boolean mIsLayoutValid;
158
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700159 private int[] mTmpIntPair = new int[2];
160
Sunny Goyalc39db732021-04-03 01:20:21 -0700161 protected EdgeEffectCompat mEdgeGlowLeft;
162 protected EdgeEffectCompat mEdgeGlowRight;
163
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 public PagedView(Context context) {
165 this(context, null);
166 }
167
168 public PagedView(Context context, AttributeSet attrs) {
169 this(context, attrs, 0);
170 }
171
172 public PagedView(Context context, AttributeSet attrs, int defStyle) {
173 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700174
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 TypedArray a = context.obtainStyledAttributes(attrs,
176 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700177 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700178 a.recycle();
179
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700181 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Sunny Goyalc39db732021-04-03 01:20:21 -0700183 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700184 mCurrentPage = 0;
Lais Andradecc5c8842021-06-04 18:20:30 +0100185 mCurrentScrollOverPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Vinit Nayaka406f722019-12-19 11:50:55 -0800187 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700188 mTouchSlop = configuration.getScaledTouchSlop();
189 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800191
Sunny Goyalcf25b522015-07-09 00:01:18 -0700192 float density = getResources().getDisplayMetrics().density;
193 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700194 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700195 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
196 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700197
Sunny Goyalc39db732021-04-03 01:20:21 -0700198 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700199 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700200 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202
Sunny Goyalc39db732021-04-03 01:20:21 -0700203 protected void initEdgeEffect() {
204 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
205 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800206 }
207
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700208 public void initParentViews(View parent) {
209 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800210 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700211 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700212 }
213 }
214
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800215 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700216 return mPageIndicator;
217 }
218
Adam Cohen674531f2013-12-13 15:07:14 -0800219 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700220 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
221 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000222 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700223 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700225 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700226 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700228
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700229 /**
230 * Returns the index of page to be shown immediately afterwards.
231 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700232 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700233 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
234 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700235
Adam Cohenf9c184a2016-01-15 16:47:43 -0800236 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 return getChildCount();
238 }
239
Sunny Goyal83a8f042015-05-19 12:52:12 -0700240 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 return getChildAt(index);
242 }
243
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800245 * Updates the scroll of the current page immediately to its final scroll position. We use this
246 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
247 * the previous tab page.
248 */
249 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700250 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800251 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700252 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Alex Chauba61b982021-06-15 16:29:19 +0100253 newPosition = getScrollForPage(mCurrentPage) + mCurrentPageScrollDiff;
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800255 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700256 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700257 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800258 }
259
Tony Wickhamf8d731a2021-05-26 14:58:13 -0700260 /**
261 * Immediately finishes any overscroll effect and jumps to the end of the scroller animation.
262 */
263 public void abortScrollerAnimation() {
264 mEdgeGlowLeft.finish();
265 mEdgeGlowRight.finish();
266 abortScrollerAnimation(true);
267 }
268
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700269 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700270 mScroller.abortAnimation();
271 // We need to clean up the next page here to avoid computeScrollHelper from
272 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700273 if (resetNextPage) {
274 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800275 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700276 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700277 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700278
Tony Wickham8f7ead32016-04-07 18:46:44 -0700279 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700280 mScroller.forceFinished(true);
281 // We need to clean up the next page here to avoid computeScrollHelper from
282 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700283 if (resetNextPage) {
284 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800285 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700286 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700287 }
288
Adam Cohen6f127a62014-06-12 14:54:41 -0700289 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700290 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700291 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100292 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
293
294 if (getPanelCount() > 1) {
295 // Always return left panel as new page
296 newPage = getLeftmostVisiblePageForIndex(newPage);
297 }
298 return newPage;
299 }
300
Andras Kloczl55edfe52021-05-14 12:21:30 +0200301 /**
302 * In most cases where panelCount is 1, this method will just return the page index that was
303 * passed in.
304 * But for example when two panel home is enabled we might need the leftmost visible page index
305 * because that page is the current page.
306 */
307 public int getLeftmostVisiblePageForIndex(int pageIndex) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100308 int panelCount = getPanelCount();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200309 return pageIndex - pageIndex % panelCount;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100310 }
311
312 /**
313 * Returns the number of pages that are shown at the same time.
314 */
315 protected int getPanelCount() {
316 return 1;
317 }
318
319 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +0200320 * Returns an IntSet with the indices of the currently visible pages
321 */
322 @VisibleForTesting(otherwise = PACKAGE_PRIVATE)
323 public IntSet getVisiblePageIndices() {
Andras Kloczl7e912442021-05-06 12:50:28 +0200324 return getPageIndices(mCurrentPage);
325 }
326
327 /**
328 * In case the panelCount is 1 this just returns the same page index in an IntSet.
329 * But in cases where the panelCount > 1 this will return all the page indices that belong
330 * together, i.e. on the Workspace they are next to each other and shown at the same time.
331 */
332 private IntSet getPageIndices(int pageIndex) {
333 // we want to make sure the pageIndex is the leftmost page
334 pageIndex = getLeftmostVisiblePageForIndex(pageIndex);
335
336 IntSet pageIndices = new IntSet();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200337 int panelCount = getPanelCount();
338 int pageCount = getPageCount();
Andras Kloczl7e912442021-05-06 12:50:28 +0200339 for (int page = pageIndex; page < pageIndex + panelCount && page < pageCount; page++) {
340 pageIndices.add(page);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200341 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200342 return pageIndices;
343 }
344
345 /**
346 * Returns an IntSet with the indices of the neighbour pages that are in the focus direction.
347 */
348 private IntSet getNeighbourPageIndices(int focus) {
349 int panelCount = getPanelCount();
350 // getNextPage is more reliable than getCurrentPage
351 int currentPage = getNextPage();
352
353 int nextPage;
354 if (focus == View.FOCUS_LEFT) {
355 nextPage = currentPage - panelCount;
356 } else if (focus == View.FOCUS_RIGHT) {
357 nextPage = currentPage + panelCount;
358 } else {
Andras Kloczlb0632942021-08-03 14:03:58 +0200359 // no neighbours to other directions
Andras Kloczl7e912442021-05-06 12:50:28 +0200360 return new IntSet();
361 }
362 nextPage = validateNewPage(nextPage);
363 if (nextPage == currentPage) {
364 // We reached the end of the pages
365 return new IntSet();
366 }
367
Andras Kloczlb0632942021-08-03 14:03:58 +0200368 return getPageIndices(nextPage);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200369 }
370
371 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700372 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100373 */
Sunny Goyal59969372021-05-06 12:11:44 -0700374 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200375 getVisiblePageIndices().forEach(pageIndex -> {
376 View page = getPageAt(pageIndex);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100377 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700378 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100379 }
Andras Kloczl55edfe52021-05-14 12:21:30 +0200380 });
Andras Kloczl2bb51342021-03-20 01:13:45 +0100381 }
382
383 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100384 * Returns true if the view is on one of the current pages, false otherwise.
385 */
386 public boolean isVisible(View child) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200387 return isVisible(indexOfChild(child));
388 }
389
390 /**
391 * Returns true if the page with the given index is currently visible, false otherwise.
392 */
393 private boolean isVisible(int pageIndex) {
394 return getLeftmostVisiblePageForIndex(pageIndex) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700395 }
396
Chet Haasebc2f0822012-10-26 17:59:53 -0700397 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700398 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
399 */
400 private int ensureWithinScrollBounds(int page) {
401 int dir = !mIsRtl ? 1 : - 1;
402 int currScroll = getScrollForPage(page);
403 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800404 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700405 page += dir;
406 prevScroll = currScroll;
407 currScroll = getScrollForPage(page);
408 if (currScroll <= prevScroll) {
409 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
410 break;
411 }
412 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800413 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700414 page -= dir;
415 prevScroll = currScroll;
416 currScroll = getScrollForPage(page);
417 if (currScroll >= prevScroll) {
418 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
419 break;
420 }
421 }
422 return page;
423 }
424
Tony Wickham03f27012019-04-25 14:22:54 -0700425 public void setCurrentPage(int currentPage) {
426 setCurrentPage(currentPage, INVALID_PAGE);
427 }
428
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700429 /**
Winson Chung86f77532010-08-24 11:08:22 -0700430 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 */
Tony Wickham03f27012019-04-25 14:22:54 -0700432 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800433 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700434 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800435 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800436 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
437 // the default
438 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800439 return;
440 }
Tony Wickham03f27012019-04-25 14:22:54 -0700441 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700442 mCurrentPage = validateNewPage(currentPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100443 mCurrentScrollOverPage = mCurrentPage;
Winson Chung181c3dc2013-07-17 15:36:20 -0700444 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700445 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800446 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 }
448
Winson Chung8c87cd82013-07-23 16:20:10 -0700449 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800450 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
451 * has settled.
452 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700453 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800454 updatePageIndicator();
455 }
456
457 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700458 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000459 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700460 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700462 protected void pageBeginTransition() {
463 if (!mIsPageInTransition) {
464 mIsPageInTransition = true;
465 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700466 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700467 }
468
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700469 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700470 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700471 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700472 mIsPageInTransition = false;
473 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700474 }
Michael Jurka0142d492010-08-25 17:46:15 -0700475 }
476
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700477 @Override
478 public void onVisibilityAggregated(boolean isVisible) {
479 pageEndTransition();
480 super.onVisibilityAggregated(isVisible);
481 }
482
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700483 protected boolean isPageInTransition() {
484 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700485 }
486
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700487 /**
488 * Called when the page starts moving as part of the scroll. Subclasses can override this
489 * to provide custom behavior during animation.
490 */
491 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700492 }
493
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700494 /**
495 * Called when the page ends moving as part of the scroll. Subclasses can override this
496 * to provide custom behavior during animation.
497 */
498 protected void onPageEndTransition() {
Alex Chauba61b982021-06-15 16:29:19 +0100499 mCurrentPageScrollDiff = 0;
vadimt4d93df52019-07-10 17:32:58 -0700500 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800501 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
502 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700503 if (mOnPageTransitionEndCallback != null) {
504 mOnPageTransitionEndCallback.run();
505 mOnPageTransitionEndCallback = null;
506 }
507 }
508
509 /**
510 * Sets a callback to run once when the scrolling finishes. If there is currently
511 * no page in transition, then the callback is called immediately.
512 */
513 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
514 if (mIsPageInTransition || callback == null) {
515 mOnPageTransitionEndCallback = callback;
516 } else {
517 callback.run();
518 }
Michael Jurka0142d492010-08-25 17:46:15 -0700519 }
520
Adam Cohen68d73932010-11-15 10:50:58 -0800521 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700522 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700523 x = Utilities.boundToRange(x,
524 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
525 y = Utilities.boundToRange(y,
526 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800527 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700528 }
529
Adam Cohen53805212013-10-01 10:39:23 -0700530 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800531 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700532 if (mCurrentPage != getNextPage()) {
533 AccessibilityEvent ev =
534 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700535 ev.setScrollable(true);
536 ev.setScrollX(getScrollX());
537 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800538 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700539 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700540 }
Adam Cohen53805212013-10-01 10:39:23 -0700541 }
542 }
543
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700544 protected void announcePageForAccessibility() {
545 if (isAccessibilityEnabled(getContext())) {
546 // Notify the user when the page changes
547 announceForAccessibility(getCurrentPageDescription());
548 }
549 }
550
Winson Chung85425412021-05-10 16:24:14 -0700551 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700553 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700554 int oldPos = mOrientationHandler.getPrimaryScroll(this);
555 int newPos = mScroller.getCurrX();
556 if (oldPos != newPos) {
557 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700558 }
Winson Chung85425412021-05-10 16:24:14 -0700559
560 if (mAllowOverScroll) {
561 if (newPos < mMinScroll && oldPos >= mMinScroll) {
562 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
563 mScroller.abortAnimation();
Lais Andradecc5c8842021-06-04 18:20:30 +0100564 onEdgeAbsorbingScroll();
Winson Chung85425412021-05-10 16:24:14 -0700565 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
566 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
567 mScroller.abortAnimation();
Lais Andradecc5c8842021-06-04 18:20:30 +0100568 onEdgeAbsorbingScroll();
Sunny Goyalc39db732021-04-03 01:20:21 -0700569 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800570 }
Adam Cohen53805212013-10-01 10:39:23 -0700571
Winson Chung85425412021-05-10 16:24:14 -0700572 // If the scroller has scrolled to the final position and there is no edge effect, then
573 // finish the scroller to skip waiting for additional settling
574 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
575 mScroller.getFinalY());
576 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
577 mScroller.abortAnimation();
578 }
579
580 invalidate();
581 return true;
582 } else if (mNextPage != INVALID_PAGE) {
583 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700584 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700585 mCurrentPage = validateNewPage(mNextPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100586 mCurrentScrollOverPage = mCurrentPage;
Winson Chung86f77532010-08-24 11:08:22 -0700587 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700588 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700589
Adam Cohen73aa9752010-11-24 16:26:58 -0800590 // We don't want to trigger a page end moving unless the page has settled
591 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700592 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700593 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800594 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700595
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700596 if (canAnnouncePageDescription()) {
597 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700598 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
Michael Jurka0142d492010-08-25 17:46:15 -0700600 return false;
601 }
602
603 @Override
604 public void computeScroll() {
605 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700606 }
607
Sunny Goyalce8809a2018-01-18 14:02:47 -0800608 public int getExpectedHeight() {
609 return getMeasuredHeight();
610 }
611
Adam Cohen410f3cd2013-09-22 12:09:32 -0700612 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800613 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800614 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700615 }
616
Sunny Goyalce8809a2018-01-18 14:02:47 -0800617 public int getExpectedWidth() {
618 return getMeasuredWidth();
619 }
620
Sunny Goyalbc683e92017-12-06 10:25:07 -0800621 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800622 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800623 - mInsets.left - mInsets.right;
624 }
625
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800627 public void requestLayout() {
628 mIsLayoutValid = false;
629 super.requestLayout();
630 }
631
632 @Override
633 public void forceLayout() {
634 mIsLayoutValid = false;
635 super.forceLayout();
636 }
637
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100638 private int getPageWidthSize(int widthSize) {
639 return (widthSize - mInsets.left - mInsets.right) / getPanelCount();
640 }
641
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800642 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700644 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700645 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
646 return;
647 }
648
Adam Cohen7d30a372013-07-01 17:03:59 -0700649 // We measure the dimensions of the PagedView to be larger than the pages so that when we
650 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700651 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
652 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
653 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700654 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700655
Adam Cohen7d30a372013-07-01 17:03:59 -0700656 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
657 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
658 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 }
660
Winson Chung8aad6102012-05-11 16:27:49 -0700661 // Return early if we aren't given a proper dimension
662 if (widthSize <= 0 || heightSize <= 0) {
663 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
664 return;
665 }
666
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700668 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700669 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700670
Sunny Goyalac00cba2017-11-13 15:58:01 -0800671 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100672 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800673 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800674 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700675
Sunny Goyalac00cba2017-11-13 15:58:01 -0800676 // measureChildren takes accounts for content padding, we only need to care about extra
677 // space due to insets.
678 measureChildren(myWidthSpec, myHeightSpec);
679 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800680 }
681
Sunny Goyalcf25b522015-07-09 00:01:18 -0700682 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700683 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700684 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800685 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700686 final int childCount = getChildCount();
687 boolean pageScrollChanged = false;
688 if (mPageScrolls == null || childCount != mPageScrolls.length) {
689 mPageScrolls = new int[childCount];
690 pageScrollChanged = true;
691 }
692
693 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700694 return;
695 }
696
Winson Chung785d2eb2011-04-14 16:08:02 -0700697 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700698
Vinit Nayaka406f722019-12-19 11:50:55 -0800699 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
700 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700701 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700702 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700703
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700704 final LayoutTransition transition = getLayoutTransition();
705 // If the transition is running defer updating max scroll, as some empty pages could
706 // still be present, and a max scroll change could cause sudden jumps in scroll.
707 if (transition != null && transition.isRunning()) {
708 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
709
710 @Override
711 public void startTransition(LayoutTransition transition, ViewGroup container,
712 View view, int transitionType) { }
713
714 @Override
715 public void endTransition(LayoutTransition transition, ViewGroup container,
716 View view, int transitionType) {
717 // Wait until all transitions are complete.
718 if (!transition.isRunning()) {
719 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700720 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700721 }
722 }
723 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700724 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700725 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700726 }
727
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700728 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
729 updateCurrentPageScroll();
730 mFirstLayout = false;
731 }
732
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800733 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700734 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700735 }
Winson Chunge3193b92010-09-10 11:44:42 -0700736 }
737
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700738 /**
739 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
740 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700741 */
742 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
743 ComputePageScrollsLogic scrollLogic) {
744 final int childCount = getChildCount();
745
746 final int startIndex = mIsRtl ? childCount - 1 : 0;
747 final int endIndex = mIsRtl ? -1 : childCount;
748 final int delta = mIsRtl ? -1 : 1;
749
Vinit Nayaka406f722019-12-19 11:50:55 -0800750 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700751
Vinit Nayaka406f722019-12-19 11:50:55 -0800752 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
753 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700754 boolean pageScrollChanged = false;
Thales Limae4457742021-07-20 16:46:38 +0100755 int panelCount = getPanelCount();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700756
Vinit Nayaka406f722019-12-19 11:50:55 -0800757 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700758 final View child = getPageAt(i);
759 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800760 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
761 pageCenter, layoutChildren);
762 final int primaryDimension = bounds.primaryDimension;
763 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700764
Alex Chaub5c4d112021-03-29 17:37:53 +0100765 // In case the pages are of different width, align the page to left edge for non-RTL
766 // or right edge for RTL.
767 final int pageScroll =
768 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700769 if (outPageScrolls[i] != pageScroll) {
770 pageScrollChanged = true;
771 outPageScrolls[i] = pageScroll;
772 }
Thales Limae4457742021-07-20 16:46:38 +0100773 childStart += primaryDimension + getChildGap();
774
775 // This makes sure that the space is added after the page, not after each panel
776 if (i % panelCount == panelCount - 1) {
777 childStart += mPageSpacing;
778 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700779 }
780 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100781
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100782 if (panelCount > 1) {
783 for (int i = 0; i < childCount; i++) {
784 // In case we have multiple panels, always use left panel's page scroll for all
785 // panels on the screen.
786 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
787 if (outPageScrolls[i] != adjustedScroll) {
788 outPageScrolls[i] = adjustedScroll;
789 pageScrollChanged = true;
790 }
791 }
792 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700793 return pageScrollChanged;
794 }
795
Sunny Goyala1fbd842015-05-20 13:40:27 -0700796 protected int getChildGap() {
797 return 0;
798 }
799
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700800 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800801 mMinScroll = computeMinScroll();
802 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700803 }
804
Vinit Nayaka406f722019-12-19 11:50:55 -0800805 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700806 return 0;
Tony50876bf2018-09-19 23:09:14 -0400807 }
808
Vinit Nayaka406f722019-12-19 11:50:55 -0800809 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700810 int childCount = getChildCount();
811 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700812 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700813 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700814 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700815 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700816 }
817 }
818
Adam Cohen3b185e22013-10-29 14:45:58 -0700819 public void setPageSpacing(int pageSpacing) {
820 mPageSpacing = pageSpacing;
821 requestLayout();
822 }
823
Tony50876bf2018-09-19 23:09:14 -0400824 public int getPageSpacing() {
825 return mPageSpacing;
826 }
827
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800828 private void dispatchPageCountChanged() {
829 if (mPageIndicator != null) {
830 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700831 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700832 // This ensures that when children are added, they get the correct transforms / alphas
833 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700834 invalidate();
835 }
836
Michael Jurka8b805b12012-04-18 14:23:14 -0700837 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800838 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800839 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800840 dispatchPageCountChanged();
841 }
842
843 @Override
844 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800845 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700846 mCurrentPage = validateNewPage(mCurrentPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100847 mCurrentScrollOverPage = mCurrentPage;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800848 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700849 }
850
Adam Cohen73894962011-10-31 13:17:17 -0700851 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700852 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800853 View pageAtIndex = getPageAt(index);
854 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700855 }
856
Alex Chau7944ee52021-01-08 19:07:25 +0000857 protected int getChildVisibleSize(int index) {
858 View layout = getPageAt(index);
859 return mOrientationHandler.getMeasuredSize(layout);
860 }
861
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 @Override
863 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200864 int page = indexOfChild(child);
865 if (!isVisible(page) || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700866 if (immediate) {
867 setCurrentPage(page);
868 } else {
869 snapToPage(page);
870 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 return true;
872 }
873 return false;
874 }
875
876 @Override
877 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700878 int focusablePage;
879 if (mNextPage != INVALID_PAGE) {
880 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700882 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 }
Winson Chung86f77532010-08-24 11:08:22 -0700884 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700886 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 }
888 return false;
889 }
890
891 @Override
892 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800893 if (super.dispatchUnhandledMove(focused, direction)) {
894 return true;
895 }
896
897 if (mIsRtl) {
898 if (direction == View.FOCUS_LEFT) {
899 direction = View.FOCUS_RIGHT;
900 } else if (direction == View.FOCUS_RIGHT) {
901 direction = View.FOCUS_LEFT;
902 }
903 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200904
905 int currentPage = getNextPage();
906 int closestNeighbourIndex = -1;
907 int closestNeighbourDistance = Integer.MAX_VALUE;
908 // Find the closest neighbour page
909 for (int neighbourPageIndex : getNeighbourPageIndices(direction)) {
910 int distance = Math.abs(neighbourPageIndex - currentPage);
911 if (closestNeighbourDistance > distance) {
912 closestNeighbourDistance = distance;
913 closestNeighbourIndex = neighbourPageIndex;
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 }
915 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200916 if (closestNeighbourIndex != -1) {
917 View page = getPageAt(closestNeighbourIndex);
918 snapToPage(closestNeighbourIndex);
919 page.requestFocus(direction);
920 return true;
921 }
922
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800923 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 }
925
926 @Override
927 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700928 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
929 return;
930 }
931
Andras Kloczl7e912442021-05-06 12:50:28 +0200932 // nextPage is more reliable when multiple control movements have been done in a short
933 // period of time
934 getPageIndices(getNextPage())
935 .addAll(getNeighbourPageIndices(direction))
936 .forEach(pageIndex ->
937 getPageAt(pageIndex).addFocusables(views, direction, focusableMode));
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 }
939
940 /**
941 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700942 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 *
Winson Chung86f77532010-08-24 11:08:22 -0700944 * This happens when live folders requery, and if they're off page, they
945 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 */
947 @Override
948 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700949 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 View v = focused;
951 while (true) {
952 if (v == current) {
953 super.focusableViewAvailable(focused);
954 return;
955 }
956 if (v == this) {
957 return;
958 }
959 ViewParent parent = v.getParent();
960 if (parent instanceof View) {
961 v = (View)v.getParent();
962 } else {
963 return;
964 }
965 }
966 }
967
968 /**
969 * {@inheritDoc}
970 */
971 @Override
972 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
973 if (disallowIntercept) {
974 // We need to make sure to cancel our long press if
975 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +0200976 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 }
978 super.requestDisallowInterceptTouchEvent(disallowIntercept);
979 }
980
981 @Override
982 public boolean onInterceptTouchEvent(MotionEvent ev) {
983 /*
984 * This method JUST determines whether we want to intercept the motion.
985 * If we return true, onTouchEvent will be called and we do the actual
986 * scrolling there.
987 */
988
Winson Chung45e1d6e2010-11-09 17:19:49 -0800989 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700990 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700991
992 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800993
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 /*
995 * Shortcut the most recurring case: the user is in the dragging
996 * state and he is moving his finger. We want to intercept this
997 * motion.
998 */
999 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -07001000 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 return true;
1002 }
1003
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 switch (action & MotionEvent.ACTION_MASK) {
1005 case MotionEvent.ACTION_MOVE: {
1006 /*
1007 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -08001008 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001010 if (mActivePointerId != INVALID_POINTER) {
1011 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001012 }
1013 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -08001014 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -07001015 // i.e. fall through to the next case (don't break)
1016 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1017 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001018 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 }
1020
1021 case MotionEvent.ACTION_DOWN: {
1022 final float x = ev.getX();
1023 final float y = ev.getY();
1024 // Remember location of down touch
1025 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001026 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -07001027 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001028 mLastMotionRemainder = 0;
1029 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001030 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -07001032 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 break;
1034 }
1035
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001037 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001038 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 break;
1040
1041 case MotionEvent.ACTION_POINTER_UP:
1042 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001043 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 break;
1045 }
1046
1047 /*
1048 * The only time we want to intercept motion events is if we are in the
1049 * drag mode.
1050 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001051 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 }
1053
Tony Wickhama0aee212019-09-18 09:24:03 -07001054 /**
1055 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1056 */
Sunny Goyalc39db732021-04-03 01:20:21 -07001057 private void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -07001058 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -07001059 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -07001060 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001061
1062 if (finishedScrolling) {
1063 mIsBeingDragged = false;
1064 if (!mScroller.isFinished() && !mFreeScroll) {
1065 setCurrentPage(getNextPage());
1066 pageEndTransition();
1067 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001068 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -07001069 } else {
1070 mIsBeingDragged = true;
1071 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001072
1073 // Catch the edge effect if it is active.
1074 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
1075 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
1076 if (!mEdgeGlowLeft.isFinished()) {
1077 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
1078 }
1079 if (!mEdgeGlowRight.isFinished()) {
1080 mEdgeGlowRight.onPullDistance(0f, displacement);
1081 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001082 }
1083
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001084 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001085 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001086 }
1087
Adam Cohenf8d28232011-02-01 21:47:00 -08001088 protected void determineScrollingStart(MotionEvent ev) {
1089 determineScrollingStart(ev, 1.0f);
1090 }
1091
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 /*
1093 * Determines if we should change the touch state to start scrolling after the
1094 * user moves their touch point too far.
1095 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001096 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001097 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001098 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001099 if (pointerIndex == -1) return;
1100
Vinit Nayaka406f722019-12-19 11:50:55 -08001101 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1102 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001103 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001104 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001105
Vinit Nayaka406f722019-12-19 11:50:55 -08001106 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001107 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001108 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001109 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1110 mLastMotion = primaryDirection;
1111 mLastMotionRemainder = 0;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001112 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001113 // Stop listening for things like pinches.
1114 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001115 }
1116 }
1117
1118 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001119 // Try canceling the long press. It could also have been scheduled
1120 // by a distant descendant, so use the mAllowLongPress flag to block
1121 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001122 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001123 }
1124
Adam Cohenb5ba0972011-09-07 18:02:31 -07001125 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001126 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenedb40762013-07-18 16:45:45 -07001127 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001128 int panelCount = getPanelCount();
1129 int pageCount = getChildCount();
Adam Cohen3b185e22013-10-29 14:45:58 -07001130
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001131 int adjacentPage = page + panelCount;
Sunny Goyal70660032015-05-14 00:07:08 -07001132 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001133 adjacentPage = page - panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001134 }
1135
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001136 final int totalDistance;
1137 if (adjacentPage < 0 || adjacentPage > pageCount - 1) {
1138 totalDistance = (v.getMeasuredWidth() + mPageSpacing) * panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001139 } else {
1140 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1141 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001142
1143 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001144 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001145 scrollProgress = Math.max(scrollProgress, -MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001146 return scrollProgress;
1147 }
1148
Adam Cohenedb40762013-07-18 16:45:45 -07001149 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001150 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001151 return 0;
1152 } else {
1153 return mPageScrolls[index];
1154 }
1155 }
1156
Adam Cohen564a2e72013-10-09 14:47:32 -07001157 // While layout transitions are occurring, a child's position may stray from its baseline
1158 // position. This method returns the magnitude of this stray at any given time.
1159 public int getLayoutTransitionOffsetForPage(int index) {
1160 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1161 return 0;
1162 } else {
1163 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001164
Sunny Goyal228153d2018-01-04 15:35:22 -08001165 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1166 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001167 return (int) (child.getX() - baselineX);
1168 }
1169 }
1170
Tony Wickham023f4042019-01-29 10:52:31 -08001171 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001172 if (mFreeScroll == freeScroll) {
1173 return;
1174 }
1175
Tony Wickham8f7ead32016-04-07 18:46:44 -07001176 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001177 mFreeScroll = freeScroll;
1178
Adam Cohenf9618852013-11-08 06:45:03 -08001179 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001180 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001181 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001182 if (getScrollForPage(getNextPage()) != getScrollX()) {
1183 snapToPage(getNextPage());
1184 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001185 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001186 }
1187
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001188 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001189 mAllowOverScroll = enable;
1190 }
1191
Alex Chau822acf42021-06-03 11:35:05 +01001192 protected float getSignificantMoveThreshold() {
1193 return SIGNIFICANT_MOVE_THRESHOLD;
1194 }
1195
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 @Override
1197 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001198 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001199 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001200
Michael Jurkab8f06722010-10-10 15:58:46 -07001201 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001202
1203 final int action = ev.getAction();
1204
1205 switch (action & MotionEvent.ACTION_MASK) {
1206 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001207 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001208
Winson Chung321e9ee2010-08-09 13:37:56 -07001209 /*
1210 * If being flinged and user touches, stop the fling. isFinished
1211 * will be false if being flinged.
1212 */
1213 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001214 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
1216
1217 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001218 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001219 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001220 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1221 mLastMotionRemainder = 0;
1222 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001223 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001225 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001226 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001227 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 break;
1229
Winson Chung4941f652020-06-18 15:11:53 -07001230 case ACTION_MOVE_ALLOW_EASY_FLING:
1231 // Start scrolling immediately
1232 determineScrollingStart(ev);
1233 mAllowEasyFling = true;
1234 break;
1235
1236 case MotionEvent.ACTION_MOVE:
1237 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 // Scroll to follow the motion event
1239 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001240
1241 if (pointerIndex == -1) return true;
Sunny Goyalc39db732021-04-03 01:20:21 -07001242 float oldScroll = mOrientationHandler.getPrimaryScroll(this);
1243 float dx = ev.getX(pointerIndex);
1244 float dy = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001245
Sunny Goyalc39db732021-04-03 01:20:21 -07001246 float direction = mOrientationHandler.getPrimaryValue(dx, dy);
Vinit Nayaka406f722019-12-19 11:50:55 -08001247 float delta = mLastMotion + mLastMotionRemainder - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001248
1249 int width = getWidth();
1250 int height = getHeight();
1251 int size = mOrientationHandler.getPrimaryValue(width, height);
1252
1253 final float displacement = mOrientationHandler.getSecondaryValue(dx, dy)
1254 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001255 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001256
Sunny Goyalc39db732021-04-03 01:20:21 -07001257 if (mAllowOverScroll) {
1258 float consumed = 0;
1259 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
1260 consumed = size * mEdgeGlowRight.onPullDistance(delta / size, displacement);
1261 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
1262 consumed = -size * mEdgeGlowLeft.onPullDistance(
1263 -delta / size, 1 - displacement);
1264 }
1265 delta -= consumed;
1266 }
Alex Chau822acf42021-06-03 11:35:05 +01001267 delta /= mOrientationHandler.getPrimaryScale(this);
Sunny Goyalc39db732021-04-03 01:20:21 -07001268
Winson Chungc0844aa2011-02-02 15:25:58 -08001269 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1270 // keep the remainder because we are actually testing if we've moved from the last
1271 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001272 mLastMotion = direction;
1273 int movedDelta = (int) delta;
1274 mLastMotionRemainder = delta - movedDelta;
Vinit Nayaka406f722019-12-19 11:50:55 -08001275
Sunny Goyalc39db732021-04-03 01:20:21 -07001276 if (delta != 0) {
1277 mOrientationHandler.set(this, VIEW_SCROLL_BY, movedDelta);
1278
1279 if (mAllowOverScroll) {
1280 final float pulledToX = oldScroll + delta;
1281
1282 if (pulledToX < mMinScroll) {
1283 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement);
1284 if (!mEdgeGlowRight.isFinished()) {
1285 mEdgeGlowRight.onRelease();
1286 }
1287 } else if (pulledToX > mMaxScroll) {
1288 mEdgeGlowRight.onPullDistance(delta / size, displacement);
1289 if (!mEdgeGlowLeft.isFinished()) {
1290 mEdgeGlowLeft.onRelease();
1291 }
1292 }
1293
1294 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1295 postInvalidateOnAnimation();
1296 }
1297 }
1298
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 } else {
1300 awakenScrollBars();
1301 }
Adam Cohen564976a2010-10-13 18:52:07 -07001302 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 determineScrollingStart(ev);
1304 }
1305 break;
1306
1307 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001308 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 final int activePointerId = mActivePointerId;
1310 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001311 if (pointerIndex == -1) return true;
1312
Vinit Nayaka406f722019-12-19 11:50:55 -08001313 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1314 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001315 final VelocityTracker velocityTracker = mVelocityTracker;
1316 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001317
Vinit Nayaka406f722019-12-19 11:50:55 -08001318 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1319 mActivePointerId);
Alex Chau822acf42021-06-03 11:35:05 +01001320 float delta = primaryDirection - mDownMotionPrimary;
1321 delta /= mOrientationHandler.getPrimaryScale(this);
Vinit Nayaka406f722019-12-19 11:50:55 -08001322 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1323
Alex Chau822acf42021-06-03 11:35:05 +01001324 boolean isSignificantMove = Math.abs(delta)
1325 > pageOrientedSize * getSignificantMoveThreshold();
Vinit Nayaka406f722019-12-19 11:50:55 -08001326
1327 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001328 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1329 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001330 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1331 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001332 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1333 Log.d("Quickswitch", "isFling=false vel=" + velocity
1334 + " threshold=" + mEasyFlingThresholdVelocity);
1335 }
Adam Cohen00481b32011-11-18 12:03:48 -08001336
Adam Cohenf358a4b2013-07-23 16:47:31 -07001337 if (!mFreeScroll) {
1338 // In the case that the page is moved far to one direction and then is flung
1339 // in the opposite direction, we use a threshold to determine whether we should
1340 // just return to the starting page, or if we should skip one further.
1341 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001342 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1343 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344 returnToOriginalPage = true;
1345 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001346
Adam Cohenf358a4b2013-07-23 16:47:31 -07001347 int finalPage;
1348 // We give flings precedence over large moves, which is why we short-circuit our
1349 // test for a large move if a fling has been registered. That is, a large
1350 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001351
Vinit Nayaka406f722019-12-19 11:50:55 -08001352 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1353 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001354 finalPage = returnToOriginalPage
1355 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001356 snapToPageWithVelocity(finalPage, velocity);
1357 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1358 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001359 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001360 finalPage = returnToOriginalPage
1361 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001362 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001363 } else {
1364 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001365 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001367 if (!mScroller.isFinished()) {
1368 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001369 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001370
Vinit Nayaka406f722019-12-19 11:50:55 -08001371 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1372 int maxScroll = mMaxScroll;
1373 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001374
Vinit Nayaka406f722019-12-19 11:50:55 -08001375 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1376 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001377 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001378 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001379 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001380 int velocity1 = -velocity;
1381 // Continue a scroll or fling in progress
1382 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1383 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001384
Sunny Goyalc39db732021-04-03 01:20:21 -07001385 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001386 mNextPage = getDestinationPage(finalPos);
Sunny Goyalc39db732021-04-03 01:20:21 -07001387 onNotSnappingToPageInFreeScroll();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001388 }
1389 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001392
Sunny Goyalc39db732021-04-03 01:20:21 -07001393 mEdgeGlowLeft.onRelease();
1394 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 // End any intermediate reordering states
1396 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 break;
1398
1399 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001400 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001401 snapToDestination();
1402 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001403 mEdgeGlowLeft.onRelease();
1404 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001405 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001406 break;
1407
1408 case MotionEvent.ACTION_POINTER_UP:
1409 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 break;
1412 }
1413
1414 return true;
1415 }
1416
Sunny Goyalc39db732021-04-03 01:20:21 -07001417 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001418
Lais Andradecc5c8842021-06-04 18:20:30 +01001419 /**
1420 * Called when the view edges absorb part of the scroll. Subclasses can override this
1421 * to provide custom behavior during animation.
1422 */
1423 protected void onEdgeAbsorbingScroll() {
1424 }
1425
1426 /**
1427 * Called when the current page closest to the center of the screen changes as part of the
1428 * scroll. Subclasses can override this to provide custom behavior during scroll.
1429 */
1430 protected void onScrollOverPageChanged() {
1431 }
1432
Vinit Nayaka406f722019-12-19 11:50:55 -08001433 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001434 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1435 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001436 }
1437
Adam Cohen7d30a372013-07-01 17:03:59 -07001438 private void resetTouchState() {
1439 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001440 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001441 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 }
1443
Winson Chung185d7162011-02-28 13:47:29 -08001444 @Override
1445 public boolean onGenericMotionEvent(MotionEvent event) {
1446 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1447 switch (event.getAction()) {
1448 case MotionEvent.ACTION_SCROLL: {
1449 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1450 final float vscroll;
1451 final float hscroll;
1452 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1453 vscroll = 0;
1454 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1455 } else {
1456 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1457 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1458 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001459 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1460 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001461 }
Winson Chung185d7162011-02-28 13:47:29 -08001462 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001463 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001464 : (hscroll > 0 || vscroll > 0);
1465 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001466 scrollRight();
1467 } else {
1468 scrollLeft();
1469 }
1470 return true;
1471 }
1472 }
1473 }
1474 }
1475 return super.onGenericMotionEvent(event);
1476 }
1477
Sunny Goyal8b37c572020-03-31 12:12:14 -07001478 /**
1479 * Returns true if the paged view can scroll for the provided vertical and horizontal
1480 * scroll values
1481 */
1482 protected boolean canScroll(float absVScroll, float absHScroll) {
1483 ActivityContext ac = ActivityContext.lookupContext(getContext());
1484 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001485 }
1486
Michael Jurkab8f06722010-10-10 15:58:46 -07001487 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1488 if (mVelocityTracker == null) {
1489 mVelocityTracker = VelocityTracker.obtain();
1490 }
1491 mVelocityTracker.addMovement(ev);
1492 }
1493
1494 private void releaseVelocityTracker() {
1495 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001496 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001497 mVelocityTracker.recycle();
1498 mVelocityTracker = null;
1499 }
1500 }
1501
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001503 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 final int pointerId = ev.getPointerId(pointerIndex);
1505 if (pointerId == mActivePointerId) {
1506 // This was our active pointer going up. Choose a new
1507 // active pointer and adjust accordingly.
1508 // TODO: Make this decision more intelligent.
1509 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001510 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1511 newPointerIndex);
1512 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 mActivePointerId = ev.getPointerId(newPointerIndex);
1514 if (mVelocityTracker != null) {
1515 mVelocityTracker.clear();
1516 }
1517 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001518 }
1519
Winson Chung321e9ee2010-08-09 13:37:56 -07001520 @Override
1521 public void requestChildFocus(View child, View focused) {
1522 super.requestChildFocus(child, focused);
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001523
1524 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1525 // which results in incorrect navigation
1526 int nextPage = getNextPage();
1527 if (nextPage != mCurrentPage) {
1528 setCurrentPage(nextPage);
1529 }
1530
Andras Kloczl7e912442021-05-06 12:50:28 +02001531 int page = indexOfChild(child);
1532 if (page >= 0 && !isVisible(page) && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001533 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 }
1535 }
1536
Alex Chaub794ea62021-02-03 18:00:30 +00001537 public int getDestinationPage() {
1538 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1539 }
1540
Alex Chaudedbc8a2021-03-17 16:53:26 +00001541 protected int getDestinationPage(int primaryScroll) {
1542 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001543 }
1544
Sunny Goyal228153d2018-01-04 15:35:22 -08001545 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001546 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001547 }
1548
Alex Chaudedbc8a2021-03-17 16:53:26 +00001549 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1550 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001551 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001553 final int childCount = getChildCount();
1554 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001555 int distanceFromScreenCenter = Math.abs(
1556 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1558 minDistanceFromScreenCenter = distanceFromScreenCenter;
1559 minDistanceFromScreenCenterIndex = i;
1560 }
1561 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001562 return minDistanceFromScreenCenterIndex;
1563 }
1564
Tony Wickham7383d4e2020-07-07 19:25:25 -07001565 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001566 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001567 int halfChildSize = (childSize / 2);
1568 int childCenter = getChildOffset(childIndex) + halfChildSize;
1569 return childCenter - screenCenter;
1570 }
1571
1572 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001573 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1574 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001575 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1576 }
1577
Alex Chaudedbc8a2021-03-17 16:53:26 +00001578 private int getScreenCenter(int primaryScroll) {
1579 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1580 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1581 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1582 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1583 + primaryPivot);
1584 }
1585
Adam Cohend19d3ca2010-09-15 14:43:42 -07001586 protected void snapToDestination() {
Sunny Goyalc39db732021-04-03 01:20:21 -07001587 snapToPage(getDestinationPage(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001588 }
1589
Adam Cohene0f66b52010-11-23 15:06:07 -08001590 // We want the duration of the page snap animation to be influenced by the distance that
1591 // the screen has to travel, however, we don't want this duration to be effected in a
1592 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1593 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001594 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001595 f -= 0.5f; // center the values about 0.
1596 f *= 0.3f * Math.PI / 2.0f;
1597 return (float) Math.sin(f);
1598 }
1599
Winson Chung05a31ed2018-02-08 17:08:43 -08001600 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001601 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001602 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001603
Vinit Nayaka406f722019-12-19 11:50:55 -08001604 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001605 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001606 int duration = 0;
1607
Sunny Goyal4d113a52015-05-27 10:05:28 -07001608 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001609 // If the velocity is low enough, then treat this more as an automatic page advance
1610 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001611 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001612 }
1613
1614 // Here we compute a "distance" that will be used in the computation of the overall
1615 // snap duration. This is a function of the actual distance that needs to be traveled;
1616 // we keep this value close to half screen size in order to reduce the variance in snap
1617 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001618 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001619 float distance = halfScreenSize + halfScreenSize *
1620 distanceInfluenceForSnapDuration(distanceRatio);
1621
1622 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001623 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001624
1625 // we want the page's snap velocity to approximately match the velocity at which the
1626 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001627 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1628 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001629
Sunny Goyalc39db732021-04-03 01:20:21 -07001630 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001631 }
1632
Winson Chung05a31ed2018-02-08 17:08:43 -08001633 public boolean snapToPage(int whichPage) {
1634 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001635 }
1636
Winson Chung05a31ed2018-02-08 17:08:43 -08001637 public boolean snapToPageImmediately(int whichPage) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001638 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001639 }
1640
Winson Chung05a31ed2018-02-08 17:08:43 -08001641 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001642 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001643 }
1644
Sunny Goyalc39db732021-04-03 01:20:21 -07001645 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001646 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001647
Vinit Nayaka406f722019-12-19 11:50:55 -08001648 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001649 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1650 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001651 }
1652
Winson Chung05a31ed2018-02-08 17:08:43 -08001653 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001654 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001655 }
Michael Jurka0142d492010-08-25 17:46:15 -07001656
Sunny Goyalc39db732021-04-03 01:20:21 -07001657 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001658 if (mFirstLayout) {
1659 setCurrentPage(whichPage);
1660 return false;
1661 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001662
Andy Wickhamda5a89b2021-07-23 12:38:14 -10001663 if (FeatureFlags.IS_STUDIO_BUILD && !Utilities.IS_RUNNING_IN_TEST_HARNESS) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001664 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1665 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001666 }
1667
Adam Cohen6f127a62014-06-12 14:54:41 -07001668 whichPage = validateNewPage(whichPage);
1669
Adam Cohen7d30a372013-07-01 17:03:59 -07001670 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001671
Winson Chung321e9ee2010-08-09 13:37:56 -07001672 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001673 if (immediate) {
1674 duration = 0;
1675 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001676 duration = Math.abs(delta);
1677 }
1678
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001679 if (duration != 0) {
1680 pageBeginTransition();
1681 }
1682
Adam Cohenf358a4b2013-07-23 16:47:31 -07001683 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001684 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001685 }
Adam Cohenf9618852013-11-08 06:45:03 -08001686
Sunny Goyalc39db732021-04-03 01:20:21 -07001687 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001688 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001689
1690 // Trigger a compute() to finish switching pages if necessary
1691 if (immediate) {
1692 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001693 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001694 }
1695
Winson Chung321e9ee2010-08-09 13:37:56 -07001696 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001697 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001698 }
1699
Vadim Tryshev98913d02018-05-18 18:41:34 -07001700 public boolean scrollLeft() {
1701 if (getNextPage() > 0) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001702 snapToPage(getNextPage() - getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001703 return true;
1704 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001705 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001706 }
1707
Vadim Tryshev98913d02018-05-18 18:41:34 -07001708 public boolean scrollRight() {
1709 if (getNextPage() < getChildCount() - 1) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001710 snapToPage(getNextPage() + getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001711 return true;
1712 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001713 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001714 }
1715
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001716 @Override
Lais Andradecc5c8842021-06-04 18:20:30 +01001717 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
1718 int newDestinationPage = getDestinationPage();
1719 if (newDestinationPage >= 0 && newDestinationPage != mCurrentScrollOverPage) {
1720 mCurrentScrollOverPage = newDestinationPage;
1721 onScrollOverPageChanged();
1722 }
1723 }
1724
1725 @Override
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001726 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 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001866
1867 @Override
1868 public void draw(Canvas canvas) {
1869 super.draw(canvas);
1870 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001871 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001872 }
1873
1874 protected void drawEdgeEffect(Canvas canvas) {
1875 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1876 final int width = getWidth();
1877 final int height = getHeight();
1878 if (!mEdgeGlowLeft.isFinished()) {
1879 final int restoreCount = canvas.save();
1880 canvas.rotate(-90);
1881 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1882 mEdgeGlowLeft.setSize(height, width);
1883 if (mEdgeGlowLeft.draw(canvas)) {
1884 postInvalidateOnAnimation();
1885 }
1886 canvas.restoreToCount(restoreCount);
1887 }
1888 if (!mEdgeGlowRight.isFinished()) {
1889 final int restoreCount = canvas.save();
1890 canvas.rotate(90, width, 0);
1891 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1892
1893 mEdgeGlowRight.setSize(height, width);
1894 if (mEdgeGlowRight.draw(canvas)) {
1895 postInvalidateOnAnimation();
1896 }
1897 canvas.restoreToCount(restoreCount);
1898 }
1899 }
1900 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001901}