blob: 4d33eae01c46607074c60b3604d19a38f817ac7c [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Andras Kloczl55edfe52021-05-14 12:21:30 +020019import static androidx.annotation.VisibleForTesting.PACKAGE_PRIVATE;
Andras Kloczldbf577a2021-05-26 15:02:52 +020020
Sunny Goyalc39db732021-04-03 01:20:21 -070021import static com.android.launcher3.anim.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070022import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
23import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyal8b37c572020-03-31 12:12:14 -070024import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070025import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
26import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
27
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070028import android.animation.LayoutTransition;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Andras Kloczlc2bd15f2022-03-30 18:06:13 +010031import android.content.res.Configuration;
32import android.content.res.Resources;
Adam Cohen9c4949e2010-10-05 12:27:22 -070033import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080034import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070036import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070037import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080040import android.view.InputDevice;
41import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.view.MotionEvent;
43import android.view.VelocityTracker;
44import android.view.View;
45import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080046import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070047import android.view.ViewGroup;
48import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityEvent;
50import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalc39db732021-04-03 01:20:21 -070051import android.widget.OverScroller;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070052import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070053
Tony Wickham62117d72020-03-26 16:56:26 -070054import androidx.annotation.Nullable;
Andras Kloczl55edfe52021-05-14 12:21:30 +020055import androidx.annotation.VisibleForTesting;
Tony Wickham62117d72020-03-26 16:56:26 -070056
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.compat.AccessibilityManagerCompat;
58import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070059import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070060import com.android.launcher3.touch.PagedOrientationHandler;
61import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070062import com.android.launcher3.util.EdgeEffectCompat;
Andras Kloczl55edfe52021-05-14 12:21:30 +020063import com.android.launcher3.util.IntSet;
Jon Miranda9e198662020-03-11 14:42:02 -070064import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070065import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070066
67import java.util.ArrayList;
Sunny Goyal59969372021-05-06 12:11:44 -070068import java.util.function.Consumer;
Jon Miranda9e198662020-03-11 14:42:02 -070069
Winson Chung321e9ee2010-08-09 13:37:56 -070070/**
71 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070072 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070073 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080074public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070075 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070076 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070077 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070078
Winson Chung4941f652020-06-18 15:11:53 -070079 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053080 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070081 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Sunny Goyal8e2133b2015-05-06 13:39:07 -070087 private static final float MAX_SCROLL_PROGRESS = 1.0f;
88
Adam Cohenf358a4b2013-07-23 16:47:31 -070089 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070090
Andras Kloczlc2bd15f2022-03-30 18:06:13 +010091 private int mFlingThresholdVelocity;
92 private int mEasyFlingThresholdVelocity;
93 private int mMinFlingVelocity;
94 private int mMinSnapVelocity;
95 private int mPageSnapAnimationDuration;
Michael Jurka0142d492010-08-25 17:46:15 -070096
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected boolean mFirstLayout = true;
98
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mCurrentPage;
Alex Chauba61b982021-06-15 16:29:19 +0100101 // Difference between current scroll position and mCurrentPage's page scroll. Used to maintain
102 // relative scroll position unchanged in updateCurrentPageScroll. Cleared when snapping to a
103 // page.
104 protected int mCurrentPageScrollDiff;
Lais Andradecc5c8842021-06-04 18:20:30 +0100105 // The current page the PagedView is scrolling over on it's way to the destination page.
106 protected int mCurrentScrollOverPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700107
Sunny Goyal4ffec482016-02-09 11:28:52 -0800108 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700109 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800110 protected int mMaxScroll;
111 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700112 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800114 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
116 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700117 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800118 private float mDownMotionPrimary;
119 private float mLastMotion;
120 private float mLastMotionRemainder;
121 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700122 // Used in special cases where the fling checks can be relaxed for an intentional gesture
123 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700124 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700125
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700126 private final ArrayList<Runnable> mOnPageScrollsInitializedCallbacks = new ArrayList<>();
127
128 // We should always check pageScrollsInitialized() is true when using mPageScrolls.
129 @Nullable protected int[] mPageScrolls = null;
Sunny Goyalaad33592018-08-07 17:20:35 -0700130 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800131
Winson Chung4941f652020-06-18 15:11:53 -0700132 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700134 // The amount of movement to begin paging
135 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700143 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700144 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Winson Chungd2be3812013-07-16 11:11:32 -0700146 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800148 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700149
John Spurlock77e1f472013-09-11 10:09:51 -0400150 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800151 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400152
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800153 // Similar to the platform implementation of isLayoutValid();
154 protected boolean mIsLayoutValid;
155
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700156 private int[] mTmpIntPair = new int[2];
157
Sunny Goyalc39db732021-04-03 01:20:21 -0700158 protected EdgeEffectCompat mEdgeGlowLeft;
159 protected EdgeEffectCompat mEdgeGlowRight;
160
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 public PagedView(Context context) {
162 this(context, null);
163 }
164
165 public PagedView(Context context, AttributeSet attrs) {
166 this(context, attrs, 0);
167 }
168
169 public PagedView(Context context, AttributeSet attrs, int defStyle) {
170 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700171
Adam Cohen9c4949e2010-10-05 12:27:22 -0700172 TypedArray a = context.obtainStyledAttributes(attrs,
173 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700174 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 a.recycle();
176
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700178 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700179
Sunny Goyalc39db732021-04-03 01:20:21 -0700180 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700181 mCurrentPage = 0;
Lais Andradecc5c8842021-06-04 18:20:30 +0100182 mCurrentScrollOverPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Vinit Nayaka406f722019-12-19 11:50:55 -0800184 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700185 mTouchSlop = configuration.getScaledTouchSlop();
186 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800188
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100189 updateVelocityValues();
Sunny Goyala9f55462018-03-25 18:15:15 -0700190
Sunny Goyalc39db732021-04-03 01:20:21 -0700191 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700192 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700193 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 }
195
Sunny Goyalc39db732021-04-03 01:20:21 -0700196 protected void initEdgeEffect() {
197 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
198 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800199 }
200
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700201 public void initParentViews(View parent) {
202 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800203 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Tony Wickhambab101c2021-09-20 15:12:44 -0700204 mPageIndicator.setMarkersCount(getChildCount() / getPanelCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700205 }
206 }
207
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800208 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700209 return mPageIndicator;
210 }
211
Adam Cohen674531f2013-12-13 15:07:14 -0800212 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700213 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
214 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000215 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700216 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700218 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700219 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700221
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700222 /**
223 * Returns the index of page to be shown immediately afterwards.
224 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700225 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700226 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
227 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700228
Adam Cohenf9c184a2016-01-15 16:47:43 -0800229 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 return getChildCount();
231 }
232
Sunny Goyal83a8f042015-05-19 12:52:12 -0700233 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 return getChildAt(index);
235 }
236
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800238 * Updates the scroll of the current page immediately to its final scroll position. We use this
239 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
240 * the previous tab page.
241 */
242 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700243 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800244 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700245 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Alex Chauba61b982021-06-15 16:29:19 +0100246 newPosition = getScrollForPage(mCurrentPage) + mCurrentPageScrollDiff;
Adam Cohen0ffac432013-07-10 11:19:26 -0700247 }
Alex Chau6499e832021-11-19 14:35:46 +0000248 mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700249 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Tony Wickhamd5521322021-09-14 14:26:50 -0700250 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800251 }
252
Tony Wickhamf8d731a2021-05-26 14:58:13 -0700253 /**
254 * Immediately finishes any overscroll effect and jumps to the end of the scroller animation.
255 */
256 public void abortScrollerAnimation() {
257 mEdgeGlowLeft.finish();
258 mEdgeGlowRight.finish();
259 abortScrollerAnimation(true);
260 }
261
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700262 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700263 mScroller.abortAnimation();
264 // We need to clean up the next page here to avoid computeScrollHelper from
265 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700266 if (resetNextPage) {
267 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800268 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700269 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700270 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271
Tony Wickhamd5521322021-09-14 14:26:50 -0700272 /**
273 * Immediately finishes any in-progress scroll, maintaining the current position. Also sets
274 * mNextPage = INVALID_PAGE and calls pageEndTransition().
275 */
276 public void forceFinishScroller() {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700277 mScroller.forceFinished(true);
278 // We need to clean up the next page here to avoid computeScrollHelper from
279 // updating current page on the pass.
Tony Wickhamd5521322021-09-14 14:26:50 -0700280 mNextPage = INVALID_PAGE;
281 pageEndTransition();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700282 }
283
Adam Cohen6f127a62014-06-12 14:54:41 -0700284 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700285 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100287 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
288
289 if (getPanelCount() > 1) {
Alex Chau1e448462021-08-13 21:48:35 +0100290 // Always return left most panel as new page
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100291 newPage = getLeftmostVisiblePageForIndex(newPage);
292 }
293 return newPage;
294 }
295
Andras Kloczl55edfe52021-05-14 12:21:30 +0200296 /**
297 * In most cases where panelCount is 1, this method will just return the page index that was
298 * passed in.
299 * But for example when two panel home is enabled we might need the leftmost visible page index
300 * because that page is the current page.
301 */
302 public int getLeftmostVisiblePageForIndex(int pageIndex) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100303 int panelCount = getPanelCount();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200304 return pageIndex - pageIndex % panelCount;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100305 }
306
307 /**
308 * Returns the number of pages that are shown at the same time.
309 */
310 protected int getPanelCount() {
311 return 1;
312 }
313
314 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +0200315 * Returns an IntSet with the indices of the currently visible pages
316 */
317 @VisibleForTesting(otherwise = PACKAGE_PRIVATE)
318 public IntSet getVisiblePageIndices() {
Andras Kloczl7e912442021-05-06 12:50:28 +0200319 return getPageIndices(mCurrentPage);
320 }
321
322 /**
323 * In case the panelCount is 1 this just returns the same page index in an IntSet.
324 * But in cases where the panelCount > 1 this will return all the page indices that belong
325 * together, i.e. on the Workspace they are next to each other and shown at the same time.
326 */
327 private IntSet getPageIndices(int pageIndex) {
328 // we want to make sure the pageIndex is the leftmost page
329 pageIndex = getLeftmostVisiblePageForIndex(pageIndex);
330
331 IntSet pageIndices = new IntSet();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200332 int panelCount = getPanelCount();
333 int pageCount = getPageCount();
Andras Kloczl7e912442021-05-06 12:50:28 +0200334 for (int page = pageIndex; page < pageIndex + panelCount && page < pageCount; page++) {
335 pageIndices.add(page);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200336 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200337 return pageIndices;
338 }
339
340 /**
341 * Returns an IntSet with the indices of the neighbour pages that are in the focus direction.
342 */
343 private IntSet getNeighbourPageIndices(int focus) {
344 int panelCount = getPanelCount();
345 // getNextPage is more reliable than getCurrentPage
346 int currentPage = getNextPage();
347
348 int nextPage;
349 if (focus == View.FOCUS_LEFT) {
350 nextPage = currentPage - panelCount;
351 } else if (focus == View.FOCUS_RIGHT) {
352 nextPage = currentPage + panelCount;
353 } else {
Andras Kloczlb0632942021-08-03 14:03:58 +0200354 // no neighbours to other directions
Andras Kloczl7e912442021-05-06 12:50:28 +0200355 return new IntSet();
356 }
357 nextPage = validateNewPage(nextPage);
358 if (nextPage == currentPage) {
359 // We reached the end of the pages
360 return new IntSet();
361 }
362
Andras Kloczlb0632942021-08-03 14:03:58 +0200363 return getPageIndices(nextPage);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200364 }
365
366 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700367 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100368 */
Sunny Goyal59969372021-05-06 12:11:44 -0700369 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200370 getVisiblePageIndices().forEach(pageIndex -> {
371 View page = getPageAt(pageIndex);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100372 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700373 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100374 }
Andras Kloczl55edfe52021-05-14 12:21:30 +0200375 });
Andras Kloczl2bb51342021-03-20 01:13:45 +0100376 }
377
378 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100379 * Returns true if the view is on one of the current pages, false otherwise.
380 */
381 public boolean isVisible(View child) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200382 return isVisible(indexOfChild(child));
383 }
384
385 /**
386 * Returns true if the page with the given index is currently visible, false otherwise.
387 */
388 private boolean isVisible(int pageIndex) {
389 return getLeftmostVisiblePageForIndex(pageIndex) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700390 }
391
Chet Haasebc2f0822012-10-26 17:59:53 -0700392 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700393 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
394 */
395 private int ensureWithinScrollBounds(int page) {
396 int dir = !mIsRtl ? 1 : - 1;
397 int currScroll = getScrollForPage(page);
398 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800399 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700400 page += dir;
401 prevScroll = currScroll;
402 currScroll = getScrollForPage(page);
403 if (currScroll <= prevScroll) {
404 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
405 break;
406 }
407 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800408 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700409 page -= dir;
410 prevScroll = currScroll;
411 currScroll = getScrollForPage(page);
412 if (currScroll >= prevScroll) {
413 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
414 break;
415 }
416 }
417 return page;
418 }
419
Tony Wickham03f27012019-04-25 14:22:54 -0700420 public void setCurrentPage(int currentPage) {
421 setCurrentPage(currentPage, INVALID_PAGE);
422 }
423
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700424 /**
Winson Chung86f77532010-08-24 11:08:22 -0700425 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 */
Tony Wickham03f27012019-04-25 14:22:54 -0700427 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800428 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700429 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800430 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800431 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
432 // the default
433 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800434 return;
435 }
Tony Wickham03f27012019-04-25 14:22:54 -0700436 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700437 mCurrentPage = validateNewPage(currentPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100438 mCurrentScrollOverPage = mCurrentPage;
Winson Chung181c3dc2013-07-17 15:36:20 -0700439 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700440 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800441 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 }
443
Winson Chung8c87cd82013-07-23 16:20:10 -0700444 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800445 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
446 * has settled.
447 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700448 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800449 updatePageIndicator();
450 }
451
452 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700453 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000454 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700455 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700457 protected void pageBeginTransition() {
458 if (!mIsPageInTransition) {
459 mIsPageInTransition = true;
460 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700461 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700462 }
463
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700464 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700465 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700466 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700467 mIsPageInTransition = false;
468 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700469 }
Michael Jurka0142d492010-08-25 17:46:15 -0700470 }
471
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700472 @Override
473 public void onVisibilityAggregated(boolean isVisible) {
474 pageEndTransition();
475 super.onVisibilityAggregated(isVisible);
476 }
477
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700478 protected boolean isPageInTransition() {
479 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700480 }
481
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700482 /**
483 * Called when the page starts moving as part of the scroll. Subclasses can override this
484 * to provide custom behavior during animation.
485 */
486 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700487 }
488
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700489 /**
490 * Called when the page ends moving as part of the scroll. Subclasses can override this
491 * to provide custom behavior during animation.
492 */
493 protected void onPageEndTransition() {
Alex Chauba61b982021-06-15 16:29:19 +0100494 mCurrentPageScrollDiff = 0;
vadimt4d93df52019-07-10 17:32:58 -0700495 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800496 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
497 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700498 if (mOnPageTransitionEndCallback != null) {
499 mOnPageTransitionEndCallback.run();
500 mOnPageTransitionEndCallback = null;
501 }
502 }
503
504 /**
505 * Sets a callback to run once when the scrolling finishes. If there is currently
506 * no page in transition, then the callback is called immediately.
507 */
508 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
509 if (mIsPageInTransition || callback == null) {
510 mOnPageTransitionEndCallback = callback;
511 } else {
512 callback.run();
513 }
Michael Jurka0142d492010-08-25 17:46:15 -0700514 }
515
Adam Cohen68d73932010-11-15 10:50:58 -0800516 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700517 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700518 x = Utilities.boundToRange(x,
519 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
520 y = Utilities.boundToRange(y,
521 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800522 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700523 }
524
Adam Cohen53805212013-10-01 10:39:23 -0700525 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800526 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700527 if (mCurrentPage != getNextPage()) {
528 AccessibilityEvent ev =
529 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700530 ev.setScrollable(true);
531 ev.setScrollX(getScrollX());
532 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800533 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700534 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700535 }
Adam Cohen53805212013-10-01 10:39:23 -0700536 }
537 }
538
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700539 protected void announcePageForAccessibility() {
540 if (isAccessibilityEnabled(getContext())) {
541 // Notify the user when the page changes
542 announceForAccessibility(getCurrentPageDescription());
543 }
544 }
545
Winson Chung85425412021-05-10 16:24:14 -0700546 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700548 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700549 int oldPos = mOrientationHandler.getPrimaryScroll(this);
550 int newPos = mScroller.getCurrX();
551 if (oldPos != newPos) {
Alex Chau6499e832021-11-19 14:35:46 +0000552 mOrientationHandler.setPrimary(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700553 }
Winson Chung85425412021-05-10 16:24:14 -0700554
555 if (mAllowOverScroll) {
556 if (newPos < mMinScroll && oldPos >= mMinScroll) {
557 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
558 mScroller.abortAnimation();
Lais Andradecc5c8842021-06-04 18:20:30 +0100559 onEdgeAbsorbingScroll();
Winson Chung85425412021-05-10 16:24:14 -0700560 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
561 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
562 mScroller.abortAnimation();
Lais Andradecc5c8842021-06-04 18:20:30 +0100563 onEdgeAbsorbingScroll();
Sunny Goyalc39db732021-04-03 01:20:21 -0700564 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800565 }
Adam Cohen53805212013-10-01 10:39:23 -0700566
Winson Chung85425412021-05-10 16:24:14 -0700567 // If the scroller has scrolled to the final position and there is no edge effect, then
568 // finish the scroller to skip waiting for additional settling
569 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
570 mScroller.getFinalY());
571 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
572 mScroller.abortAnimation();
573 }
574
575 invalidate();
576 return true;
577 } else if (mNextPage != INVALID_PAGE) {
578 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700579 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700580 mCurrentPage = validateNewPage(mNextPage);
Lais Andradecc5c8842021-06-04 18:20:30 +0100581 mCurrentScrollOverPage = mCurrentPage;
Winson Chung86f77532010-08-24 11:08:22 -0700582 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700583 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700584
Adam Cohen73aa9752010-11-24 16:26:58 -0800585 // We don't want to trigger a page end moving unless the page has settled
586 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700587 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700588 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800589 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700590
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700591 if (canAnnouncePageDescription()) {
592 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700593 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
Michael Jurka0142d492010-08-25 17:46:15 -0700595 return false;
596 }
597
598 @Override
599 public void computeScroll() {
600 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 }
602
Sunny Goyalce8809a2018-01-18 14:02:47 -0800603 public int getExpectedHeight() {
604 return getMeasuredHeight();
605 }
606
Adam Cohen410f3cd2013-09-22 12:09:32 -0700607 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800608 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800609 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700610 }
611
Sunny Goyalce8809a2018-01-18 14:02:47 -0800612 public int getExpectedWidth() {
613 return getMeasuredWidth();
614 }
615
Sunny Goyalbc683e92017-12-06 10:25:07 -0800616 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800617 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800618 - mInsets.left - mInsets.right;
619 }
620
Andras Kloczlc2bd15f2022-03-30 18:06:13 +0100621 private void updateVelocityValues() {
622 Resources res = getResources();
623 mFlingThresholdVelocity = res.getDimensionPixelSize(R.dimen.fling_threshold_velocity);
624 mEasyFlingThresholdVelocity =
625 res.getDimensionPixelSize(R.dimen.easy_fling_threshold_velocity);
626 mMinFlingVelocity = res.getDimensionPixelSize(R.dimen.min_fling_velocity);
627 mMinSnapVelocity = res.getDimensionPixelSize(R.dimen.min_page_snap_velocity);
628 mPageSnapAnimationDuration = res.getInteger(R.integer.config_pageSnapAnimationDuration);
629 }
630
631 @Override
632 protected void onConfigurationChanged(Configuration newConfig) {
633 super.onConfigurationChanged(newConfig);
634 updateVelocityValues();
635 }
636
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800638 public void requestLayout() {
639 mIsLayoutValid = false;
640 super.requestLayout();
641 }
642
643 @Override
644 public void forceLayout() {
645 mIsLayoutValid = false;
646 super.forceLayout();
647 }
648
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100649 private int getPageWidthSize(int widthSize) {
Thales Lima7a6752d2021-09-23 14:31:00 +0100650 // It's necessary to add the padding back because it is remove when measuring children,
651 // like when MeasureSpec.getSize in CellLayout.
652 return (widthSize - mInsets.left - mInsets.right - getPaddingLeft() - getPaddingRight())
653 / getPanelCount() + getPaddingLeft() + getPaddingRight();
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100654 }
655
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800656 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700657 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700658 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700659 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
660 return;
661 }
662
Adam Cohen7d30a372013-07-01 17:03:59 -0700663 // We measure the dimensions of the PagedView to be larger than the pages so that when we
664 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700665 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
666 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
667 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700668 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700669
Adam Cohen7d30a372013-07-01 17:03:59 -0700670 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
671 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
672 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 }
674
Winson Chung8aad6102012-05-11 16:27:49 -0700675 // Return early if we aren't given a proper dimension
676 if (widthSize <= 0 || heightSize <= 0) {
677 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
678 return;
679 }
680
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700682 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700684
Sunny Goyalac00cba2017-11-13 15:58:01 -0800685 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100686 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800687 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800688 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700689
Sunny Goyalac00cba2017-11-13 15:58:01 -0800690 // measureChildren takes accounts for content padding, we only need to care about extra
691 // space due to insets.
692 measureChildren(myWidthSpec, myHeightSpec);
693 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800694 }
695
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700696 /** Returns true iff this PagedView's scroll amounts are initialized to each page index. */
697 protected boolean pageScrollsInitialized() {
698 return mPageScrolls != null && mPageScrolls.length == getChildCount();
699 }
700
701 /**
702 * Queues the given callback to be run once {@code mPageScrolls} has been initialized.
703 */
704 public void runOnPageScrollsInitialized(Runnable callback) {
705 mOnPageScrollsInitializedCallbacks.add(callback);
706 if (pageScrollsInitialized()) {
707 onPageScrollsInitialized();
708 }
709 }
710
711 private void onPageScrollsInitialized() {
712 for (Runnable callback : mOnPageScrollsInitializedCallbacks) {
713 callback.run();
714 }
715 mOnPageScrollsInitializedCallbacks.clear();
716 }
717
Sunny Goyalcf25b522015-07-09 00:01:18 -0700718 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700719 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700720 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800721 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700722 final int childCount = getChildCount();
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700723 int[] pageScrolls = mPageScrolls;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700724 boolean pageScrollChanged = false;
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700725 if (!pageScrollsInitialized()) {
726 pageScrolls = new int[childCount];
Vadim Trysheveb701c62018-05-14 16:00:15 -0700727 pageScrollChanged = true;
728 }
729
730 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700731 return;
732 }
733
Winson Chung785d2eb2011-04-14 16:08:02 -0700734 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700735
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700736 pageScrollChanged |= getPageScrolls(pageScrolls, true, SIMPLE_SCROLL_LOGIC);
737 mPageScrolls = pageScrolls;
Winson Chungc3665fa2011-09-14 17:56:27 -0700738
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700739 final LayoutTransition transition = getLayoutTransition();
740 // If the transition is running defer updating max scroll, as some empty pages could
741 // still be present, and a max scroll change could cause sudden jumps in scroll.
742 if (transition != null && transition.isRunning()) {
743 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
744
745 @Override
746 public void startTransition(LayoutTransition transition, ViewGroup container,
747 View view, int transitionType) { }
748
749 @Override
750 public void endTransition(LayoutTransition transition, ViewGroup container,
751 View view, int transitionType) {
752 // Wait until all transitions are complete.
753 if (!transition.isRunning()) {
754 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700755 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700756 }
757 }
758 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700759 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700760 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700761 }
762
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700763 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
764 updateCurrentPageScroll();
765 mFirstLayout = false;
766 }
767
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800768 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700769 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700770 }
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700771 onPageScrollsInitialized();
Winson Chunge3193b92010-09-10 11:44:42 -0700772 }
773
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700774 /**
775 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
776 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700777 */
778 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
779 ComputePageScrollsLogic scrollLogic) {
780 final int childCount = getChildCount();
781
782 final int startIndex = mIsRtl ? childCount - 1 : 0;
783 final int endIndex = mIsRtl ? -1 : childCount;
784 final int delta = mIsRtl ? -1 : 1;
785
Vinit Nayaka406f722019-12-19 11:50:55 -0800786 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700787
Vinit Nayaka406f722019-12-19 11:50:55 -0800788 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
789 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700790 boolean pageScrollChanged = false;
Thales Limae4457742021-07-20 16:46:38 +0100791 int panelCount = getPanelCount();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700792
Vinit Nayaka406f722019-12-19 11:50:55 -0800793 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700794 final View child = getPageAt(i);
795 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800796 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
797 pageCenter, layoutChildren);
798 final int primaryDimension = bounds.primaryDimension;
799 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700800
Alex Chaub5c4d112021-03-29 17:37:53 +0100801 // In case the pages are of different width, align the page to left edge for non-RTL
802 // or right edge for RTL.
803 final int pageScroll =
804 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700805 if (outPageScrolls[i] != pageScroll) {
806 pageScrollChanged = true;
807 outPageScrolls[i] = pageScroll;
808 }
Alex Chau2e487602022-03-15 19:34:08 +0000809 childStart += primaryDimension + getChildGap(i, i + delta);
Thales Limae4457742021-07-20 16:46:38 +0100810
811 // This makes sure that the space is added after the page, not after each panel
Thales Limac2a6f6f2021-09-09 17:06:22 +0100812 int lastPanel = mIsRtl ? 0 : panelCount - 1;
813 if (i % panelCount == lastPanel) {
Thales Limae4457742021-07-20 16:46:38 +0100814 childStart += mPageSpacing;
815 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700816 }
817 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100818
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100819 if (panelCount > 1) {
820 for (int i = 0; i < childCount; i++) {
Alex Chau1e448462021-08-13 21:48:35 +0100821 // In case we have multiple panels, always use left most panel's page scroll for all
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100822 // panels on the screen.
823 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
824 if (outPageScrolls[i] != adjustedScroll) {
825 outPageScrolls[i] = adjustedScroll;
826 pageScrollChanged = true;
827 }
828 }
829 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700830 return pageScrollChanged;
831 }
832
Alex Chau2e487602022-03-15 19:34:08 +0000833 protected int getChildGap(int fromIndex, int toIndex) {
Sunny Goyala1fbd842015-05-20 13:40:27 -0700834 return 0;
835 }
836
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700837 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800838 mMinScroll = computeMinScroll();
839 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700840 }
841
Vinit Nayaka406f722019-12-19 11:50:55 -0800842 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700843 return 0;
Tony50876bf2018-09-19 23:09:14 -0400844 }
845
Vinit Nayaka406f722019-12-19 11:50:55 -0800846 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700847 int childCount = getChildCount();
848 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700849 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700850 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700851 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700852 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700853 }
854 }
855
Adam Cohen3b185e22013-10-29 14:45:58 -0700856 public void setPageSpacing(int pageSpacing) {
857 mPageSpacing = pageSpacing;
858 requestLayout();
859 }
860
Tony50876bf2018-09-19 23:09:14 -0400861 public int getPageSpacing() {
862 return mPageSpacing;
863 }
864
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800865 private void dispatchPageCountChanged() {
866 if (mPageIndicator != null) {
Tony Wickhambab101c2021-09-20 15:12:44 -0700867 mPageIndicator.setMarkersCount(getChildCount() / getPanelCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700868 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700869 // This ensures that when children are added, they get the correct transforms / alphas
870 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700871 invalidate();
872 }
873
Michael Jurka8b805b12012-04-18 14:23:14 -0700874 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800875 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800876 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800877 dispatchPageCountChanged();
878 }
879
880 @Override
881 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800882 super.onViewRemoved(child);
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -0700883 runOnPageScrollsInitialized(() -> {
884 mCurrentPage = validateNewPage(mCurrentPage);
885 mCurrentScrollOverPage = mCurrentPage;
886 });
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800887 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700888 }
889
Adam Cohen73894962011-10-31 13:17:17 -0700890 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700891 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800892 View pageAtIndex = getPageAt(index);
893 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700894 }
895
Alex Chau7944ee52021-01-08 19:07:25 +0000896 protected int getChildVisibleSize(int index) {
897 View layout = getPageAt(index);
898 return mOrientationHandler.getMeasuredSize(layout);
899 }
900
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 @Override
902 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200903 int page = indexOfChild(child);
904 if (!isVisible(page) || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700905 if (immediate) {
906 setCurrentPage(page);
907 } else {
908 snapToPage(page);
909 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 return true;
911 }
912 return false;
913 }
914
915 @Override
916 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700917 int focusablePage;
918 if (mNextPage != INVALID_PAGE) {
919 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700921 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 }
Winson Chung86f77532010-08-24 11:08:22 -0700923 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700925 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700926 }
927 return false;
928 }
929
930 @Override
931 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800932 if (super.dispatchUnhandledMove(focused, direction)) {
933 return true;
934 }
935
936 if (mIsRtl) {
937 if (direction == View.FOCUS_LEFT) {
938 direction = View.FOCUS_RIGHT;
939 } else if (direction == View.FOCUS_RIGHT) {
940 direction = View.FOCUS_LEFT;
941 }
942 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200943
944 int currentPage = getNextPage();
945 int closestNeighbourIndex = -1;
946 int closestNeighbourDistance = Integer.MAX_VALUE;
947 // Find the closest neighbour page
948 for (int neighbourPageIndex : getNeighbourPageIndices(direction)) {
949 int distance = Math.abs(neighbourPageIndex - currentPage);
950 if (closestNeighbourDistance > distance) {
951 closestNeighbourDistance = distance;
952 closestNeighbourIndex = neighbourPageIndex;
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 }
954 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200955 if (closestNeighbourIndex != -1) {
956 View page = getPageAt(closestNeighbourIndex);
957 snapToPage(closestNeighbourIndex);
958 page.requestFocus(direction);
959 return true;
960 }
961
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800962 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 }
964
965 @Override
966 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700967 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
968 return;
969 }
970
Andras Kloczl7e912442021-05-06 12:50:28 +0200971 // nextPage is more reliable when multiple control movements have been done in a short
972 // period of time
973 getPageIndices(getNextPage())
974 .addAll(getNeighbourPageIndices(direction))
975 .forEach(pageIndex ->
976 getPageAt(pageIndex).addFocusables(views, direction, focusableMode));
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 }
978
979 /**
980 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700981 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 *
Winson Chung86f77532010-08-24 11:08:22 -0700983 * This happens when live folders requery, and if they're off page, they
984 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 */
986 @Override
987 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700988 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 View v = focused;
990 while (true) {
991 if (v == current) {
992 super.focusableViewAvailable(focused);
993 return;
994 }
995 if (v == this) {
996 return;
997 }
998 ViewParent parent = v.getParent();
999 if (parent instanceof View) {
1000 v = (View)v.getParent();
1001 } else {
1002 return;
1003 }
1004 }
1005 }
1006
1007 /**
1008 * {@inheritDoc}
1009 */
1010 @Override
1011 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1012 if (disallowIntercept) {
1013 // We need to make sure to cancel our long press if
1014 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +02001015 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 }
1017 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1018 }
1019
1020 @Override
1021 public boolean onInterceptTouchEvent(MotionEvent ev) {
1022 /*
1023 * This method JUST determines whether we want to intercept the motion.
1024 * If we return true, onTouchEvent will be called and we do the actual
1025 * scrolling there.
1026 */
1027
Winson Chung45e1d6e2010-11-09 17:19:49 -08001028 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001029 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -07001030
1031 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001032
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 /*
1034 * Shortcut the most recurring case: the user is in the dragging
1035 * state and he is moving his finger. We want to intercept this
1036 * motion.
1037 */
1038 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -07001039 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 return true;
1041 }
1042
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 switch (action & MotionEvent.ACTION_MASK) {
1044 case MotionEvent.ACTION_MOVE: {
1045 /*
1046 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -08001047 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001049 if (mActivePointerId != INVALID_POINTER) {
1050 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001051 }
1052 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -08001053 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -07001054 // i.e. fall through to the next case (don't break)
1055 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1056 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001057 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 }
1059
1060 case MotionEvent.ACTION_DOWN: {
1061 final float x = ev.getX();
1062 final float y = ev.getY();
1063 // Remember location of down touch
1064 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001065 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -07001066 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001067 mLastMotionRemainder = 0;
1068 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001069 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -07001071 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 break;
1073 }
1074
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001076 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001077 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 break;
1079
1080 case MotionEvent.ACTION_POINTER_UP:
1081 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001082 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001083 break;
1084 }
1085
1086 /*
1087 * The only time we want to intercept motion events is if we are in the
1088 * drag mode.
1089 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001090 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001091 }
1092
Tony Wickhama0aee212019-09-18 09:24:03 -07001093 /**
1094 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1095 */
Sunny Goyalbb030562021-07-19 16:28:39 -07001096 protected void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -07001097 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -07001098 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -07001099 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001100
1101 if (finishedScrolling) {
1102 mIsBeingDragged = false;
1103 if (!mScroller.isFinished() && !mFreeScroll) {
1104 setCurrentPage(getNextPage());
1105 pageEndTransition();
1106 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001107 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -07001108 } else {
1109 mIsBeingDragged = true;
1110 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001111
1112 // Catch the edge effect if it is active.
1113 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
1114 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
1115 if (!mEdgeGlowLeft.isFinished()) {
1116 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
1117 }
1118 if (!mEdgeGlowRight.isFinished()) {
1119 mEdgeGlowRight.onPullDistance(0f, displacement);
1120 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001121 }
1122
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001123 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001124 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001125 }
1126
Adam Cohenf8d28232011-02-01 21:47:00 -08001127 protected void determineScrollingStart(MotionEvent ev) {
1128 determineScrollingStart(ev, 1.0f);
1129 }
1130
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 /*
1132 * Determines if we should change the touch state to start scrolling after the
1133 * user moves their touch point too far.
1134 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001135 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001136 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001138 if (pointerIndex == -1) return;
1139
Vinit Nayaka406f722019-12-19 11:50:55 -08001140 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1141 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001142 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001143 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001144
Vinit Nayaka406f722019-12-19 11:50:55 -08001145 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001146 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001147 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001148 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1149 mLastMotion = primaryDirection;
1150 mLastMotionRemainder = 0;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001151 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001152 // Stop listening for things like pinches.
1153 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001154 }
1155 }
1156
1157 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001158 // Try canceling the long press. It could also have been scheduled
1159 // by a distant descendant, so use the mAllowLongPress flag to block
1160 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001161 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 }
1163
Adam Cohenb5ba0972011-09-07 18:02:31 -07001164 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001165 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenedb40762013-07-18 16:45:45 -07001166 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001167 int panelCount = getPanelCount();
1168 int pageCount = getChildCount();
Adam Cohen3b185e22013-10-29 14:45:58 -07001169
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001170 int adjacentPage = page + panelCount;
Sunny Goyal70660032015-05-14 00:07:08 -07001171 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001172 adjacentPage = page - panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001173 }
1174
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001175 final int totalDistance;
1176 if (adjacentPage < 0 || adjacentPage > pageCount - 1) {
1177 totalDistance = (v.getMeasuredWidth() + mPageSpacing) * panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001178 } else {
1179 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1180 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001181
1182 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001183 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001184 scrollProgress = Math.max(scrollProgress, -MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001185 return scrollProgress;
1186 }
1187
Adam Cohenedb40762013-07-18 16:45:45 -07001188 public int getScrollForPage(int index) {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -07001189 if (!pageScrollsInitialized() || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001190 return 0;
1191 } else {
1192 return mPageScrolls[index];
1193 }
1194 }
1195
Adam Cohen564a2e72013-10-09 14:47:32 -07001196 // While layout transitions are occurring, a child's position may stray from its baseline
1197 // position. This method returns the magnitude of this stray at any given time.
1198 public int getLayoutTransitionOffsetForPage(int index) {
Schneider Victor-tuliasbb44c032022-04-07 15:25:00 -07001199 if (!pageScrollsInitialized() || index >= mPageScrolls.length || index < 0) {
Adam Cohen564a2e72013-10-09 14:47:32 -07001200 return 0;
1201 } else {
1202 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001203
Sunny Goyal228153d2018-01-04 15:35:22 -08001204 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1205 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001206 return (int) (child.getX() - baselineX);
1207 }
1208 }
1209
Tony Wickham023f4042019-01-29 10:52:31 -08001210 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001211 if (mFreeScroll == freeScroll) {
1212 return;
1213 }
1214
Tony Wickham8f7ead32016-04-07 18:46:44 -07001215 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001216 mFreeScroll = freeScroll;
1217
Adam Cohenf9618852013-11-08 06:45:03 -08001218 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001219 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001220 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001221 if (getScrollForPage(getNextPage()) != getScrollX()) {
1222 snapToPage(getNextPage());
1223 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001224 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001225 }
1226
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001227 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001228 mAllowOverScroll = enable;
1229 }
1230
Andras Kloczl07111f22022-03-30 17:25:54 +01001231 protected boolean isSignificantMove(float absoluteDelta, int pageOrientedSize) {
1232 return absoluteDelta > pageOrientedSize * SIGNIFICANT_MOVE_THRESHOLD;
Alex Chau822acf42021-06-03 11:35:05 +01001233 }
1234
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 @Override
1236 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001237 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001238 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001239
Michael Jurkab8f06722010-10-10 15:58:46 -07001240 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241
1242 final int action = ev.getAction();
1243
1244 switch (action & MotionEvent.ACTION_MASK) {
1245 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001246 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001247
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 /*
1249 * If being flinged and user touches, stop the fling. isFinished
1250 * will be false if being flinged.
1251 */
1252 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001253 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001254 }
1255
1256 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001257 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001258 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001259 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1260 mLastMotionRemainder = 0;
1261 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001262 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001264 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001265 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001266 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 break;
1268
Winson Chung4941f652020-06-18 15:11:53 -07001269 case ACTION_MOVE_ALLOW_EASY_FLING:
1270 // Start scrolling immediately
1271 determineScrollingStart(ev);
1272 mAllowEasyFling = true;
1273 break;
1274
1275 case MotionEvent.ACTION_MOVE:
1276 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 // Scroll to follow the motion event
1278 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001279
1280 if (pointerIndex == -1) return true;
Sunny Goyalc39db732021-04-03 01:20:21 -07001281 float oldScroll = mOrientationHandler.getPrimaryScroll(this);
1282 float dx = ev.getX(pointerIndex);
1283 float dy = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001284
Sunny Goyalc39db732021-04-03 01:20:21 -07001285 float direction = mOrientationHandler.getPrimaryValue(dx, dy);
Vinit Nayaka406f722019-12-19 11:50:55 -08001286 float delta = mLastMotion + mLastMotionRemainder - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001287
1288 int width = getWidth();
1289 int height = getHeight();
1290 int size = mOrientationHandler.getPrimaryValue(width, height);
1291
1292 final float displacement = mOrientationHandler.getSecondaryValue(dx, dy)
1293 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001294 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001295
Sunny Goyalc39db732021-04-03 01:20:21 -07001296 if (mAllowOverScroll) {
1297 float consumed = 0;
1298 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
1299 consumed = size * mEdgeGlowRight.onPullDistance(delta / size, displacement);
1300 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
1301 consumed = -size * mEdgeGlowLeft.onPullDistance(
1302 -delta / size, 1 - displacement);
1303 }
1304 delta -= consumed;
1305 }
Alex Chau822acf42021-06-03 11:35:05 +01001306 delta /= mOrientationHandler.getPrimaryScale(this);
Sunny Goyalc39db732021-04-03 01:20:21 -07001307
Winson Chungc0844aa2011-02-02 15:25:58 -08001308 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1309 // keep the remainder because we are actually testing if we've moved from the last
1310 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001311 mLastMotion = direction;
1312 int movedDelta = (int) delta;
1313 mLastMotionRemainder = delta - movedDelta;
Vinit Nayaka406f722019-12-19 11:50:55 -08001314
Sunny Goyalc39db732021-04-03 01:20:21 -07001315 if (delta != 0) {
Alex Chau6499e832021-11-19 14:35:46 +00001316 mOrientationHandler.setPrimary(this, VIEW_SCROLL_BY, movedDelta);
Sunny Goyalc39db732021-04-03 01:20:21 -07001317
1318 if (mAllowOverScroll) {
1319 final float pulledToX = oldScroll + delta;
1320
1321 if (pulledToX < mMinScroll) {
1322 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement);
1323 if (!mEdgeGlowRight.isFinished()) {
1324 mEdgeGlowRight.onRelease();
1325 }
1326 } else if (pulledToX > mMaxScroll) {
1327 mEdgeGlowRight.onPullDistance(delta / size, displacement);
1328 if (!mEdgeGlowLeft.isFinished()) {
1329 mEdgeGlowLeft.onRelease();
1330 }
1331 }
1332
1333 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1334 postInvalidateOnAnimation();
1335 }
1336 }
1337
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 } else {
1339 awakenScrollBars();
1340 }
Adam Cohen564976a2010-10-13 18:52:07 -07001341 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 determineScrollingStart(ev);
1343 }
1344 break;
1345
1346 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001347 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 final int activePointerId = mActivePointerId;
1349 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001350 if (pointerIndex == -1) return true;
1351
Vinit Nayaka406f722019-12-19 11:50:55 -08001352 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1353 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001354 final VelocityTracker velocityTracker = mVelocityTracker;
1355 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001356
Vinit Nayaka406f722019-12-19 11:50:55 -08001357 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
Andras Kloczl07111f22022-03-30 17:25:54 +01001358 mActivePointerId);
Alex Chau822acf42021-06-03 11:35:05 +01001359 float delta = primaryDirection - mDownMotionPrimary;
Andras Kloczl07111f22022-03-30 17:25:54 +01001360 int pageOrientedSize = (int) (mOrientationHandler.getMeasuredSize(
1361 getPageAt(mCurrentPage))
1362 * mOrientationHandler.getPrimaryScale(this));
1363 boolean isSignificantMove = isSignificantMove(Math.abs(delta), pageOrientedSize);
Vinit Nayaka406f722019-12-19 11:50:55 -08001364
1365 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001366 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1367 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001368 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1369 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001370 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1371 Log.d("Quickswitch", "isFling=false vel=" + velocity
1372 + " threshold=" + mEasyFlingThresholdVelocity);
1373 }
Adam Cohen00481b32011-11-18 12:03:48 -08001374
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375 if (!mFreeScroll) {
1376 // In the case that the page is moved far to one direction and then is flung
1377 // in the opposite direction, we use a threshold to determine whether we should
1378 // just return to the starting page, or if we should skip one further.
1379 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001380 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1381 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001382 returnToOriginalPage = true;
1383 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001384
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 int finalPage;
1386 // We give flings precedence over large moves, which is why we short-circuit our
1387 // test for a large move if a fling has been registered. That is, a large
1388 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001389
Vinit Nayaka406f722019-12-19 11:50:55 -08001390 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1391 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001392 finalPage = returnToOriginalPage
1393 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001394 snapToPageWithVelocity(finalPage, velocity);
1395 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1396 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001398 finalPage = returnToOriginalPage
1399 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001400 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 } else {
1402 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001405 if (!mScroller.isFinished()) {
1406 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001408
Vinit Nayaka406f722019-12-19 11:50:55 -08001409 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1410 int maxScroll = mMaxScroll;
1411 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001412
Vinit Nayaka406f722019-12-19 11:50:55 -08001413 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1414 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001415 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001416 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001417 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001418 int velocity1 = -velocity;
1419 // Continue a scroll or fling in progress
1420 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1421 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001422
Sunny Goyalc39db732021-04-03 01:20:21 -07001423 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001424 mNextPage = getDestinationPage(finalPos);
Sunny Goyalc39db732021-04-03 01:20:21 -07001425 onNotSnappingToPageInFreeScroll();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001426 }
1427 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001430
Sunny Goyalc39db732021-04-03 01:20:21 -07001431 mEdgeGlowLeft.onRelease();
1432 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001433 // End any intermediate reordering states
1434 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 break;
1436
1437 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001438 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001439 snapToDestination();
1440 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001441 mEdgeGlowLeft.onRelease();
1442 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001443 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 break;
1445
1446 case MotionEvent.ACTION_POINTER_UP:
1447 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001448 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001449 break;
1450 }
1451
1452 return true;
1453 }
1454
Sunny Goyalc39db732021-04-03 01:20:21 -07001455 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001456
Lais Andradecc5c8842021-06-04 18:20:30 +01001457 /**
1458 * Called when the view edges absorb part of the scroll. Subclasses can override this
1459 * to provide custom behavior during animation.
1460 */
1461 protected void onEdgeAbsorbingScroll() {
1462 }
1463
1464 /**
1465 * Called when the current page closest to the center of the screen changes as part of the
1466 * scroll. Subclasses can override this to provide custom behavior during scroll.
1467 */
1468 protected void onScrollOverPageChanged() {
1469 }
1470
Vinit Nayaka406f722019-12-19 11:50:55 -08001471 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001472 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1473 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001474 }
1475
Alex Chau9d8eeab2022-04-05 17:13:56 +01001476 protected void resetTouchState() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001477 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001478 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001479 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001480 }
1481
Winson Chung185d7162011-02-28 13:47:29 -08001482 @Override
1483 public boolean onGenericMotionEvent(MotionEvent event) {
1484 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1485 switch (event.getAction()) {
1486 case MotionEvent.ACTION_SCROLL: {
1487 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1488 final float vscroll;
1489 final float hscroll;
1490 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1491 vscroll = 0;
1492 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1493 } else {
1494 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1495 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1496 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001497 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1498 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001499 }
Winson Chung185d7162011-02-28 13:47:29 -08001500 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001501 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001502 : (hscroll > 0 || vscroll > 0);
1503 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001504 scrollRight();
1505 } else {
1506 scrollLeft();
1507 }
1508 return true;
1509 }
1510 }
1511 }
1512 }
1513 return super.onGenericMotionEvent(event);
1514 }
1515
Sunny Goyal8b37c572020-03-31 12:12:14 -07001516 /**
1517 * Returns true if the paged view can scroll for the provided vertical and horizontal
1518 * scroll values
1519 */
1520 protected boolean canScroll(float absVScroll, float absHScroll) {
1521 ActivityContext ac = ActivityContext.lookupContext(getContext());
1522 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001523 }
1524
Michael Jurkab8f06722010-10-10 15:58:46 -07001525 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1526 if (mVelocityTracker == null) {
1527 mVelocityTracker = VelocityTracker.obtain();
1528 }
1529 mVelocityTracker.addMovement(ev);
1530 }
1531
1532 private void releaseVelocityTracker() {
1533 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001534 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001535 mVelocityTracker.recycle();
1536 mVelocityTracker = null;
1537 }
1538 }
1539
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001541 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 final int pointerId = ev.getPointerId(pointerIndex);
1543 if (pointerId == mActivePointerId) {
1544 // This was our active pointer going up. Choose a new
1545 // active pointer and adjust accordingly.
1546 // TODO: Make this decision more intelligent.
1547 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001548 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1549 newPointerIndex);
1550 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001551 mActivePointerId = ev.getPointerId(newPointerIndex);
1552 if (mVelocityTracker != null) {
1553 mVelocityTracker.clear();
1554 }
1555 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001556 }
1557
Winson Chung321e9ee2010-08-09 13:37:56 -07001558 @Override
1559 public void requestChildFocus(View child, View focused) {
1560 super.requestChildFocus(child, focused);
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001561
1562 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1563 // which results in incorrect navigation
1564 int nextPage = getNextPage();
1565 if (nextPage != mCurrentPage) {
1566 setCurrentPage(nextPage);
1567 }
1568
Andras Kloczl7e912442021-05-06 12:50:28 +02001569 int page = indexOfChild(child);
1570 if (page >= 0 && !isVisible(page) && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001571 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001572 }
1573 }
1574
Alex Chaub794ea62021-02-03 18:00:30 +00001575 public int getDestinationPage() {
1576 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1577 }
1578
Alex Chaudedbc8a2021-03-17 16:53:26 +00001579 protected int getDestinationPage(int primaryScroll) {
1580 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001581 }
1582
Sunny Goyal228153d2018-01-04 15:35:22 -08001583 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001584 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001585 }
1586
Alex Chaudedbc8a2021-03-17 16:53:26 +00001587 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1588 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001589 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001591 final int childCount = getChildCount();
1592 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001593 int distanceFromScreenCenter = Math.abs(
1594 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001595 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1596 minDistanceFromScreenCenter = distanceFromScreenCenter;
1597 minDistanceFromScreenCenterIndex = i;
1598 }
1599 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001600 return minDistanceFromScreenCenterIndex;
1601 }
1602
Tony Wickham7383d4e2020-07-07 19:25:25 -07001603 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001604 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001605 int halfChildSize = (childSize / 2);
1606 int childCenter = getChildOffset(childIndex) + halfChildSize;
1607 return childCenter - screenCenter;
1608 }
1609
1610 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001611 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1612 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001613 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1614 }
1615
Andras Kloczl9732df12021-09-09 19:40:42 +01001616 protected int getScreenCenter(int primaryScroll) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001617 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1618 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1619 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1620 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1621 + primaryPivot);
1622 }
1623
Adam Cohend19d3ca2010-09-15 14:43:42 -07001624 protected void snapToDestination() {
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001625 snapToPage(getDestinationPage(), mPageSnapAnimationDuration);
Winson Chung321e9ee2010-08-09 13:37:56 -07001626 }
1627
Adam Cohene0f66b52010-11-23 15:06:07 -08001628 // We want the duration of the page snap animation to be influenced by the distance that
1629 // the screen has to travel, however, we don't want this duration to be effected in a
1630 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1631 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001632 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001633 f -= 0.5f; // center the values about 0.
1634 f *= 0.3f * Math.PI / 2.0f;
1635 return (float) Math.sin(f);
1636 }
1637
Winson Chung05a31ed2018-02-08 17:08:43 -08001638 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001639 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001640 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001641
Vinit Nayaka406f722019-12-19 11:50:55 -08001642 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001643 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001644 int duration = 0;
1645
Sunny Goyal4d113a52015-05-27 10:05:28 -07001646 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001647 // If the velocity is low enough, then treat this more as an automatic page advance
1648 // as opposed to an apparent physical response to flinging
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001649 return snapToPage(whichPage, mPageSnapAnimationDuration);
Adam Cohene0f66b52010-11-23 15:06:07 -08001650 }
1651
1652 // Here we compute a "distance" that will be used in the computation of the overall
1653 // snap duration. This is a function of the actual distance that needs to be traveled;
1654 // we keep this value close to half screen size in order to reduce the variance in snap
1655 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001656 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001657 float distance = halfScreenSize + halfScreenSize *
1658 distanceInfluenceForSnapDuration(distanceRatio);
1659
1660 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001661 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001662
1663 // we want the page's snap velocity to approximately match the velocity at which the
1664 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001665 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1666 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001667
Sunny Goyalc39db732021-04-03 01:20:21 -07001668 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001669 }
1670
Winson Chung05a31ed2018-02-08 17:08:43 -08001671 public boolean snapToPage(int whichPage) {
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001672 return snapToPage(whichPage, mPageSnapAnimationDuration);
Winson Chung321e9ee2010-08-09 13:37:56 -07001673 }
1674
Winson Chung05a31ed2018-02-08 17:08:43 -08001675 public boolean snapToPageImmediately(int whichPage) {
Andras Kloczlc2bd15f2022-03-30 18:06:13 +01001676 return snapToPage(whichPage, mPageSnapAnimationDuration, true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 }
1678
Winson Chung05a31ed2018-02-08 17:08:43 -08001679 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001680 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001681 }
1682
Sunny Goyalc39db732021-04-03 01:20:21 -07001683 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001684 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001685
Vinit Nayaka406f722019-12-19 11:50:55 -08001686 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001687 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1688 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001689 }
1690
Winson Chung05a31ed2018-02-08 17:08:43 -08001691 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001692 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001693 }
Michael Jurka0142d492010-08-25 17:46:15 -07001694
Sunny Goyalc39db732021-04-03 01:20:21 -07001695 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001696 if (mFirstLayout) {
1697 setCurrentPage(whichPage);
1698 return false;
1699 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001700
Andy Wickhamda5a89b2021-07-23 12:38:14 -10001701 if (FeatureFlags.IS_STUDIO_BUILD && !Utilities.IS_RUNNING_IN_TEST_HARNESS) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001702 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1703 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001704 }
1705
Adam Cohen6f127a62014-06-12 14:54:41 -07001706 whichPage = validateNewPage(whichPage);
1707
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001709
Winson Chung321e9ee2010-08-09 13:37:56 -07001710 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001711 if (immediate) {
1712 duration = 0;
1713 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001714 duration = Math.abs(delta);
1715 }
1716
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001717 if (duration != 0) {
1718 pageBeginTransition();
1719 }
1720
Adam Cohenf358a4b2013-07-23 16:47:31 -07001721 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001722 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001723 }
Adam Cohenf9618852013-11-08 06:45:03 -08001724
Sunny Goyalc39db732021-04-03 01:20:21 -07001725 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001726 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001727
1728 // Trigger a compute() to finish switching pages if necessary
1729 if (immediate) {
1730 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001731 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001732 }
1733
Winson Chung321e9ee2010-08-09 13:37:56 -07001734 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001735 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001736 }
1737
Vadim Tryshev98913d02018-05-18 18:41:34 -07001738 public boolean scrollLeft() {
1739 if (getNextPage() > 0) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001740 snapToPage(getNextPage() - getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001741 return true;
1742 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001743 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001744 }
1745
Vadim Tryshev98913d02018-05-18 18:41:34 -07001746 public boolean scrollRight() {
1747 if (getNextPage() < getChildCount() - 1) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001748 snapToPage(getNextPage() + getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001749 return true;
1750 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001751 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001752 }
1753
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001754 @Override
Lais Andradecc5c8842021-06-04 18:20:30 +01001755 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Lais Andradef6e31b12021-10-27 12:04:28 +01001756 if (mScroller.isFinished()) {
1757 // This was not caused by the scroller, skip it.
1758 return;
1759 }
Lais Andradecc5c8842021-06-04 18:20:30 +01001760 int newDestinationPage = getDestinationPage();
1761 if (newDestinationPage >= 0 && newDestinationPage != mCurrentScrollOverPage) {
1762 mCurrentScrollOverPage = newDestinationPage;
1763 onScrollOverPageChanged();
1764 }
1765 }
1766
1767 @Override
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001768 public CharSequence getAccessibilityClassName() {
1769 // Some accessibility services have special logic for ScrollView. Since we provide same
1770 // accessibility info as ScrollView, inform the service to handle use the same way.
1771 return ScrollView.class.getName();
1772 }
1773
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001774 protected boolean isPageOrderFlipped() {
1775 return false;
1776 }
1777
Winson Chung6a0f57d2011-06-29 20:10:49 -07001778 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001779 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001780 @Override
1781 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1782 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001783 final boolean pagesFlipped = isPageOrderFlipped();
Alex Chau9ead9ca2021-11-26 16:22:49 +00001784 info.setScrollable(getPageCount() > 0);
1785 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1786 if (getCurrentPage() < getPageCount() - getPanelCount()
1787 || (getCurrentPage() == getPageCount() - getPanelCount()
1788 && primaryScroll != getScrollForPage(getPageCount() - getPanelCount()))) {
yingleiw02cc8482019-08-02 16:14:27 -07001789 info.addAction(pagesFlipped ?
Alex Chau9ead9ca2021-11-26 16:22:49 +00001790 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1791 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
yingleiw02cc8482019-08-02 16:14:27 -07001792 info.addAction(mIsRtl ?
1793 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1794 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001795 }
Alex Chau9ead9ca2021-11-26 16:22:49 +00001796 if (getCurrentPage() > 0
1797 || (getCurrentPage() == 0 && primaryScroll != getScrollForPage(0))) {
yingleiw02cc8482019-08-02 16:14:27 -07001798 info.addAction(pagesFlipped ?
Alex Chau9ead9ca2021-11-26 16:22:49 +00001799 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1800 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
yingleiw02cc8482019-08-02 16:14:27 -07001801 info.addAction(mIsRtl ?
1802 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1803 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001804 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001805 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1806 // Besides disabling the accessibility long-click, this also prevents this view from getting
1807 // accessibility focus.
1808 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001809 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001810 }
1811
1812 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001813 public void sendAccessibilityEvent(int eventType) {
1814 // Don't let the view send real scroll events.
1815 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1816 super.sendAccessibilityEvent(eventType);
1817 }
1818 }
1819
1820 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001821 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1822 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001823 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001824 }
1825
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001826 @Override
1827 public boolean performAccessibilityAction(int action, Bundle arguments) {
1828 if (super.performAccessibilityAction(action, arguments)) {
1829 return true;
1830 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001831 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001832 switch (action) {
1833 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001834 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001835 return true;
1836 }
1837 } break;
1838 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001839 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001840 return true;
1841 }
yingleiw02cc8482019-08-02 16:14:27 -07001842 } break;
1843 case android.R.id.accessibilityActionPageRight: {
1844 if (!mIsRtl) {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001845 return scrollRight();
yingleiw02cc8482019-08-02 16:14:27 -07001846 } else {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001847 return scrollLeft();
yingleiw02cc8482019-08-02 16:14:27 -07001848 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001849 }
yingleiw02cc8482019-08-02 16:14:27 -07001850 case android.R.id.accessibilityActionPageLeft: {
1851 if (!mIsRtl) {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001852 return scrollLeft();
yingleiw02cc8482019-08-02 16:14:27 -07001853 } else {
Alex Chau9ead9ca2021-11-26 16:22:49 +00001854 return scrollRight();
yingleiw02cc8482019-08-02 16:14:27 -07001855 }
1856 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001857 }
1858 return false;
1859 }
1860
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001861 protected boolean canAnnouncePageDescription() {
1862 return true;
1863 }
1864
Adam Cohen0ffac432013-07-10 11:19:26 -07001865 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001866 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001867 getNextPage() + 1, getChildCount());
1868 }
1869
Tony Mak2e564402018-02-27 17:19:34 +00001870 protected float getDownMotionX() {
1871 return mDownMotionX;
1872 }
1873
1874 protected float getDownMotionY() {
1875 return mDownMotionY;
1876 }
1877
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001878 protected interface ComputePageScrollsLogic {
1879
1880 boolean shouldIncludeView(View view);
1881 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001882
1883 public int[] getVisibleChildrenRange() {
1884 float visibleLeft = 0;
1885 float visibleRight = visibleLeft + getMeasuredWidth();
1886 float scaleX = getScaleX();
1887 if (scaleX < 1 && scaleX > 0) {
1888 float mid = getMeasuredWidth() / 2;
1889 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1890 visibleRight = mid + ((visibleRight - mid) / scaleX);
1891 }
1892
1893 int leftChild = -1;
1894 int rightChild = -1;
1895 final int childCount = getChildCount();
1896 for (int i = 0; i < childCount; i++) {
1897 final View child = getPageAt(i);
1898
1899 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1900 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1901 if (leftChild == -1) {
1902 leftChild = i;
1903 }
1904 rightChild = i;
1905 }
1906 }
1907 mTmpIntPair[0] = leftChild;
1908 mTmpIntPair[1] = rightChild;
1909 return mTmpIntPair;
1910 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001911
1912 @Override
1913 public void draw(Canvas canvas) {
1914 super.draw(canvas);
1915 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001916 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001917 }
1918
1919 protected void drawEdgeEffect(Canvas canvas) {
1920 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1921 final int width = getWidth();
1922 final int height = getHeight();
1923 if (!mEdgeGlowLeft.isFinished()) {
1924 final int restoreCount = canvas.save();
1925 canvas.rotate(-90);
1926 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1927 mEdgeGlowLeft.setSize(height, width);
1928 if (mEdgeGlowLeft.draw(canvas)) {
1929 postInvalidateOnAnimation();
1930 }
1931 canvas.restoreToCount(restoreCount);
1932 }
1933 if (!mEdgeGlowRight.isFinished()) {
1934 final int restoreCount = canvas.save();
1935 canvas.rotate(90, width, 0);
1936 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1937
1938 mEdgeGlowRight.setSize(height, width);
1939 if (mEdgeGlowRight.draw(canvas)) {
1940 postInvalidateOnAnimation();
1941 }
1942 canvas.restoreToCount(restoreCount);
1943 }
1944 }
1945 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001946}