blob: 2b5bd8ff77f5cb5bdf7257755d172aca7d47f522 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyalc39db732021-04-03 01:20:21 -070019import static com.android.launcher3.anim.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070020import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
21import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyal8b37c572020-03-31 12:12:14 -070022import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070023import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
24import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
25
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070026import android.animation.LayoutTransition;
Sunny Goyalcf25b522015-07-09 00:01:18 -070027import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.content.Context;
Andras Kloczlc2bd15f2022-03-30 18:06:13 +010029import android.content.res.Configuration;
30import android.content.res.Resources;
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;
53
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.compat.AccessibilityManagerCompat;
55import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070056import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.touch.PagedOrientationHandler;
58import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070059import com.android.launcher3.util.EdgeEffectCompat;
Andras Kloczl55edfe52021-05-14 12:21:30 +020060import com.android.launcher3.util.IntSet;
Jon Miranda9e198662020-03-11 14:42:02 -070061import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070062import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070063
64import java.util.ArrayList;
Sunny Goyal59969372021-05-06 12:11:44 -070065import java.util.function.Consumer;
Jon Miranda9e198662020-03-11 14:42:02 -070066
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080071public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070074 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070075
Winson Chung4941f652020-06-18 15:11:53 -070076 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053077 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070078 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Adam Cohenb64cb5a2011-02-15 13:53:42 -080080 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080081 // The page is moved more than halfway, automatically move to the next page on touch up.
82 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080083
Sunny Goyal8e2133b2015-05-06 13:39:07 -070084 private static final float MAX_SCROLL_PROGRESS = 1.0f;
85
Adam Cohenf358a4b2013-07-23 16:47:31 -070086 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070087
Andras Kloczlc2bd15f2022-03-30 18:06:13 +010088 private int mFlingThresholdVelocity;
89 private int mEasyFlingThresholdVelocity;
90 private int mMinFlingVelocity;
91 private int mMinSnapVelocity;
92 private int mPageSnapAnimationDuration;
Michael Jurka0142d492010-08-25 17:46:15 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected boolean mFirstLayout = true;
95
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Alex Chauba61b982021-06-15 16:29:19 +010098 // Difference between current scroll position and mCurrentPage's page scroll. Used to maintain
99 // relative scroll position unchanged in updateCurrentPageScroll. Cleared when snapping to a
100 // page.
101 protected int mCurrentPageScrollDiff;
Lais Andradecc5c8842021-06-04 18:20:30 +0100102 // The current page the PagedView is scrolling over on it's way to the destination page.
103 protected int mCurrentScrollOverPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800107 protected int mMaxScroll;
108 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700109 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800111 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
113 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700114 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800115 private float mDownMotionPrimary;
116 private float mLastMotion;
117 private float mLastMotionRemainder;
118 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700119 // Used in special cases where the fling checks can be relaxed for an intentional gesture
120 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700121 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700122
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700123 private final ArrayList<Runnable> mOnPageScrollsInitializedCallbacks = new ArrayList<>();
124
125 // We should always check pageScrollsInitialized() is true when using mPageScrolls.
126 @Nullable protected int[] mPageScrolls = null;
Sunny Goyalaad33592018-08-07 17:20:35 -0700127 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800128
Winson Chung4941f652020-06-18 15:11:53 -0700129 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800130 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700131 // The amount of movement to begin paging
132 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800134 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka5f1c5092010-09-03 14:15:02 -0700136 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka5f1c5092010-09-03 14:15:02 -0700138 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700140 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700141 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700142
Winson Chungd2be3812013-07-16 11:11:32 -0700143 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700144 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800145 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700146
John Spurlock77e1f472013-09-11 10:09:51 -0400147 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800148 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400149
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800150 // Similar to the platform implementation of isLayoutValid();
151 protected boolean mIsLayoutValid;
152
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700153 private int[] mTmpIntPair = new int[2];
154
Sunny Goyalc39db732021-04-03 01:20:21 -0700155 protected EdgeEffectCompat mEdgeGlowLeft;
156 protected EdgeEffectCompat mEdgeGlowRight;
157
Winson Chung321e9ee2010-08-09 13:37:56 -0700158 public PagedView(Context context) {
159 this(context, null);
160 }
161
162 public PagedView(Context context, AttributeSet attrs) {
163 this(context, attrs, 0);
164 }
165
166 public PagedView(Context context, AttributeSet attrs, int defStyle) {
167 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700168
Adam Cohen9c4949e2010-10-05 12:27:22 -0700169 TypedArray a = context.obtainStyledAttributes(attrs,
170 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700171 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700172 a.recycle();
173
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700175 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700176
Sunny Goyalc39db732021-04-03 01:20:21 -0700177 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700178 mCurrentPage = 0;
Lais Andradecc5c8842021-06-04 18:20:30 +0100179 mCurrentScrollOverPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Vinit Nayaka406f722019-12-19 11:50:55 -0800181 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700182 mTouchSlop = configuration.getScaledTouchSlop();
183 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800185
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100186 updateVelocityValues();
Sunny Goyala9f55462018-03-25 18:15:15 -0700187
Sunny Goyalc39db732021-04-03 01:20:21 -0700188 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700189 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700190 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Sunny Goyalc39db732021-04-03 01:20:21 -0700193 protected void initEdgeEffect() {
194 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
195 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800196 }
197
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700198 public void initParentViews(View parent) {
199 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800200 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Tony Wickhambab101c2021-09-20 15:12:44 -0700201 mPageIndicator.setMarkersCount(getChildCount() / getPanelCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700202 }
203 }
204
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800205 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700206 return mPageIndicator;
207 }
208
Adam Cohen674531f2013-12-13 15:07:14 -0800209 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700210 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
211 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000212 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700213 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700215 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700216 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700218
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700219 /**
220 * Returns the index of page to be shown immediately afterwards.
221 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700222 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700223 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
224 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700225
Adam Cohenf9c184a2016-01-15 16:47:43 -0800226 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 return getChildCount();
228 }
229
Sunny Goyal83a8f042015-05-19 12:52:12 -0700230 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 return getChildAt(index);
232 }
233
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800235 * Updates the scroll of the current page immediately to its final scroll position. We use this
236 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
237 * the previous tab page.
238 */
239 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700240 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800241 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700242 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Alex Chauba61b982021-06-15 16:29:19 +0100243 newPosition = getScrollForPage(mCurrentPage) + mCurrentPageScrollDiff;
Adam Cohen0ffac432013-07-10 11:19:26 -0700244 }
Alex Chau6499e832021-11-19 14:35:46 +0000245 mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700246 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Tony Wickhamd5521322021-09-14 14:26:50 -0700247 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 }
249
Tony Wickhamf8d731a2021-05-26 14:58:13 -0700250 /**
251 * Immediately finishes any overscroll effect and jumps to the end of the scroller animation.
252 */
253 public void abortScrollerAnimation() {
254 mEdgeGlowLeft.finish();
255 mEdgeGlowRight.finish();
256 abortScrollerAnimation(true);
257 }
258
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700259 protected void onScrollerAnimationAborted() {
260 // No-Op
261 }
262
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700263 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700264 mScroller.abortAnimation();
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700265 onScrollerAnimationAborted();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700266 // We need to clean up the next page here to avoid computeScrollHelper from
267 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700268 if (resetNextPage) {
269 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800270 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700271 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700272 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273
Tony Wickhamd5521322021-09-14 14:26:50 -0700274 /**
275 * Immediately finishes any in-progress scroll, maintaining the current position. Also sets
276 * mNextPage = INVALID_PAGE and calls pageEndTransition().
277 */
278 public void forceFinishScroller() {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700279 mScroller.forceFinished(true);
280 // We need to clean up the next page here to avoid computeScrollHelper from
281 // updating current page on the pass.
Tony Wickhamd5521322021-09-14 14:26:50 -0700282 mNextPage = INVALID_PAGE;
283 pageEndTransition();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700284 }
285
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700287 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700288 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100289 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
290
291 if (getPanelCount() > 1) {
Alex Chau1e448462021-08-13 21:48:35 +0100292 // Always return left most panel as new page
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100293 newPage = getLeftmostVisiblePageForIndex(newPage);
294 }
295 return newPage;
296 }
297
Andras Kloczl55edfe52021-05-14 12:21:30 +0200298 /**
299 * In most cases where panelCount is 1, this method will just return the page index that was
300 * passed in.
301 * But for example when two panel home is enabled we might need the leftmost visible page index
302 * because that page is the current page.
303 */
304 public int getLeftmostVisiblePageForIndex(int pageIndex) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100305 int panelCount = getPanelCount();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200306 return pageIndex - pageIndex % panelCount;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100307 }
308
309 /**
310 * Returns the number of pages that are shown at the same time.
311 */
312 protected int getPanelCount() {
313 return 1;
314 }
315
316 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +0200317 * Returns an IntSet with the indices of the currently visible pages
318 */
Andras Kloczl55edfe52021-05-14 12:21:30 +0200319 public IntSet getVisiblePageIndices() {
Andras Kloczl7e912442021-05-06 12:50:28 +0200320 return getPageIndices(mCurrentPage);
321 }
322
323 /**
324 * In case the panelCount is 1 this just returns the same page index in an IntSet.
325 * But in cases where the panelCount > 1 this will return all the page indices that belong
326 * together, i.e. on the Workspace they are next to each other and shown at the same time.
327 */
328 private IntSet getPageIndices(int pageIndex) {
329 // we want to make sure the pageIndex is the leftmost page
330 pageIndex = getLeftmostVisiblePageForIndex(pageIndex);
331
332 IntSet pageIndices = new IntSet();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200333 int panelCount = getPanelCount();
334 int pageCount = getPageCount();
Andras Kloczl7e912442021-05-06 12:50:28 +0200335 for (int page = pageIndex; page < pageIndex + panelCount && page < pageCount; page++) {
336 pageIndices.add(page);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200337 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200338 return pageIndices;
339 }
340
341 /**
342 * Returns an IntSet with the indices of the neighbour pages that are in the focus direction.
343 */
344 private IntSet getNeighbourPageIndices(int focus) {
345 int panelCount = getPanelCount();
346 // getNextPage is more reliable than getCurrentPage
347 int currentPage = getNextPage();
348
349 int nextPage;
350 if (focus == View.FOCUS_LEFT) {
351 nextPage = currentPage - panelCount;
352 } else if (focus == View.FOCUS_RIGHT) {
353 nextPage = currentPage + panelCount;
354 } else {
Andras Kloczlb0632942021-08-03 14:03:58 +0200355 // no neighbours to other directions
Andras Kloczl7e912442021-05-06 12:50:28 +0200356 return new IntSet();
357 }
358 nextPage = validateNewPage(nextPage);
359 if (nextPage == currentPage) {
360 // We reached the end of the pages
361 return new IntSet();
362 }
363
Andras Kloczlb0632942021-08-03 14:03:58 +0200364 return getPageIndices(nextPage);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200365 }
366
367 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700368 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100369 */
Sunny Goyal59969372021-05-06 12:11:44 -0700370 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200371 getVisiblePageIndices().forEach(pageIndex -> {
372 View page = getPageAt(pageIndex);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100373 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700374 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100375 }
Andras Kloczl55edfe52021-05-14 12:21:30 +0200376 });
Andras Kloczl2bb51342021-03-20 01:13:45 +0100377 }
378
379 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100380 * Returns true if the view is on one of the current pages, false otherwise.
381 */
382 public boolean isVisible(View child) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200383 return isVisible(indexOfChild(child));
384 }
385
386 /**
387 * Returns true if the page with the given index is currently visible, false otherwise.
388 */
389 private boolean isVisible(int pageIndex) {
390 return getLeftmostVisiblePageForIndex(pageIndex) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700391 }
392
Chet Haasebc2f0822012-10-26 17:59:53 -0700393 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700394 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
395 */
396 private int ensureWithinScrollBounds(int page) {
397 int dir = !mIsRtl ? 1 : - 1;
398 int currScroll = getScrollForPage(page);
399 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800400 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700401 page += dir;
402 prevScroll = currScroll;
403 currScroll = getScrollForPage(page);
404 if (currScroll <= prevScroll) {
405 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
406 break;
407 }
408 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800409 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700410 page -= dir;
411 prevScroll = currScroll;
412 currScroll = getScrollForPage(page);
413 if (currScroll >= prevScroll) {
414 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
415 break;
416 }
417 }
418 return page;
419 }
420
Tony Wickham03f27012019-04-25 14:22:54 -0700421 public void setCurrentPage(int currentPage) {
422 setCurrentPage(currentPage, INVALID_PAGE);
423 }
424
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700425 /**
Winson Chung86f77532010-08-24 11:08:22 -0700426 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 */
Tony Wickham03f27012019-04-25 14:22:54 -0700428 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800429 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700430 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800431 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800432 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
433 // the default
434 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800435 return;
436 }
Tony Wickham03f27012019-04-25 14:22:54 -0700437 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700438 mCurrentPage = validateNewPage(currentPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100439 mCurrentScrollOverPage = mCurrentPage;
Winson Chung181c3dc2013-07-17 15:36:20 -0700440 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700441 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800442 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700443 }
444
Winson Chung8c87cd82013-07-23 16:20:10 -0700445 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800446 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
447 * has settled.
448 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700449 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800450 updatePageIndicator();
451 }
452
453 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700454 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000455 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700456 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700458 protected void pageBeginTransition() {
459 if (!mIsPageInTransition) {
460 mIsPageInTransition = true;
461 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700462 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700463 }
464
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700465 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700466 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700467 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700468 mIsPageInTransition = false;
469 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700470 }
Michael Jurka0142d492010-08-25 17:46:15 -0700471 }
472
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700473 @Override
474 public void onVisibilityAggregated(boolean isVisible) {
475 pageEndTransition();
476 super.onVisibilityAggregated(isVisible);
477 }
478
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700479 protected boolean isPageInTransition() {
480 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700481 }
482
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700483 /**
484 * Called when the page starts moving as part of the scroll. Subclasses can override this
485 * to provide custom behavior during animation.
486 */
487 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700488 }
489
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700490 /**
491 * Called when the page ends moving as part of the scroll. Subclasses can override this
492 * to provide custom behavior during animation.
493 */
494 protected void onPageEndTransition() {
Alex Chauba61b982021-06-15 16:29:19 +0100495 mCurrentPageScrollDiff = 0;
vadimt4d93df52019-07-10 17:32:58 -0700496 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800497 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
498 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700499 if (mOnPageTransitionEndCallback != null) {
500 mOnPageTransitionEndCallback.run();
501 mOnPageTransitionEndCallback = null;
502 }
503 }
504
505 /**
506 * Sets a callback to run once when the scrolling finishes. If there is currently
507 * no page in transition, then the callback is called immediately.
508 */
509 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
510 if (mIsPageInTransition || callback == null) {
511 mOnPageTransitionEndCallback = callback;
512 } else {
513 callback.run();
514 }
Michael Jurka0142d492010-08-25 17:46:15 -0700515 }
516
Adam Cohen68d73932010-11-15 10:50:58 -0800517 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700518 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700519 x = Utilities.boundToRange(x,
520 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
521 y = Utilities.boundToRange(y,
522 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800523 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700524 }
525
Adam Cohen53805212013-10-01 10:39:23 -0700526 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800527 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700528 if (mCurrentPage != getNextPage()) {
529 AccessibilityEvent ev =
530 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700531 ev.setScrollable(true);
532 ev.setScrollX(getScrollX());
533 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800534 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700535 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700536 }
Adam Cohen53805212013-10-01 10:39:23 -0700537 }
538 }
539
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700540 protected void announcePageForAccessibility() {
541 if (isAccessibilityEnabled(getContext())) {
542 // Notify the user when the page changes
543 announceForAccessibility(getCurrentPageDescription());
544 }
545 }
546
Winson Chung85425412021-05-10 16:24:14 -0700547 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700549 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700550 int oldPos = mOrientationHandler.getPrimaryScroll(this);
551 int newPos = mScroller.getCurrX();
552 if (oldPos != newPos) {
Alex Chau6499e832021-11-19 14:35:46 +0000553 mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700554 }
Winson Chung85425412021-05-10 16:24:14 -0700555
556 if (mAllowOverScroll) {
557 if (newPos < mMinScroll && oldPos >= mMinScroll) {
558 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700559 abortScrollerAnimation(false);
Lais Andradecc5c8842021-06-04 18:20:30 +0100560 onEdgeAbsorbingScroll();
Winson Chung85425412021-05-10 16:24:14 -0700561 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
562 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700563 abortScrollerAnimation(false);
Lais Andradecc5c8842021-06-04 18:20:30 +0100564 onEdgeAbsorbingScroll();
Sunny Goyalc39db732021-04-03 01:20:21 -0700565 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800566 }
Adam Cohen53805212013-10-01 10:39:23 -0700567
Winson Chung85425412021-05-10 16:24:14 -0700568 // If the scroller has scrolled to the final position and there is no edge effect, then
569 // finish the scroller to skip waiting for additional settling
570 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
571 mScroller.getFinalY());
572 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
Schneider Victor-tuliasa602a0a2022-09-06 13:34:00 -0700573 abortScrollerAnimation(false);
Winson Chung85425412021-05-10 16:24:14 -0700574 }
575
576 invalidate();
577 return true;
578 } else if (mNextPage != INVALID_PAGE) {
579 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700580 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700581 mCurrentPage = validateNewPage(mNextPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100582 mCurrentScrollOverPage = mCurrentPage;
Winson Chung86f77532010-08-24 11:08:22 -0700583 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700584 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700585
Adam Cohen73aa9752010-11-24 16:26:58 -0800586 // We don't want to trigger a page end moving unless the page has settled
587 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700588 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700589 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800590 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700591
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700592 if (canAnnouncePageDescription()) {
593 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700594 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 }
Michael Jurka0142d492010-08-25 17:46:15 -0700596 return false;
597 }
598
599 @Override
600 public void computeScroll() {
601 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700602 }
603
Sunny Goyalce8809a2018-01-18 14:02:47 -0800604 public int getExpectedHeight() {
605 return getMeasuredHeight();
606 }
607
Adam Cohen410f3cd2013-09-22 12:09:32 -0700608 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800609 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800610 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700611 }
612
Sunny Goyalce8809a2018-01-18 14:02:47 -0800613 public int getExpectedWidth() {
614 return getMeasuredWidth();
615 }
616
Sunny Goyalbc683e92017-12-06 10:25:07 -0800617 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800618 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800619 - mInsets.left - mInsets.right;
620 }
621
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100622 private void updateVelocityValues() {
623 Resources res = getResources();
624 mFlingThresholdVelocity = res.getDimensionPixelSize(R.dimen.fling_threshold_velocity);
625 mEasyFlingThresholdVelocity =
626 res.getDimensionPixelSize(R.dimen.easy_fling_threshold_velocity);
627 mMinFlingVelocity = res.getDimensionPixelSize(R.dimen.min_fling_velocity);
628 mMinSnapVelocity = res.getDimensionPixelSize(R.dimen.min_page_snap_velocity);
629 mPageSnapAnimationDuration = res.getInteger(R.integer.config_pageSnapAnimationDuration);
630 }
631
632 @Override
633 protected void onConfigurationChanged(Configuration newConfig) {
634 super.onConfigurationChanged(newConfig);
635 updateVelocityValues();
636 }
637
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800639 public void requestLayout() {
640 mIsLayoutValid = false;
641 super.requestLayout();
642 }
643
644 @Override
645 public void forceLayout() {
646 mIsLayoutValid = false;
647 super.forceLayout();
648 }
649
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100650 private int getPageWidthSize(int widthSize) {
Thales Lima7a6752d2021-09-23 14:31:00 +0100651 // It's necessary to add the padding back because it is remove when measuring children,
652 // like when MeasureSpec.getSize in CellLayout.
653 return (widthSize - mInsets.left - mInsets.right - getPaddingLeft() - getPaddingRight())
654 / getPanelCount() + getPaddingLeft() + getPaddingRight();
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100655 }
656
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800657 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700659 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700660 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
661 return;
662 }
663
Adam Cohen7d30a372013-07-01 17:03:59 -0700664 // We measure the dimensions of the PagedView to be larger than the pages so that when we
665 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700666 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
667 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
668 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700669 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700670
Adam Cohen7d30a372013-07-01 17:03:59 -0700671 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
672 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
673 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700674 }
675
Winson Chung8aad6102012-05-11 16:27:49 -0700676 // Return early if we aren't given a proper dimension
677 if (widthSize <= 0 || heightSize <= 0) {
678 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
679 return;
680 }
681
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700683 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700685
Sunny Goyalac00cba2017-11-13 15:58:01 -0800686 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100687 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800688 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800689 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700690
Sunny Goyalac00cba2017-11-13 15:58:01 -0800691 // measureChildren takes accounts for content padding, we only need to care about extra
692 // space due to insets.
693 measureChildren(myWidthSpec, myHeightSpec);
694 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800695 }
696
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700697 /** Returns true iff this PagedView's scroll amounts are initialized to each page index. */
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800698 protected boolean isPageScrollsInitialized() {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700699 return mPageScrolls != null && mPageScrolls.length == getChildCount();
700 }
701
702 /**
703 * Queues the given callback to be run once {@code mPageScrolls} has been initialized.
704 */
705 public void runOnPageScrollsInitialized(Runnable callback) {
706 mOnPageScrollsInitializedCallbacks.add(callback);
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800707 if (isPageScrollsInitialized()) {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700708 onPageScrollsInitialized();
709 }
710 }
711
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800712 protected void onPageScrollsInitialized() {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700713 for (Runnable callback : mOnPageScrollsInitializedCallbacks) {
714 callback.run();
715 }
716 mOnPageScrollsInitializedCallbacks.clear();
717 }
718
Sunny Goyalcf25b522015-07-09 00:01:18 -0700719 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700721 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800722 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700723 final int childCount = getChildCount();
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700724 int[] pageScrolls = mPageScrolls;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700725 boolean pageScrollChanged = false;
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -0800726 if (!isPageScrollsInitialized()) {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700727 pageScrolls = new int[childCount];
Vadim Trysheveb701c62018-05-14 16:00:15 -0700728 pageScrollChanged = true;
729 }
730
Winson Chung785d2eb2011-04-14 16:08:02 -0700731 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700732
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700733 pageScrollChanged |= getPageScrolls(pageScrolls, true, SIMPLE_SCROLL_LOGIC);
734 mPageScrolls = pageScrolls;
Winson Chungc3665fa2011-09-14 17:56:27 -0700735
Winson Chung359cb5d2022-05-26 23:38:48 +0000736 if (childCount == 0) {
737 onPageScrollsInitialized();
738 return;
739 }
740
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700741 final LayoutTransition transition = getLayoutTransition();
742 // If the transition is running defer updating max scroll, as some empty pages could
743 // still be present, and a max scroll change could cause sudden jumps in scroll.
744 if (transition != null && transition.isRunning()) {
745 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
746
747 @Override
748 public void startTransition(LayoutTransition transition, ViewGroup container,
749 View view, int transitionType) { }
750
751 @Override
752 public void endTransition(LayoutTransition transition, ViewGroup container,
753 View view, int transitionType) {
754 // Wait until all transitions are complete.
755 if (!transition.isRunning()) {
756 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700757 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700758 }
759 }
760 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700761 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700762 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700763 }
764
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700765 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
766 updateCurrentPageScroll();
767 mFirstLayout = false;
768 }
769
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800770 if (mScroller.isFinished() && pageScrollChanged) {
Alex Chau0733ebd2022-10-07 15:11:09 +0100771 // TODO(b/246283207): Remove logging once root cause of flake detected.
Brian Isganitisfdd044e2023-03-02 19:45:23 +0000772 if (Utilities.isRunningInTestHarness() && !(this instanceof Workspace)) {
Alex Chau0733ebd2022-10-07 15:11:09 +0100773 Log.d("b/246283207", this.getClass().getSimpleName() + "#onLayout() -> "
Pat Manning24b95542022-08-09 18:06:58 +0100774 + "if(mScroller.isFinished() && pageScrollChanged) -> getNextPage(): "
775 + getNextPage() + ", getScrollForPage(getNextPage()): "
776 + getScrollForPage(getNextPage()));
777 }
Sunny Goyalc82c6392018-06-06 15:39:13 -0700778 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700779 }
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700780 onPageScrollsInitialized();
Winson Chunge3193b92010-09-10 11:44:42 -0700781 }
782
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700783 /**
784 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
785 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700786 */
787 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
788 ComputePageScrollsLogic scrollLogic) {
789 final int childCount = getChildCount();
790
791 final int startIndex = mIsRtl ? childCount - 1 : 0;
792 final int endIndex = mIsRtl ? -1 : childCount;
793 final int delta = mIsRtl ? -1 : 1;
794
Vinit Nayaka406f722019-12-19 11:50:55 -0800795 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700796
Vinit Nayaka406f722019-12-19 11:50:55 -0800797 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
798 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700799 boolean pageScrollChanged = false;
Thales Limae4457742021-07-20 16:46:38 +0100800 int panelCount = getPanelCount();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700801
Vinit Nayaka406f722019-12-19 11:50:55 -0800802 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700803 final View child = getPageAt(i);
804 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800805 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
806 pageCenter, layoutChildren);
807 final int primaryDimension = bounds.primaryDimension;
808 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700809
Alex Chaub5c4d112021-03-29 17:37:53 +0100810 // In case the pages are of different width, align the page to left edge for non-RTL
811 // or right edge for RTL.
812 final int pageScroll =
813 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700814 if (outPageScrolls[i] != pageScroll) {
815 pageScrollChanged = true;
816 outPageScrolls[i] = pageScroll;
817 }
Alex Chau2e487602022-03-15 19:34:08 +0000818 childStart += primaryDimension + getChildGap(i, i + delta);
Thales Limae4457742021-07-20 16:46:38 +0100819
820 // This makes sure that the space is added after the page, not after each panel
Thales Limac2a6f6f2021-09-09 17:06:22 +0100821 int lastPanel = mIsRtl ? 0 : panelCount - 1;
822 if (i % panelCount == lastPanel) {
Thales Limae4457742021-07-20 16:46:38 +0100823 childStart += mPageSpacing;
824 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700825 }
826 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100827
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100828 if (panelCount > 1) {
829 for (int i = 0; i < childCount; i++) {
Alex Chau1e448462021-08-13 21:48:35 +0100830 // In case we have multiple panels, always use left most panel's page scroll for all
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100831 // panels on the screen.
832 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
833 if (outPageScrolls[i] != adjustedScroll) {
834 outPageScrolls[i] = adjustedScroll;
835 pageScrollChanged = true;
836 }
837 }
838 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700839 return pageScrollChanged;
840 }
841
Alex Chau2e487602022-03-15 19:34:08 +0000842 protected int getChildGap(int fromIndex, int toIndex) {
Sunny Goyala1fbd842015-05-20 13:40:27 -0700843 return 0;
844 }
845
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700846 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800847 mMinScroll = computeMinScroll();
848 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700849 }
850
Vinit Nayaka406f722019-12-19 11:50:55 -0800851 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700852 return 0;
Tony50876bf2018-09-19 23:09:14 -0400853 }
854
Vinit Nayaka406f722019-12-19 11:50:55 -0800855 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700856 int childCount = getChildCount();
857 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700858 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700859 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700860 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700861 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700862 }
863 }
864
Adam Cohen3b185e22013-10-29 14:45:58 -0700865 public void setPageSpacing(int pageSpacing) {
866 mPageSpacing = pageSpacing;
867 requestLayout();
868 }
869
Tony50876bf2018-09-19 23:09:14 -0400870 public int getPageSpacing() {
871 return mPageSpacing;
872 }
873
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800874 private void dispatchPageCountChanged() {
875 if (mPageIndicator != null) {
Tony Wickhambab101c2021-09-20 15:12:44 -0700876 mPageIndicator.setMarkersCount(getChildCount() / getPanelCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700877 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700878 // This ensures that when children are added, they get the correct transforms / alphas
879 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700880 invalidate();
881 }
882
Michael Jurka8b805b12012-04-18 14:23:14 -0700883 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800884 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800885 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800886 dispatchPageCountChanged();
887 }
888
889 @Override
890 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800891 super.onViewRemoved(child);
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700892 runOnPageScrollsInitialized(() -> {
893 mCurrentPage = validateNewPage(mCurrentPage);
894 mCurrentScrollOverPage = mCurrentPage;
895 });
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800896 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700897 }
898
Adam Cohen73894962011-10-31 13:17:17 -0700899 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700900 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800901 View pageAtIndex = getPageAt(index);
902 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700903 }
904
Alex Chau7944ee52021-01-08 19:07:25 +0000905 protected int getChildVisibleSize(int index) {
906 View layout = getPageAt(index);
907 return mOrientationHandler.getMeasuredSize(layout);
908 }
909
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 @Override
911 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200912 int page = indexOfChild(child);
913 if (!isVisible(page) || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700914 if (immediate) {
915 setCurrentPage(page);
916 } else {
917 snapToPage(page);
918 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 return true;
920 }
921 return false;
922 }
923
924 @Override
925 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700926 int focusablePage;
927 if (mNextPage != INVALID_PAGE) {
928 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700930 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 }
Winson Chung86f77532010-08-24 11:08:22 -0700932 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700934 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 }
936 return false;
937 }
938
939 @Override
940 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800941 if (super.dispatchUnhandledMove(focused, direction)) {
942 return true;
943 }
944
945 if (mIsRtl) {
946 if (direction == View.FOCUS_LEFT) {
947 direction = View.FOCUS_RIGHT;
948 } else if (direction == View.FOCUS_RIGHT) {
949 direction = View.FOCUS_LEFT;
950 }
951 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200952
953 int currentPage = getNextPage();
954 int closestNeighbourIndex = -1;
955 int closestNeighbourDistance = Integer.MAX_VALUE;
956 // Find the closest neighbour page
957 for (int neighbourPageIndex : getNeighbourPageIndices(direction)) {
958 int distance = Math.abs(neighbourPageIndex - currentPage);
959 if (closestNeighbourDistance > distance) {
960 closestNeighbourDistance = distance;
961 closestNeighbourIndex = neighbourPageIndex;
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 }
963 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200964 if (closestNeighbourIndex != -1) {
965 View page = getPageAt(closestNeighbourIndex);
966 snapToPage(closestNeighbourIndex);
967 page.requestFocus(direction);
968 return true;
969 }
970
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800971 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 }
973
974 @Override
975 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700976 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
977 return;
978 }
979
Andras Kloczl7e912442021-05-06 12:50:28 +0200980 // nextPage is more reliable when multiple control movements have been done in a short
981 // period of time
982 getPageIndices(getNextPage())
983 .addAll(getNeighbourPageIndices(direction))
984 .forEach(pageIndex ->
985 getPageAt(pageIndex).addFocusables(views, direction, focusableMode));
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 }
987
988 /**
989 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700990 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 *
Winson Chung86f77532010-08-24 11:08:22 -0700992 * This happens when live folders requery, and if they're off page, they
993 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 */
995 @Override
996 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700997 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 View v = focused;
999 while (true) {
1000 if (v == current) {
1001 super.focusableViewAvailable(focused);
1002 return;
1003 }
1004 if (v == this) {
1005 return;
1006 }
1007 ViewParent parent = v.getParent();
1008 if (parent instanceof View) {
1009 v = (View)v.getParent();
1010 } else {
1011 return;
1012 }
1013 }
1014 }
1015
1016 /**
1017 * {@inheritDoc}
1018 */
1019 @Override
1020 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1021 if (disallowIntercept) {
1022 // We need to make sure to cancel our long press if
1023 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +02001024 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 }
1026 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1027 }
1028
1029 @Override
1030 public boolean onInterceptTouchEvent(MotionEvent ev) {
1031 /*
1032 * This method JUST determines whether we want to intercept the motion.
1033 * If we return true, onTouchEvent will be called and we do the actual
1034 * scrolling there.
1035 */
1036
Winson Chung45e1d6e2010-11-09 17:19:49 -08001037 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001038 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -07001039
1040 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001041
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 /*
1043 * Shortcut the most recurring case: the user is in the dragging
1044 * state and he is moving his finger. We want to intercept this
1045 * motion.
1046 */
1047 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -07001048 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 return true;
1050 }
1051
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 switch (action & MotionEvent.ACTION_MASK) {
1053 case MotionEvent.ACTION_MOVE: {
1054 /*
1055 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -08001056 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001058 if (mActivePointerId != INVALID_POINTER) {
1059 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001060 }
1061 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -08001062 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -07001063 // i.e. fall through to the next case (don't break)
1064 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1065 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001066 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068
1069 case MotionEvent.ACTION_DOWN: {
1070 final float x = ev.getX();
1071 final float y = ev.getY();
1072 // Remember location of down touch
1073 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001074 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -07001075 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001076 mLastMotionRemainder = 0;
1077 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001078 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -07001080 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 break;
1082 }
1083
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001085 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001086 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001087 break;
1088
1089 case MotionEvent.ACTION_POINTER_UP:
1090 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001091 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 break;
1093 }
1094
1095 /*
1096 * The only time we want to intercept motion events is if we are in the
1097 * drag mode.
1098 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001099 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001100 }
1101
Tony Wickhama0aee212019-09-18 09:24:03 -07001102 /**
1103 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1104 */
Sunny Goyalbb030562021-07-19 16:28:39 -07001105 protected void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -07001106 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -07001107 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -07001108 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001109
1110 if (finishedScrolling) {
1111 mIsBeingDragged = false;
1112 if (!mScroller.isFinished() && !mFreeScroll) {
1113 setCurrentPage(getNextPage());
1114 pageEndTransition();
1115 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001116 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -07001117 } else {
1118 mIsBeingDragged = true;
1119 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001120
1121 // Catch the edge effect if it is active.
1122 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
1123 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
1124 if (!mEdgeGlowLeft.isFinished()) {
1125 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
1126 }
1127 if (!mEdgeGlowRight.isFinished()) {
1128 mEdgeGlowRight.onPullDistance(0f, displacement);
1129 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001130 }
1131
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001132 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001133 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001134 }
1135
Adam Cohenf8d28232011-02-01 21:47:00 -08001136 protected void determineScrollingStart(MotionEvent ev) {
1137 determineScrollingStart(ev, 1.0f);
1138 }
1139
Winson Chung321e9ee2010-08-09 13:37:56 -07001140 /*
1141 * Determines if we should change the touch state to start scrolling after the
1142 * user moves their touch point too far.
1143 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001144 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001145 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001146 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001147 if (pointerIndex == -1) return;
1148
Vinit Nayaka406f722019-12-19 11:50:55 -08001149 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1150 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001151 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001152 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001153
Vinit Nayaka406f722019-12-19 11:50:55 -08001154 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001155 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001156 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001157 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1158 mLastMotion = primaryDirection;
1159 mLastMotionRemainder = 0;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001160 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001161 // Stop listening for things like pinches.
1162 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001163 }
1164 }
1165
1166 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001167 // Try canceling the long press. It could also have been scheduled
1168 // by a distant descendant, so use the mAllowLongPress flag to block
1169 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001170 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 }
1172
Adam Cohenb5ba0972011-09-07 18:02:31 -07001173 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001174 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenedb40762013-07-18 16:45:45 -07001175 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001176 int panelCount = getPanelCount();
1177 int pageCount = getChildCount();
Adam Cohen3b185e22013-10-29 14:45:58 -07001178
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001179 int adjacentPage = page + panelCount;
Sunny Goyal70660032015-05-14 00:07:08 -07001180 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001181 adjacentPage = page - panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001182 }
1183
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001184 final int totalDistance;
1185 if (adjacentPage < 0 || adjacentPage > pageCount - 1) {
1186 totalDistance = (v.getMeasuredWidth() + mPageSpacing) * panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001187 } else {
1188 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1189 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001190
1191 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001192 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001193 scrollProgress = Math.max(scrollProgress, -MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001194 return scrollProgress;
1195 }
1196
Adam Cohenedb40762013-07-18 16:45:45 -07001197 public int getScrollForPage(int index) {
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -08001198 if (!isPageScrollsInitialized() || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001199 return 0;
1200 } else {
1201 return mPageScrolls[index];
1202 }
1203 }
1204
Adam Cohen564a2e72013-10-09 14:47:32 -07001205 // While layout transitions are occurring, a child's position may stray from its baseline
1206 // position. This method returns the magnitude of this stray at any given time.
1207 public int getLayoutTransitionOffsetForPage(int index) {
Schneider Victor-tulias7bb558a2022-12-15 05:42:53 -08001208 if (!isPageScrollsInitialized() || index >= mPageScrolls.length || index < 0) {
Adam Cohen564a2e72013-10-09 14:47:32 -07001209 return 0;
1210 } else {
1211 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001212
Sunny Goyal228153d2018-01-04 15:35:22 -08001213 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1214 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001215 return (int) (child.getX() - baselineX);
1216 }
1217 }
1218
Tony Wickham023f4042019-01-29 10:52:31 -08001219 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001220 if (mFreeScroll == freeScroll) {
1221 return;
1222 }
1223
Tony Wickham8f7ead32016-04-07 18:46:44 -07001224 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001225 mFreeScroll = freeScroll;
1226
Adam Cohenf9618852013-11-08 06:45:03 -08001227 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001228 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001229 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001230 if (getScrollForPage(getNextPage()) != getScrollX()) {
1231 snapToPage(getNextPage());
1232 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001233 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001234 }
1235
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001236 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001237 mAllowOverScroll = enable;
1238 }
1239
Andras Kloczl07111f22022-03-30 17:25:54 +01001240 protected boolean isSignificantMove(float absoluteDelta, int pageOrientedSize) {
1241 return absoluteDelta > pageOrientedSize * SIGNIFICANT_MOVE_THRESHOLD;
Alex Chau822acf42021-06-03 11:35:05 +01001242 }
1243
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 @Override
1245 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001246 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001247 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001248
Michael Jurkab8f06722010-10-10 15:58:46 -07001249 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001250
1251 final int action = ev.getAction();
1252
1253 switch (action & MotionEvent.ACTION_MASK) {
1254 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001255 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001256
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 /*
1258 * If being flinged and user touches, stop the fling. isFinished
1259 * will be false if being flinged.
1260 */
1261 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001262 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 }
1264
1265 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001266 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001267 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001268 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1269 mLastMotionRemainder = 0;
1270 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001271 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001273 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001274 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001275 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 break;
1277
Winson Chung4941f652020-06-18 15:11:53 -07001278 case ACTION_MOVE_ALLOW_EASY_FLING:
1279 // Start scrolling immediately
1280 determineScrollingStart(ev);
1281 mAllowEasyFling = true;
1282 break;
1283
1284 case MotionEvent.ACTION_MOVE:
1285 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 // Scroll to follow the motion event
1287 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001288
1289 if (pointerIndex == -1) return true;
Sunny Goyalc39db732021-04-03 01:20:21 -07001290 float oldScroll = mOrientationHandler.getPrimaryScroll(this);
1291 float dx = ev.getX(pointerIndex);
1292 float dy = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001293
Sunny Goyalc39db732021-04-03 01:20:21 -07001294 float direction = mOrientationHandler.getPrimaryValue(dx, dy);
Vinit Nayaka406f722019-12-19 11:50:55 -08001295 float delta = mLastMotion + mLastMotionRemainder - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001296
1297 int width = getWidth();
1298 int height = getHeight();
1299 int size = mOrientationHandler.getPrimaryValue(width, height);
1300
1301 final float displacement = mOrientationHandler.getSecondaryValue(dx, dy)
1302 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001303 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001304
Sunny Goyalc39db732021-04-03 01:20:21 -07001305 if (mAllowOverScroll) {
1306 float consumed = 0;
1307 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
1308 consumed = size * mEdgeGlowRight.onPullDistance(delta / size, displacement);
1309 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
1310 consumed = -size * mEdgeGlowLeft.onPullDistance(
1311 -delta / size, 1 - displacement);
1312 }
1313 delta -= consumed;
1314 }
Alex Chau822acf42021-06-03 11:35:05 +01001315 delta /= mOrientationHandler.getPrimaryScale(this);
Sunny Goyalc39db732021-04-03 01:20:21 -07001316
Winson Chungc0844aa2011-02-02 15:25:58 -08001317 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1318 // keep the remainder because we are actually testing if we've moved from the last
1319 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001320 mLastMotion = direction;
1321 int movedDelta = (int) delta;
1322 mLastMotionRemainder = delta - movedDelta;
Vinit Nayaka406f722019-12-19 11:50:55 -08001323
Sunny Goyalc39db732021-04-03 01:20:21 -07001324 if (delta != 0) {
Alex Chau6499e832021-11-19 14:35:46 +00001325 mOrientationHandler.setPrimary(this, VIEW_SCROLL_BY, movedDelta);
Sunny Goyalc39db732021-04-03 01:20:21 -07001326
1327 if (mAllowOverScroll) {
1328 final float pulledToX = oldScroll + delta;
1329
1330 if (pulledToX < mMinScroll) {
1331 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement);
1332 if (!mEdgeGlowRight.isFinished()) {
1333 mEdgeGlowRight.onRelease();
1334 }
1335 } else if (pulledToX > mMaxScroll) {
1336 mEdgeGlowRight.onPullDistance(delta / size, displacement);
1337 if (!mEdgeGlowLeft.isFinished()) {
1338 mEdgeGlowLeft.onRelease();
1339 }
1340 }
1341
1342 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1343 postInvalidateOnAnimation();
1344 }
1345 }
1346
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 } else {
1348 awakenScrollBars();
1349 }
Adam Cohen564976a2010-10-13 18:52:07 -07001350 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 determineScrollingStart(ev);
1352 }
1353 break;
1354
1355 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001356 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 final int activePointerId = mActivePointerId;
1358 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001359 if (pointerIndex == -1) return true;
1360
Vinit Nayaka406f722019-12-19 11:50:55 -08001361 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1362 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001363 final VelocityTracker velocityTracker = mVelocityTracker;
1364 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001365
Vinit Nayaka406f722019-12-19 11:50:55 -08001366 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
Andras Kloczl07111f22022-03-30 17:25:54 +01001367 mActivePointerId);
Alex Chau822acf42021-06-03 11:35:05 +01001368 float delta = primaryDirection - mDownMotionPrimary;
Randy Pfohl583241e2023-03-02 03:26:56 +00001369
1370 View current = getPageAt(mCurrentPage);
1371 if (current == null) {
1372 Log.e(TAG, "current page was null. this should not happen.");
1373 return true;
1374 }
1375
1376 int pageOrientedSize = (int) (mOrientationHandler.getMeasuredSize(current)
Andras Kloczl07111f22022-03-30 17:25:54 +01001377 * mOrientationHandler.getPrimaryScale(this));
1378 boolean isSignificantMove = isSignificantMove(Math.abs(delta), pageOrientedSize);
Vinit Nayaka406f722019-12-19 11:50:55 -08001379
1380 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001381 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1382 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001383 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1384 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001385 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1386 Log.d("Quickswitch", "isFling=false vel=" + velocity
1387 + " threshold=" + mEasyFlingThresholdVelocity);
1388 }
Adam Cohen00481b32011-11-18 12:03:48 -08001389
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 if (!mFreeScroll) {
1391 // In the case that the page is moved far to one direction and then is flung
1392 // in the opposite direction, we use a threshold to determine whether we should
1393 // just return to the starting page, or if we should skip one further.
1394 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001395 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1396 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 returnToOriginalPage = true;
1398 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001399
Adam Cohenf358a4b2013-07-23 16:47:31 -07001400 int finalPage;
1401 // We give flings precedence over large moves, which is why we short-circuit our
1402 // test for a large move if a fling has been registered. That is, a large
1403 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001404
Vinit Nayaka406f722019-12-19 11:50:55 -08001405 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1406 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001407 finalPage = returnToOriginalPage
1408 ? mCurrentPage : mCurrentPage - getPanelCount();
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001409 runOnPageScrollsInitialized(
1410 () -> snapToPageWithVelocity(finalPage, velocity));
Vinit Nayaka406f722019-12-19 11:50:55 -08001411 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1412 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001413 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001414 finalPage = returnToOriginalPage
1415 ? mCurrentPage : mCurrentPage + getPanelCount();
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001416 runOnPageScrollsInitialized(
1417 () -> snapToPageWithVelocity(finalPage, velocity));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 } else {
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001419 runOnPageScrollsInitialized(this::snapToDestination);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001422 if (!mScroller.isFinished()) {
1423 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001425
Vinit Nayaka406f722019-12-19 11:50:55 -08001426 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1427 int maxScroll = mMaxScroll;
1428 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001429
Vinit Nayaka406f722019-12-19 11:50:55 -08001430 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1431 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001432 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001433 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001434 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001435 int velocity1 = -velocity;
1436 // Continue a scroll or fling in progress
1437 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1438 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001439
Sunny Goyalc39db732021-04-03 01:20:21 -07001440 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001441 mNextPage = getDestinationPage(finalPos);
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001442 runOnPageScrollsInitialized(this::onNotSnappingToPageInFreeScroll);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001443 }
1444 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001447
Sunny Goyalc39db732021-04-03 01:20:21 -07001448 mEdgeGlowLeft.onRelease();
1449 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001450 // End any intermediate reordering states
1451 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 break;
1453
1454 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001455 if (mIsBeingDragged) {
Schneider Victor-tulias50f66512022-12-14 02:53:38 -08001456 runOnPageScrollsInitialized(this::snapToDestination);
Michael Jurkab8f06722010-10-10 15:58:46 -07001457 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001458 mEdgeGlowLeft.onRelease();
1459 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001460 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 break;
1462
1463 case MotionEvent.ACTION_POINTER_UP:
1464 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001465 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 break;
1467 }
1468
1469 return true;
1470 }
1471
Sunny Goyalc39db732021-04-03 01:20:21 -07001472 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001473
Lais Andradecc5c8842021-06-04 18:20:30 +01001474 /**
1475 * Called when the view edges absorb part of the scroll. Subclasses can override this
1476 * to provide custom behavior during animation.
1477 */
1478 protected void onEdgeAbsorbingScroll() {
1479 }
1480
1481 /**
1482 * Called when the current page closest to the center of the screen changes as part of the
1483 * scroll. Subclasses can override this to provide custom behavior during scroll.
1484 */
1485 protected void onScrollOverPageChanged() {
1486 }
1487
Vinit Nayaka406f722019-12-19 11:50:55 -08001488 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001489 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1490 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001491 }
1492
Alex Chau9d8eeab2022-04-05 17:13:56 +01001493 protected void resetTouchState() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001495 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001496 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001497 }
1498
Winson Chung185d7162011-02-28 13:47:29 -08001499 @Override
1500 public boolean onGenericMotionEvent(MotionEvent event) {
1501 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1502 switch (event.getAction()) {
1503 case MotionEvent.ACTION_SCROLL: {
1504 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1505 final float vscroll;
1506 final float hscroll;
1507 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1508 vscroll = 0;
1509 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1510 } else {
1511 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1512 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1513 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001514 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1515 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001516 }
Winson Chung185d7162011-02-28 13:47:29 -08001517 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001518 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001519 : (hscroll > 0 || vscroll > 0);
1520 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001521 scrollRight();
1522 } else {
1523 scrollLeft();
1524 }
1525 return true;
1526 }
1527 }
1528 }
1529 }
1530 return super.onGenericMotionEvent(event);
1531 }
1532
Sunny Goyal8b37c572020-03-31 12:12:14 -07001533 /**
1534 * Returns true if the paged view can scroll for the provided vertical and horizontal
1535 * scroll values
1536 */
1537 protected boolean canScroll(float absVScroll, float absHScroll) {
1538 ActivityContext ac = ActivityContext.lookupContext(getContext());
1539 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001540 }
1541
Michael Jurkab8f06722010-10-10 15:58:46 -07001542 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1543 if (mVelocityTracker == null) {
1544 mVelocityTracker = VelocityTracker.obtain();
1545 }
1546 mVelocityTracker.addMovement(ev);
1547 }
1548
1549 private void releaseVelocityTracker() {
1550 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001551 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001552 mVelocityTracker.recycle();
1553 mVelocityTracker = null;
1554 }
1555 }
1556
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001558 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001559 final int pointerId = ev.getPointerId(pointerIndex);
1560 if (pointerId == mActivePointerId) {
1561 // This was our active pointer going up. Choose a new
1562 // active pointer and adjust accordingly.
1563 // TODO: Make this decision more intelligent.
1564 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001565 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1566 newPointerIndex);
1567 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001568 mActivePointerId = ev.getPointerId(newPointerIndex);
1569 if (mVelocityTracker != null) {
1570 mVelocityTracker.clear();
1571 }
1572 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001573 }
1574
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 @Override
1576 public void requestChildFocus(View child, View focused) {
1577 super.requestChildFocus(child, focused);
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001578
1579 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1580 // which results in incorrect navigation
1581 int nextPage = getNextPage();
1582 if (nextPage != mCurrentPage) {
1583 setCurrentPage(nextPage);
1584 }
1585
Andras Kloczl7e912442021-05-06 12:50:28 +02001586 int page = indexOfChild(child);
1587 if (page >= 0 && !isVisible(page) && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001588 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 }
1590 }
1591
Alex Chaub794ea62021-02-03 18:00:30 +00001592 public int getDestinationPage() {
1593 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1594 }
1595
Alex Chaudedbc8a2021-03-17 16:53:26 +00001596 protected int getDestinationPage(int primaryScroll) {
1597 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001598 }
1599
Sunny Goyal228153d2018-01-04 15:35:22 -08001600 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001601 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001602 }
1603
Alex Chaudedbc8a2021-03-17 16:53:26 +00001604 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1605 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001606 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001608 final int childCount = getChildCount();
1609 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001610 int distanceFromScreenCenter = Math.abs(
1611 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001612 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1613 minDistanceFromScreenCenter = distanceFromScreenCenter;
1614 minDistanceFromScreenCenterIndex = i;
1615 }
1616 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001617 return minDistanceFromScreenCenterIndex;
1618 }
1619
Tony Wickham7383d4e2020-07-07 19:25:25 -07001620 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Cole Faust50ec4af2022-10-15 21:33:28 -07001621 int childSize = getChildVisibleSize(childIndex);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001622 int halfChildSize = (childSize / 2);
1623 int childCenter = getChildOffset(childIndex) + halfChildSize;
1624 return childCenter - screenCenter;
1625 }
1626
1627 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001628 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1629 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001630 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1631 }
1632
Andras Kloczl9732df12021-09-09 19:40:42 +01001633 protected int getScreenCenter(int primaryScroll) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001634 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1635 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1636 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1637 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1638 + primaryPivot);
1639 }
1640
Adam Cohend19d3ca2010-09-15 14:43:42 -07001641 protected void snapToDestination() {
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001642 snapToPage(getDestinationPage(), mPageSnapAnimationDuration);
Winson Chung321e9ee2010-08-09 13:37:56 -07001643 }
1644
Adam Cohene0f66b52010-11-23 15:06:07 -08001645 // We want the duration of the page snap animation to be influenced by the distance that
1646 // the screen has to travel, however, we don't want this duration to be effected in a
1647 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1648 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001649 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001650 f -= 0.5f; // center the values about 0.
1651 f *= 0.3f * Math.PI / 2.0f;
1652 return (float) Math.sin(f);
1653 }
1654
Winson Chung05a31ed2018-02-08 17:08:43 -08001655 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001656 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001657 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001658
Vinit Nayaka406f722019-12-19 11:50:55 -08001659 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001660 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001661 int duration = 0;
1662
Sunny Goyal4d113a52015-05-27 10:05:28 -07001663 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001664 // If the velocity is low enough, then treat this more as an automatic page advance
1665 // as opposed to an apparent physical response to flinging
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001666 return snapToPage(whichPage, mPageSnapAnimationDuration);
Adam Cohene0f66b52010-11-23 15:06:07 -08001667 }
1668
1669 // Here we compute a "distance" that will be used in the computation of the overall
1670 // snap duration. This is a function of the actual distance that needs to be traveled;
1671 // we keep this value close to half screen size in order to reduce the variance in snap
1672 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001673 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001674 float distance = halfScreenSize + halfScreenSize *
1675 distanceInfluenceForSnapDuration(distanceRatio);
1676
1677 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001678 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001679
1680 // we want the page's snap velocity to approximately match the velocity at which the
1681 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001682 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1683 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001684
Sunny Goyalc39db732021-04-03 01:20:21 -07001685 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001686 }
1687
Winson Chung05a31ed2018-02-08 17:08:43 -08001688 public boolean snapToPage(int whichPage) {
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001689 return snapToPage(whichPage, mPageSnapAnimationDuration);
Winson Chung321e9ee2010-08-09 13:37:56 -07001690 }
1691
Winson Chung05a31ed2018-02-08 17:08:43 -08001692 public boolean snapToPageImmediately(int whichPage) {
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001693 return snapToPage(whichPage, mPageSnapAnimationDuration, true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001694 }
1695
Winson Chung05a31ed2018-02-08 17:08:43 -08001696 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001697 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001698 }
1699
Sunny Goyalc39db732021-04-03 01:20:21 -07001700 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001701 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001702
Vinit Nayaka406f722019-12-19 11:50:55 -08001703 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001704 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1705 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001706 }
1707
Winson Chung05a31ed2018-02-08 17:08:43 -08001708 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001709 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001710 }
Michael Jurka0142d492010-08-25 17:46:15 -07001711
Sunny Goyalc39db732021-04-03 01:20:21 -07001712 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001713 if (mFirstLayout) {
1714 setCurrentPage(whichPage);
1715 return false;
1716 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001717
Brian Isganitisfdd044e2023-03-02 19:45:23 +00001718 if (FeatureFlags.IS_STUDIO_BUILD && !Utilities.isRunningInTestHarness()) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001719 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1720 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001721 }
1722
Adam Cohen6f127a62014-06-12 14:54:41 -07001723 whichPage = validateNewPage(whichPage);
1724
Adam Cohen7d30a372013-07-01 17:03:59 -07001725 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001726
Winson Chung321e9ee2010-08-09 13:37:56 -07001727 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001728 if (immediate) {
1729 duration = 0;
1730 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001731 duration = Math.abs(delta);
1732 }
1733
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001734 if (duration != 0) {
1735 pageBeginTransition();
1736 }
1737
Adam Cohenf358a4b2013-07-23 16:47:31 -07001738 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001739 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001740 }
Adam Cohenf9618852013-11-08 06:45:03 -08001741
Sunny Goyalc39db732021-04-03 01:20:21 -07001742 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001743 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001744
1745 // Trigger a compute() to finish switching pages if necessary
1746 if (immediate) {
1747 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001748 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001749 }
1750
Winson Chung321e9ee2010-08-09 13:37:56 -07001751 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001752 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001753 }
1754
Vadim Tryshev98913d02018-05-18 18:41:34 -07001755 public boolean scrollLeft() {
1756 if (getNextPage() > 0) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001757 snapToPage(getNextPage() - getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001758 return true;
1759 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001760 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001761 }
1762
Vadim Tryshev98913d02018-05-18 18:41:34 -07001763 public boolean scrollRight() {
1764 if (getNextPage() < getChildCount() - 1) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001765 snapToPage(getNextPage() + getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001766 return true;
1767 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001768 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001769 }
1770
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001771 @Override
Lais Andradecc5c8842021-06-04 18:20:30 +01001772 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Lais Andradef6e31b12021-10-27 12:04:28 +01001773 if (mScroller.isFinished()) {
1774 // This was not caused by the scroller, skip it.
1775 return;
1776 }
Lais Andradecc5c8842021-06-04 18:20:30 +01001777 int newDestinationPage = getDestinationPage();
1778 if (newDestinationPage >= 0 && newDestinationPage != mCurrentScrollOverPage) {
1779 mCurrentScrollOverPage = newDestinationPage;
1780 onScrollOverPageChanged();
1781 }
1782 }
1783
1784 @Override
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001785 public CharSequence getAccessibilityClassName() {
1786 // Some accessibility services have special logic for ScrollView. Since we provide same
1787 // accessibility info as ScrollView, inform the service to handle use the same way.
1788 return ScrollView.class.getName();
1789 }
1790
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001791 protected boolean isPageOrderFlipped() {
1792 return false;
1793 }
1794
Winson Chung6a0f57d2011-06-29 20:10:49 -07001795 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001796 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001797 @Override
1798 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1799 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001800 final boolean pagesFlipped = isPageOrderFlipped();
Alex Chau9ead9ca2021-11-26 16:22:49 +00001801 info.setScrollable(getPageCount() > 0);
1802 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1803 if (getCurrentPage() < getPageCount() - getPanelCount()
1804 || (getCurrentPage() == getPageCount() - getPanelCount()
1805 && primaryScroll != getScrollForPage(getPageCount() - getPanelCount()))) {
yingleiw02cc8482019-08-02 16:14:27 -07001806 info.addAction(pagesFlipped ?
Alex Chau9ead9ca2021-11-26 16:22:49 +00001807 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1808 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
yingleiw02cc8482019-08-02 16:14:27 -07001809 info.addAction(mIsRtl ?
1810 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1811 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001812 }
Alex Chau9ead9ca2021-11-26 16:22:49 +00001813 if (getCurrentPage() > 0
1814 || (getCurrentPage() == 0 && primaryScroll != getScrollForPage(0))) {
yingleiw02cc8482019-08-02 16:14:27 -07001815 info.addAction(pagesFlipped ?
Alex Chau9ead9ca2021-11-26 16:22:49 +00001816 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1817 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
yingleiw02cc8482019-08-02 16:14:27 -07001818 info.addAction(mIsRtl ?
1819 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1820 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001821 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001822 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1823 // Besides disabling the accessibility long-click, this also prevents this view from getting
1824 // accessibility focus.
1825 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001826 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001827 }
1828
1829 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001830 public void sendAccessibilityEvent(int eventType) {
1831 // Don't let the view send real scroll events.
1832 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1833 super.sendAccessibilityEvent(eventType);
1834 }
1835 }
1836
1837 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001838 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1839 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001840 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001841 }
1842
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001843 @Override
1844 public boolean performAccessibilityAction(int action, Bundle arguments) {
1845 if (super.performAccessibilityAction(action, arguments)) {
1846 return true;
1847 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001848 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001849 switch (action) {
1850 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001851 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001852 return true;
1853 }
1854 } break;
1855 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001856 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001857 return true;
1858 }
yingleiw02cc8482019-08-02 16:14:27 -07001859 } break;
1860 case android.R.id.accessibilityActionPageRight: {
1861 if (!mIsRtl) {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001862 return scrollRight();
yingleiw02cc8482019-08-02 16:14:27 -07001863 } else {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001864 return scrollLeft();
yingleiw02cc8482019-08-02 16:14:27 -07001865 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001866 }
yingleiw02cc8482019-08-02 16:14:27 -07001867 case android.R.id.accessibilityActionPageLeft: {
1868 if (!mIsRtl) {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001869 return scrollLeft();
yingleiw02cc8482019-08-02 16:14:27 -07001870 } else {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001871 return scrollRight();
yingleiw02cc8482019-08-02 16:14:27 -07001872 }
1873 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001874 }
1875 return false;
1876 }
1877
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001878 protected boolean canAnnouncePageDescription() {
1879 return true;
1880 }
1881
Adam Cohen0ffac432013-07-10 11:19:26 -07001882 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001883 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001884 getNextPage() + 1, getChildCount());
1885 }
1886
Tony Mak2e564402018-02-27 17:19:34 +00001887 protected float getDownMotionX() {
1888 return mDownMotionX;
1889 }
1890
1891 protected float getDownMotionY() {
1892 return mDownMotionY;
1893 }
1894
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001895 protected interface ComputePageScrollsLogic {
1896
1897 boolean shouldIncludeView(View view);
1898 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001899
1900 public int[] getVisibleChildrenRange() {
1901 float visibleLeft = 0;
1902 float visibleRight = visibleLeft + getMeasuredWidth();
1903 float scaleX = getScaleX();
1904 if (scaleX < 1 && scaleX > 0) {
1905 float mid = getMeasuredWidth() / 2;
1906 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1907 visibleRight = mid + ((visibleRight - mid) / scaleX);
1908 }
1909
1910 int leftChild = -1;
1911 int rightChild = -1;
1912 final int childCount = getChildCount();
1913 for (int i = 0; i < childCount; i++) {
1914 final View child = getPageAt(i);
1915
1916 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1917 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1918 if (leftChild == -1) {
1919 leftChild = i;
1920 }
1921 rightChild = i;
1922 }
1923 }
1924 mTmpIntPair[0] = leftChild;
1925 mTmpIntPair[1] = rightChild;
1926 return mTmpIntPair;
1927 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001928
1929 @Override
1930 public void draw(Canvas canvas) {
1931 super.draw(canvas);
1932 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001933 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001934 }
1935
1936 protected void drawEdgeEffect(Canvas canvas) {
1937 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1938 final int width = getWidth();
1939 final int height = getHeight();
1940 if (!mEdgeGlowLeft.isFinished()) {
1941 final int restoreCount = canvas.save();
1942 canvas.rotate(-90);
1943 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1944 mEdgeGlowLeft.setSize(height, width);
1945 if (mEdgeGlowLeft.draw(canvas)) {
1946 postInvalidateOnAnimation();
1947 }
1948 canvas.restoreToCount(restoreCount);
1949 }
1950 if (!mEdgeGlowRight.isFinished()) {
1951 final int restoreCount = canvas.save();
1952 canvas.rotate(90, width, 0);
1953 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1954
1955 mEdgeGlowRight.setSize(height, width);
1956 if (mEdgeGlowRight.draw(canvas)) {
1957 postInvalidateOnAnimation();
1958 }
1959 canvas.restoreToCount(restoreCount);
1960 }
1961 }
1962 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001963}