blob: e10f974e2ab4d8efc63280a8ff23d68aec7a98e6 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyald0030b02017-12-08 15:07:24 -080019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070027import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070029import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070030import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.ViewGroup;
41import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070045import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070046
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070047import com.android.launcher3.anim.Interpolators;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070048import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070050import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070051import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
Winson Chung6a0f57d2011-06-29 20:10:49 -070053import java.util.ArrayList;
54
Winson Chung321e9ee2010-08-09 13:37:56 -070055/**
56 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070057 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070058 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080059public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070060 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070061 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070062
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070064 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Vadim Tryshevfedca432015-08-19 17:55:02 -070066 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080067 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Sunny Goyalb72d8b22017-07-14 00:02:27 -070069 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070070 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070071
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080073 // The page is moved more than halfway, automatically move to the next page on touch up.
74 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Sunny Goyal8e2133b2015-05-06 13:39:07 -070076 private static final float MAX_SCROLL_PROGRESS = 1.0f;
77
Adam Cohen265b9a62011-12-07 14:37:18 -080078 // The following constants need to be scaled based on density. The scaled versions will be
79 // assigned to the corresponding member variables below.
80 private static final int FLING_THRESHOLD_VELOCITY = 500;
81 private static final int MIN_SNAP_VELOCITY = 1500;
82 private static final int MIN_FLING_VELOCITY = 250;
83
Adam Cohen21cd0022013-10-09 18:57:02 -070084 public static final int INVALID_RESTORE_PAGE = -1001;
85
Adam Cohenf358a4b2013-07-23 16:47:31 -070086 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080087 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070088
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
94
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070097 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070098
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800101 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800102 protected LauncherScroller mScroller;
103 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800105 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
107 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700108 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700109 private float mLastMotionX;
110 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700111 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700112
113 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected final static int TOUCH_STATE_REST = 0;
116 protected final static int TOUCH_STATE_SCROLLING = 1;
117 protected final static int TOUCH_STATE_PREV_PAGE = 2;
118 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700119
Michael Jurka0142d492010-08-25 17:46:15 -0700120 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800121
Michael Jurka7426c422010-11-11 15:23:47 -0800122 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800124 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Michael Jurka5f1c5092010-09-03 14:15:02 -0700126 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka5f1c5092010-09-03 14:15:02 -0700128 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700130 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700131
Sunny Goyal061380a2016-02-29 15:15:03 -0800132 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700133
Adam Cohen8d769d62017-06-23 17:27:38 -0700134 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
135 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
136 // the screens from continuing to translate beyond the normal bounds.
137 protected int mOverScrollX;
138
139 protected int mUnboundedScrollX;
140
Winson Chungd2be3812013-07-16 11:11:32 -0700141 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700142 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800143 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700144
Adam Cohen7d30a372013-07-01 17:03:59 -0700145 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700146 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700147 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700148 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700149
John Spurlock77e1f472013-09-11 10:09:51 -0400150 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800151 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400152
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800153 // Similar to the platform implementation of isLayoutValid();
154 protected boolean mIsLayoutValid;
155
Winson Chung321e9ee2010-08-09 13:37:56 -0700156 public PagedView(Context context) {
157 this(context, null);
158 }
159
160 public PagedView(Context context, AttributeSet attrs) {
161 this(context, attrs, 0);
162 }
163
164 public PagedView(Context context, AttributeSet attrs, int defStyle) {
165 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700166
Adam Cohen9c4949e2010-10-05 12:27:22 -0700167 TypedArray a = context.obtainStyledAttributes(attrs,
168 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700169 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700170 a.recycle();
171
Winson Chung321e9ee2010-08-09 13:37:56 -0700172 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700173 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700174 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 }
176
177 /**
178 * Initializes various states for this workspace.
179 */
Michael Jurka0142d492010-08-25 17:46:15 -0700180 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800181 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700182 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700183 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
185 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700186 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800188
Sunny Goyalcf25b522015-07-09 00:01:18 -0700189 float density = getResources().getDisplayMetrics().density;
190 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
191 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
192 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700193
194 if (Utilities.ATLEAST_OREO) {
195 setDefaultFocusHighlightEnabled(false);
196 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198
Adam Cohenf9618852013-11-08 06:45:03 -0800199 protected void setDefaultInterpolator(Interpolator interpolator) {
200 mDefaultInterpolator = interpolator;
201 mScroller.setInterpolator(mDefaultInterpolator);
202 }
203
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700204 public void initParentViews(View parent) {
205 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800206 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700207 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700208 }
209 }
210
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800211 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700212 return mPageIndicator;
213 }
214
Adam Cohen674531f2013-12-13 15:07:14 -0800215 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700216 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
217 * that the user was on before entering free scroll mode (e.g. the home screen page they
218 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
219 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700221 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700222 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700224
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700225 /**
226 * Returns the index of page to be shown immediately afterwards.
227 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700228 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700229 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
230 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700231
Adam Cohenf9c184a2016-01-15 16:47:43 -0800232 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 return getChildCount();
234 }
235
Sunny Goyal83a8f042015-05-19 12:52:12 -0700236 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 return getChildAt(index);
238 }
239
Adam Cohenae4f1552011-10-20 00:15:42 -0700240 protected int indexToPage(int index) {
241 return index;
242 }
243
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800245 * Updates the scroll of the current page immediately to its final scroll position. We use this
246 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
247 * the previous tab page.
248 */
249 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700250 // If the current page is invalid, just reset the scroll position to zero
251 int newX = 0;
252 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700253 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700254 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800255 scrollTo(newX, 0);
256 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700257 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800258 }
259
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700260 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700261 mScroller.abortAnimation();
262 // We need to clean up the next page here to avoid computeScrollHelper from
263 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700264 if (resetNextPage) {
265 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800266 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700267 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700268 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700269
Tony Wickham8f7ead32016-04-07 18:46:44 -0700270 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271 mScroller.forceFinished(true);
272 // We need to clean up the next page here to avoid computeScrollHelper from
273 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700274 if (resetNextPage) {
275 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800276 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700277 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700278 }
279
Adam Cohen6f127a62014-06-12 14:54:41 -0700280 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700281 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700282 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700283 }
284
Chet Haasebc2f0822012-10-26 17:59:53 -0700285 /**
Winson Chung86f77532010-08-24 11:08:22 -0700286 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700288 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800289 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700290 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800291 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800292 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
293 // the default
294 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800295 return;
296 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700297 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700298 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700299 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700300 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800301 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 }
303
Winson Chung8c87cd82013-07-23 16:20:10 -0700304 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800305 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
306 * has settled.
307 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700308 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800309 updatePageIndicator();
310 }
311
312 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700313 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000314 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700315 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700316 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700317 protected void pageBeginTransition() {
318 if (!mIsPageInTransition) {
319 mIsPageInTransition = true;
320 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700321 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700322 }
323
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700324 protected void pageEndTransition() {
325 if (mIsPageInTransition) {
326 mIsPageInTransition = false;
327 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700328 }
Michael Jurka0142d492010-08-25 17:46:15 -0700329 }
330
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700331 protected boolean isPageInTransition() {
332 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700333 }
334
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700335 /**
336 * Called when the page starts moving as part of the scroll. Subclasses can override this
337 * to provide custom behavior during animation.
338 */
339 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700340 }
341
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700342 /**
343 * Called when the page ends moving as part of the scroll. Subclasses can override this
344 * to provide custom behavior during animation.
345 */
346 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700347 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700348 }
349
Sunny Goyalc86df472016-02-25 09:19:38 -0800350 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700351 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800352 }
353
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800355 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800356 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800357 }
358
359 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700360 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700361 // In free scroll mode, we clamp the scrollX
362 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700363 // If the scroller is trying to move to a location beyond the maximum allowed
364 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700365 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700366 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700367 }
368
Sunny Goyal4d519f22017-10-24 10:32:40 -0700369 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700370 }
371
Adam Cohen8d769d62017-06-23 17:27:38 -0700372 mUnboundedScrollX = x;
373
Sunny Goyal70660032015-05-14 00:07:08 -0700374 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
375 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700376 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700377 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800378 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700379 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700380 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700381 overScroll(x - mMaxScrollX);
382 } else {
383 overScroll(x);
384 }
Adam Cohen68d73932010-11-15 10:50:58 -0800385 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700386 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700387 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800388 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700389 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700390 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700391 overScroll(x);
392 } else {
393 overScroll(x - mMaxScrollX);
394 }
Adam Cohen68d73932010-11-15 10:50:58 -0800395 }
396 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700397 if (mWasInOverscroll) {
398 overScroll(0);
399 mWasInOverscroll = false;
400 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700401 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800402 super.scrollTo(x, y);
403 }
Michael Jurka0142d492010-08-25 17:46:15 -0700404 }
405
Adam Cohen53805212013-10-01 10:39:23 -0700406 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800407 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700408 if (mCurrentPage != getNextPage()) {
409 AccessibilityEvent ev =
410 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700411 ev.setScrollable(true);
412 ev.setScrollX(getScrollX());
413 ev.setScrollY(getScrollY());
414 ev.setMaxScrollX(mMaxScrollX);
415 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700416
Vadim Tryshevf4715972015-05-08 17:21:03 -0700417 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700418 }
Adam Cohen53805212013-10-01 10:39:23 -0700419 }
420 }
421
Michael Jurka0142d492010-08-25 17:46:15 -0700422 // we moved this functionality to a helper function so SmoothPagedView can reuse it
423 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800424 return computeScrollHelper(true);
425 }
426
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700427 protected void announcePageForAccessibility() {
428 if (isAccessibilityEnabled(getContext())) {
429 // Notify the user when the page changes
430 announceForAccessibility(getCurrentPageDescription());
431 }
432 }
433
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800434 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700436 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800437 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700438 || getScrollY() != mScroller.getCurrY()
439 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700440 float scaleX = mFreeScroll ? getScaleX() : 1f;
441 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
442 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700443 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800444 if (shouldInvalidate) {
445 invalidate();
446 }
Michael Jurka0142d492010-08-25 17:46:15 -0700447 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800448 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700449 sendScrollAccessibilityEvent();
450
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700451 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700452 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700453 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700454 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700455
Adam Cohen73aa9752010-11-24 16:26:58 -0800456 // We don't want to trigger a page end moving unless the page has settled
457 // and the user has stopped scrolling
458 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700459 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800460 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700461
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700462 if (canAnnouncePageDescription()) {
463 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700464 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 }
Michael Jurka0142d492010-08-25 17:46:15 -0700466 return false;
467 }
468
469 @Override
470 public void computeScroll() {
471 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 }
473
Sunny Goyalce8809a2018-01-18 14:02:47 -0800474 public int getExpectedHeight() {
475 return getMeasuredHeight();
476 }
477
Adam Cohen410f3cd2013-09-22 12:09:32 -0700478 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800479 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800480 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700481 }
482
Sunny Goyalce8809a2018-01-18 14:02:47 -0800483 public int getExpectedWidth() {
484 return getMeasuredWidth();
485 }
486
Sunny Goyalbc683e92017-12-06 10:25:07 -0800487 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800488 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800489 - mInsets.left - mInsets.right;
490 }
491
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800493 public void requestLayout() {
494 mIsLayoutValid = false;
495 super.requestLayout();
496 }
497
498 @Override
499 public void forceLayout() {
500 mIsLayoutValid = false;
501 super.forceLayout();
502 }
503
504 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700506 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700507 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
508 return;
509 }
510
Adam Cohen7d30a372013-07-01 17:03:59 -0700511 // We measure the dimensions of the PagedView to be larger than the pages so that when we
512 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700513 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
514 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
515 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700516 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700517
Adam Cohen7d30a372013-07-01 17:03:59 -0700518 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
519 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
520 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 }
522
Winson Chung8aad6102012-05-11 16:27:49 -0700523 // Return early if we aren't given a proper dimension
524 if (widthSize <= 0 || heightSize <= 0) {
525 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
526 return;
527 }
528
Winson Chung321e9ee2010-08-09 13:37:56 -0700529 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700530 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700531 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700532
Sunny Goyalac00cba2017-11-13 15:58:01 -0800533 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800534 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800535 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800536 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700537
Sunny Goyalac00cba2017-11-13 15:58:01 -0800538 // measureChildren takes accounts for content padding, we only need to care about extra
539 // space due to insets.
540 measureChildren(myWidthSpec, myHeightSpec);
541 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800542 }
543
Sunny Goyalcf25b522015-07-09 00:01:18 -0700544 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700546 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800547 mIsLayoutValid = true;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700548 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700549 return;
550 }
551
Winson Chung785d2eb2011-04-14 16:08:02 -0700552 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700554
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800555 boolean pageScrollChanged = false;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700556 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
557 mPageScrolls = new int[childCount];
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800558 pageScrollChanged = true;
Adam Cohenedb40762013-07-18 16:45:45 -0700559 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700560 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
561 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700563
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700564 final LayoutTransition transition = getLayoutTransition();
565 // If the transition is running defer updating max scroll, as some empty pages could
566 // still be present, and a max scroll change could cause sudden jumps in scroll.
567 if (transition != null && transition.isRunning()) {
568 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
569
570 @Override
571 public void startTransition(LayoutTransition transition, ViewGroup container,
572 View view, int transitionType) { }
573
574 @Override
575 public void endTransition(LayoutTransition transition, ViewGroup container,
576 View view, int transitionType) {
577 // Wait until all transitions are complete.
578 if (!transition.isRunning()) {
579 transition.removeTransitionListener(this);
580 updateMaxScrollX();
581 }
582 }
583 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700584 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700585 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700586 }
587
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700588 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
589 updateCurrentPageScroll();
590 mFirstLayout = false;
591 }
592
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800593 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700594 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700595 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700596 mChildCountOnLastLayout = childCount;
Winson Chunge3193b92010-09-10 11:44:42 -0700597 }
598
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700599 /**
600 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
601 * of {@code outPageScrolls} should be same as the the childCount
602 *
603 */
604 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
605 ComputePageScrollsLogic scrollLogic) {
606 final int childCount = getChildCount();
607
608 final int startIndex = mIsRtl ? childCount - 1 : 0;
609 final int endIndex = mIsRtl ? -1 : childCount;
610 final int delta = mIsRtl ? -1 : 1;
611
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700612 final int verticalCenter = (getPaddingTop() + getMeasuredHeight() + mInsets.top
613 - mInsets.bottom - getPaddingBottom()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700614
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700615 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700616 boolean pageScrollChanged = false;
617
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700618 for (int i = startIndex, childLeft = scrollOffsetLeft + offsetForPageScrolls();
619 i != endIndex;
620 i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700621 final View child = getPageAt(i);
622 if (scrollLogic.shouldIncludeView(child)) {
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700623 final int childTop = verticalCenter - child.getMeasuredHeight() / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700624 final int childWidth = child.getMeasuredWidth();
625
626 if (layoutChildren) {
627 final int childHeight = child.getMeasuredHeight();
628 child.layout(childLeft, childTop,
629 childLeft + child.getMeasuredWidth(), childTop + childHeight);
630 }
631
632 final int pageScroll = childLeft - scrollOffsetLeft;
633 if (outPageScrolls[i] != pageScroll) {
634 pageScrollChanged = true;
635 outPageScrolls[i] = pageScroll;
636 }
637
638 childLeft += childWidth + mPageSpacing + getChildGap();
639 }
640 }
641 return pageScrollChanged;
642 }
643
Sunny Goyala1fbd842015-05-20 13:40:27 -0700644 protected int getChildGap() {
645 return 0;
646 }
647
Sunny Goyal4d519f22017-10-24 10:32:40 -0700648 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700649 mMaxScrollX = computeMaxScrollX();
650 }
651
652 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700653 int childCount = getChildCount();
654 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700655 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700656 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700657 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700658 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700659 }
660 }
661
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700662 protected int offsetForPageScrolls() {
663 return 0;
664 }
665
Adam Cohen3b185e22013-10-29 14:45:58 -0700666 public void setPageSpacing(int pageSpacing) {
667 mPageSpacing = pageSpacing;
668 requestLayout();
669 }
670
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800671 private void dispatchPageCountChanged() {
672 if (mPageIndicator != null) {
673 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700674 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700675 // This ensures that when children are added, they get the correct transforms / alphas
676 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700677 invalidate();
678 }
679
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800681 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800682 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800683 dispatchPageCountChanged();
684 }
685
686 @Override
687 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800688 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700689 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800690 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700691 }
692
Adam Cohen73894962011-10-31 13:17:17 -0700693 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700694 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800695 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700696 }
697
Winson Chung321e9ee2010-08-09 13:37:56 -0700698 @Override
699 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700700 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700701 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700702 if (immediate) {
703 setCurrentPage(page);
704 } else {
705 snapToPage(page);
706 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700707 return true;
708 }
709 return false;
710 }
711
712 @Override
713 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700714 int focusablePage;
715 if (mNextPage != INVALID_PAGE) {
716 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700718 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700719 }
Winson Chung86f77532010-08-24 11:08:22 -0700720 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700721 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700722 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 }
724 return false;
725 }
726
727 @Override
728 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800729 if (super.dispatchUnhandledMove(focused, direction)) {
730 return true;
731 }
732
733 if (mIsRtl) {
734 if (direction == View.FOCUS_LEFT) {
735 direction = View.FOCUS_RIGHT;
736 } else if (direction == View.FOCUS_RIGHT) {
737 direction = View.FOCUS_LEFT;
738 }
739 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700741 if (getCurrentPage() > 0) {
742 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 return true;
744 }
745 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700746 if (getCurrentPage() < getPageCount() - 1) {
747 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 return true;
749 }
750 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800751 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700752 }
753
754 @Override
755 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700756 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
757 return;
758 }
759
Adam Cohen0ffac432013-07-10 11:19:26 -0700760 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700761 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700762 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 }
764 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700765 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700766 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 }
768 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700769 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700770 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 }
772 }
773 }
774
775 /**
776 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700777 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 *
Winson Chung86f77532010-08-24 11:08:22 -0700779 * This happens when live folders requery, and if they're off page, they
780 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 */
782 @Override
783 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700784 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 View v = focused;
786 while (true) {
787 if (v == current) {
788 super.focusableViewAvailable(focused);
789 return;
790 }
791 if (v == this) {
792 return;
793 }
794 ViewParent parent = v.getParent();
795 if (parent instanceof View) {
796 v = (View)v.getParent();
797 } else {
798 return;
799 }
800 }
801 }
802
803 /**
804 * {@inheritDoc}
805 */
806 @Override
807 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
808 if (disallowIntercept) {
809 // We need to make sure to cancel our long press if
810 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700811 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700812 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 }
814 super.requestDisallowInterceptTouchEvent(disallowIntercept);
815 }
816
Adam Cohen7d30a372013-07-01 17:03:59 -0700817 /** Returns whether x and y originated within the buffered viewport */
818 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800819 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700820 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700821 }
822
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 @Override
824 public boolean onInterceptTouchEvent(MotionEvent ev) {
825 /*
826 * This method JUST determines whether we want to intercept the motion.
827 * If we return true, onTouchEvent will be called and we do the actual
828 * scrolling there.
829 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800830 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831
Winson Chung45e1d6e2010-11-09 17:19:49 -0800832 // Skip touch handling if there are no pages to swipe
833 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
834
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 /*
836 * Shortcut the most recurring case: the user is in the dragging
837 * state and he is moving his finger. We want to intercept this
838 * motion.
839 */
840 final int action = ev.getAction();
841 if ((action == MotionEvent.ACTION_MOVE) &&
842 (mTouchState == TOUCH_STATE_SCROLLING)) {
843 return true;
844 }
845
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 switch (action & MotionEvent.ACTION_MASK) {
847 case MotionEvent.ACTION_MOVE: {
848 /*
849 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
850 * whether the user has moved far enough from his original down touch.
851 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700852 if (mActivePointerId != INVALID_POINTER) {
853 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700854 }
855 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
856 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
857 // i.e. fall through to the next case (don't break)
858 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
859 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700860 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 }
862
863 case MotionEvent.ACTION_DOWN: {
864 final float x = ev.getX();
865 final float y = ev.getY();
866 // Remember location of down touch
867 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700868 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800870 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800871 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700873
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 /*
875 * If being flinged and user touches the screen, initiate drag;
876 * otherwise don't. mScroller.isFinished should be false when
877 * being flinged.
878 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700879 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000880 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800881
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700882 if (finishedScrolling) {
883 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800884 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000885 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700886 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800887 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700888 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700889 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
890 mTouchState = TOUCH_STATE_SCROLLING;
891 } else {
892 mTouchState = TOUCH_STATE_REST;
893 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700894 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700895
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 break;
897 }
898
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800900 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700901 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 break;
903
904 case MotionEvent.ACTION_POINTER_UP:
905 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800906 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 break;
908 }
909
910 /*
911 * The only time we want to intercept motion events is if we are in the
912 * drag mode.
913 */
914 return mTouchState != TOUCH_STATE_REST;
915 }
916
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800917 public boolean isHandlingTouch() {
918 return mTouchState != TOUCH_STATE_REST;
919 }
920
Adam Cohenf8d28232011-02-01 21:47:00 -0800921 protected void determineScrollingStart(MotionEvent ev) {
922 determineScrollingStart(ev, 1.0f);
923 }
924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 /*
926 * Determines if we should change the touch state to start scrolling after the
927 * user moves their touch point too far.
928 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800929 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700930 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700932 if (pointerIndex == -1) return;
933
934 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 final float x = ev.getX(pointerIndex);
936 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -0700937 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
938
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700940
Adam Cohenf8d28232011-02-01 21:47:00 -0800941 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700943
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700944 if (xMoved) {
945 // Scroll if the user moved far enough along the X axis
946 mTouchState = TOUCH_STATE_SCROLLING;
947 mTotalMotionX += Math.abs(mLastMotionX - x);
948 mLastMotionX = x;
949 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700950 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700951 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800952 // Stop listening for things like pinches.
953 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800954 }
955 }
956
957 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700958 // Try canceling the long press. It could also have been scheduled
959 // by a distant descendant, so use the mAllowLongPress flag to block
960 // everything
961 final View currentPage = getPageAt(mCurrentPage);
962 if (currentPage != null) {
963 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 }
965 }
966
Adam Cohenb5ba0972011-09-07 18:02:31 -0700967 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800968 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700969
Adam Cohenedb40762013-07-18 16:45:45 -0700970 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700971 int count = getChildCount();
972
973 final int totalDistance;
974
975 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700976 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700977 adjacentPage = page - 1;
978 }
979
980 if (adjacentPage < 0 || adjacentPage > count - 1) {
981 totalDistance = v.getMeasuredWidth() + mPageSpacing;
982 } else {
983 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
984 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700985
986 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700987 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
988 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700989 return scrollProgress;
990 }
991
Adam Cohenedb40762013-07-18 16:45:45 -0700992 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700993 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -0700994 return 0;
995 } else {
996 return mPageScrolls[index];
997 }
998 }
999
Adam Cohen564a2e72013-10-09 14:47:32 -07001000 // While layout transitions are occurring, a child's position may stray from its baseline
1001 // position. This method returns the magnitude of this stray at any given time.
1002 public int getLayoutTransitionOffsetForPage(int index) {
1003 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1004 return 0;
1005 } else {
1006 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001007
Sunny Goyal228153d2018-01-04 15:35:22 -08001008 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1009 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001010 return (int) (child.getX() - baselineX);
1011 }
1012 }
1013
Adam Cohenb5ba0972011-09-07 18:02:31 -07001014 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001015 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001016
Sunny Goyal228153d2018-01-04 15:35:22 -08001017 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001018 if (amount < 0) {
1019 mOverScrollX = overScrollAmount;
1020 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001021 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001022 mOverScrollX = mMaxScrollX + overScrollAmount;
1023 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001024 }
1025 invalidate();
1026 }
1027
Adam Cohenb5ba0972011-09-07 18:02:31 -07001028 protected void overScroll(float amount) {
1029 dampedOverScroll(amount);
1030 }
1031
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001032
1033 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001034 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001035 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001036 }
1037
Adam Cohenf9618852013-11-08 06:45:03 -08001038 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001039 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001040 mFreeScroll = freeScroll;
1041
Adam Cohenf9618852013-11-08 06:45:03 -08001042 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001043 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001044 } else if (wasFreeScroll) {
1045 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001046 }
1047
1048 setEnableOverscroll(!freeScroll);
1049 }
1050
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001051 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001052 mAllowOverScroll = enable;
1053 }
1054
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 @Override
1056 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001057 super.onTouchEvent(ev);
1058
Winson Chung45e1d6e2010-11-09 17:19:49 -08001059 // Skip touch handling if there are no pages to swipe
1060 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1061
Michael Jurkab8f06722010-10-10 15:58:46 -07001062 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001063
1064 final int action = ev.getAction();
1065
1066 switch (action & MotionEvent.ACTION_MASK) {
1067 case MotionEvent.ACTION_DOWN:
1068 /*
1069 * If being flinged and user touches, stop the fling. isFinished
1070 * will be false if being flinged.
1071 */
1072 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001073 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 }
1075
1076 // Remember where the motion event started
1077 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001078 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001079 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001080 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001082
Michael Jurka0142d492010-08-25 17:46:15 -07001083 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001084 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001085 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001086 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001087 break;
1088
1089 case MotionEvent.ACTION_MOVE:
1090 if (mTouchState == TOUCH_STATE_SCROLLING) {
1091 // Scroll to follow the motion event
1092 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001093
1094 if (pointerIndex == -1) return true;
1095
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001097 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001098
Adam Cohenaefd4e12011-02-14 16:39:38 -08001099 mTotalMotionX += Math.abs(deltaX);
1100
Winson Chungc0844aa2011-02-02 15:25:58 -08001101 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1102 // keep the remainder because we are actually testing if we've moved from the last
1103 // scrolled position (which is discrete).
1104 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001105 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001106 mLastMotionX = x;
1107 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001108 } else {
1109 awakenScrollBars();
1110 }
Adam Cohen564976a2010-10-13 18:52:07 -07001111 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001112 determineScrollingStart(ev);
1113 }
1114 break;
1115
1116 case MotionEvent.ACTION_UP:
1117 if (mTouchState == TOUCH_STATE_SCROLLING) {
1118 final int activePointerId = mActivePointerId;
1119 final int pointerIndex = ev.findPointerIndex(activePointerId);
1120 final float x = ev.getX(pointerIndex);
1121 final VelocityTracker velocityTracker = mVelocityTracker;
1122 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1123 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001124 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001125 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001126 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1127 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001128
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001129 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001130 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001131
Adam Cohenf358a4b2013-07-23 16:47:31 -07001132 if (!mFreeScroll) {
1133 // In the case that the page is moved far to one direction and then is flung
1134 // in the opposite direction, we use a threshold to determine whether we should
1135 // just return to the starting page, or if we should skip one further.
1136 boolean returnToOriginalPage = false;
1137 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1138 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1139 returnToOriginalPage = true;
1140 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001141
Adam Cohenf358a4b2013-07-23 16:47:31 -07001142 int finalPage;
1143 // We give flings precedence over large moves, which is why we short-circuit our
1144 // test for a large move if a fling has been registered. That is, a large
1145 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001146 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1147 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001148 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1149 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1150 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1151 snapToPageWithVelocity(finalPage, velocityX);
1152 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1153 (isFling && isVelocityXLeft)) &&
1154 mCurrentPage < getChildCount() - 1) {
1155 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1156 snapToPageWithVelocity(finalPage, velocityX);
1157 } else {
1158 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001159 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001161 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001162 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001163 }
1164
1165 float scaleX = getScaleX();
1166 int vX = (int) (-velocityX * scaleX);
1167 int initialScrollX = (int) (getScrollX() * scaleX);
1168
Adam Cohenf9618852013-11-08 06:45:03 -08001169 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001170 mScroller.fling(initialScrollX,
1171 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001172 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1173 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1174 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1175 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vadim Trysheva9283be2018-05-01 19:22:37 -07001176 if (mSettleOnPageInFreeScroll && unscaledScrollX > 0
1177 && unscaledScrollX < mMaxScrollX) {
1178 // If scrolling ends in the half of the added space that is closer to the
1179 // end, settle to the end. Otherwise snap to the nearest page.
1180 // If flinging past one of the ends, don't change the velocity as it will
1181 // get stopped at the end anyway.
1182 final int finalX = unscaledScrollX < firstPageScroll / 2 ?
1183 0 :
1184 unscaledScrollX > (lastPageScroll + mMaxScrollX) / 2 ?
1185 mMaxScrollX :
1186 getScrollForPage(mNextPage);
1187
1188 mScroller.setFinalX((int) (finalX * getScaleX()));
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001189 // Ensure the scroll/snap doesn't happen too fast;
1190 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1191 - mScroller.getDuration();
1192 if (extraScrollDuration > 0) {
1193 mScroller.extendDuration(extraScrollDuration);
1194 }
1195 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001196 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001198 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001199 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1200 // at this point we have not moved beyond the touch slop
1201 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1202 // we can just page
1203 int nextPage = Math.max(0, mCurrentPage - 1);
1204 if (nextPage != mCurrentPage) {
1205 snapToPage(nextPage);
1206 } else {
1207 snapToDestination();
1208 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001209 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 // at this point we have not moved beyond the touch slop
1211 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1212 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001213 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1214 if (nextPage != mCurrentPage) {
1215 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 } else {
1217 snapToDestination();
1218 }
1219 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001220
Adam Cohen7d30a372013-07-01 17:03:59 -07001221 // End any intermediate reordering states
1222 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 break;
1224
1225 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001226 if (mTouchState == TOUCH_STATE_SCROLLING) {
1227 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001228 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001229 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001230 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 break;
1232
1233 case MotionEvent.ACTION_POINTER_UP:
1234 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001235 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 break;
1237 }
1238
1239 return true;
1240 }
1241
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001242 protected boolean shouldFlingForVelocity(int velocityX) {
1243 return Math.abs(velocityX) > mFlingThresholdVelocity;
1244 }
1245
Adam Cohen7d30a372013-07-01 17:03:59 -07001246 private void resetTouchState() {
1247 releaseVelocityTracker();
Adam Cohen7d30a372013-07-01 17:03:59 -07001248 mTouchState = TOUCH_STATE_REST;
1249 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001250 }
1251
Adam Cohenc2d6e892014-10-16 09:49:24 -07001252 /**
1253 * Triggered by scrolling via touch
1254 */
1255 protected void onScrollInteractionBegin() {
1256 }
1257
1258 protected void onScrollInteractionEnd() {
1259 }
1260
Winson Chung185d7162011-02-28 13:47:29 -08001261 @Override
1262 public boolean onGenericMotionEvent(MotionEvent event) {
1263 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1264 switch (event.getAction()) {
1265 case MotionEvent.ACTION_SCROLL: {
1266 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1267 final float vscroll;
1268 final float hscroll;
1269 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1270 vscroll = 0;
1271 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1272 } else {
1273 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1274 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1275 }
1276 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001277 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001278 : (hscroll > 0 || vscroll > 0);
1279 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001280 scrollRight();
1281 } else {
1282 scrollLeft();
1283 }
1284 return true;
1285 }
1286 }
1287 }
1288 }
1289 return super.onGenericMotionEvent(event);
1290 }
1291
Michael Jurkab8f06722010-10-10 15:58:46 -07001292 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1293 if (mVelocityTracker == null) {
1294 mVelocityTracker = VelocityTracker.obtain();
1295 }
1296 mVelocityTracker.addMovement(ev);
1297 }
1298
1299 private void releaseVelocityTracker() {
1300 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001301 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001302 mVelocityTracker.recycle();
1303 mVelocityTracker = null;
1304 }
1305 }
1306
Winson Chung321e9ee2010-08-09 13:37:56 -07001307 private void onSecondaryPointerUp(MotionEvent ev) {
1308 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1309 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1310 final int pointerId = ev.getPointerId(pointerIndex);
1311 if (pointerId == mActivePointerId) {
1312 // This was our active pointer going up. Choose a new
1313 // active pointer and adjust accordingly.
1314 // TODO: Make this decision more intelligent.
1315 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1316 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001317 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 mActivePointerId = ev.getPointerId(newPointerIndex);
1319 if (mVelocityTracker != null) {
1320 mVelocityTracker.clear();
1321 }
1322 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001323 }
1324
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 @Override
1326 public void requestChildFocus(View child, View focused) {
1327 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001328 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001329 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001330 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 }
1332 }
1333
Sunny Goyal228153d2018-01-04 15:35:22 -08001334 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001335 return getPageNearestToCenterOfScreen(getScrollX());
1336 }
1337
1338 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001339 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001340 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 final int childCount = getChildCount();
1343 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001344 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001345 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001347 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1349 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1350 minDistanceFromScreenCenter = distanceFromScreenCenter;
1351 minDistanceFromScreenCenterIndex = i;
1352 }
1353 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001354 return minDistanceFromScreenCenterIndex;
1355 }
1356
1357 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001358 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1359 }
1360
1361 protected boolean isInOverScroll() {
1362 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1363 }
1364
1365 protected int getPageSnapDuration() {
1366 if (isInOverScroll()) {
1367 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1368 }
1369 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 }
1371
Adam Cohene0f66b52010-11-23 15:06:07 -08001372 // We want the duration of the page snap animation to be influenced by the distance that
1373 // the screen has to travel, however, we don't want this duration to be effected in a
1374 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1375 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001376 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001377 f -= 0.5f; // center the values about 0.
1378 f *= 0.3f * Math.PI / 2.0f;
1379 return (float) Math.sin(f);
1380 }
1381
Winson Chung05a31ed2018-02-08 17:08:43 -08001382 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001383 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001384 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001385
Adam Cohenedb40762013-07-18 16:45:45 -07001386 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001387 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001388 int duration = 0;
1389
Sunny Goyal4d113a52015-05-27 10:05:28 -07001390 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001391 // If the velocity is low enough, then treat this more as an automatic page advance
1392 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001393 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001394 }
1395
1396 // Here we compute a "distance" that will be used in the computation of the overall
1397 // snap duration. This is a function of the actual distance that needs to be traveled;
1398 // we keep this value close to half screen size in order to reduce the variance in snap
1399 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001400 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001401 float distance = halfScreenSize + halfScreenSize *
1402 distanceInfluenceForSnapDuration(distanceRatio);
1403
1404 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001405 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001406
1407 // we want the page's snap velocity to approximately match the velocity at which the
1408 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001409 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1410 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001411
Winson Chung05a31ed2018-02-08 17:08:43 -08001412 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001413 }
1414
Winson Chung05a31ed2018-02-08 17:08:43 -08001415 public boolean snapToPage(int whichPage) {
1416 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001417 }
1418
Winson Chung05a31ed2018-02-08 17:08:43 -08001419 public boolean snapToPageImmediately(int whichPage) {
1420 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001421 }
1422
Winson Chung05a31ed2018-02-08 17:08:43 -08001423 public boolean snapToPage(int whichPage, int duration) {
1424 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001425 }
1426
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001427 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001428 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001429 }
1430
Winson Chung05a31ed2018-02-08 17:08:43 -08001431 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001432 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001433 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001434
Adam Cohenedb40762013-07-18 16:45:45 -07001435 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001436 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001437 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001438 }
1439
Winson Chung05a31ed2018-02-08 17:08:43 -08001440 protected boolean snapToPage(int whichPage, int delta, int duration) {
1441 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 }
Michael Jurka0142d492010-08-25 17:46:15 -07001443
Winson Chung05a31ed2018-02-08 17:08:43 -08001444 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001445 TimeInterpolator interpolator) {
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001446
1447 if (FeatureFlags.IS_DOGFOOD_BUILD) {
1448 duration *= Settings.System.getFloat(getContext().getContentResolver(),
1449 Settings.System.WINDOW_ANIMATION_SCALE, 1);
1450 }
1451
Adam Cohen6f127a62014-06-12 14:54:41 -07001452 whichPage = validateNewPage(whichPage);
1453
Adam Cohen7d30a372013-07-01 17:03:59 -07001454 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001455
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001457 if (immediate) {
1458 duration = 0;
1459 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 duration = Math.abs(delta);
1461 }
1462
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001463 if (duration != 0) {
1464 pageBeginTransition();
1465 }
1466
Adam Cohenf358a4b2013-07-23 16:47:31 -07001467 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001468 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001469 }
Adam Cohenf9618852013-11-08 06:45:03 -08001470
1471 if (interpolator != null) {
1472 mScroller.setInterpolator(interpolator);
1473 } else {
1474 mScroller.setInterpolator(mDefaultInterpolator);
1475 }
1476
Sunny Goyalc86df472016-02-25 09:19:38 -08001477 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001478
Adam Cohen674531f2013-12-13 15:07:14 -08001479 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001480
1481 // Trigger a compute() to finish switching pages if necessary
1482 if (immediate) {
1483 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001484 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001485 }
1486
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001488 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 }
1490
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001492 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 }
1494
1495 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001496 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 }
1498
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001499 @Override
1500 public CharSequence getAccessibilityClassName() {
1501 // Some accessibility services have special logic for ScrollView. Since we provide same
1502 // accessibility info as ScrollView, inform the service to handle use the same way.
1503 return ScrollView.class.getName();
1504 }
1505
Winson Chung6a0f57d2011-06-29 20:10:49 -07001506 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001507 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001508 @Override
1509 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1510 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001511 info.setScrollable(getPageCount() > 1);
1512 if (getCurrentPage() < getPageCount() - 1) {
1513 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1514 }
1515 if (getCurrentPage() > 0) {
1516 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1517 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001518
1519 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1520 // Besides disabling the accessibility long-click, this also prevents this view from getting
1521 // accessibility focus.
1522 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001523 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001524 }
1525
1526 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001527 public void sendAccessibilityEvent(int eventType) {
1528 // Don't let the view send real scroll events.
1529 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1530 super.sendAccessibilityEvent(eventType);
1531 }
1532 }
1533
1534 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001535 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1536 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001537 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001538 }
1539
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001540 @Override
1541 public boolean performAccessibilityAction(int action, Bundle arguments) {
1542 if (super.performAccessibilityAction(action, arguments)) {
1543 return true;
1544 }
1545 switch (action) {
1546 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1547 if (getCurrentPage() < getPageCount() - 1) {
1548 scrollRight();
1549 return true;
1550 }
1551 } break;
1552 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1553 if (getCurrentPage() > 0) {
1554 scrollLeft();
1555 return true;
1556 }
1557 } break;
1558 }
1559 return false;
1560 }
1561
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001562 protected boolean canAnnouncePageDescription() {
1563 return true;
1564 }
1565
Adam Cohen0ffac432013-07-10 11:19:26 -07001566 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001567 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001568 getNextPage() + 1, getChildCount());
1569 }
1570
Tony Mak2e564402018-02-27 17:19:34 +00001571 protected float getDownMotionX() {
1572 return mDownMotionX;
1573 }
1574
1575 protected float getDownMotionY() {
1576 return mDownMotionY;
1577 }
1578
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001579 protected interface ComputePageScrollsLogic {
1580
1581 boolean shouldIncludeView(View view);
1582 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001583}