blob: 97a44c13d939ec21c1cf986961a8ee97dc1cd115 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Andras Kloczl55edfe52021-05-14 12:21:30 +020019import static androidx.annotation.VisibleForTesting.PACKAGE_PRIVATE;
Sunny Goyalc39db732021-04-03 01:20:21 -070020import static com.android.launcher3.anim.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070021import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
22import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyal8b37c572020-03-31 12:12:14 -070023import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070024import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
25import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
26
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070027import android.animation.LayoutTransition;
Sunny Goyalcf25b522015-07-09 00:01:18 -070028import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070030import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080031import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070034import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080037import android.view.InputDevice;
38import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070039import android.view.MotionEvent;
40import android.view.VelocityTracker;
41import android.view.View;
42import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080043import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
47import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalc39db732021-04-03 01:20:21 -070048import android.widget.OverScroller;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070049import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070050
Tony Wickham62117d72020-03-26 16:56:26 -070051import androidx.annotation.Nullable;
Andras Kloczl55edfe52021-05-14 12:21:30 +020052import androidx.annotation.VisibleForTesting;
Tony Wickham62117d72020-03-26 16:56:26 -070053
Jon Miranda9e198662020-03-11 14:42:02 -070054import com.android.launcher3.compat.AccessibilityManagerCompat;
55import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070056import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.touch.PagedOrientationHandler;
58import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070059import com.android.launcher3.util.EdgeEffectCompat;
Andras Kloczl55edfe52021-05-14 12:21:30 +020060import com.android.launcher3.util.IntSet;
Jon Miranda9e198662020-03-11 14:42:02 -070061import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070062import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070063
64import java.util.ArrayList;
Sunny Goyal59969372021-05-06 12:11:44 -070065import java.util.function.Consumer;
Jon Miranda9e198662020-03-11 14:42:02 -070066
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080071public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070074 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070075
Winson Chung4941f652020-06-18 15:11:53 -070076 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053077 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070078 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Vadim Tryshevfedca432015-08-19 17:55:02 -070080 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Adam Cohenb64cb5a2011-02-15 13:53:42 -080082 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080083 // The page is moved more than halfway, automatically move to the next page on touch up.
84 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080085
Sunny Goyal8e2133b2015-05-06 13:39:07 -070086 private static final float MAX_SCROLL_PROGRESS = 1.0f;
87
Adam Cohen265b9a62011-12-07 14:37:18 -080088 // The following constants need to be scaled based on density. The scaled versions will be
89 // assigned to the corresponding member variables below.
90 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070091 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080092 private static final int MIN_SNAP_VELOCITY = 1500;
93 private static final int MIN_FLING_VELOCITY = 250;
94
Adam Cohenf358a4b2013-07-23 16:47:31 -070095 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070096
Winson Chung4941f652020-06-18 15:11:53 -070097 protected final int mFlingThresholdVelocity;
98 protected final int mEasyFlingThresholdVelocity;
99 protected final int mMinFlingVelocity;
100 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700101
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected boolean mFirstLayout = true;
103
Sunny Goyal4ffec482016-02-09 11:28:52 -0800104 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -0700106
Sunny Goyal4ffec482016-02-09 11:28:52 -0800107 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700108 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800109 protected int mMaxScroll;
110 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700111 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800113 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
115 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700116 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800117 private float mDownMotionPrimary;
118 private float mLastMotion;
119 private float mLastMotionRemainder;
120 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700121 // Used in special cases where the fling checks can be relaxed for an intentional gesture
122 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700123 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700124
Vadim Trysheve628c472018-05-16 14:28:18 -0700125 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700126 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800127
Winson Chung4941f652020-06-18 15:11:53 -0700128 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800129 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700130 // The amount of movement to begin paging
131 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800133 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka5f1c5092010-09-03 14:15:02 -0700135 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Michael Jurka5f1c5092010-09-03 14:15:02 -0700137 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700139 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700140 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700141
Winson Chungd2be3812013-07-16 11:11:32 -0700142 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700143 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800144 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700145
John Spurlock77e1f472013-09-11 10:09:51 -0400146 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800147 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400148
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800149 // Similar to the platform implementation of isLayoutValid();
150 protected boolean mIsLayoutValid;
151
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700152 private int[] mTmpIntPair = new int[2];
153
Sunny Goyalc39db732021-04-03 01:20:21 -0700154 protected EdgeEffectCompat mEdgeGlowLeft;
155 protected EdgeEffectCompat mEdgeGlowRight;
156
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 public PagedView(Context context) {
158 this(context, null);
159 }
160
161 public PagedView(Context context, AttributeSet attrs) {
162 this(context, attrs, 0);
163 }
164
165 public PagedView(Context context, AttributeSet attrs, int defStyle) {
166 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700167
Adam Cohen9c4949e2010-10-05 12:27:22 -0700168 TypedArray a = context.obtainStyledAttributes(attrs,
169 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700170 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700171 a.recycle();
172
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700174 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700175
Sunny Goyalc39db732021-04-03 01:20:21 -0700176 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700177 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700178
Vinit Nayaka406f722019-12-19 11:50:55 -0800179 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700180 mTouchSlop = configuration.getScaledTouchSlop();
181 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700182 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800183
Sunny Goyalcf25b522015-07-09 00:01:18 -0700184 float density = getResources().getDisplayMetrics().density;
185 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700186 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700187 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
188 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700189
Sunny Goyalc39db732021-04-03 01:20:21 -0700190 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700191 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700192 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 }
194
Sunny Goyalc39db732021-04-03 01:20:21 -0700195 protected void initEdgeEffect() {
196 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
197 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800198 }
199
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700200 public void initParentViews(View parent) {
201 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800202 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700203 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700204 }
205 }
206
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800207 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700208 return mPageIndicator;
209 }
210
Adam Cohen674531f2013-12-13 15:07:14 -0800211 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700212 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
213 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000214 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700215 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700217 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700218 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700220
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700221 /**
222 * Returns the index of page to be shown immediately afterwards.
223 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700224 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700225 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
226 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700227
Adam Cohenf9c184a2016-01-15 16:47:43 -0800228 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 return getChildCount();
230 }
231
Sunny Goyal83a8f042015-05-19 12:52:12 -0700232 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 return getChildAt(index);
234 }
235
Adam Cohenae4f1552011-10-20 00:15:42 -0700236 protected int indexToPage(int index) {
237 return index;
238 }
239
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800241 * Updates the scroll of the current page immediately to its final scroll position. We use this
242 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
243 * the previous tab page.
244 */
245 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700246 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800247 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700248 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800249 newPosition = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700250 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800251 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700252 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700253 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800254 }
255
Tony Wickhamf8d731a2021-05-26 14:58:13 -0700256 /**
257 * Immediately finishes any overscroll effect and jumps to the end of the scroller animation.
258 */
259 public void abortScrollerAnimation() {
260 mEdgeGlowLeft.finish();
261 mEdgeGlowRight.finish();
262 abortScrollerAnimation(true);
263 }
264
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700265 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700266 mScroller.abortAnimation();
267 // We need to clean up the next page here to avoid computeScrollHelper from
268 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700269 if (resetNextPage) {
270 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800271 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700272 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700273 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700274
Tony Wickham8f7ead32016-04-07 18:46:44 -0700275 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700276 mScroller.forceFinished(true);
277 // We need to clean up the next page here to avoid computeScrollHelper from
278 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700279 if (resetNextPage) {
280 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800281 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700282 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700283 }
284
Adam Cohen6f127a62014-06-12 14:54:41 -0700285 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700286 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700287 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100288 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
289
290 if (getPanelCount() > 1) {
291 // Always return left panel as new page
292 newPage = getLeftmostVisiblePageForIndex(newPage);
293 }
294 return newPage;
295 }
296
Andras Kloczl55edfe52021-05-14 12:21:30 +0200297 /**
298 * In most cases where panelCount is 1, this method will just return the page index that was
299 * passed in.
300 * But for example when two panel home is enabled we might need the leftmost visible page index
301 * because that page is the current page.
302 */
303 public int getLeftmostVisiblePageForIndex(int pageIndex) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100304 int panelCount = getPanelCount();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200305 return pageIndex - pageIndex % panelCount;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100306 }
307
308 /**
309 * Returns the number of pages that are shown at the same time.
310 */
311 protected int getPanelCount() {
312 return 1;
313 }
314
315 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +0200316 * Returns an IntSet with the indices of the currently visible pages
317 */
318 @VisibleForTesting(otherwise = PACKAGE_PRIVATE)
319 public IntSet getVisiblePageIndices() {
320 IntSet visiblePageIndices = new IntSet();
321 int panelCount = getPanelCount();
322 int pageCount = getPageCount();
323
324 // If a device goes from one panel to two panel (i.e. unfolding a foldable device) while
325 // an odd indexed page is the current page, then the new leftmost visible page will be
326 // different from the old mCurrentPage.
327 int currentPage = getLeftmostVisiblePageForIndex(mCurrentPage);
328 for (int page = currentPage; page < currentPage + panelCount && page < pageCount; page++) {
329 visiblePageIndices.add(page);
330 }
331 return visiblePageIndices;
332 }
333
334 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700335 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100336 */
Sunny Goyal59969372021-05-06 12:11:44 -0700337 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200338 getVisiblePageIndices().forEach(pageIndex -> {
339 View page = getPageAt(pageIndex);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100340 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700341 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100342 }
Andras Kloczl55edfe52021-05-14 12:21:30 +0200343 });
Andras Kloczl2bb51342021-03-20 01:13:45 +0100344 }
345
346 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100347 * Returns true if the view is on one of the current pages, false otherwise.
348 */
349 public boolean isVisible(View child) {
350 return getLeftmostVisiblePageForIndex(indexOfChild(child)) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700351 }
352
Chet Haasebc2f0822012-10-26 17:59:53 -0700353 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700354 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
355 */
356 private int ensureWithinScrollBounds(int page) {
357 int dir = !mIsRtl ? 1 : - 1;
358 int currScroll = getScrollForPage(page);
359 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800360 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700361 page += dir;
362 prevScroll = currScroll;
363 currScroll = getScrollForPage(page);
364 if (currScroll <= prevScroll) {
365 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
366 break;
367 }
368 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800369 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700370 page -= dir;
371 prevScroll = currScroll;
372 currScroll = getScrollForPage(page);
373 if (currScroll >= prevScroll) {
374 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
375 break;
376 }
377 }
378 return page;
379 }
380
Tony Wickham03f27012019-04-25 14:22:54 -0700381 public void setCurrentPage(int currentPage) {
382 setCurrentPage(currentPage, INVALID_PAGE);
383 }
384
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700385 /**
Winson Chung86f77532010-08-24 11:08:22 -0700386 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 */
Tony Wickham03f27012019-04-25 14:22:54 -0700388 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800389 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700390 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800391 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800392 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
393 // the default
394 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800395 return;
396 }
Tony Wickham03f27012019-04-25 14:22:54 -0700397 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700398 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700399 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700400 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800401 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 }
403
Winson Chung8c87cd82013-07-23 16:20:10 -0700404 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800405 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
406 * has settled.
407 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700408 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800409 updatePageIndicator();
410 }
411
412 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700413 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000414 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700415 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700417 protected void pageBeginTransition() {
418 if (!mIsPageInTransition) {
419 mIsPageInTransition = true;
420 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700421 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700422 }
423
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700424 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700425 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700426 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700427 mIsPageInTransition = false;
428 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700429 }
Michael Jurka0142d492010-08-25 17:46:15 -0700430 }
431
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700432 @Override
433 public void onVisibilityAggregated(boolean isVisible) {
434 pageEndTransition();
435 super.onVisibilityAggregated(isVisible);
436 }
437
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700438 protected boolean isPageInTransition() {
439 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700440 }
441
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700442 /**
443 * Called when the page starts moving as part of the scroll. Subclasses can override this
444 * to provide custom behavior during animation.
445 */
446 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700447 }
448
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700449 /**
450 * Called when the page ends moving as part of the scroll. Subclasses can override this
451 * to provide custom behavior during animation.
452 */
453 protected void onPageEndTransition() {
vadimt4d93df52019-07-10 17:32:58 -0700454 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800455 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
456 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700457 if (mOnPageTransitionEndCallback != null) {
458 mOnPageTransitionEndCallback.run();
459 mOnPageTransitionEndCallback = null;
460 }
461 }
462
463 /**
464 * Sets a callback to run once when the scrolling finishes. If there is currently
465 * no page in transition, then the callback is called immediately.
466 */
467 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
468 if (mIsPageInTransition || callback == null) {
469 mOnPageTransitionEndCallback = callback;
470 } else {
471 callback.run();
472 }
Michael Jurka0142d492010-08-25 17:46:15 -0700473 }
474
Adam Cohen68d73932010-11-15 10:50:58 -0800475 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700476 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700477 x = Utilities.boundToRange(x,
478 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
479 y = Utilities.boundToRange(y,
480 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800481 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700482 }
483
Adam Cohen53805212013-10-01 10:39:23 -0700484 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800485 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700486 if (mCurrentPage != getNextPage()) {
487 AccessibilityEvent ev =
488 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700489 ev.setScrollable(true);
490 ev.setScrollX(getScrollX());
491 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800492 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700493 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700494 }
Adam Cohen53805212013-10-01 10:39:23 -0700495 }
496 }
497
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700498 protected void announcePageForAccessibility() {
499 if (isAccessibilityEnabled(getContext())) {
500 // Notify the user when the page changes
501 announceForAccessibility(getCurrentPageDescription());
502 }
503 }
504
Winson Chung85425412021-05-10 16:24:14 -0700505 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700507 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700508 int oldPos = mOrientationHandler.getPrimaryScroll(this);
509 int newPos = mScroller.getCurrX();
510 if (oldPos != newPos) {
511 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700512 }
Winson Chung85425412021-05-10 16:24:14 -0700513
514 if (mAllowOverScroll) {
515 if (newPos < mMinScroll && oldPos >= mMinScroll) {
516 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
517 mScroller.abortAnimation();
518 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
519 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
520 mScroller.abortAnimation();
Sunny Goyalc39db732021-04-03 01:20:21 -0700521 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800522 }
Adam Cohen53805212013-10-01 10:39:23 -0700523
Winson Chung85425412021-05-10 16:24:14 -0700524 // If the scroller has scrolled to the final position and there is no edge effect, then
525 // finish the scroller to skip waiting for additional settling
526 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
527 mScroller.getFinalY());
528 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
529 mScroller.abortAnimation();
530 }
531
532 invalidate();
533 return true;
534 } else if (mNextPage != INVALID_PAGE) {
535 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700536 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700537 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700538 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700539 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700540
Adam Cohen73aa9752010-11-24 16:26:58 -0800541 // We don't want to trigger a page end moving unless the page has settled
542 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700543 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700544 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800545 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700546
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700547 if (canAnnouncePageDescription()) {
548 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700549 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 }
Michael Jurka0142d492010-08-25 17:46:15 -0700551 return false;
552 }
553
554 @Override
555 public void computeScroll() {
556 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
558
Sunny Goyalce8809a2018-01-18 14:02:47 -0800559 public int getExpectedHeight() {
560 return getMeasuredHeight();
561 }
562
Adam Cohen410f3cd2013-09-22 12:09:32 -0700563 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800564 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800565 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700566 }
567
Sunny Goyalce8809a2018-01-18 14:02:47 -0800568 public int getExpectedWidth() {
569 return getMeasuredWidth();
570 }
571
Sunny Goyalbc683e92017-12-06 10:25:07 -0800572 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800573 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800574 - mInsets.left - mInsets.right;
575 }
576
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800578 public void requestLayout() {
579 mIsLayoutValid = false;
580 super.requestLayout();
581 }
582
583 @Override
584 public void forceLayout() {
585 mIsLayoutValid = false;
586 super.forceLayout();
587 }
588
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100589 private int getPageWidthSize(int widthSize) {
590 return (widthSize - mInsets.left - mInsets.right) / getPanelCount();
591 }
592
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800593 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700595 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700596 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
597 return;
598 }
599
Adam Cohen7d30a372013-07-01 17:03:59 -0700600 // We measure the dimensions of the PagedView to be larger than the pages so that when we
601 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700602 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
603 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
604 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700605 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700606
Adam Cohen7d30a372013-07-01 17:03:59 -0700607 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
608 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
609 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
611
Winson Chung8aad6102012-05-11 16:27:49 -0700612 // Return early if we aren't given a proper dimension
613 if (widthSize <= 0 || heightSize <= 0) {
614 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
615 return;
616 }
617
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700619 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700620 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700621
Sunny Goyalac00cba2017-11-13 15:58:01 -0800622 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100623 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800624 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800625 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700626
Sunny Goyalac00cba2017-11-13 15:58:01 -0800627 // measureChildren takes accounts for content padding, we only need to care about extra
628 // space due to insets.
629 measureChildren(myWidthSpec, myHeightSpec);
630 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800631 }
632
Sunny Goyalcf25b522015-07-09 00:01:18 -0700633 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700635 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800636 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700637 final int childCount = getChildCount();
638 boolean pageScrollChanged = false;
639 if (mPageScrolls == null || childCount != mPageScrolls.length) {
640 mPageScrolls = new int[childCount];
641 pageScrollChanged = true;
642 }
643
644 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700645 return;
646 }
647
Winson Chung785d2eb2011-04-14 16:08:02 -0700648 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700649
Vinit Nayaka406f722019-12-19 11:50:55 -0800650 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
651 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700652 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700654
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700655 final LayoutTransition transition = getLayoutTransition();
656 // If the transition is running defer updating max scroll, as some empty pages could
657 // still be present, and a max scroll change could cause sudden jumps in scroll.
658 if (transition != null && transition.isRunning()) {
659 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
660
661 @Override
662 public void startTransition(LayoutTransition transition, ViewGroup container,
663 View view, int transitionType) { }
664
665 @Override
666 public void endTransition(LayoutTransition transition, ViewGroup container,
667 View view, int transitionType) {
668 // Wait until all transitions are complete.
669 if (!transition.isRunning()) {
670 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700671 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700672 }
673 }
674 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700675 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700676 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700677 }
678
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700679 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
680 updateCurrentPageScroll();
681 mFirstLayout = false;
682 }
683
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800684 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700685 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700686 }
Winson Chunge3193b92010-09-10 11:44:42 -0700687 }
688
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700689 /**
690 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
691 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700692 */
693 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
694 ComputePageScrollsLogic scrollLogic) {
695 final int childCount = getChildCount();
696
697 final int startIndex = mIsRtl ? childCount - 1 : 0;
698 final int endIndex = mIsRtl ? -1 : childCount;
699 final int delta = mIsRtl ? -1 : 1;
700
Vinit Nayaka406f722019-12-19 11:50:55 -0800701 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700702
Vinit Nayaka406f722019-12-19 11:50:55 -0800703 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
704 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700705 boolean pageScrollChanged = false;
706
Vinit Nayaka406f722019-12-19 11:50:55 -0800707 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700708 final View child = getPageAt(i);
709 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800710 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
711 pageCenter, layoutChildren);
712 final int primaryDimension = bounds.primaryDimension;
713 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700714
Alex Chaub5c4d112021-03-29 17:37:53 +0100715 // In case the pages are of different width, align the page to left edge for non-RTL
716 // or right edge for RTL.
717 final int pageScroll =
718 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700719 if (outPageScrolls[i] != pageScroll) {
720 pageScrollChanged = true;
721 outPageScrolls[i] = pageScroll;
722 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800723 childStart += primaryDimension + mPageSpacing + getChildGap();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700724 }
725 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100726
727 int panelCount = getPanelCount();
728 if (panelCount > 1) {
729 for (int i = 0; i < childCount; i++) {
730 // In case we have multiple panels, always use left panel's page scroll for all
731 // panels on the screen.
732 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
733 if (outPageScrolls[i] != adjustedScroll) {
734 outPageScrolls[i] = adjustedScroll;
735 pageScrollChanged = true;
736 }
737 }
738 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700739 return pageScrollChanged;
740 }
741
Sunny Goyala1fbd842015-05-20 13:40:27 -0700742 protected int getChildGap() {
743 return 0;
744 }
745
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700746 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800747 mMinScroll = computeMinScroll();
748 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700749 }
750
Vinit Nayaka406f722019-12-19 11:50:55 -0800751 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700752 return 0;
Tony50876bf2018-09-19 23:09:14 -0400753 }
754
Vinit Nayaka406f722019-12-19 11:50:55 -0800755 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700756 int childCount = getChildCount();
757 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700758 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700759 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700760 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700761 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700762 }
763 }
764
Adam Cohen3b185e22013-10-29 14:45:58 -0700765 public void setPageSpacing(int pageSpacing) {
766 mPageSpacing = pageSpacing;
767 requestLayout();
768 }
769
Tony50876bf2018-09-19 23:09:14 -0400770 public int getPageSpacing() {
771 return mPageSpacing;
772 }
773
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800774 private void dispatchPageCountChanged() {
775 if (mPageIndicator != null) {
776 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700777 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700778 // This ensures that when children are added, they get the correct transforms / alphas
779 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700780 invalidate();
781 }
782
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800784 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800785 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800786 dispatchPageCountChanged();
787 }
788
789 @Override
790 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800791 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700792 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800793 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700794 }
795
Adam Cohen73894962011-10-31 13:17:17 -0700796 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700797 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800798 View pageAtIndex = getPageAt(index);
799 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700800 }
801
Alex Chau7944ee52021-01-08 19:07:25 +0000802 protected int getChildVisibleSize(int index) {
803 View layout = getPageAt(index);
804 return mOrientationHandler.getMeasuredSize(layout);
805 }
806
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 @Override
808 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700809 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700810 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700811 if (immediate) {
812 setCurrentPage(page);
813 } else {
814 snapToPage(page);
815 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 return true;
817 }
818 return false;
819 }
820
821 @Override
822 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700823 int focusablePage;
824 if (mNextPage != INVALID_PAGE) {
825 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700827 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 }
Winson Chung86f77532010-08-24 11:08:22 -0700829 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700831 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 }
833 return false;
834 }
835
836 @Override
837 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800838 if (super.dispatchUnhandledMove(focused, direction)) {
839 return true;
840 }
841
842 if (mIsRtl) {
843 if (direction == View.FOCUS_LEFT) {
844 direction = View.FOCUS_RIGHT;
845 } else if (direction == View.FOCUS_RIGHT) {
846 direction = View.FOCUS_LEFT;
847 }
848 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700850 if (getCurrentPage() > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100851 int nextPage = validateNewPage(getCurrentPage() - 1);
852 snapToPage(nextPage);
853 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 return true;
855 }
856 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700857 if (getCurrentPage() < getPageCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100858 int nextPage = validateNewPage(getCurrentPage() + 1);
859 snapToPage(nextPage);
860 getChildAt(nextPage).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 return true;
862 }
863 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800864 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866
867 @Override
868 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700869 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
870 return;
871 }
872
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200873 // Add the current page's views as focusable and the next possible page's too. If the
874 // last focus change action was left then the left neighbour's views will be added, and
875 // if it was right then the right neighbour's views will be added.
876 // Unfortunately mCurrentPage can be outdated if there were multiple control actions in a
877 // short period of time, but mNextPage is up to date because it is always updated by
878 // method snapToPage.
879 int nextPage = getNextPage();
Adam Cohen0ffac432013-07-10 11:19:26 -0700880 // XXX-RTL: This will be fixed in a future CL
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200881 if (nextPage >= 0 && nextPage < getPageCount()) {
882 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 }
884 if (direction == View.FOCUS_LEFT) {
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200885 if (nextPage > 0) {
886 nextPage = validateNewPage(nextPage - 1);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100887 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100889 } else if (direction == View.FOCUS_RIGHT) {
Andras Kloczl359d1ae2021-05-04 22:25:27 +0200890 if (nextPage < getPageCount() - 1) {
891 nextPage = validateNewPage(nextPage + 1);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100892 getPageAt(nextPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700893 }
894 }
895 }
896
897 /**
898 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700899 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 *
Winson Chung86f77532010-08-24 11:08:22 -0700901 * This happens when live folders requery, and if they're off page, they
902 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 */
904 @Override
905 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700906 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 View v = focused;
908 while (true) {
909 if (v == current) {
910 super.focusableViewAvailable(focused);
911 return;
912 }
913 if (v == this) {
914 return;
915 }
916 ViewParent parent = v.getParent();
917 if (parent instanceof View) {
918 v = (View)v.getParent();
919 } else {
920 return;
921 }
922 }
923 }
924
925 /**
926 * {@inheritDoc}
927 */
928 @Override
929 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
930 if (disallowIntercept) {
931 // We need to make sure to cancel our long press if
932 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +0200933 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 }
935 super.requestDisallowInterceptTouchEvent(disallowIntercept);
936 }
937
938 @Override
939 public boolean onInterceptTouchEvent(MotionEvent ev) {
940 /*
941 * This method JUST determines whether we want to intercept the motion.
942 * If we return true, onTouchEvent will be called and we do the actual
943 * scrolling there.
944 */
945
Winson Chung45e1d6e2010-11-09 17:19:49 -0800946 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700947 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700948
949 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800950
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 /*
952 * Shortcut the most recurring case: the user is in the dragging
953 * state and he is moving his finger. We want to intercept this
954 * motion.
955 */
956 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700957 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 return true;
959 }
960
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 switch (action & MotionEvent.ACTION_MASK) {
962 case MotionEvent.ACTION_MOVE: {
963 /*
964 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -0800965 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700967 if (mActivePointerId != INVALID_POINTER) {
968 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700969 }
970 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -0800971 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -0700972 // i.e. fall through to the next case (don't break)
973 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
974 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700975 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 }
977
978 case MotionEvent.ACTION_DOWN: {
979 final float x = ev.getX();
980 final float y = ev.getY();
981 // Remember location of down touch
982 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700983 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -0700984 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -0800985 mLastMotionRemainder = 0;
986 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -0700987 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -0700989 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 break;
991 }
992
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800994 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700995 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 break;
997
998 case MotionEvent.ACTION_POINTER_UP:
999 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001000 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 break;
1002 }
1003
1004 /*
1005 * The only time we want to intercept motion events is if we are in the
1006 * drag mode.
1007 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001008 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 }
1010
Tony Wickhama0aee212019-09-18 09:24:03 -07001011 /**
1012 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1013 */
Sunny Goyalc39db732021-04-03 01:20:21 -07001014 private void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -07001015 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -07001016 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -07001017 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001018
1019 if (finishedScrolling) {
1020 mIsBeingDragged = false;
1021 if (!mScroller.isFinished() && !mFreeScroll) {
1022 setCurrentPage(getNextPage());
1023 pageEndTransition();
1024 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001025 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -07001026 } else {
1027 mIsBeingDragged = true;
1028 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001029
1030 // Catch the edge effect if it is active.
1031 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
1032 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
1033 if (!mEdgeGlowLeft.isFinished()) {
1034 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
1035 }
1036 if (!mEdgeGlowRight.isFinished()) {
1037 mEdgeGlowRight.onPullDistance(0f, displacement);
1038 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001039 }
1040
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001041 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001042 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001043 }
1044
Adam Cohenf8d28232011-02-01 21:47:00 -08001045 protected void determineScrollingStart(MotionEvent ev) {
1046 determineScrollingStart(ev, 1.0f);
1047 }
1048
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 /*
1050 * Determines if we should change the touch state to start scrolling after the
1051 * user moves their touch point too far.
1052 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001053 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001054 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001056 if (pointerIndex == -1) return;
1057
Vinit Nayaka406f722019-12-19 11:50:55 -08001058 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1059 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001060 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001061 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001062
Vinit Nayaka406f722019-12-19 11:50:55 -08001063 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001064 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001065 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001066 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1067 mLastMotion = primaryDirection;
1068 mLastMotionRemainder = 0;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001069 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001070 // Stop listening for things like pinches.
1071 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001072 }
1073 }
1074
1075 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001076 // Try canceling the long press. It could also have been scheduled
1077 // by a distant descendant, so use the mAllowLongPress flag to block
1078 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001079 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 }
1081
Adam Cohenb5ba0972011-09-07 18:02:31 -07001082 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001083 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001084
Adam Cohenedb40762013-07-18 16:45:45 -07001085 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001086 int count = getChildCount();
1087
1088 final int totalDistance;
1089
1090 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001091 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001092 adjacentPage = page - 1;
1093 }
1094
1095 if (adjacentPage < 0 || adjacentPage > count - 1) {
1096 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1097 } else {
1098 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1099 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001100
1101 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001102 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1103 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001104 return scrollProgress;
1105 }
1106
Adam Cohenedb40762013-07-18 16:45:45 -07001107 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001108 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001109 return 0;
1110 } else {
1111 return mPageScrolls[index];
1112 }
1113 }
1114
Adam Cohen564a2e72013-10-09 14:47:32 -07001115 // While layout transitions are occurring, a child's position may stray from its baseline
1116 // position. This method returns the magnitude of this stray at any given time.
1117 public int getLayoutTransitionOffsetForPage(int index) {
1118 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1119 return 0;
1120 } else {
1121 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001122
Sunny Goyal228153d2018-01-04 15:35:22 -08001123 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1124 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001125 return (int) (child.getX() - baselineX);
1126 }
1127 }
1128
Tony Wickham023f4042019-01-29 10:52:31 -08001129 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001130 if (mFreeScroll == freeScroll) {
1131 return;
1132 }
1133
Tony Wickham8f7ead32016-04-07 18:46:44 -07001134 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001135 mFreeScroll = freeScroll;
1136
Adam Cohenf9618852013-11-08 06:45:03 -08001137 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001138 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001139 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001140 if (getScrollForPage(getNextPage()) != getScrollX()) {
1141 snapToPage(getNextPage());
1142 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001143 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001144 }
1145
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001146 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001147 mAllowOverScroll = enable;
1148 }
1149
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 @Override
1151 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001152 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001153 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001154
Michael Jurkab8f06722010-10-10 15:58:46 -07001155 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001156
1157 final int action = ev.getAction();
1158
1159 switch (action & MotionEvent.ACTION_MASK) {
1160 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001161 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001162
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 /*
1164 * If being flinged and user touches, stop the fling. isFinished
1165 * will be false if being flinged.
1166 */
1167 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001168 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 }
1170
1171 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001172 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001173 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001174 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1175 mLastMotionRemainder = 0;
1176 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001177 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001179 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001180 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001181 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 break;
1183
Winson Chung4941f652020-06-18 15:11:53 -07001184 case ACTION_MOVE_ALLOW_EASY_FLING:
1185 // Start scrolling immediately
1186 determineScrollingStart(ev);
1187 mAllowEasyFling = true;
1188 break;
1189
1190 case MotionEvent.ACTION_MOVE:
1191 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 // Scroll to follow the motion event
1193 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001194
1195 if (pointerIndex == -1) return true;
Sunny Goyalc39db732021-04-03 01:20:21 -07001196 float oldScroll = mOrientationHandler.getPrimaryScroll(this);
1197 float dx = ev.getX(pointerIndex);
1198 float dy = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001199
Sunny Goyalc39db732021-04-03 01:20:21 -07001200 float direction = mOrientationHandler.getPrimaryValue(dx, dy);
Vinit Nayaka406f722019-12-19 11:50:55 -08001201 float delta = mLastMotion + mLastMotionRemainder - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001202
1203 int width = getWidth();
1204 int height = getHeight();
1205 int size = mOrientationHandler.getPrimaryValue(width, height);
1206
1207 final float displacement = mOrientationHandler.getSecondaryValue(dx, dy)
1208 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001209 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001210
Sunny Goyalc39db732021-04-03 01:20:21 -07001211 if (mAllowOverScroll) {
1212 float consumed = 0;
1213 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
1214 consumed = size * mEdgeGlowRight.onPullDistance(delta / size, displacement);
1215 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
1216 consumed = -size * mEdgeGlowLeft.onPullDistance(
1217 -delta / size, 1 - displacement);
1218 }
1219 delta -= consumed;
1220 }
1221
Winson Chungc0844aa2011-02-02 15:25:58 -08001222 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1223 // keep the remainder because we are actually testing if we've moved from the last
1224 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001225 mLastMotion = direction;
1226 int movedDelta = (int) delta;
1227 mLastMotionRemainder = delta - movedDelta;
Vinit Nayaka406f722019-12-19 11:50:55 -08001228
Sunny Goyalc39db732021-04-03 01:20:21 -07001229 if (delta != 0) {
1230 mOrientationHandler.set(this, VIEW_SCROLL_BY, movedDelta);
1231
1232 if (mAllowOverScroll) {
1233 final float pulledToX = oldScroll + delta;
1234
1235 if (pulledToX < mMinScroll) {
1236 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement);
1237 if (!mEdgeGlowRight.isFinished()) {
1238 mEdgeGlowRight.onRelease();
1239 }
1240 } else if (pulledToX > mMaxScroll) {
1241 mEdgeGlowRight.onPullDistance(delta / size, displacement);
1242 if (!mEdgeGlowLeft.isFinished()) {
1243 mEdgeGlowLeft.onRelease();
1244 }
1245 }
1246
1247 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1248 postInvalidateOnAnimation();
1249 }
1250 }
1251
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 } else {
1253 awakenScrollBars();
1254 }
Adam Cohen564976a2010-10-13 18:52:07 -07001255 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 determineScrollingStart(ev);
1257 }
1258 break;
1259
1260 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001261 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 final int activePointerId = mActivePointerId;
1263 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001264 if (pointerIndex == -1) return true;
1265
Vinit Nayaka406f722019-12-19 11:50:55 -08001266 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1267 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001268 final VelocityTracker velocityTracker = mVelocityTracker;
1269 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001270
Vinit Nayaka406f722019-12-19 11:50:55 -08001271 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1272 mActivePointerId);
1273 int delta = (int) (primaryDirection - mDownMotionPrimary);
1274 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1275
1276 boolean isSignificantMove = Math.abs(delta) > pageOrientedSize *
1277 SIGNIFICANT_MOVE_THRESHOLD;
1278
1279 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001280 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1281 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001282 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1283 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001284 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1285 Log.d("Quickswitch", "isFling=false vel=" + velocity
1286 + " threshold=" + mEasyFlingThresholdVelocity);
1287 }
Adam Cohen00481b32011-11-18 12:03:48 -08001288
Adam Cohenf358a4b2013-07-23 16:47:31 -07001289 if (!mFreeScroll) {
1290 // In the case that the page is moved far to one direction and then is flung
1291 // in the opposite direction, we use a threshold to determine whether we should
1292 // just return to the starting page, or if we should skip one further.
1293 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001294 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1295 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001296 returnToOriginalPage = true;
1297 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001298
Adam Cohenf358a4b2013-07-23 16:47:31 -07001299 int finalPage;
1300 // We give flings precedence over large moves, which is why we short-circuit our
1301 // test for a large move if a fling has been registered. That is, a large
1302 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001303
Vinit Nayaka406f722019-12-19 11:50:55 -08001304 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1305 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001306 finalPage = returnToOriginalPage
1307 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001308 snapToPageWithVelocity(finalPage, velocity);
1309 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1310 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001311 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001312 finalPage = returnToOriginalPage
1313 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001314 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001315 } else {
1316 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001317 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001319 if (!mScroller.isFinished()) {
1320 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001321 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001322
Vinit Nayaka406f722019-12-19 11:50:55 -08001323 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1324 int maxScroll = mMaxScroll;
1325 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001326
Vinit Nayaka406f722019-12-19 11:50:55 -08001327 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1328 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001329 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001330 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001331 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001332 int velocity1 = -velocity;
1333 // Continue a scroll or fling in progress
1334 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1335 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001336
Sunny Goyalc39db732021-04-03 01:20:21 -07001337 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001338 mNextPage = getDestinationPage(finalPos);
Sunny Goyalc39db732021-04-03 01:20:21 -07001339 onNotSnappingToPageInFreeScroll();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001340 }
1341 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001344
Sunny Goyalc39db732021-04-03 01:20:21 -07001345 mEdgeGlowLeft.onRelease();
1346 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001347 // End any intermediate reordering states
1348 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 break;
1350
1351 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001352 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001353 snapToDestination();
1354 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001355 mEdgeGlowLeft.onRelease();
1356 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 break;
1359
1360 case MotionEvent.ACTION_POINTER_UP:
1361 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001362 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 break;
1364 }
1365
1366 return true;
1367 }
1368
Sunny Goyalc39db732021-04-03 01:20:21 -07001369 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001370
Vinit Nayaka406f722019-12-19 11:50:55 -08001371 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001372 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1373 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001374 }
1375
Adam Cohen7d30a372013-07-01 17:03:59 -07001376 private void resetTouchState() {
1377 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001378 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001379 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001380 }
1381
Winson Chung185d7162011-02-28 13:47:29 -08001382 @Override
1383 public boolean onGenericMotionEvent(MotionEvent event) {
1384 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1385 switch (event.getAction()) {
1386 case MotionEvent.ACTION_SCROLL: {
1387 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1388 final float vscroll;
1389 final float hscroll;
1390 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1391 vscroll = 0;
1392 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1393 } else {
1394 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1395 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1396 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001397 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1398 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001399 }
Winson Chung185d7162011-02-28 13:47:29 -08001400 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001401 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001402 : (hscroll > 0 || vscroll > 0);
1403 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001404 scrollRight();
1405 } else {
1406 scrollLeft();
1407 }
1408 return true;
1409 }
1410 }
1411 }
1412 }
1413 return super.onGenericMotionEvent(event);
1414 }
1415
Sunny Goyal8b37c572020-03-31 12:12:14 -07001416 /**
1417 * Returns true if the paged view can scroll for the provided vertical and horizontal
1418 * scroll values
1419 */
1420 protected boolean canScroll(float absVScroll, float absHScroll) {
1421 ActivityContext ac = ActivityContext.lookupContext(getContext());
1422 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001423 }
1424
Michael Jurkab8f06722010-10-10 15:58:46 -07001425 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1426 if (mVelocityTracker == null) {
1427 mVelocityTracker = VelocityTracker.obtain();
1428 }
1429 mVelocityTracker.addMovement(ev);
1430 }
1431
1432 private void releaseVelocityTracker() {
1433 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001434 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001435 mVelocityTracker.recycle();
1436 mVelocityTracker = null;
1437 }
1438 }
1439
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001441 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 final int pointerId = ev.getPointerId(pointerIndex);
1443 if (pointerId == mActivePointerId) {
1444 // This was our active pointer going up. Choose a new
1445 // active pointer and adjust accordingly.
1446 // TODO: Make this decision more intelligent.
1447 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001448 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1449 newPointerIndex);
1450 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 mActivePointerId = ev.getPointerId(newPointerIndex);
1452 if (mVelocityTracker != null) {
1453 mVelocityTracker.clear();
1454 }
1455 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001456 }
1457
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 @Override
1459 public void requestChildFocus(View child, View focused) {
1460 super.requestChildFocus(child, focused);
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001461
1462 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1463 // which results in incorrect navigation
1464 int nextPage = getNextPage();
1465 if (nextPage != mCurrentPage) {
1466 setCurrentPage(nextPage);
1467 }
1468
Adam Cohenae4f1552011-10-20 00:15:42 -07001469 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001470 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001471 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 }
1473 }
1474
Alex Chaub794ea62021-02-03 18:00:30 +00001475 public int getDestinationPage() {
1476 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1477 }
1478
Alex Chaudedbc8a2021-03-17 16:53:26 +00001479 protected int getDestinationPage(int primaryScroll) {
1480 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001481 }
1482
Sunny Goyal228153d2018-01-04 15:35:22 -08001483 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001484 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001485 }
1486
Alex Chaudedbc8a2021-03-17 16:53:26 +00001487 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1488 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001489 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 final int childCount = getChildCount();
1492 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001493 int distanceFromScreenCenter = Math.abs(
1494 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1496 minDistanceFromScreenCenter = distanceFromScreenCenter;
1497 minDistanceFromScreenCenterIndex = i;
1498 }
1499 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001500 return minDistanceFromScreenCenterIndex;
1501 }
1502
Tony Wickham7383d4e2020-07-07 19:25:25 -07001503 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001504 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001505 int halfChildSize = (childSize / 2);
1506 int childCenter = getChildOffset(childIndex) + halfChildSize;
1507 return childCenter - screenCenter;
1508 }
1509
1510 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001511 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1512 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001513 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1514 }
1515
Alex Chaudedbc8a2021-03-17 16:53:26 +00001516 private int getScreenCenter(int primaryScroll) {
1517 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1518 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1519 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1520 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1521 + primaryPivot);
1522 }
1523
Adam Cohend19d3ca2010-09-15 14:43:42 -07001524 protected void snapToDestination() {
Sunny Goyalc39db732021-04-03 01:20:21 -07001525 snapToPage(getDestinationPage(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001526 }
1527
Adam Cohene0f66b52010-11-23 15:06:07 -08001528 // We want the duration of the page snap animation to be influenced by the distance that
1529 // the screen has to travel, however, we don't want this duration to be effected in a
1530 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1531 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001532 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001533 f -= 0.5f; // center the values about 0.
1534 f *= 0.3f * Math.PI / 2.0f;
1535 return (float) Math.sin(f);
1536 }
1537
Winson Chung05a31ed2018-02-08 17:08:43 -08001538 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001539 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001540 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001541
Vinit Nayaka406f722019-12-19 11:50:55 -08001542 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001543 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001544 int duration = 0;
1545
Sunny Goyal4d113a52015-05-27 10:05:28 -07001546 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001547 // If the velocity is low enough, then treat this more as an automatic page advance
1548 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001549 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001550 }
1551
1552 // Here we compute a "distance" that will be used in the computation of the overall
1553 // snap duration. This is a function of the actual distance that needs to be traveled;
1554 // we keep this value close to half screen size in order to reduce the variance in snap
1555 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001556 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001557 float distance = halfScreenSize + halfScreenSize *
1558 distanceInfluenceForSnapDuration(distanceRatio);
1559
1560 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001561 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001562
1563 // we want the page's snap velocity to approximately match the velocity at which the
1564 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001565 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1566 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001567
Sunny Goyalc39db732021-04-03 01:20:21 -07001568 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001569 }
1570
Winson Chung05a31ed2018-02-08 17:08:43 -08001571 public boolean snapToPage(int whichPage) {
1572 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001573 }
1574
Winson Chung05a31ed2018-02-08 17:08:43 -08001575 public boolean snapToPageImmediately(int whichPage) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001576 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001577 }
1578
Winson Chung05a31ed2018-02-08 17:08:43 -08001579 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001580 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001581 }
1582
Sunny Goyalc39db732021-04-03 01:20:21 -07001583 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001584 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001585
Vinit Nayaka406f722019-12-19 11:50:55 -08001586 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001587 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1588 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001589 }
1590
Winson Chung05a31ed2018-02-08 17:08:43 -08001591 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001592 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001593 }
Michael Jurka0142d492010-08-25 17:46:15 -07001594
Sunny Goyalc39db732021-04-03 01:20:21 -07001595 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001596 if (mFirstLayout) {
1597 setCurrentPage(whichPage);
1598 return false;
1599 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001600
Zak Cohen3eeb41d2020-02-14 14:15:13 -08001601 if (FeatureFlags.IS_STUDIO_BUILD) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001602 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1603 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001604 }
1605
Adam Cohen6f127a62014-06-12 14:54:41 -07001606 whichPage = validateNewPage(whichPage);
1607
Adam Cohen7d30a372013-07-01 17:03:59 -07001608 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001609
Winson Chung321e9ee2010-08-09 13:37:56 -07001610 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001611 if (immediate) {
1612 duration = 0;
1613 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001614 duration = Math.abs(delta);
1615 }
1616
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001617 if (duration != 0) {
1618 pageBeginTransition();
1619 }
1620
Adam Cohenf358a4b2013-07-23 16:47:31 -07001621 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001622 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001623 }
Adam Cohenf9618852013-11-08 06:45:03 -08001624
Sunny Goyalc39db732021-04-03 01:20:21 -07001625 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001626 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001627
1628 // Trigger a compute() to finish switching pages if necessary
1629 if (immediate) {
1630 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001631 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001632 }
1633
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001635 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001636 }
1637
Vadim Tryshev98913d02018-05-18 18:41:34 -07001638 public boolean scrollLeft() {
1639 if (getNextPage() > 0) {
1640 snapToPage(getNextPage() - 1);
1641 return true;
1642 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001643 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001644 }
1645
Vadim Tryshev98913d02018-05-18 18:41:34 -07001646 public boolean scrollRight() {
1647 if (getNextPage() < getChildCount() - 1) {
1648 snapToPage(getNextPage() + 1);
1649 return true;
1650 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001651 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001652 }
1653
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001654 @Override
1655 public CharSequence getAccessibilityClassName() {
1656 // Some accessibility services have special logic for ScrollView. Since we provide same
1657 // accessibility info as ScrollView, inform the service to handle use the same way.
1658 return ScrollView.class.getName();
1659 }
1660
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001661 protected boolean isPageOrderFlipped() {
1662 return false;
1663 }
1664
Winson Chung6a0f57d2011-06-29 20:10:49 -07001665 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001666 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001667 @Override
1668 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1669 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001670 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001671 int offset = (mAllowOverScroll ? 0 : 1);
1672 info.setScrollable(getPageCount() > offset);
1673 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001674 info.addAction(pagesFlipped ?
1675 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1676 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1677 info.addAction(mIsRtl ?
1678 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1679 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001680 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001681 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001682 info.addAction(pagesFlipped ?
1683 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1684 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1685 info.addAction(mIsRtl ?
1686 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1687 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001688 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001689 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1690 // Besides disabling the accessibility long-click, this also prevents this view from getting
1691 // accessibility focus.
1692 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001693 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001694 }
1695
1696 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001697 public void sendAccessibilityEvent(int eventType) {
1698 // Don't let the view send real scroll events.
1699 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1700 super.sendAccessibilityEvent(eventType);
1701 }
1702 }
1703
1704 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001705 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1706 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001707 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001708 }
1709
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001710 @Override
1711 public boolean performAccessibilityAction(int action, Bundle arguments) {
1712 if (super.performAccessibilityAction(action, arguments)) {
1713 return true;
1714 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001715 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001716 switch (action) {
1717 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001718 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001719 return true;
1720 }
1721 } break;
1722 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001723 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001724 return true;
1725 }
yingleiw02cc8482019-08-02 16:14:27 -07001726 } break;
1727 case android.R.id.accessibilityActionPageRight: {
1728 if (!mIsRtl) {
1729 return scrollRight();
1730 } else {
1731 return scrollLeft();
1732 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001733 }
yingleiw02cc8482019-08-02 16:14:27 -07001734 case android.R.id.accessibilityActionPageLeft: {
1735 if (!mIsRtl) {
1736 return scrollLeft();
1737 } else {
1738 return scrollRight();
1739 }
1740 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001741 }
1742 return false;
1743 }
1744
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001745 protected boolean canAnnouncePageDescription() {
1746 return true;
1747 }
1748
Adam Cohen0ffac432013-07-10 11:19:26 -07001749 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001750 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001751 getNextPage() + 1, getChildCount());
1752 }
1753
Tony Mak2e564402018-02-27 17:19:34 +00001754 protected float getDownMotionX() {
1755 return mDownMotionX;
1756 }
1757
1758 protected float getDownMotionY() {
1759 return mDownMotionY;
1760 }
1761
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001762 protected interface ComputePageScrollsLogic {
1763
1764 boolean shouldIncludeView(View view);
1765 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001766
1767 public int[] getVisibleChildrenRange() {
1768 float visibleLeft = 0;
1769 float visibleRight = visibleLeft + getMeasuredWidth();
1770 float scaleX = getScaleX();
1771 if (scaleX < 1 && scaleX > 0) {
1772 float mid = getMeasuredWidth() / 2;
1773 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1774 visibleRight = mid + ((visibleRight - mid) / scaleX);
1775 }
1776
1777 int leftChild = -1;
1778 int rightChild = -1;
1779 final int childCount = getChildCount();
1780 for (int i = 0; i < childCount; i++) {
1781 final View child = getPageAt(i);
1782
1783 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1784 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1785 if (leftChild == -1) {
1786 leftChild = i;
1787 }
1788 rightChild = i;
1789 }
1790 }
1791 mTmpIntPair[0] = leftChild;
1792 mTmpIntPair[1] = rightChild;
1793 return mTmpIntPair;
1794 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001795
1796 @Override
1797 public void draw(Canvas canvas) {
1798 super.draw(canvas);
1799 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001800 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001801 }
1802
1803 protected void drawEdgeEffect(Canvas canvas) {
1804 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1805 final int width = getWidth();
1806 final int height = getHeight();
1807 if (!mEdgeGlowLeft.isFinished()) {
1808 final int restoreCount = canvas.save();
1809 canvas.rotate(-90);
1810 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1811 mEdgeGlowLeft.setSize(height, width);
1812 if (mEdgeGlowLeft.draw(canvas)) {
1813 postInvalidateOnAnimation();
1814 }
1815 canvas.restoreToCount(restoreCount);
1816 }
1817 if (!mEdgeGlowRight.isFinished()) {
1818 final int restoreCount = canvas.save();
1819 canvas.rotate(90, width, 0);
1820 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1821
1822 mEdgeGlowRight.setSize(height, width);
1823 if (mEdgeGlowRight.draw(canvas)) {
1824 postInvalidateOnAnimation();
1825 }
1826 canvas.restoreToCount(restoreCount);
1827 }
1828 }
1829 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001830}