blob: adca3278452d9f215d3e82e6490079bb1f337cf5 [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;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080032import android.view.InputDevice;
33import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.MotionEvent;
35import android.view.VelocityTracker;
36import android.view.View;
37import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070039import android.view.ViewGroup;
40import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityEvent;
42import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080043import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070044import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070045
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070046import com.android.launcher3.anim.Interpolators;
Tony Wickhamf549dab2016-05-16 09:54:06 -070047import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070048import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070049import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070050
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080057public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070058 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070060
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070062 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070063
Vadim Tryshevfedca432015-08-19 17:55:02 -070064 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080065 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Sunny Goyalb72d8b22017-07-14 00:02:27 -070067 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070068 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070069
Adam Cohenb64cb5a2011-02-15 13:53:42 -080070 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080071 // The page is moved more than halfway, automatically move to the next page on touch up.
72 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080073
Sunny Goyal8e2133b2015-05-06 13:39:07 -070074 private static final float MAX_SCROLL_PROGRESS = 1.0f;
75
Adam Cohen265b9a62011-12-07 14:37:18 -080076 // The following constants need to be scaled based on density. The scaled versions will be
77 // assigned to the corresponding member variables below.
78 private static final int FLING_THRESHOLD_VELOCITY = 500;
79 private static final int MIN_SNAP_VELOCITY = 1500;
80 private static final int MIN_FLING_VELOCITY = 250;
81
Adam Cohen21cd0022013-10-09 18:57:02 -070082 public static final int INVALID_RESTORE_PAGE = -1001;
83
Adam Cohenf358a4b2013-07-23 16:47:31 -070084 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080085 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070086
Adam Cohen265b9a62011-12-07 14:37:18 -080087 protected int mFlingThresholdVelocity;
88 protected int mMinFlingVelocity;
89 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070090
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected boolean mFirstLayout = true;
92
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070095 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070096
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -080099 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800100 protected LauncherScroller mScroller;
101 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800103 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104
105 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700106 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700107 private float mLastMotionX;
108 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700109 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700110
111 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected final static int TOUCH_STATE_REST = 0;
114 protected final static int TOUCH_STATE_SCROLLING = 1;
115 protected final static int TOUCH_STATE_PREV_PAGE = 2;
116 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700117
Michael Jurka0142d492010-08-25 17:46:15 -0700118 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800119
Michael Jurka7426c422010-11-11 15:23:47 -0800120 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800122 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Michael Jurka5f1c5092010-09-03 14:15:02 -0700124 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Michael Jurka5f1c5092010-09-03 14:15:02 -0700126 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700128 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700129
Sunny Goyal061380a2016-02-29 15:15:03 -0800130 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700131
Adam Cohen8d769d62017-06-23 17:27:38 -0700132 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
133 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
134 // the screens from continuing to translate beyond the normal bounds.
135 protected int mOverScrollX;
136
137 protected int mUnboundedScrollX;
138
Winson Chungd2be3812013-07-16 11:11:32 -0700139 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800141 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700142
Adam Cohen7d30a372013-07-01 17:03:59 -0700143 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700144 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700145 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700146 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700147
John Spurlock77e1f472013-09-11 10:09:51 -0400148 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800149 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400150
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800151 // Similar to the platform implementation of isLayoutValid();
152 protected boolean mIsLayoutValid;
153
Winson Chung321e9ee2010-08-09 13:37:56 -0700154 public PagedView(Context context) {
155 this(context, null);
156 }
157
158 public PagedView(Context context, AttributeSet attrs) {
159 this(context, attrs, 0);
160 }
161
162 public PagedView(Context context, AttributeSet attrs, int defStyle) {
163 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700164
Adam Cohen9c4949e2010-10-05 12:27:22 -0700165 TypedArray a = context.obtainStyledAttributes(attrs,
166 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700167 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700168 a.recycle();
169
Winson Chung321e9ee2010-08-09 13:37:56 -0700170 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700171 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700172 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 }
174
175 /**
176 * Initializes various states for this workspace.
177 */
Michael Jurka0142d492010-08-25 17:46:15 -0700178 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800179 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700180 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700181 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
183 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700185 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800186
Sunny Goyalcf25b522015-07-09 00:01:18 -0700187 float density = getResources().getDisplayMetrics().density;
188 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
189 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
190 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700191
192 if (Utilities.ATLEAST_OREO) {
193 setDefaultFocusHighlightEnabled(false);
194 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700195 }
196
Adam Cohenf9618852013-11-08 06:45:03 -0800197 protected void setDefaultInterpolator(Interpolator interpolator) {
198 mDefaultInterpolator = interpolator;
199 mScroller.setInterpolator(mDefaultInterpolator);
200 }
201
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700202 public void initParentViews(View parent) {
203 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800204 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700205 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700206 }
207 }
208
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800209 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700210 return mPageIndicator;
211 }
212
Adam Cohen674531f2013-12-13 15:07:14 -0800213 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700214 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
215 * that the user was on before entering free scroll mode (e.g. the home screen page they
216 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
217 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700219 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700220 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700222
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700223 /**
224 * Returns the index of page to be shown immediately afterwards.
225 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700226 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700227 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
228 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700229
Adam Cohenf9c184a2016-01-15 16:47:43 -0800230 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 return getChildCount();
232 }
233
Sunny Goyal83a8f042015-05-19 12:52:12 -0700234 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 return getChildAt(index);
236 }
237
Adam Cohenae4f1552011-10-20 00:15:42 -0700238 protected int indexToPage(int index) {
239 return index;
240 }
241
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800243 * Updates the scroll of the current page immediately to its final scroll position. We use this
244 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
245 * the previous tab page.
246 */
247 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700248 // If the current page is invalid, just reset the scroll position to zero
249 int newX = 0;
250 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700251 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700252 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800253 scrollTo(newX, 0);
254 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700255 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800256 }
257
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700258 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700259 mScroller.abortAnimation();
260 // We need to clean up the next page here to avoid computeScrollHelper from
261 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700262 if (resetNextPage) {
263 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800264 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700265 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700266 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700267
Tony Wickham8f7ead32016-04-07 18:46:44 -0700268 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700269 mScroller.forceFinished(true);
270 // We need to clean up the next page here to avoid computeScrollHelper from
271 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700272 if (resetNextPage) {
273 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800274 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700275 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700276 }
277
Adam Cohen6f127a62014-06-12 14:54:41 -0700278 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700279 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700280 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700281 }
282
Chet Haasebc2f0822012-10-26 17:59:53 -0700283 /**
Winson Chung86f77532010-08-24 11:08:22 -0700284 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700286 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800287 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700288 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800289 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800290 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
291 // the default
292 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800293 return;
294 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700295 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700296 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700297 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700298 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800299 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 }
301
Winson Chung8c87cd82013-07-23 16:20:10 -0700302 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800303 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
304 * has settled.
305 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700306 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800307 updatePageIndicator();
308 }
309
310 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700311 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000312 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700313 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700315 protected void pageBeginTransition() {
316 if (!mIsPageInTransition) {
317 mIsPageInTransition = true;
318 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700319 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700320 }
321
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700322 protected void pageEndTransition() {
323 if (mIsPageInTransition) {
324 mIsPageInTransition = false;
325 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700326 }
Michael Jurka0142d492010-08-25 17:46:15 -0700327 }
328
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700329 protected boolean isPageInTransition() {
330 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700331 }
332
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700333 /**
334 * Called when the page starts moving as part of the scroll. Subclasses can override this
335 * to provide custom behavior during animation.
336 */
337 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700338 }
339
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700340 /**
341 * Called when the page ends moving as part of the scroll. Subclasses can override this
342 * to provide custom behavior during animation.
343 */
344 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700345 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700346 }
347
Sunny Goyalc86df472016-02-25 09:19:38 -0800348 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700349 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800350 }
351
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800353 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800354 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800355 }
356
357 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700358 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700359 // In free scroll mode, we clamp the scrollX
360 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700361 // If the scroller is trying to move to a location beyond the maximum allowed
362 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700363 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700364 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700365 }
366
Sunny Goyal4d519f22017-10-24 10:32:40 -0700367 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700368 }
369
Adam Cohen8d769d62017-06-23 17:27:38 -0700370 mUnboundedScrollX = x;
371
Sunny Goyal70660032015-05-14 00:07:08 -0700372 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
373 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700374 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700375 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800376 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700377 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700378 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700379 overScroll(x - mMaxScrollX);
380 } else {
381 overScroll(x);
382 }
Adam Cohen68d73932010-11-15 10:50:58 -0800383 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700384 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700385 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800386 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700387 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700388 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700389 overScroll(x);
390 } else {
391 overScroll(x - mMaxScrollX);
392 }
Adam Cohen68d73932010-11-15 10:50:58 -0800393 }
394 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700395 if (mWasInOverscroll) {
396 overScroll(0);
397 mWasInOverscroll = false;
398 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700399 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800400 super.scrollTo(x, y);
401 }
Michael Jurka0142d492010-08-25 17:46:15 -0700402 }
403
Adam Cohen53805212013-10-01 10:39:23 -0700404 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800405 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700406 if (mCurrentPage != getNextPage()) {
407 AccessibilityEvent ev =
408 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700409 ev.setScrollable(true);
410 ev.setScrollX(getScrollX());
411 ev.setScrollY(getScrollY());
412 ev.setMaxScrollX(mMaxScrollX);
413 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700414
Vadim Tryshevf4715972015-05-08 17:21:03 -0700415 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700416 }
Adam Cohen53805212013-10-01 10:39:23 -0700417 }
418 }
419
Michael Jurka0142d492010-08-25 17:46:15 -0700420 // we moved this functionality to a helper function so SmoothPagedView can reuse it
421 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800422 return computeScrollHelper(true);
423 }
424
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700425 protected void announcePageForAccessibility() {
426 if (isAccessibilityEnabled(getContext())) {
427 // Notify the user when the page changes
428 announceForAccessibility(getCurrentPageDescription());
429 }
430 }
431
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800432 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700434 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800435 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700436 || getScrollY() != mScroller.getCurrY()
437 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700438 float scaleX = mFreeScroll ? getScaleX() : 1f;
439 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
440 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700441 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800442 if (shouldInvalidate) {
443 invalidate();
444 }
Michael Jurka0142d492010-08-25 17:46:15 -0700445 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800446 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700447 sendScrollAccessibilityEvent();
448
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700449 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700450 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700451 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700452 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700453
Adam Cohen73aa9752010-11-24 16:26:58 -0800454 // We don't want to trigger a page end moving unless the page has settled
455 // and the user has stopped scrolling
456 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700457 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800458 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700459
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700460 if (canAnnouncePageDescription()) {
461 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700462 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 }
Michael Jurka0142d492010-08-25 17:46:15 -0700464 return false;
465 }
466
467 @Override
468 public void computeScroll() {
469 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 }
471
Sunny Goyalce8809a2018-01-18 14:02:47 -0800472 public int getExpectedHeight() {
473 return getMeasuredHeight();
474 }
475
Adam Cohen410f3cd2013-09-22 12:09:32 -0700476 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800477 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800478 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700479 }
480
Sunny Goyalce8809a2018-01-18 14:02:47 -0800481 public int getExpectedWidth() {
482 return getMeasuredWidth();
483 }
484
Sunny Goyalbc683e92017-12-06 10:25:07 -0800485 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800486 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800487 - mInsets.left - mInsets.right;
488 }
489
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800491 public void requestLayout() {
492 mIsLayoutValid = false;
493 super.requestLayout();
494 }
495
496 @Override
497 public void forceLayout() {
498 mIsLayoutValid = false;
499 super.forceLayout();
500 }
501
502 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700504 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700505 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
506 return;
507 }
508
Adam Cohen7d30a372013-07-01 17:03:59 -0700509 // We measure the dimensions of the PagedView to be larger than the pages so that when we
510 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700511 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
512 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
513 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700514 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700515
Adam Cohen7d30a372013-07-01 17:03:59 -0700516 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
517 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
518 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
520
Winson Chung8aad6102012-05-11 16:27:49 -0700521 // Return early if we aren't given a proper dimension
522 if (widthSize <= 0 || heightSize <= 0) {
523 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
524 return;
525 }
526
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700528 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700529 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700530
Sunny Goyalac00cba2017-11-13 15:58:01 -0800531 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800532 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800533 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800534 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700535
Sunny Goyalac00cba2017-11-13 15:58:01 -0800536 // measureChildren takes accounts for content padding, we only need to care about extra
537 // space due to insets.
538 measureChildren(myWidthSpec, myHeightSpec);
539 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800540 }
541
Sunny Goyalcf25b522015-07-09 00:01:18 -0700542 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700544 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800545 mIsLayoutValid = true;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700546 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700547 return;
548 }
549
Winson Chung785d2eb2011-04-14 16:08:02 -0700550 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700551 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700552
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800553 boolean pageScrollChanged = false;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700554 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
555 mPageScrolls = new int[childCount];
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800556 pageScrollChanged = true;
Adam Cohenedb40762013-07-18 16:45:45 -0700557 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700558 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
559 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700561
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700562 final LayoutTransition transition = getLayoutTransition();
563 // If the transition is running defer updating max scroll, as some empty pages could
564 // still be present, and a max scroll change could cause sudden jumps in scroll.
565 if (transition != null && transition.isRunning()) {
566 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
567
568 @Override
569 public void startTransition(LayoutTransition transition, ViewGroup container,
570 View view, int transitionType) { }
571
572 @Override
573 public void endTransition(LayoutTransition transition, ViewGroup container,
574 View view, int transitionType) {
575 // Wait until all transitions are complete.
576 if (!transition.isRunning()) {
577 transition.removeTransitionListener(this);
578 updateMaxScrollX();
579 }
580 }
581 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700582 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700583 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700584 }
585
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700586 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
587 updateCurrentPageScroll();
588 mFirstLayout = false;
589 }
590
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800591 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700592 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700593 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700594 mChildCountOnLastLayout = childCount;
Winson Chunge3193b92010-09-10 11:44:42 -0700595 }
596
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700597 /**
598 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
599 * of {@code outPageScrolls} should be same as the the childCount
600 *
601 */
602 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
603 ComputePageScrollsLogic scrollLogic) {
604 final int childCount = getChildCount();
605
606 final int startIndex = mIsRtl ? childCount - 1 : 0;
607 final int endIndex = mIsRtl ? -1 : childCount;
608 final int delta = mIsRtl ? -1 : 1;
609
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700610 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700611
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700612 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700613 boolean pageScrollChanged = false;
614
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700615 for (int i = startIndex, childLeft = scrollOffsetLeft + offsetForPageScrolls();
616 i != endIndex;
617 i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700618 final View child = getPageAt(i);
619 if (scrollLogic.shouldIncludeView(child)) {
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700620 final int childTop = getPaddingTop() +
621 (getMeasuredHeight() - mInsets.bottom - verticalPadding
622 - child.getMeasuredHeight()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700623 final int childWidth = child.getMeasuredWidth();
624
625 if (layoutChildren) {
626 final int childHeight = child.getMeasuredHeight();
627 child.layout(childLeft, childTop,
628 childLeft + child.getMeasuredWidth(), childTop + childHeight);
629 }
630
631 final int pageScroll = childLeft - scrollOffsetLeft;
632 if (outPageScrolls[i] != pageScroll) {
633 pageScrollChanged = true;
634 outPageScrolls[i] = pageScroll;
635 }
636
637 childLeft += childWidth + mPageSpacing + getChildGap();
638 }
639 }
640 return pageScrollChanged;
641 }
642
Sunny Goyala1fbd842015-05-20 13:40:27 -0700643 protected int getChildGap() {
644 return 0;
645 }
646
Sunny Goyal4d519f22017-10-24 10:32:40 -0700647 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700648 mMaxScrollX = computeMaxScrollX();
649 }
650
651 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700652 int childCount = getChildCount();
653 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700654 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700655 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700656 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700657 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700658 }
659 }
660
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700661 protected int offsetForPageScrolls() {
662 return 0;
663 }
664
Adam Cohen3b185e22013-10-29 14:45:58 -0700665 public void setPageSpacing(int pageSpacing) {
666 mPageSpacing = pageSpacing;
667 requestLayout();
668 }
669
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800670 private void dispatchPageCountChanged() {
671 if (mPageIndicator != null) {
672 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700673 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700674 // This ensures that when children are added, they get the correct transforms / alphas
675 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700676 invalidate();
677 }
678
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800680 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800681 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800682 dispatchPageCountChanged();
683 }
684
685 @Override
686 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800687 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700688 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800689 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700690 }
691
Adam Cohen73894962011-10-31 13:17:17 -0700692 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700693 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800694 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700695 }
696
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 @Override
698 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700699 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700700 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700701 if (immediate) {
702 setCurrentPage(page);
703 } else {
704 snapToPage(page);
705 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700706 return true;
707 }
708 return false;
709 }
710
711 @Override
712 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700713 int focusablePage;
714 if (mNextPage != INVALID_PAGE) {
715 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700717 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 }
Winson Chung86f77532010-08-24 11:08:22 -0700719 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700721 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 }
723 return false;
724 }
725
726 @Override
727 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800728 if (super.dispatchUnhandledMove(focused, direction)) {
729 return true;
730 }
731
732 if (mIsRtl) {
733 if (direction == View.FOCUS_LEFT) {
734 direction = View.FOCUS_RIGHT;
735 } else if (direction == View.FOCUS_RIGHT) {
736 direction = View.FOCUS_LEFT;
737 }
738 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700739 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700740 if (getCurrentPage() > 0) {
741 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 return true;
743 }
744 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700745 if (getCurrentPage() < getPageCount() - 1) {
746 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 return true;
748 }
749 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800750 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752
753 @Override
754 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700755 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
756 return;
757 }
758
Adam Cohen0ffac432013-07-10 11:19:26 -0700759 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700760 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700761 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 }
763 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700764 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700765 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700766 }
767 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700768 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700769 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 }
771 }
772 }
773
774 /**
775 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700776 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 *
Winson Chung86f77532010-08-24 11:08:22 -0700778 * This happens when live folders requery, and if they're off page, they
779 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 */
781 @Override
782 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700783 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 View v = focused;
785 while (true) {
786 if (v == current) {
787 super.focusableViewAvailable(focused);
788 return;
789 }
790 if (v == this) {
791 return;
792 }
793 ViewParent parent = v.getParent();
794 if (parent instanceof View) {
795 v = (View)v.getParent();
796 } else {
797 return;
798 }
799 }
800 }
801
802 /**
803 * {@inheritDoc}
804 */
805 @Override
806 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
807 if (disallowIntercept) {
808 // We need to make sure to cancel our long press if
809 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700810 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700811 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 }
813 super.requestDisallowInterceptTouchEvent(disallowIntercept);
814 }
815
Adam Cohen7d30a372013-07-01 17:03:59 -0700816 /** Returns whether x and y originated within the buffered viewport */
817 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800818 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700819 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700820 }
821
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 @Override
823 public boolean onInterceptTouchEvent(MotionEvent ev) {
824 /*
825 * This method JUST determines whether we want to intercept the motion.
826 * If we return true, onTouchEvent will be called and we do the actual
827 * scrolling there.
828 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800829 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700830
Winson Chung45e1d6e2010-11-09 17:19:49 -0800831 // Skip touch handling if there are no pages to swipe
832 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
833
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 /*
835 * Shortcut the most recurring case: the user is in the dragging
836 * state and he is moving his finger. We want to intercept this
837 * motion.
838 */
839 final int action = ev.getAction();
840 if ((action == MotionEvent.ACTION_MOVE) &&
841 (mTouchState == TOUCH_STATE_SCROLLING)) {
842 return true;
843 }
844
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 switch (action & MotionEvent.ACTION_MASK) {
846 case MotionEvent.ACTION_MOVE: {
847 /*
848 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
849 * whether the user has moved far enough from his original down touch.
850 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700851 if (mActivePointerId != INVALID_POINTER) {
852 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700853 }
854 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
855 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
856 // i.e. fall through to the next case (don't break)
857 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
858 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700859 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 }
861
862 case MotionEvent.ACTION_DOWN: {
863 final float x = ev.getX();
864 final float y = ev.getY();
865 // Remember location of down touch
866 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700867 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800869 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800870 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700872
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 /*
874 * If being flinged and user touches the screen, initiate drag;
875 * otherwise don't. mScroller.isFinished should be false when
876 * being flinged.
877 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700878 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000879 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800880
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700881 if (finishedScrolling) {
882 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800883 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000884 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700885 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800886 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700887 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700888 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
889 mTouchState = TOUCH_STATE_SCROLLING;
890 } else {
891 mTouchState = TOUCH_STATE_REST;
892 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700893 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700894
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 break;
896 }
897
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800899 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700900 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 break;
902
903 case MotionEvent.ACTION_POINTER_UP:
904 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800905 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 break;
907 }
908
909 /*
910 * The only time we want to intercept motion events is if we are in the
911 * drag mode.
912 */
913 return mTouchState != TOUCH_STATE_REST;
914 }
915
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800916 public boolean isHandlingTouch() {
917 return mTouchState != TOUCH_STATE_REST;
918 }
919
Adam Cohenf8d28232011-02-01 21:47:00 -0800920 protected void determineScrollingStart(MotionEvent ev) {
921 determineScrollingStart(ev, 1.0f);
922 }
923
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 /*
925 * Determines if we should change the touch state to start scrolling after the
926 * user moves their touch point too far.
927 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800928 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700929 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700931 if (pointerIndex == -1) return;
932
933 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 final float x = ev.getX(pointerIndex);
935 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -0700936 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
937
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700939
Adam Cohenf8d28232011-02-01 21:47:00 -0800940 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700942
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700943 if (xMoved) {
944 // Scroll if the user moved far enough along the X axis
945 mTouchState = TOUCH_STATE_SCROLLING;
946 mTotalMotionX += Math.abs(mLastMotionX - x);
947 mLastMotionX = x;
948 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700949 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700950 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800951 // Stop listening for things like pinches.
952 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800953 }
954 }
955
956 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700957 // Try canceling the long press. It could also have been scheduled
958 // by a distant descendant, so use the mAllowLongPress flag to block
959 // everything
960 final View currentPage = getPageAt(mCurrentPage);
961 if (currentPage != null) {
962 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 }
964 }
965
Adam Cohenb5ba0972011-09-07 18:02:31 -0700966 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800967 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700968
Adam Cohenedb40762013-07-18 16:45:45 -0700969 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700970 int count = getChildCount();
971
972 final int totalDistance;
973
974 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700975 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700976 adjacentPage = page - 1;
977 }
978
979 if (adjacentPage < 0 || adjacentPage > count - 1) {
980 totalDistance = v.getMeasuredWidth() + mPageSpacing;
981 } else {
982 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
983 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700984
985 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700986 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
987 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700988 return scrollProgress;
989 }
990
Adam Cohenedb40762013-07-18 16:45:45 -0700991 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700992 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -0700993 return 0;
994 } else {
995 return mPageScrolls[index];
996 }
997 }
998
Adam Cohen564a2e72013-10-09 14:47:32 -0700999 // While layout transitions are occurring, a child's position may stray from its baseline
1000 // position. This method returns the magnitude of this stray at any given time.
1001 public int getLayoutTransitionOffsetForPage(int index) {
1002 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1003 return 0;
1004 } else {
1005 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001006
Sunny Goyal228153d2018-01-04 15:35:22 -08001007 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1008 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001009 return (int) (child.getX() - baselineX);
1010 }
1011 }
1012
Adam Cohenb5ba0972011-09-07 18:02:31 -07001013 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001014 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001015
Sunny Goyal228153d2018-01-04 15:35:22 -08001016 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001017 if (amount < 0) {
1018 mOverScrollX = overScrollAmount;
1019 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001020 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001021 mOverScrollX = mMaxScrollX + overScrollAmount;
1022 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001023 }
1024 invalidate();
1025 }
1026
Adam Cohenb5ba0972011-09-07 18:02:31 -07001027 protected void overScroll(float amount) {
1028 dampedOverScroll(amount);
1029 }
1030
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001031
1032 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001033 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001034 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001035 }
1036
Adam Cohenf9618852013-11-08 06:45:03 -08001037 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001038 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001039 mFreeScroll = freeScroll;
1040
Adam Cohenf9618852013-11-08 06:45:03 -08001041 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001042 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001043 } else if (wasFreeScroll) {
1044 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001045 }
1046
1047 setEnableOverscroll(!freeScroll);
1048 }
1049
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001050 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001051 mAllowOverScroll = enable;
1052 }
1053
Winson Chung321e9ee2010-08-09 13:37:56 -07001054 @Override
1055 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001056 super.onTouchEvent(ev);
1057
Winson Chung45e1d6e2010-11-09 17:19:49 -08001058 // Skip touch handling if there are no pages to swipe
1059 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1060
Michael Jurkab8f06722010-10-10 15:58:46 -07001061 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001062
1063 final int action = ev.getAction();
1064
1065 switch (action & MotionEvent.ACTION_MASK) {
1066 case MotionEvent.ACTION_DOWN:
1067 /*
1068 * If being flinged and user touches, stop the fling. isFinished
1069 * will be false if being flinged.
1070 */
1071 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001072 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074
1075 // Remember where the motion event started
1076 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001077 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001078 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001079 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001081
Michael Jurka0142d492010-08-25 17:46:15 -07001082 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001083 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001084 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001085 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001086 break;
1087
1088 case MotionEvent.ACTION_MOVE:
1089 if (mTouchState == TOUCH_STATE_SCROLLING) {
1090 // Scroll to follow the motion event
1091 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001092
1093 if (pointerIndex == -1) return true;
1094
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001096 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001097
Adam Cohenaefd4e12011-02-14 16:39:38 -08001098 mTotalMotionX += Math.abs(deltaX);
1099
Winson Chungc0844aa2011-02-02 15:25:58 -08001100 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1101 // keep the remainder because we are actually testing if we've moved from the last
1102 // scrolled position (which is discrete).
1103 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001104 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001105 mLastMotionX = x;
1106 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 } else {
1108 awakenScrollBars();
1109 }
Adam Cohen564976a2010-10-13 18:52:07 -07001110 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 determineScrollingStart(ev);
1112 }
1113 break;
1114
1115 case MotionEvent.ACTION_UP:
1116 if (mTouchState == TOUCH_STATE_SCROLLING) {
1117 final int activePointerId = mActivePointerId;
1118 final int pointerIndex = ev.findPointerIndex(activePointerId);
1119 final float x = ev.getX(pointerIndex);
1120 final VelocityTracker velocityTracker = mVelocityTracker;
1121 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1122 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001123 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001124 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001125 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1126 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001127
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001128 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001129 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001130
Adam Cohenf358a4b2013-07-23 16:47:31 -07001131 if (!mFreeScroll) {
1132 // In the case that the page is moved far to one direction and then is flung
1133 // in the opposite direction, we use a threshold to determine whether we should
1134 // just return to the starting page, or if we should skip one further.
1135 boolean returnToOriginalPage = false;
1136 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1137 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1138 returnToOriginalPage = true;
1139 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001140
Adam Cohenf358a4b2013-07-23 16:47:31 -07001141 int finalPage;
1142 // We give flings precedence over large moves, which is why we short-circuit our
1143 // test for a large move if a fling has been registered. That is, a large
1144 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001145 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1146 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001147 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1148 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1149 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1150 snapToPageWithVelocity(finalPage, velocityX);
1151 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1152 (isFling && isVelocityXLeft)) &&
1153 mCurrentPage < getChildCount() - 1) {
1154 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1155 snapToPageWithVelocity(finalPage, velocityX);
1156 } else {
1157 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001158 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001160 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001161 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001162 }
1163
1164 float scaleX = getScaleX();
1165 int vX = (int) (-velocityX * scaleX);
1166 int initialScrollX = (int) (getScrollX() * scaleX);
1167
Adam Cohenf9618852013-11-08 06:45:03 -08001168 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001169 mScroller.fling(initialScrollX,
1170 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001171 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1172 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1173 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1174 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1175 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1176 && unscaledScrollX < lastPageScroll) {
1177 // Make sure we land directly on a page. If flinging past one of the ends,
1178 // don't change the velocity as it will get stopped at the end anyway.
1179 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1180 // Ensure the scroll/snap doesn't happen too fast;
1181 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1182 - mScroller.getDuration();
1183 if (extraScrollDuration > 0) {
1184 mScroller.extendDuration(extraScrollDuration);
1185 }
1186 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001187 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001189 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001190 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1191 // at this point we have not moved beyond the touch slop
1192 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1193 // we can just page
1194 int nextPage = Math.max(0, mCurrentPage - 1);
1195 if (nextPage != mCurrentPage) {
1196 snapToPage(nextPage);
1197 } else {
1198 snapToDestination();
1199 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001200 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 // at this point we have not moved beyond the touch slop
1202 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1203 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001204 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1205 if (nextPage != mCurrentPage) {
1206 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 } else {
1208 snapToDestination();
1209 }
1210 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001211
Adam Cohen7d30a372013-07-01 17:03:59 -07001212 // End any intermediate reordering states
1213 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 break;
1215
1216 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001217 if (mTouchState == TOUCH_STATE_SCROLLING) {
1218 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001219 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001220 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001221 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 break;
1223
1224 case MotionEvent.ACTION_POINTER_UP:
1225 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001226 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 break;
1228 }
1229
1230 return true;
1231 }
1232
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001233 protected boolean shouldFlingForVelocity(int velocityX) {
1234 return Math.abs(velocityX) > mFlingThresholdVelocity;
1235 }
1236
Adam Cohen7d30a372013-07-01 17:03:59 -07001237 private void resetTouchState() {
1238 releaseVelocityTracker();
Adam Cohen7d30a372013-07-01 17:03:59 -07001239 mTouchState = TOUCH_STATE_REST;
1240 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001241 }
1242
Adam Cohenc2d6e892014-10-16 09:49:24 -07001243 /**
1244 * Triggered by scrolling via touch
1245 */
1246 protected void onScrollInteractionBegin() {
1247 }
1248
1249 protected void onScrollInteractionEnd() {
1250 }
1251
Winson Chung185d7162011-02-28 13:47:29 -08001252 @Override
1253 public boolean onGenericMotionEvent(MotionEvent event) {
1254 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1255 switch (event.getAction()) {
1256 case MotionEvent.ACTION_SCROLL: {
1257 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1258 final float vscroll;
1259 final float hscroll;
1260 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1261 vscroll = 0;
1262 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1263 } else {
1264 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1265 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1266 }
1267 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001268 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001269 : (hscroll > 0 || vscroll > 0);
1270 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001271 scrollRight();
1272 } else {
1273 scrollLeft();
1274 }
1275 return true;
1276 }
1277 }
1278 }
1279 }
1280 return super.onGenericMotionEvent(event);
1281 }
1282
Michael Jurkab8f06722010-10-10 15:58:46 -07001283 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1284 if (mVelocityTracker == null) {
1285 mVelocityTracker = VelocityTracker.obtain();
1286 }
1287 mVelocityTracker.addMovement(ev);
1288 }
1289
1290 private void releaseVelocityTracker() {
1291 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001292 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001293 mVelocityTracker.recycle();
1294 mVelocityTracker = null;
1295 }
1296 }
1297
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 private void onSecondaryPointerUp(MotionEvent ev) {
1299 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1300 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1301 final int pointerId = ev.getPointerId(pointerIndex);
1302 if (pointerId == mActivePointerId) {
1303 // This was our active pointer going up. Choose a new
1304 // active pointer and adjust accordingly.
1305 // TODO: Make this decision more intelligent.
1306 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1307 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001308 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 mActivePointerId = ev.getPointerId(newPointerIndex);
1310 if (mVelocityTracker != null) {
1311 mVelocityTracker.clear();
1312 }
1313 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001314 }
1315
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 @Override
1317 public void requestChildFocus(View child, View focused) {
1318 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001319 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001320 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001321 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 }
1323 }
1324
Sunny Goyal228153d2018-01-04 15:35:22 -08001325 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001326 return getPageNearestToCenterOfScreen(getScrollX());
1327 }
1328
1329 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001330 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001331 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001333 final int childCount = getChildCount();
1334 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001335 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001336 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001338 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1340 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1341 minDistanceFromScreenCenter = distanceFromScreenCenter;
1342 minDistanceFromScreenCenterIndex = i;
1343 }
1344 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001345 return minDistanceFromScreenCenterIndex;
1346 }
1347
1348 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001349 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1350 }
1351
1352 protected boolean isInOverScroll() {
1353 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1354 }
1355
1356 protected int getPageSnapDuration() {
1357 if (isInOverScroll()) {
1358 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1359 }
1360 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 }
1362
Adam Cohene0f66b52010-11-23 15:06:07 -08001363 // We want the duration of the page snap animation to be influenced by the distance that
1364 // the screen has to travel, however, we don't want this duration to be effected in a
1365 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1366 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001367 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001368 f -= 0.5f; // center the values about 0.
1369 f *= 0.3f * Math.PI / 2.0f;
1370 return (float) Math.sin(f);
1371 }
1372
Winson Chung05a31ed2018-02-08 17:08:43 -08001373 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001374 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001375 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001376
Adam Cohenedb40762013-07-18 16:45:45 -07001377 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001378 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001379 int duration = 0;
1380
Sunny Goyal4d113a52015-05-27 10:05:28 -07001381 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001382 // If the velocity is low enough, then treat this more as an automatic page advance
1383 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001384 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001385 }
1386
1387 // Here we compute a "distance" that will be used in the computation of the overall
1388 // snap duration. This is a function of the actual distance that needs to be traveled;
1389 // we keep this value close to half screen size in order to reduce the variance in snap
1390 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001391 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001392 float distance = halfScreenSize + halfScreenSize *
1393 distanceInfluenceForSnapDuration(distanceRatio);
1394
1395 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001396 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001397
1398 // we want the page's snap velocity to approximately match the velocity at which the
1399 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001400 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1401 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001402
Winson Chung05a31ed2018-02-08 17:08:43 -08001403 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001404 }
1405
Winson Chung05a31ed2018-02-08 17:08:43 -08001406 public boolean snapToPage(int whichPage) {
1407 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 }
1409
Winson Chung05a31ed2018-02-08 17:08:43 -08001410 public boolean snapToPageImmediately(int whichPage) {
1411 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001412 }
1413
Winson Chung05a31ed2018-02-08 17:08:43 -08001414 public boolean snapToPage(int whichPage, int duration) {
1415 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001416 }
1417
Winson Chung05a31ed2018-02-08 17:08:43 -08001418 protected boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1419 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001420 }
1421
Winson Chung05a31ed2018-02-08 17:08:43 -08001422 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001423 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001424 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001425
Adam Cohenedb40762013-07-18 16:45:45 -07001426 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001427 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001428 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001429 }
1430
Winson Chung05a31ed2018-02-08 17:08:43 -08001431 protected boolean snapToPage(int whichPage, int delta, int duration) {
1432 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001433 }
Michael Jurka0142d492010-08-25 17:46:15 -07001434
Winson Chung05a31ed2018-02-08 17:08:43 -08001435 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001436 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001437 whichPage = validateNewPage(whichPage);
1438
Adam Cohen7d30a372013-07-01 17:03:59 -07001439 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001440
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 if (immediate) {
1443 duration = 0;
1444 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 duration = Math.abs(delta);
1446 }
1447
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001448 if (duration != 0) {
1449 pageBeginTransition();
1450 }
1451
Adam Cohenf358a4b2013-07-23 16:47:31 -07001452 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001453 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001454 }
Adam Cohenf9618852013-11-08 06:45:03 -08001455
1456 if (interpolator != null) {
1457 mScroller.setInterpolator(interpolator);
1458 } else {
1459 mScroller.setInterpolator(mDefaultInterpolator);
1460 }
1461
Sunny Goyalc86df472016-02-25 09:19:38 -08001462 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001463
Adam Cohen674531f2013-12-13 15:07:14 -08001464 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001465
1466 // Trigger a compute() to finish switching pages if necessary
1467 if (immediate) {
1468 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001469 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001470 }
1471
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001473 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 }
1475
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001477 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 }
1479
1480 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001481 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 }
1483
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001484 @Override
1485 public CharSequence getAccessibilityClassName() {
1486 // Some accessibility services have special logic for ScrollView. Since we provide same
1487 // accessibility info as ScrollView, inform the service to handle use the same way.
1488 return ScrollView.class.getName();
1489 }
1490
Winson Chung6a0f57d2011-06-29 20:10:49 -07001491 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001492 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001493 @Override
1494 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1495 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001496 info.setScrollable(getPageCount() > 1);
1497 if (getCurrentPage() < getPageCount() - 1) {
1498 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1499 }
1500 if (getCurrentPage() > 0) {
1501 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1502 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001503
1504 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1505 // Besides disabling the accessibility long-click, this also prevents this view from getting
1506 // accessibility focus.
1507 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001508 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001509 }
1510
1511 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001512 public void sendAccessibilityEvent(int eventType) {
1513 // Don't let the view send real scroll events.
1514 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1515 super.sendAccessibilityEvent(eventType);
1516 }
1517 }
1518
1519 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001520 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1521 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001522 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001523 }
1524
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001525 @Override
1526 public boolean performAccessibilityAction(int action, Bundle arguments) {
1527 if (super.performAccessibilityAction(action, arguments)) {
1528 return true;
1529 }
1530 switch (action) {
1531 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1532 if (getCurrentPage() < getPageCount() - 1) {
1533 scrollRight();
1534 return true;
1535 }
1536 } break;
1537 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1538 if (getCurrentPage() > 0) {
1539 scrollLeft();
1540 return true;
1541 }
1542 } break;
1543 }
1544 return false;
1545 }
1546
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001547 protected boolean canAnnouncePageDescription() {
1548 return true;
1549 }
1550
Adam Cohen0ffac432013-07-10 11:19:26 -07001551 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001552 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001553 getNextPage() + 1, getChildCount());
1554 }
1555
Tony Mak2e564402018-02-27 17:19:34 +00001556 protected float getDownMotionX() {
1557 return mDownMotionX;
1558 }
1559
1560 protected float getDownMotionY() {
1561 return mDownMotionY;
1562 }
1563
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001564 protected interface ComputePageScrollsLogic {
1565
1566 boolean shouldIncludeView(View view);
1567 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001568}