blob: b26a7ea22d28fc0f13699b08ca9311fa15e18506 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyalc39db732021-04-03 01:20:21 -070019import static com.android.launcher3.anim.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070020import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
21import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyal8b37c572020-03-31 12:12:14 -070022import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070023import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
24import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
25
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070026import android.animation.LayoutTransition;
Sunny Goyalcf25b522015-07-09 00:01:18 -070027import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070029import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080030import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070033import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080036import android.view.InputDevice;
37import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.MotionEvent;
39import android.view.VelocityTracker;
40import android.view.View;
41import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080042import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.view.ViewGroup;
44import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070045import android.view.accessibility.AccessibilityEvent;
46import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalc39db732021-04-03 01:20:21 -070047import android.widget.OverScroller;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070048import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049
Tony Wickham62117d72020-03-26 16:56:26 -070050import androidx.annotation.Nullable;
51
Jon Miranda9e198662020-03-11 14:42:02 -070052import com.android.launcher3.compat.AccessibilityManagerCompat;
53import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070055import com.android.launcher3.touch.PagedOrientationHandler;
56import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070057import com.android.launcher3.util.EdgeEffectCompat;
Jon Miranda9e198662020-03-11 14:42:02 -070058import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070059import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070060
61import java.util.ArrayList;
Sunny Goyal59969372021-05-06 12:11:44 -070062import java.util.function.Consumer;
Jon Miranda9e198662020-03-11 14:42:02 -070063
Winson Chung321e9ee2010-08-09 13:37:56 -070064/**
65 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070066 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070067 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080068public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070069 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070070 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070071 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070072
Winson Chung4941f652020-06-18 15:11:53 -070073 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053074 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070075 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070076
Vadim Tryshevfedca432015-08-19 17:55:02 -070077 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070078
Adam Cohenb64cb5a2011-02-15 13:53:42 -080079 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080080 // The page is moved more than halfway, automatically move to the next page on touch up.
81 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080082
Sunny Goyal8e2133b2015-05-06 13:39:07 -070083 private static final float MAX_SCROLL_PROGRESS = 1.0f;
84
Adam Cohen265b9a62011-12-07 14:37:18 -080085 // The following constants need to be scaled based on density. The scaled versions will be
86 // assigned to the corresponding member variables below.
87 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070088 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080089 private static final int MIN_SNAP_VELOCITY = 1500;
90 private static final int MIN_FLING_VELOCITY = 250;
91
Adam Cohenf358a4b2013-07-23 16:47:31 -070092 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070093
Winson Chung4941f652020-06-18 15:11:53 -070094 protected final int mFlingThresholdVelocity;
95 protected final int mEasyFlingThresholdVelocity;
96 protected final int mMinFlingVelocity;
97 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070098
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected boolean mFirstLayout = true;
100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700103
Sunny Goyal4ffec482016-02-09 11:28:52 -0800104 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800106 protected int mMaxScroll;
107 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700108 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800110 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
112 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700113 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800114 private float mDownMotionPrimary;
115 private float mLastMotion;
116 private float mLastMotionRemainder;
117 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700118 // Used in special cases where the fling checks can be relaxed for an intentional gesture
119 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700120 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700121
Vadim Trysheve628c472018-05-16 14:28:18 -0700122 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700123 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800124
Winson Chung4941f652020-06-18 15:11:53 -0700125 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800126 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700127 // The amount of movement to begin paging
128 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800130 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Michael Jurka5f1c5092010-09-03 14:15:02 -0700132 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Michael Jurka5f1c5092010-09-03 14:15:02 -0700134 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700136 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700137 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700138
Winson Chungd2be3812013-07-16 11:11:32 -0700139 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800141 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700142
John Spurlock77e1f472013-09-11 10:09:51 -0400143 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800144 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400145
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800146 // Similar to the platform implementation of isLayoutValid();
147 protected boolean mIsLayoutValid;
148
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700149 private int[] mTmpIntPair = new int[2];
150
Sunny Goyalc39db732021-04-03 01:20:21 -0700151 protected EdgeEffectCompat mEdgeGlowLeft;
152 protected EdgeEffectCompat mEdgeGlowRight;
153
Winson Chung321e9ee2010-08-09 13:37:56 -0700154 public PagedView(Context context) {
155 this(context, null);
156 }
157
158 public PagedView(Context context, AttributeSet attrs) {
159 this(context, attrs, 0);
160 }
161
162 public PagedView(Context context, AttributeSet attrs, int defStyle) {
163 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700164
Adam Cohen9c4949e2010-10-05 12:27:22 -0700165 TypedArray a = context.obtainStyledAttributes(attrs,
166 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700167 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700168 a.recycle();
169
Winson Chung321e9ee2010-08-09 13:37:56 -0700170 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700171 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700172
Sunny Goyalc39db732021-04-03 01:20:21 -0700173 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700174 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700175
Vinit Nayaka406f722019-12-19 11:50:55 -0800176 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700177 mTouchSlop = configuration.getScaledTouchSlop();
178 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700179 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800180
Sunny Goyalcf25b522015-07-09 00:01:18 -0700181 float density = getResources().getDisplayMetrics().density;
182 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700183 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700184 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
185 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700186
Sunny Goyalc39db732021-04-03 01:20:21 -0700187 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700188 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700189 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 }
191
Sunny Goyalc39db732021-04-03 01:20:21 -0700192 protected void initEdgeEffect() {
193 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
194 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800195 }
196
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700197 public void initParentViews(View parent) {
198 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800199 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700200 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700201 }
202 }
203
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800204 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700205 return mPageIndicator;
206 }
207
Adam Cohen674531f2013-12-13 15:07:14 -0800208 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700209 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
210 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000211 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700212 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700214 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700215 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700217
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700218 /**
219 * Returns the index of page to be shown immediately afterwards.
220 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700221 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700222 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
223 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700224
Adam Cohenf9c184a2016-01-15 16:47:43 -0800225 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 return getChildCount();
227 }
228
Sunny Goyal83a8f042015-05-19 12:52:12 -0700229 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 return getChildAt(index);
231 }
232
Adam Cohenae4f1552011-10-20 00:15:42 -0700233 protected int indexToPage(int index) {
234 return 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()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800246 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700247 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800248 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700249 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700250 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800251 }
252
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700253 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700254 mScroller.abortAnimation();
255 // We need to clean up the next page here to avoid computeScrollHelper from
256 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700257 if (resetNextPage) {
258 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800259 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700260 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700261 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700262
Tony Wickham8f7ead32016-04-07 18:46:44 -0700263 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700264 mScroller.forceFinished(true);
265 // We need to clean up the next page here to avoid computeScrollHelper from
266 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700267 if (resetNextPage) {
268 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800269 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700270 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271 }
272
Adam Cohen6f127a62014-06-12 14:54:41 -0700273 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700274 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700275 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100276 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
277
278 if (getPanelCount() > 1) {
279 // Always return left panel as new page
280 newPage = getLeftmostVisiblePageForIndex(newPage);
281 }
282 return newPage;
283 }
284
285 private int getLeftmostVisiblePageForIndex(int pageIndex) {
286 int panelCount = getPanelCount();
287 return (pageIndex / panelCount) * panelCount;
288 }
289
290 /**
291 * Returns the number of pages that are shown at the same time.
292 */
293 protected int getPanelCount() {
294 return 1;
295 }
296
297 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700298 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100299 */
Sunny Goyal59969372021-05-06 12:11:44 -0700300 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl2bb51342021-03-20 01:13:45 +0100301 int panelCount = getPanelCount();
Andras Kloczl2bb51342021-03-20 01:13:45 +0100302 for (int i = mCurrentPage; i < mCurrentPage + panelCount; i++) {
303 View page = getPageAt(i);
304 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700305 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100306 }
307 }
Andras Kloczl2bb51342021-03-20 01:13:45 +0100308 }
309
310 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100311 * Returns true if the view is on one of the current pages, false otherwise.
312 */
313 public boolean isVisible(View child) {
314 return getLeftmostVisiblePageForIndex(indexOfChild(child)) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700315 }
316
Chet Haasebc2f0822012-10-26 17:59:53 -0700317 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700318 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
319 */
320 private int ensureWithinScrollBounds(int page) {
321 int dir = !mIsRtl ? 1 : - 1;
322 int currScroll = getScrollForPage(page);
323 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800324 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700325 page += dir;
326 prevScroll = currScroll;
327 currScroll = getScrollForPage(page);
328 if (currScroll <= prevScroll) {
329 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
330 break;
331 }
332 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800333 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700334 page -= dir;
335 prevScroll = currScroll;
336 currScroll = getScrollForPage(page);
337 if (currScroll >= prevScroll) {
338 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
339 break;
340 }
341 }
342 return page;
343 }
344
Tony Wickham03f27012019-04-25 14:22:54 -0700345 public void setCurrentPage(int currentPage) {
346 setCurrentPage(currentPage, INVALID_PAGE);
347 }
348
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700349 /**
Winson Chung86f77532010-08-24 11:08:22 -0700350 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 */
Tony Wickham03f27012019-04-25 14:22:54 -0700352 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800353 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700354 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800355 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800356 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
357 // the default
358 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800359 return;
360 }
Tony Wickham03f27012019-04-25 14:22:54 -0700361 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700362 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700363 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700364 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800365 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 }
367
Winson Chung8c87cd82013-07-23 16:20:10 -0700368 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800369 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
370 * has settled.
371 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700372 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800373 updatePageIndicator();
374 }
375
376 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700377 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000378 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700379 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700381 protected void pageBeginTransition() {
382 if (!mIsPageInTransition) {
383 mIsPageInTransition = true;
384 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700385 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700386 }
387
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700388 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700389 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700390 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700391 mIsPageInTransition = false;
392 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700393 }
Michael Jurka0142d492010-08-25 17:46:15 -0700394 }
395
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700396 @Override
397 public void onVisibilityAggregated(boolean isVisible) {
398 pageEndTransition();
399 super.onVisibilityAggregated(isVisible);
400 }
401
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700402 protected boolean isPageInTransition() {
403 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700404 }
405
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700406 /**
407 * Called when the page starts moving as part of the scroll. Subclasses can override this
408 * to provide custom behavior during animation.
409 */
410 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700411 }
412
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700413 /**
414 * Called when the page ends moving as part of the scroll. Subclasses can override this
415 * to provide custom behavior during animation.
416 */
417 protected void onPageEndTransition() {
vadimt4d93df52019-07-10 17:32:58 -0700418 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800419 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
420 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700421 if (mOnPageTransitionEndCallback != null) {
422 mOnPageTransitionEndCallback.run();
423 mOnPageTransitionEndCallback = null;
424 }
425 }
426
427 /**
428 * Sets a callback to run once when the scrolling finishes. If there is currently
429 * no page in transition, then the callback is called immediately.
430 */
431 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
432 if (mIsPageInTransition || callback == null) {
433 mOnPageTransitionEndCallback = callback;
434 } else {
435 callback.run();
436 }
Michael Jurka0142d492010-08-25 17:46:15 -0700437 }
438
Adam Cohen68d73932010-11-15 10:50:58 -0800439 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700440 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700441 x = Utilities.boundToRange(x,
442 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
443 y = Utilities.boundToRange(y,
444 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800445 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700446 }
447
Adam Cohen53805212013-10-01 10:39:23 -0700448 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800449 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700450 if (mCurrentPage != getNextPage()) {
451 AccessibilityEvent ev =
452 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700453 ev.setScrollable(true);
454 ev.setScrollX(getScrollX());
455 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800456 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700457 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700458 }
Adam Cohen53805212013-10-01 10:39:23 -0700459 }
460 }
461
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700462 protected void announcePageForAccessibility() {
463 if (isAccessibilityEnabled(getContext())) {
464 // Notify the user when the page changes
465 announceForAccessibility(getCurrentPageDescription());
466 }
467 }
468
Winson Chung85425412021-05-10 16:24:14 -0700469 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700471 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700472 int oldPos = mOrientationHandler.getPrimaryScroll(this);
473 int newPos = mScroller.getCurrX();
474 if (oldPos != newPos) {
475 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700476 }
Winson Chung85425412021-05-10 16:24:14 -0700477
478 if (mAllowOverScroll) {
479 if (newPos < mMinScroll && oldPos >= mMinScroll) {
480 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
481 mScroller.abortAnimation();
482 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
483 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
484 mScroller.abortAnimation();
Sunny Goyalc39db732021-04-03 01:20:21 -0700485 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800486 }
Adam Cohen53805212013-10-01 10:39:23 -0700487
Winson Chung85425412021-05-10 16:24:14 -0700488 // If the scroller has scrolled to the final position and there is no edge effect, then
489 // finish the scroller to skip waiting for additional settling
490 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
491 mScroller.getFinalY());
492 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
493 mScroller.abortAnimation();
494 }
495
496 invalidate();
497 return true;
498 } else if (mNextPage != INVALID_PAGE) {
499 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700500 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700501 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700502 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700503 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700504
Adam Cohen73aa9752010-11-24 16:26:58 -0800505 // We don't want to trigger a page end moving unless the page has settled
506 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700507 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700508 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800509 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700510
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700511 if (canAnnouncePageDescription()) {
512 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 }
Michael Jurka0142d492010-08-25 17:46:15 -0700515 return false;
516 }
517
518 @Override
519 public void computeScroll() {
520 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 }
522
Sunny Goyalce8809a2018-01-18 14:02:47 -0800523 public int getExpectedHeight() {
524 return getMeasuredHeight();
525 }
526
Adam Cohen410f3cd2013-09-22 12:09:32 -0700527 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800528 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800529 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700530 }
531
Sunny Goyalce8809a2018-01-18 14:02:47 -0800532 public int getExpectedWidth() {
533 return getMeasuredWidth();
534 }
535
Sunny Goyalbc683e92017-12-06 10:25:07 -0800536 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800537 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800538 - mInsets.left - mInsets.right;
539 }
540
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800542 public void requestLayout() {
543 mIsLayoutValid = false;
544 super.requestLayout();
545 }
546
547 @Override
548 public void forceLayout() {
549 mIsLayoutValid = false;
550 super.forceLayout();
551 }
552
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100553 private int getPageWidthSize(int widthSize) {
554 return (widthSize - mInsets.left - mInsets.right) / getPanelCount();
555 }
556
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800557 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700559 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700560 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
561 return;
562 }
563
Adam Cohen7d30a372013-07-01 17:03:59 -0700564 // We measure the dimensions of the PagedView to be larger than the pages so that when we
565 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700566 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
567 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
568 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700569 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700570
Adam Cohen7d30a372013-07-01 17:03:59 -0700571 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
572 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
573 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 }
575
Winson Chung8aad6102012-05-11 16:27:49 -0700576 // Return early if we aren't given a proper dimension
577 if (widthSize <= 0 || heightSize <= 0) {
578 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
579 return;
580 }
581
Winson Chung321e9ee2010-08-09 13:37:56 -0700582 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700583 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700584 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700585
Sunny Goyalac00cba2017-11-13 15:58:01 -0800586 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100587 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800588 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800589 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700590
Sunny Goyalac00cba2017-11-13 15:58:01 -0800591 // measureChildren takes accounts for content padding, we only need to care about extra
592 // space due to insets.
593 measureChildren(myWidthSpec, myHeightSpec);
594 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800595 }
596
Sunny Goyalcf25b522015-07-09 00:01:18 -0700597 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700599 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800600 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700601 final int childCount = getChildCount();
602 boolean pageScrollChanged = false;
603 if (mPageScrolls == null || childCount != mPageScrolls.length) {
604 mPageScrolls = new int[childCount];
605 pageScrollChanged = true;
606 }
607
608 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700609 return;
610 }
611
Winson Chung785d2eb2011-04-14 16:08:02 -0700612 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700613
Vinit Nayaka406f722019-12-19 11:50:55 -0800614 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
615 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700616 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700618
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700619 final LayoutTransition transition = getLayoutTransition();
620 // If the transition is running defer updating max scroll, as some empty pages could
621 // still be present, and a max scroll change could cause sudden jumps in scroll.
622 if (transition != null && transition.isRunning()) {
623 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
624
625 @Override
626 public void startTransition(LayoutTransition transition, ViewGroup container,
627 View view, int transitionType) { }
628
629 @Override
630 public void endTransition(LayoutTransition transition, ViewGroup container,
631 View view, int transitionType) {
632 // Wait until all transitions are complete.
633 if (!transition.isRunning()) {
634 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700635 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700636 }
637 }
638 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700639 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700640 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700641 }
642
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700643 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
644 updateCurrentPageScroll();
645 mFirstLayout = false;
646 }
647
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800648 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700649 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700650 }
Winson Chunge3193b92010-09-10 11:44:42 -0700651 }
652
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700653 /**
654 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
655 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700656 */
657 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
658 ComputePageScrollsLogic scrollLogic) {
659 final int childCount = getChildCount();
660
661 final int startIndex = mIsRtl ? childCount - 1 : 0;
662 final int endIndex = mIsRtl ? -1 : childCount;
663 final int delta = mIsRtl ? -1 : 1;
664
Vinit Nayaka406f722019-12-19 11:50:55 -0800665 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700666
Vinit Nayaka406f722019-12-19 11:50:55 -0800667 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
668 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700669 boolean pageScrollChanged = false;
670
Vinit Nayaka406f722019-12-19 11:50:55 -0800671 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700672 final View child = getPageAt(i);
673 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800674 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
675 pageCenter, layoutChildren);
676 final int primaryDimension = bounds.primaryDimension;
677 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700678
Alex Chaub5c4d112021-03-29 17:37:53 +0100679 // In case the pages are of different width, align the page to left edge for non-RTL
680 // or right edge for RTL.
681 final int pageScroll =
682 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700683 if (outPageScrolls[i] != pageScroll) {
684 pageScrollChanged = true;
685 outPageScrolls[i] = pageScroll;
686 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800687 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700688 }
689 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100690
691 int panelCount = getPanelCount();
692 if (panelCount > 1) {
693 for (int i = 0; i < childCount; i++) {
694 // In case we have multiple panels, always use left panel's page scroll for all
695 // panels on the screen.
696 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
697 if (outPageScrolls[i] != adjustedScroll) {
698 outPageScrolls[i] = adjustedScroll;
699 pageScrollChanged = true;
700 }
701 }
702 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700703 return pageScrollChanged;
704 }
705
Sunny Goyala1fbd842015-05-20 13:40:27 -0700706 protected int getChildGap() {
707 return 0;
708 }
709
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700710 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800711 mMinScroll = computeMinScroll();
712 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700713 }
714
Vinit Nayaka406f722019-12-19 11:50:55 -0800715 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700716 return 0;
Tony50876bf2018-09-19 23:09:14 -0400717 }
718
Vinit Nayaka406f722019-12-19 11:50:55 -0800719 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700720 int childCount = getChildCount();
721 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700722 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700723 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700724 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700725 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700726 }
727 }
728
Adam Cohen3b185e22013-10-29 14:45:58 -0700729 public void setPageSpacing(int pageSpacing) {
730 mPageSpacing = pageSpacing;
731 requestLayout();
732 }
733
Tony50876bf2018-09-19 23:09:14 -0400734 public int getPageSpacing() {
735 return mPageSpacing;
736 }
737
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800738 private void dispatchPageCountChanged() {
739 if (mPageIndicator != null) {
740 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700741 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700742 // This ensures that when children are added, they get the correct transforms / alphas
743 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700744 invalidate();
745 }
746
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800748 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800749 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800750 dispatchPageCountChanged();
751 }
752
753 @Override
754 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800755 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700756 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800757 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700758 }
759
Adam Cohen73894962011-10-31 13:17:17 -0700760 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700761 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800762 View pageAtIndex = getPageAt(index);
763 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700764 }
765
Alex Chau7944ee52021-01-08 19:07:25 +0000766 protected int getChildVisibleSize(int index) {
767 View layout = getPageAt(index);
768 return mOrientationHandler.getMeasuredSize(layout);
769 }
770
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 @Override
772 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700773 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700774 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700775 if (immediate) {
776 setCurrentPage(page);
777 } else {
778 snapToPage(page);
779 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 return true;
781 }
782 return false;
783 }
784
785 @Override
786 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700787 int focusablePage;
788 if (mNextPage != INVALID_PAGE) {
789 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700791 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 }
Winson Chung86f77532010-08-24 11:08:22 -0700793 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700795 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 }
797 return false;
798 }
799
800 @Override
801 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800802 if (super.dispatchUnhandledMove(focused, direction)) {
803 return true;
804 }
805
806 if (mIsRtl) {
807 if (direction == View.FOCUS_LEFT) {
808 direction = View.FOCUS_RIGHT;
809 } else if (direction == View.FOCUS_RIGHT) {
810 direction = View.FOCUS_LEFT;
811 }
812 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700814 if (getCurrentPage() > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100815 int nextPage = validateNewPage(getCurrentPage() - 1);
816 snapToPage(nextPage);
817 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 return true;
819 }
820 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700821 if (getCurrentPage() < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100822 int nextPage = validateNewPage(getCurrentPage() + 1);
823 snapToPage(nextPage);
824 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700825 return true;
826 }
827 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800828 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 }
830
831 @Override
832 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700833 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
834 return;
835 }
836
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200837 // Add the current page's views as focusable and the next possible page's too. If the
838 // last focus change action was left then the left neighbour's views will be added, and
839 // if it was right then the right neighbour's views will be added.
840 // Unfortunately mCurrentPage can be outdated if there were multiple control actions in a
841 // short period of time, but mNextPage is up to date because it is always updated by
842 // method snapToPage.
843 int nextPage = getNextPage();
Adam Cohen0ffac432013-07-10 11:19:26 -0700844 // XXX-RTL: This will be fixed in a future CL
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200845 if (nextPage >= 0 && nextPage < getPageCount()) {
846 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848 if (direction == View.FOCUS_LEFT) {
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200849 if (nextPage > 0) {
850 nextPage = validateNewPage(nextPage - 1);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100851 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100853 } else if (direction == View.FOCUS_RIGHT) {
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200854 if (nextPage < getPageCount() - 1) {
855 nextPage = validateNewPage(nextPage + 1);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100856 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 }
858 }
859 }
860
861 /**
862 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700863 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 *
Winson Chung86f77532010-08-24 11:08:22 -0700865 * This happens when live folders requery, and if they're off page, they
866 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 */
868 @Override
869 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700870 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 View v = focused;
872 while (true) {
873 if (v == current) {
874 super.focusableViewAvailable(focused);
875 return;
876 }
877 if (v == this) {
878 return;
879 }
880 ViewParent parent = v.getParent();
881 if (parent instanceof View) {
882 v = (View)v.getParent();
883 } else {
884 return;
885 }
886 }
887 }
888
889 /**
890 * {@inheritDoc}
891 */
892 @Override
893 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
894 if (disallowIntercept) {
895 // We need to make sure to cancel our long press if
896 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +0200897 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 }
899 super.requestDisallowInterceptTouchEvent(disallowIntercept);
900 }
901
902 @Override
903 public boolean onInterceptTouchEvent(MotionEvent ev) {
904 /*
905 * This method JUST determines whether we want to intercept the motion.
906 * If we return true, onTouchEvent will be called and we do the actual
907 * scrolling there.
908 */
909
Winson Chung45e1d6e2010-11-09 17:19:49 -0800910 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700911 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700912
913 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800914
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 /*
916 * Shortcut the most recurring case: the user is in the dragging
917 * state and he is moving his finger. We want to intercept this
918 * motion.
919 */
920 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700921 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 return true;
923 }
924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 switch (action & MotionEvent.ACTION_MASK) {
926 case MotionEvent.ACTION_MOVE: {
927 /*
928 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800929 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700931 if (mActivePointerId != INVALID_POINTER) {
932 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700933 }
934 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800935 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700936 // i.e. fall through to the next case (don't break)
937 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
938 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700939 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 }
941
942 case MotionEvent.ACTION_DOWN: {
943 final float x = ev.getX();
944 final float y = ev.getY();
945 // Remember location of down touch
946 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700947 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700948 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800949 mLastMotionRemainder = 0;
950 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700951 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -0700953 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 break;
955 }
956
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800958 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700959 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 break;
961
962 case MotionEvent.ACTION_POINTER_UP:
963 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800964 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 break;
966 }
967
968 /*
969 * The only time we want to intercept motion events is if we are in the
970 * drag mode.
971 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700972 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 }
974
Tony Wickhama0aee212019-09-18 09:24:03 -0700975 /**
976 * If being flinged and user touches the screen, initiate drag; otherwise don't.
977 */
Sunny Goyalc39db732021-04-03 01:20:21 -0700978 private void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -0700979 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -0700980 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -0700981 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -0700982
983 if (finishedScrolling) {
984 mIsBeingDragged = false;
985 if (!mScroller.isFinished() && !mFreeScroll) {
986 setCurrentPage(getNextPage());
987 pageEndTransition();
988 }
Sunny Goyalc39db732021-04-03 01:20:21 -0700989 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -0700990 } else {
991 mIsBeingDragged = true;
992 }
Sunny Goyalc39db732021-04-03 01:20:21 -0700993
994 // Catch the edge effect if it is active.
995 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
996 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
997 if (!mEdgeGlowLeft.isFinished()) {
998 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
999 }
1000 if (!mEdgeGlowRight.isFinished()) {
1001 mEdgeGlowRight.onPullDistance(0f, displacement);
1002 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001003 }
1004
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001005 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001006 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001007 }
1008
Adam Cohenf8d28232011-02-01 21:47:00 -08001009 protected void determineScrollingStart(MotionEvent ev) {
1010 determineScrollingStart(ev, 1.0f);
1011 }
1012
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 /*
1014 * Determines if we should change the touch state to start scrolling after the
1015 * user moves their touch point too far.
1016 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001017 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001018 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001020 if (pointerIndex == -1) return;
1021
Vinit Nayaka406f722019-12-19 11:50:55 -08001022 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1023 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001024 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001025 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001026
Vinit Nayaka406f722019-12-19 11:50:55 -08001027 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001028 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001029 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001030 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1031 mLastMotion = primaryDirection;
1032 mLastMotionRemainder = 0;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001033 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001034 // Stop listening for things like pinches.
1035 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001036 }
1037 }
1038
1039 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001040 // Try canceling the long press. It could also have been scheduled
1041 // by a distant descendant, so use the mAllowLongPress flag to block
1042 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001043 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 }
1045
Adam Cohenb5ba0972011-09-07 18:02:31 -07001046 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001047 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001048
Adam Cohenedb40762013-07-18 16:45:45 -07001049 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001050 int count = getChildCount();
1051
1052 final int totalDistance;
1053
1054 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001055 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001056 adjacentPage = page - 1;
1057 }
1058
1059 if (adjacentPage < 0 || adjacentPage > count - 1) {
1060 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1061 } else {
1062 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1063 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001064
1065 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001066 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1067 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001068 return scrollProgress;
1069 }
1070
Adam Cohenedb40762013-07-18 16:45:45 -07001071 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001072 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001073 return 0;
1074 } else {
1075 return mPageScrolls[index];
1076 }
1077 }
1078
Adam Cohen564a2e72013-10-09 14:47:32 -07001079 // While layout transitions are occurring, a child's position may stray from its baseline
1080 // position. This method returns the magnitude of this stray at any given time.
1081 public int getLayoutTransitionOffsetForPage(int index) {
1082 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1083 return 0;
1084 } else {
1085 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001086
Sunny Goyal228153d2018-01-04 15:35:22 -08001087 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1088 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001089 return (int) (child.getX() - baselineX);
1090 }
1091 }
1092
Tony Wickham023f4042019-01-29 10:52:31 -08001093 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001094 if (mFreeScroll == freeScroll) {
1095 return;
1096 }
1097
Tony Wickham8f7ead32016-04-07 18:46:44 -07001098 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001099 mFreeScroll = freeScroll;
1100
Adam Cohenf9618852013-11-08 06:45:03 -08001101 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001102 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001103 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001104 if (getScrollForPage(getNextPage()) != getScrollX()) {
1105 snapToPage(getNextPage());
1106 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001107 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001108 }
1109
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001110 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001111 mAllowOverScroll = enable;
1112 }
1113
Winson Chung321e9ee2010-08-09 13:37:56 -07001114 @Override
1115 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001116 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001117 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001118
Michael Jurkab8f06722010-10-10 15:58:46 -07001119 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001120
1121 final int action = ev.getAction();
1122
1123 switch (action & MotionEvent.ACTION_MASK) {
1124 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001125 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001126
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 /*
1128 * If being flinged and user touches, stop the fling. isFinished
1129 * will be false if being flinged.
1130 */
1131 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001132 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001133 }
1134
1135 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001136 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001137 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001138 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1139 mLastMotionRemainder = 0;
1140 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001141 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001143 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001144 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001145 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001146 break;
1147
Winson Chung4941f652020-06-18 15:11:53 -07001148 case ACTION_MOVE_ALLOW_EASY_FLING:
1149 // Start scrolling immediately
1150 determineScrollingStart(ev);
1151 mAllowEasyFling = true;
1152 break;
1153
1154 case MotionEvent.ACTION_MOVE:
1155 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 // Scroll to follow the motion event
1157 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001158
1159 if (pointerIndex == -1) return true;
Sunny Goyalc39db732021-04-03 01:20:21 -07001160 float oldScroll = mOrientationHandler.getPrimaryScroll(this);
1161 float dx = ev.getX(pointerIndex);
1162 float dy = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001163
Sunny Goyalc39db732021-04-03 01:20:21 -07001164 float direction = mOrientationHandler.getPrimaryValue(dx, dy);
Vinit Nayaka406f722019-12-19 11:50:55 -08001165 float delta = mLastMotion + mLastMotionRemainder - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001166
1167 int width = getWidth();
1168 int height = getHeight();
1169 int size = mOrientationHandler.getPrimaryValue(width, height);
1170
1171 final float displacement = mOrientationHandler.getSecondaryValue(dx, dy)
1172 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001173 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001174
Sunny Goyalc39db732021-04-03 01:20:21 -07001175 if (mAllowOverScroll) {
1176 float consumed = 0;
1177 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
1178 consumed = size * mEdgeGlowRight.onPullDistance(delta / size, displacement);
1179 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
1180 consumed = -size * mEdgeGlowLeft.onPullDistance(
1181 -delta / size, 1 - displacement);
1182 }
1183 delta -= consumed;
1184 }
1185
Winson Chungc0844aa2011-02-02 15:25:58 -08001186 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1187 // keep the remainder because we are actually testing if we've moved from the last
1188 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001189 mLastMotion = direction;
1190 int movedDelta = (int) delta;
1191 mLastMotionRemainder = delta - movedDelta;
Vinit Nayaka406f722019-12-19 11:50:55 -08001192
Sunny Goyalc39db732021-04-03 01:20:21 -07001193 if (delta != 0) {
1194 mOrientationHandler.set(this, VIEW_SCROLL_BY, movedDelta);
1195
1196 if (mAllowOverScroll) {
1197 final float pulledToX = oldScroll + delta;
1198
1199 if (pulledToX < mMinScroll) {
1200 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement);
1201 if (!mEdgeGlowRight.isFinished()) {
1202 mEdgeGlowRight.onRelease();
1203 }
1204 } else if (pulledToX > mMaxScroll) {
1205 mEdgeGlowRight.onPullDistance(delta / size, displacement);
1206 if (!mEdgeGlowLeft.isFinished()) {
1207 mEdgeGlowLeft.onRelease();
1208 }
1209 }
1210
1211 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1212 postInvalidateOnAnimation();
1213 }
1214 }
1215
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 } else {
1217 awakenScrollBars();
1218 }
Adam Cohen564976a2010-10-13 18:52:07 -07001219 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 determineScrollingStart(ev);
1221 }
1222 break;
1223
1224 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001225 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 final int activePointerId = mActivePointerId;
1227 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001228 if (pointerIndex == -1) return true;
1229
Vinit Nayaka406f722019-12-19 11:50:55 -08001230 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1231 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001232 final VelocityTracker velocityTracker = mVelocityTracker;
1233 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001234
Vinit Nayaka406f722019-12-19 11:50:55 -08001235 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1236 mActivePointerId);
1237 int delta = (int) (primaryDirection - mDownMotionPrimary);
1238 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1239
1240 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1241 SIGNIFICANT_MOVE_THRESHOLD;
1242
1243 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001244 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1245 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001246 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1247 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001248 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1249 Log.d("Quickswitch", "isFling=false vel=" + velocity
1250 + " threshold=" + mEasyFlingThresholdVelocity);
1251 }
Adam Cohen00481b32011-11-18 12:03:48 -08001252
Adam Cohenf358a4b2013-07-23 16:47:31 -07001253 if (!mFreeScroll) {
1254 // In the case that the page is moved far to one direction and then is flung
1255 // in the opposite direction, we use a threshold to determine whether we should
1256 // just return to the starting page, or if we should skip one further.
1257 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001258 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1259 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 returnToOriginalPage = true;
1261 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001262
Adam Cohenf358a4b2013-07-23 16:47:31 -07001263 int finalPage;
1264 // We give flings precedence over large moves, which is why we short-circuit our
1265 // test for a large move if a fling has been registered. That is, a large
1266 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001267
Vinit Nayaka406f722019-12-19 11:50:55 -08001268 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1269 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001270 finalPage = returnToOriginalPage
1271 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001272 snapToPageWithVelocity(finalPage, velocity);
1273 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1274 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001275 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001276 finalPage = returnToOriginalPage
1277 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001278 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001279 } else {
1280 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001281 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001283 if (!mScroller.isFinished()) {
1284 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001285 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001286
Vinit Nayaka406f722019-12-19 11:50:55 -08001287 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1288 int maxScroll = mMaxScroll;
1289 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001290
Vinit Nayaka406f722019-12-19 11:50:55 -08001291 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1292 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001293 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001294 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001295 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001296 int velocity1 = -velocity;
1297 // Continue a scroll or fling in progress
1298 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1299 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001300
Sunny Goyalc39db732021-04-03 01:20:21 -07001301 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001302 mNextPage = getDestinationPage(finalPos);
Sunny Goyalc39db732021-04-03 01:20:21 -07001303 onNotSnappingToPageInFreeScroll();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001304 }
1305 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001307 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001308
Sunny Goyalc39db732021-04-03 01:20:21 -07001309 mEdgeGlowLeft.onRelease();
1310 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001311 // End any intermediate reordering states
1312 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001313 break;
1314
1315 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001316 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001317 snapToDestination();
1318 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001319 mEdgeGlowLeft.onRelease();
1320 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001321 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 break;
1323
1324 case MotionEvent.ACTION_POINTER_UP:
1325 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001326 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 break;
1328 }
1329
1330 return true;
1331 }
1332
Sunny Goyalc39db732021-04-03 01:20:21 -07001333 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001334
Vinit Nayaka406f722019-12-19 11:50:55 -08001335 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001336 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1337 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001338 }
1339
Adam Cohen7d30a372013-07-01 17:03:59 -07001340 private void resetTouchState() {
1341 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001342 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 }
1345
Winson Chung185d7162011-02-28 13:47:29 -08001346 @Override
1347 public boolean onGenericMotionEvent(MotionEvent event) {
1348 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1349 switch (event.getAction()) {
1350 case MotionEvent.ACTION_SCROLL: {
1351 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1352 final float vscroll;
1353 final float hscroll;
1354 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1355 vscroll = 0;
1356 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1357 } else {
1358 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1359 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1360 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001361 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1362 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001363 }
Winson Chung185d7162011-02-28 13:47:29 -08001364 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001365 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001366 : (hscroll > 0 || vscroll > 0);
1367 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001368 scrollRight();
1369 } else {
1370 scrollLeft();
1371 }
1372 return true;
1373 }
1374 }
1375 }
1376 }
1377 return super.onGenericMotionEvent(event);
1378 }
1379
Sunny Goyal8b37c572020-03-31 12:12:14 -07001380 /**
1381 * Returns true if the paged view can scroll for the provided vertical and horizontal
1382 * scroll values
1383 */
1384 protected boolean canScroll(float absVScroll, float absHScroll) {
1385 ActivityContext ac = ActivityContext.lookupContext(getContext());
1386 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001387 }
1388
Michael Jurkab8f06722010-10-10 15:58:46 -07001389 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1390 if (mVelocityTracker == null) {
1391 mVelocityTracker = VelocityTracker.obtain();
1392 }
1393 mVelocityTracker.addMovement(ev);
1394 }
1395
1396 private void releaseVelocityTracker() {
1397 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001398 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001399 mVelocityTracker.recycle();
1400 mVelocityTracker = null;
1401 }
1402 }
1403
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001405 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001406 final int pointerId = ev.getPointerId(pointerIndex);
1407 if (pointerId == mActivePointerId) {
1408 // This was our active pointer going up. Choose a new
1409 // active pointer and adjust accordingly.
1410 // TODO: Make this decision more intelligent.
1411 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001412 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1413 newPointerIndex);
1414 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 mActivePointerId = ev.getPointerId(newPointerIndex);
1416 if (mVelocityTracker != null) {
1417 mVelocityTracker.clear();
1418 }
1419 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001420 }
1421
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 @Override
1423 public void requestChildFocus(View child, View focused) {
1424 super.requestChildFocus(child, focused);
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001425
1426 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1427 // which results in incorrect navigation
1428 int nextPage = getNextPage();
1429 if (nextPage != mCurrentPage) {
1430 setCurrentPage(nextPage);
1431 }
1432
Adam Cohenae4f1552011-10-20 00:15:42 -07001433 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001434 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001435 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 }
1437 }
1438
Alex Chaub794ea62021-02-03 18:00:30 +00001439 public int getDestinationPage() {
1440 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1441 }
1442
Alex Chaudedbc8a2021-03-17 16:53:26 +00001443 protected int getDestinationPage(int primaryScroll) {
1444 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001445 }
1446
Sunny Goyal228153d2018-01-04 15:35:22 -08001447 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001448 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001449 }
1450
Alex Chaudedbc8a2021-03-17 16:53:26 +00001451 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1452 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001453 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001455 final int childCount = getChildCount();
1456 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001457 int distanceFromScreenCenter = Math.abs(
1458 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1460 minDistanceFromScreenCenter = distanceFromScreenCenter;
1461 minDistanceFromScreenCenterIndex = i;
1462 }
1463 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001464 return minDistanceFromScreenCenterIndex;
1465 }
1466
Tony Wickham7383d4e2020-07-07 19:25:25 -07001467 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001468 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001469 int halfChildSize = (childSize / 2);
1470 int childCenter = getChildOffset(childIndex) + halfChildSize;
1471 return childCenter - screenCenter;
1472 }
1473
1474 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001475 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1476 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001477 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1478 }
1479
Alex Chaudedbc8a2021-03-17 16:53:26 +00001480 private int getScreenCenter(int primaryScroll) {
1481 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1482 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1483 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1484 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1485 + primaryPivot);
1486 }
1487
Adam Cohend19d3ca2010-09-15 14:43:42 -07001488 protected void snapToDestination() {
Sunny Goyalc39db732021-04-03 01:20:21 -07001489 snapToPage(getDestinationPage(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 }
1491
Adam Cohene0f66b52010-11-23 15:06:07 -08001492 // We want the duration of the page snap animation to be influenced by the distance that
1493 // the screen has to travel, however, we don't want this duration to be effected in a
1494 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1495 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001496 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001497 f -= 0.5f; // center the values about 0.
1498 f *= 0.3f * Math.PI / 2.0f;
1499 return (float) Math.sin(f);
1500 }
1501
Winson Chung05a31ed2018-02-08 17:08:43 -08001502 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001503 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001504 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001505
Vinit Nayaka406f722019-12-19 11:50:55 -08001506 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001507 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001508 int duration = 0;
1509
Sunny Goyal4d113a52015-05-27 10:05:28 -07001510 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001511 // If the velocity is low enough, then treat this more as an automatic page advance
1512 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001513 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001514 }
1515
1516 // Here we compute a "distance" that will be used in the computation of the overall
1517 // snap duration. This is a function of the actual distance that needs to be traveled;
1518 // we keep this value close to half screen size in order to reduce the variance in snap
1519 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001520 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001521 float distance = halfScreenSize + halfScreenSize *
1522 distanceInfluenceForSnapDuration(distanceRatio);
1523
1524 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001525 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001526
1527 // we want the page's snap velocity to approximately match the velocity at which the
1528 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001529 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1530 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001531
Sunny Goyalc39db732021-04-03 01:20:21 -07001532 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001533 }
1534
Winson Chung05a31ed2018-02-08 17:08:43 -08001535 public boolean snapToPage(int whichPage) {
1536 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001537 }
1538
Winson Chung05a31ed2018-02-08 17:08:43 -08001539 public boolean snapToPageImmediately(int whichPage) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001540 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001541 }
1542
Winson Chung05a31ed2018-02-08 17:08:43 -08001543 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001544 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001545 }
1546
Sunny Goyalc39db732021-04-03 01:20:21 -07001547 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001548 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001549
Vinit Nayaka406f722019-12-19 11:50:55 -08001550 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001551 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1552 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001553 }
1554
Winson Chung05a31ed2018-02-08 17:08:43 -08001555 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001556 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001557 }
Michael Jurka0142d492010-08-25 17:46:15 -07001558
Sunny Goyalc39db732021-04-03 01:20:21 -07001559 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001560 if (mFirstLayout) {
1561 setCurrentPage(whichPage);
1562 return false;
1563 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001564
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001565 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001566 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1567 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001568 }
1569
Adam Cohen6f127a62014-06-12 14:54:41 -07001570 whichPage = validateNewPage(whichPage);
1571
Adam Cohen7d30a372013-07-01 17:03:59 -07001572 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001573
Winson Chung321e9ee2010-08-09 13:37:56 -07001574 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001575 if (immediate) {
1576 duration = 0;
1577 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001578 duration = Math.abs(delta);
1579 }
1580
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001581 if (duration != 0) {
1582 pageBeginTransition();
1583 }
1584
Adam Cohenf358a4b2013-07-23 16:47:31 -07001585 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001586 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001587 }
Adam Cohenf9618852013-11-08 06:45:03 -08001588
Sunny Goyalc39db732021-04-03 01:20:21 -07001589 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001590 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001591
1592 // Trigger a compute() to finish switching pages if necessary
1593 if (immediate) {
1594 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001595 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001596 }
1597
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001599 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001600 }
1601
Vadim Tryshev98913d02018-05-18 18:41:34 -07001602 public boolean scrollLeft() {
1603 if (getNextPage() > 0) {
1604 snapToPage(getNextPage() - 1);
1605 return true;
1606 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001607 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001608 }
1609
Vadim Tryshev98913d02018-05-18 18:41:34 -07001610 public boolean scrollRight() {
1611 if (getNextPage() < getChildCount() - 1) {
1612 snapToPage(getNextPage() + 1);
1613 return true;
1614 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001615 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001616 }
1617
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001618 @Override
1619 public CharSequence getAccessibilityClassName() {
1620 // Some accessibility services have special logic for ScrollView. Since we provide same
1621 // accessibility info as ScrollView, inform the service to handle use the same way.
1622 return ScrollView.class.getName();
1623 }
1624
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001625 protected boolean isPageOrderFlipped() {
1626 return false;
1627 }
1628
Winson Chung6a0f57d2011-06-29 20:10:49 -07001629 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001630 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001631 @Override
1632 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1633 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001634 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001635 int offset = (mAllowOverScroll ? 0 : 1);
1636 info.setScrollable(getPageCount() > offset);
1637 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001638 info.addAction(pagesFlipped ?
1639 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1640 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1641 info.addAction(mIsRtl ?
1642 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1643 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001644 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001645 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001646 info.addAction(pagesFlipped ?
1647 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1648 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1649 info.addAction(mIsRtl ?
1650 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1651 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001652 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001653 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1654 // Besides disabling the accessibility long-click, this also prevents this view from getting
1655 // accessibility focus.
1656 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001657 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001658 }
1659
1660 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001661 public void sendAccessibilityEvent(int eventType) {
1662 // Don't let the view send real scroll events.
1663 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1664 super.sendAccessibilityEvent(eventType);
1665 }
1666 }
1667
1668 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001669 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1670 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001671 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001672 }
1673
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001674 @Override
1675 public boolean performAccessibilityAction(int action, Bundle arguments) {
1676 if (super.performAccessibilityAction(action, arguments)) {
1677 return true;
1678 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001679 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001680 switch (action) {
1681 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001682 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001683 return true;
1684 }
1685 } break;
1686 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001687 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001688 return true;
1689 }
yingleiw02cc8482019-08-02 16:14:27 -07001690 } break;
1691 case android.R.id.accessibilityActionPageRight: {
1692 if (!mIsRtl) {
1693 return scrollRight();
1694 } else {
1695 return scrollLeft();
1696 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001697 }
yingleiw02cc8482019-08-02 16:14:27 -07001698 case android.R.id.accessibilityActionPageLeft: {
1699 if (!mIsRtl) {
1700 return scrollLeft();
1701 } else {
1702 return scrollRight();
1703 }
1704 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001705 }
1706 return false;
1707 }
1708
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001709 protected boolean canAnnouncePageDescription() {
1710 return true;
1711 }
1712
Adam Cohen0ffac432013-07-10 11:19:26 -07001713 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001714 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001715 getNextPage() + 1, getChildCount());
1716 }
1717
Tony Mak2e564402018-02-27 17:19:34 +00001718 protected float getDownMotionX() {
1719 return mDownMotionX;
1720 }
1721
1722 protected float getDownMotionY() {
1723 return mDownMotionY;
1724 }
1725
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001726 protected interface ComputePageScrollsLogic {
1727
1728 boolean shouldIncludeView(View view);
1729 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001730
1731 public int[] getVisibleChildrenRange() {
1732 float visibleLeft = 0;
1733 float visibleRight = visibleLeft + getMeasuredWidth();
1734 float scaleX = getScaleX();
1735 if (scaleX < 1 && scaleX > 0) {
1736 float mid = getMeasuredWidth() / 2;
1737 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1738 visibleRight = mid + ((visibleRight - mid) / scaleX);
1739 }
1740
1741 int leftChild = -1;
1742 int rightChild = -1;
1743 final int childCount = getChildCount();
1744 for (int i = 0; i < childCount; i++) {
1745 final View child = getPageAt(i);
1746
1747 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1748 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1749 if (leftChild == -1) {
1750 leftChild = i;
1751 }
1752 rightChild = i;
1753 }
1754 }
1755 mTmpIntPair[0] = leftChild;
1756 mTmpIntPair[1] = rightChild;
1757 return mTmpIntPair;
1758 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001759
1760 @Override
1761 public void draw(Canvas canvas) {
1762 super.draw(canvas);
1763 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001764 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001765 }
1766
1767 protected void drawEdgeEffect(Canvas canvas) {
1768 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1769 final int width = getWidth();
1770 final int height = getHeight();
1771 if (!mEdgeGlowLeft.isFinished()) {
1772 final int restoreCount = canvas.save();
1773 canvas.rotate(-90);
1774 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1775 mEdgeGlowLeft.setSize(height, width);
1776 if (mEdgeGlowLeft.draw(canvas)) {
1777 postInvalidateOnAnimation();
1778 }
1779 canvas.restoreToCount(restoreCount);
1780 }
1781 if (!mEdgeGlowRight.isFinished()) {
1782 final int restoreCount = canvas.save();
1783 canvas.rotate(90, width, 0);
1784 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1785
1786 mEdgeGlowRight.setSize(height, width);
1787 if (mEdgeGlowRight.draw(canvas)) {
1788 postInvalidateOnAnimation();
1789 }
1790 canvas.restoreToCount(restoreCount);
1791 }
1792 }
1793 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001794}