blob: 9abb9c1ad5bf129652b449e2af9d865637c5a002 [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;
Tony Wickhamf549dab2016-05-16 09:54:06 -070044
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070045import com.android.launcher3.anim.Interpolators;
Tony Wickhamf549dab2016-05-16 09:54:06 -070046import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070047import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070048import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import java.util.ArrayList;
51
Winson Chung321e9ee2010-08-09 13:37:56 -070052/**
53 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070054 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070055 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080056public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070057 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070058 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Vadim Tryshevfedca432015-08-19 17:55:02 -070061 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080062 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070063
Sunny Goyalb72d8b22017-07-14 00:02:27 -070064 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070065 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070066
Adam Cohenb64cb5a2011-02-15 13:53:42 -080067 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080068 // The page is moved more than halfway, automatically move to the next page on touch up.
69 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080070
Sunny Goyal8e2133b2015-05-06 13:39:07 -070071 private static final float MAX_SCROLL_PROGRESS = 1.0f;
72
Adam Cohen265b9a62011-12-07 14:37:18 -080073 // The following constants need to be scaled based on density. The scaled versions will be
74 // assigned to the corresponding member variables below.
75 private static final int FLING_THRESHOLD_VELOCITY = 500;
76 private static final int MIN_SNAP_VELOCITY = 1500;
77 private static final int MIN_FLING_VELOCITY = 250;
78
Adam Cohen21cd0022013-10-09 18:57:02 -070079 public static final int INVALID_RESTORE_PAGE = -1001;
80
Adam Cohenf358a4b2013-07-23 16:47:31 -070081 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080082 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083
Adam Cohen265b9a62011-12-07 14:37:18 -080084 protected int mFlingThresholdVelocity;
85 protected int mMinFlingVelocity;
86 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070087
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected boolean mFirstLayout = true;
89
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070092 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070093
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -080096 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -080097 protected LauncherScroller mScroller;
98 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070099 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800100 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
102 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700103 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700104 private float mLastMotionX;
105 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700106 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700107
108 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected final static int TOUCH_STATE_REST = 0;
111 protected final static int TOUCH_STATE_SCROLLING = 1;
112 protected final static int TOUCH_STATE_PREV_PAGE = 2;
113 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700114
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800119 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka5f1c5092010-09-03 14:15:02 -0700121 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Michael Jurka5f1c5092010-09-03 14:15:02 -0700123 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700125 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700126
Sunny Goyal061380a2016-02-29 15:15:03 -0800127 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700128
Adam Cohen8d769d62017-06-23 17:27:38 -0700129 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
130 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
131 // the screens from continuing to translate beyond the normal bounds.
132 protected int mOverScrollX;
133
134 protected int mUnboundedScrollX;
135
Winson Chungd2be3812013-07-16 11:11:32 -0700136 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800138 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700139
Adam Cohen7d30a372013-07-01 17:03:59 -0700140 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700141 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700142 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700143 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700144
John Spurlock77e1f472013-09-11 10:09:51 -0400145 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800146 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400147
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800148 // Similar to the platform implementation of isLayoutValid();
149 protected boolean mIsLayoutValid;
150
Winson Chung321e9ee2010-08-09 13:37:56 -0700151 public PagedView(Context context) {
152 this(context, null);
153 }
154
155 public PagedView(Context context, AttributeSet attrs) {
156 this(context, attrs, 0);
157 }
158
159 public PagedView(Context context, AttributeSet attrs, int defStyle) {
160 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700161
Adam Cohen9c4949e2010-10-05 12:27:22 -0700162 TypedArray a = context.obtainStyledAttributes(attrs,
163 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700164 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700165 a.recycle();
166
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700168 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700169 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700170 }
171
172 /**
173 * Initializes various states for this workspace.
174 */
Michael Jurka0142d492010-08-25 17:46:15 -0700175 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800176 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700177 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700178 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700179
180 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700182 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800183
Sunny Goyalcf25b522015-07-09 00:01:18 -0700184 float density = getResources().getDisplayMetrics().density;
185 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
186 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
187 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700188 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 }
190
Adam Cohenf9618852013-11-08 06:45:03 -0800191 protected void setDefaultInterpolator(Interpolator interpolator) {
192 mDefaultInterpolator = interpolator;
193 mScroller.setInterpolator(mDefaultInterpolator);
194 }
195
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700196 public void initParentViews(View parent) {
197 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800198 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700199 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800200 mPageIndicator.setPageDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700201 }
202 }
203
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800204 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700205 return mPageIndicator;
206 }
207
Adam Cohen674531f2013-12-13 15:07:14 -0800208 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700209 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
210 * that the user was on before entering free scroll mode (e.g. the home screen page they
211 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
212 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700214 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700215 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700217
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700218 /**
219 * Returns the index of page to be shown immediately afterwards.
220 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700221 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700222 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
223 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700224
Adam Cohenf9c184a2016-01-15 16:47:43 -0800225 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 return getChildCount();
227 }
228
Sunny Goyal83a8f042015-05-19 12:52:12 -0700229 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 return getChildAt(index);
231 }
232
Adam Cohenae4f1552011-10-20 00:15:42 -0700233 protected int indexToPage(int index) {
234 return index;
235 }
236
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800238 * Updates the scroll of the current page immediately to its final scroll position. We use this
239 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
240 * the previous tab page.
241 */
242 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700243 // If the current page is invalid, just reset the scroll position to zero
244 int newX = 0;
245 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700246 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700247 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 scrollTo(newX, 0);
249 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700250 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800251 }
252
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700253 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700254 mScroller.abortAnimation();
255 // We need to clean up the next page here to avoid computeScrollHelper from
256 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700257 if (resetNextPage) {
258 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800259 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700260 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700261 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700262
Tony Wickham8f7ead32016-04-07 18:46:44 -0700263 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700264 mScroller.forceFinished(true);
265 // We need to clean up the next page here to avoid computeScrollHelper from
266 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700267 if (resetNextPage) {
268 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800269 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700270 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700271 }
272
Adam Cohen6f127a62014-06-12 14:54:41 -0700273 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700274 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700275 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700276 }
277
Chet Haasebc2f0822012-10-26 17:59:53 -0700278 /**
Winson Chung86f77532010-08-24 11:08:22 -0700279 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700281 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800282 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700283 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800284 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800285 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
286 // the default
287 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800288 return;
289 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700290 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700291 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700292 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700293 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800294 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 }
296
Winson Chung8c87cd82013-07-23 16:20:10 -0700297 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800298 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
299 * has settled.
300 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700301 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800302 updatePageIndicator();
303 }
304
305 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700306 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700307 if (mPageIndicator != null) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800308 mPageIndicator.setPageDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700309 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700311 protected void pageBeginTransition() {
312 if (!mIsPageInTransition) {
313 mIsPageInTransition = true;
314 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700315 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700316 }
317
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700318 protected void pageEndTransition() {
319 if (mIsPageInTransition) {
320 mIsPageInTransition = false;
321 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700322 }
Michael Jurka0142d492010-08-25 17:46:15 -0700323 }
324
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700325 protected boolean isPageInTransition() {
326 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700327 }
328
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700329 /**
330 * Called when the page starts moving as part of the scroll. Subclasses can override this
331 * to provide custom behavior during animation.
332 */
333 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700334 }
335
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700336 /**
337 * Called when the page ends moving as part of the scroll. Subclasses can override this
338 * to provide custom behavior during animation.
339 */
340 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700341 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700342 }
343
Sunny Goyalc86df472016-02-25 09:19:38 -0800344 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700345 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800346 }
347
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800349 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800350 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800351 }
352
353 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700354 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700355 // In free scroll mode, we clamp the scrollX
356 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700357 // If the scroller is trying to move to a location beyond the maximum allowed
358 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700359 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700360 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700361 }
362
Sunny Goyal4d519f22017-10-24 10:32:40 -0700363 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700364 }
365
Adam Cohen8d769d62017-06-23 17:27:38 -0700366 mUnboundedScrollX = x;
367
Sunny Goyal70660032015-05-14 00:07:08 -0700368 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
369 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700370 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700371 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800372 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700373 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700374 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700375 overScroll(x - mMaxScrollX);
376 } else {
377 overScroll(x);
378 }
Adam Cohen68d73932010-11-15 10:50:58 -0800379 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700380 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700381 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800382 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700383 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700384 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700385 overScroll(x);
386 } else {
387 overScroll(x - mMaxScrollX);
388 }
Adam Cohen68d73932010-11-15 10:50:58 -0800389 }
390 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700391 if (mWasInOverscroll) {
392 overScroll(0);
393 mWasInOverscroll = false;
394 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700395 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800396 super.scrollTo(x, y);
397 }
Michael Jurka0142d492010-08-25 17:46:15 -0700398 }
399
Adam Cohen53805212013-10-01 10:39:23 -0700400 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800401 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700402 if (mCurrentPage != getNextPage()) {
403 AccessibilityEvent ev =
404 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700405 ev.setScrollable(true);
406 ev.setScrollX(getScrollX());
407 ev.setScrollY(getScrollY());
408 ev.setMaxScrollX(mMaxScrollX);
409 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700410
Vadim Tryshevf4715972015-05-08 17:21:03 -0700411 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700412 }
Adam Cohen53805212013-10-01 10:39:23 -0700413 }
414 }
415
Michael Jurka0142d492010-08-25 17:46:15 -0700416 // we moved this functionality to a helper function so SmoothPagedView can reuse it
417 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800418 return computeScrollHelper(true);
419 }
420
421 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700423 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800424 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700425 || getScrollY() != mScroller.getCurrY()
426 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700427 float scaleX = mFreeScroll ? getScaleX() : 1f;
428 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
429 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700430 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800431 if (shouldInvalidate) {
432 invalidate();
433 }
Michael Jurka0142d492010-08-25 17:46:15 -0700434 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800435 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700436 sendScrollAccessibilityEvent();
437
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700438 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700439 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700440 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700441 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700442
Adam Cohen73aa9752010-11-24 16:26:58 -0800443 // We don't want to trigger a page end moving unless the page has settled
444 // and the user has stopped scrolling
445 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700446 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800447 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700448
Sunny Goyald0030b02017-12-08 15:07:24 -0800449 if (isAccessibilityEnabled(getContext())) {
Adam Cohen53805212013-10-01 10:39:23 -0700450 // Notify the user when the page changes
451 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700452 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 }
Michael Jurka0142d492010-08-25 17:46:15 -0700454 return false;
455 }
456
457 @Override
458 public void computeScroll() {
459 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 }
461
Sunny Goyalce8809a2018-01-18 14:02:47 -0800462 public int getExpectedHeight() {
463 return getMeasuredHeight();
464 }
465
Adam Cohen410f3cd2013-09-22 12:09:32 -0700466 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800467 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800468 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700469 }
470
Sunny Goyalce8809a2018-01-18 14:02:47 -0800471 public int getExpectedWidth() {
472 return getMeasuredWidth();
473 }
474
Sunny Goyalbc683e92017-12-06 10:25:07 -0800475 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800476 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800477 - mInsets.left - mInsets.right;
478 }
479
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800481 public void requestLayout() {
482 mIsLayoutValid = false;
483 super.requestLayout();
484 }
485
486 @Override
487 public void forceLayout() {
488 mIsLayoutValid = false;
489 super.forceLayout();
490 }
491
492 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700493 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700494 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700495 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
496 return;
497 }
498
Adam Cohen7d30a372013-07-01 17:03:59 -0700499 // We measure the dimensions of the PagedView to be larger than the pages so that when we
500 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700501 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
502 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
503 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700504 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700505
Adam Cohen7d30a372013-07-01 17:03:59 -0700506 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
507 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
508 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700509 }
510
Winson Chung8aad6102012-05-11 16:27:49 -0700511 // Return early if we aren't given a proper dimension
512 if (widthSize <= 0 || heightSize <= 0) {
513 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
514 return;
515 }
516
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700518 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700519 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700520
Sunny Goyalac00cba2017-11-13 15:58:01 -0800521 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800522 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800523 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800524 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700525
Sunny Goyalac00cba2017-11-13 15:58:01 -0800526 // measureChildren takes accounts for content padding, we only need to care about extra
527 // space due to insets.
528 measureChildren(myWidthSpec, myHeightSpec);
529 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800530 }
531
Sunny Goyalcf25b522015-07-09 00:01:18 -0700532 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700534 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800535 mIsLayoutValid = true;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700536 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700537 return;
538 }
539
Winson Chung785d2eb2011-04-14 16:08:02 -0700540 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700542
Sunny Goyal70660032015-05-14 00:07:08 -0700543 final int startIndex = mIsRtl ? childCount - 1 : 0;
544 final int endIndex = mIsRtl ? -1 : childCount;
545 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700546
Adam Cohen7d30a372013-07-01 17:03:59 -0700547 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700548
Sunny Goyal228153d2018-01-04 15:35:22 -0800549 int scrollOffsetLeft = mInsets.left + getPaddingLeft();
550 int childLeft = scrollOffsetLeft;
551
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800552 boolean pageScrollChanged = false;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700553 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
554 mPageScrolls = new int[childCount];
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800555 pageScrollChanged = true;
Adam Cohenedb40762013-07-18 16:45:45 -0700556 }
557
Adam Cohen0ffac432013-07-10 11:19:26 -0700558 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700559 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 if (child.getVisibility() != View.GONE) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800561 int childTop = getPaddingTop() + mInsets.top;
562 childTop += (getMeasuredHeight() - mInsets.top - mInsets.bottom - verticalPadding
Sunny Goyalac00cba2017-11-13 15:58:01 -0800563 - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100564
Adam Cohen96d30a12013-07-16 18:13:21 -0700565 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700566 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800567
Winson Chung785d2eb2011-04-14 16:08:02 -0700568 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700569 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800570 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700571
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800572 final int pageScroll = childLeft - scrollOffsetLeft;
573 if (mPageScrolls[i] != pageScroll) {
574 pageScrollChanged = true;
575 mPageScrolls[i] = pageScroll;
576 }
Adam Cohen84a465a2013-11-11 18:49:56 +0000577
Sunny Goyalac00cba2017-11-13 15:58:01 -0800578 childLeft += childWidth + mPageSpacing + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 }
580 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700581
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700582 final LayoutTransition transition = getLayoutTransition();
583 // If the transition is running defer updating max scroll, as some empty pages could
584 // still be present, and a max scroll change could cause sudden jumps in scroll.
585 if (transition != null && transition.isRunning()) {
586 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
587
588 @Override
589 public void startTransition(LayoutTransition transition, ViewGroup container,
590 View view, int transitionType) { }
591
592 @Override
593 public void endTransition(LayoutTransition transition, ViewGroup container,
594 View view, int transitionType) {
595 // Wait until all transitions are complete.
596 if (!transition.isRunning()) {
597 transition.removeTransitionListener(this);
598 updateMaxScrollX();
599 }
600 }
601 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700602 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700603 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700604 }
605
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700606 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
607 updateCurrentPageScroll();
608 mFirstLayout = false;
609 }
610
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800611 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700612 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700613 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700614 mChildCountOnLastLayout = childCount;
Winson Chunge3193b92010-09-10 11:44:42 -0700615 }
616
Sunny Goyala1fbd842015-05-20 13:40:27 -0700617 protected int getChildGap() {
618 return 0;
619 }
620
Sunny Goyal4d519f22017-10-24 10:32:40 -0700621 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700622 mMaxScrollX = computeMaxScrollX();
623 }
624
625 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700626 int childCount = getChildCount();
627 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700628 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700629 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700630 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700631 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700632 }
633 }
634
Adam Cohen3b185e22013-10-29 14:45:58 -0700635 public void setPageSpacing(int pageSpacing) {
636 mPageSpacing = pageSpacing;
637 requestLayout();
638 }
639
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800640 private void dispatchPageCountChanged() {
641 if (mPageIndicator != null) {
642 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700643 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700644 // This ensures that when children are added, they get the correct transforms / alphas
645 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700646 invalidate();
647 }
648
Michael Jurka8b805b12012-04-18 14:23:14 -0700649 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800650 public void onViewAdded(View child) {
651 dispatchPageCountChanged();
652 }
653
654 @Override
655 public void onViewRemoved(View child) {
Tonya361c722017-07-04 09:43:06 -0700656 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800657 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700658 }
659
Adam Cohen73894962011-10-31 13:17:17 -0700660 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700661 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800662 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700663 }
664
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 @Override
666 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700667 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700668 if (page != mCurrentPage || !mScroller.isFinished()) {
669 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 return true;
671 }
672 return false;
673 }
674
675 @Override
676 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700677 int focusablePage;
678 if (mNextPage != INVALID_PAGE) {
679 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700681 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 }
Winson Chung86f77532010-08-24 11:08:22 -0700683 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700684 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700685 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 }
687 return false;
688 }
689
690 @Override
691 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800692 if (super.dispatchUnhandledMove(focused, direction)) {
693 return true;
694 }
695
696 if (mIsRtl) {
697 if (direction == View.FOCUS_LEFT) {
698 direction = View.FOCUS_RIGHT;
699 } else if (direction == View.FOCUS_RIGHT) {
700 direction = View.FOCUS_LEFT;
701 }
702 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700704 if (getCurrentPage() > 0) {
705 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700706 return true;
707 }
708 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700709 if (getCurrentPage() < getPageCount() - 1) {
710 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 return true;
712 }
713 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800714 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700715 }
716
717 @Override
718 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700719 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
720 return;
721 }
722
Adam Cohen0ffac432013-07-10 11:19:26 -0700723 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700724 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700725 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 }
727 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700728 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700729 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 }
731 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700732 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700733 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 }
735 }
736 }
737
738 /**
739 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700740 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 *
Winson Chung86f77532010-08-24 11:08:22 -0700742 * This happens when live folders requery, and if they're off page, they
743 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700744 */
745 @Override
746 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700747 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 View v = focused;
749 while (true) {
750 if (v == current) {
751 super.focusableViewAvailable(focused);
752 return;
753 }
754 if (v == this) {
755 return;
756 }
757 ViewParent parent = v.getParent();
758 if (parent instanceof View) {
759 v = (View)v.getParent();
760 } else {
761 return;
762 }
763 }
764 }
765
766 /**
767 * {@inheritDoc}
768 */
769 @Override
770 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
771 if (disallowIntercept) {
772 // We need to make sure to cancel our long press if
773 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700774 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700775 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 }
777 super.requestDisallowInterceptTouchEvent(disallowIntercept);
778 }
779
Adam Cohen7d30a372013-07-01 17:03:59 -0700780 /** Returns whether x and y originated within the buffered viewport */
781 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800782 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700783 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700784 }
785
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 @Override
787 public boolean onInterceptTouchEvent(MotionEvent ev) {
788 /*
789 * This method JUST determines whether we want to intercept the motion.
790 * If we return true, onTouchEvent will be called and we do the actual
791 * scrolling there.
792 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800793 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794
Winson Chung45e1d6e2010-11-09 17:19:49 -0800795 // Skip touch handling if there are no pages to swipe
796 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
797
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 /*
799 * Shortcut the most recurring case: the user is in the dragging
800 * state and he is moving his finger. We want to intercept this
801 * motion.
802 */
803 final int action = ev.getAction();
804 if ((action == MotionEvent.ACTION_MOVE) &&
805 (mTouchState == TOUCH_STATE_SCROLLING)) {
806 return true;
807 }
808
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 switch (action & MotionEvent.ACTION_MASK) {
810 case MotionEvent.ACTION_MOVE: {
811 /*
812 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
813 * whether the user has moved far enough from his original down touch.
814 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700815 if (mActivePointerId != INVALID_POINTER) {
816 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700817 }
818 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
819 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
820 // i.e. fall through to the next case (don't break)
821 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
822 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700823 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
825
826 case MotionEvent.ACTION_DOWN: {
827 final float x = ev.getX();
828 final float y = ev.getY();
829 // Remember location of down touch
830 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700831 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800833 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800834 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700836
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 /*
838 * If being flinged and user touches the screen, initiate drag;
839 * otherwise don't. mScroller.isFinished should be false when
840 * being flinged.
841 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700842 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000843 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800844
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700845 if (finishedScrolling) {
846 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800847 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000848 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700849 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800850 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700851 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700852 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
853 mTouchState = TOUCH_STATE_SCROLLING;
854 } else {
855 mTouchState = TOUCH_STATE_REST;
856 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700857 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700858
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 break;
860 }
861
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800863 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700864 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 break;
866
867 case MotionEvent.ACTION_POINTER_UP:
868 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800869 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 break;
871 }
872
873 /*
874 * The only time we want to intercept motion events is if we are in the
875 * drag mode.
876 */
877 return mTouchState != TOUCH_STATE_REST;
878 }
879
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800880 public boolean isHandlingTouch() {
881 return mTouchState != TOUCH_STATE_REST;
882 }
883
Adam Cohenf8d28232011-02-01 21:47:00 -0800884 protected void determineScrollingStart(MotionEvent ev) {
885 determineScrollingStart(ev, 1.0f);
886 }
887
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 /*
889 * Determines if we should change the touch state to start scrolling after the
890 * user moves their touch point too far.
891 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800892 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700893 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700895 if (pointerIndex == -1) return;
896
897 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 final float x = ev.getX(pointerIndex);
899 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -0700900 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
901
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700903
Adam Cohenf8d28232011-02-01 21:47:00 -0800904 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700906
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700907 if (xMoved) {
908 // Scroll if the user moved far enough along the X axis
909 mTouchState = TOUCH_STATE_SCROLLING;
910 mTotalMotionX += Math.abs(mLastMotionX - x);
911 mLastMotionX = x;
912 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700913 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700914 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800915 // Stop listening for things like pinches.
916 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800917 }
918 }
919
920 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700921 // Try canceling the long press. It could also have been scheduled
922 // by a distant descendant, so use the mAllowLongPress flag to block
923 // everything
924 final View currentPage = getPageAt(mCurrentPage);
925 if (currentPage != null) {
926 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 }
928 }
929
Adam Cohenb5ba0972011-09-07 18:02:31 -0700930 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800931 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700932
Adam Cohenedb40762013-07-18 16:45:45 -0700933 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700934 int count = getChildCount();
935
936 final int totalDistance;
937
938 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700939 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700940 adjacentPage = page - 1;
941 }
942
943 if (adjacentPage < 0 || adjacentPage > count - 1) {
944 totalDistance = v.getMeasuredWidth() + mPageSpacing;
945 } else {
946 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
947 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700948
949 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700950 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
951 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700952 return scrollProgress;
953 }
954
Adam Cohenedb40762013-07-18 16:45:45 -0700955 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700956 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -0700957 return 0;
958 } else {
959 return mPageScrolls[index];
960 }
961 }
962
Adam Cohen564a2e72013-10-09 14:47:32 -0700963 // While layout transitions are occurring, a child's position may stray from its baseline
964 // position. This method returns the magnitude of this stray at any given time.
965 public int getLayoutTransitionOffsetForPage(int index) {
966 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
967 return 0;
968 } else {
969 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -0700970
Sunny Goyal228153d2018-01-04 15:35:22 -0800971 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
972 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -0700973 return (int) (child.getX() - baselineX);
974 }
975 }
976
Adam Cohenb5ba0972011-09-07 18:02:31 -0700977 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -0700978 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -0700979
Sunny Goyal228153d2018-01-04 15:35:22 -0800980 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -0700981 if (amount < 0) {
982 mOverScrollX = overScrollAmount;
983 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -0800984 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -0700985 mOverScrollX = mMaxScrollX + overScrollAmount;
986 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -0800987 }
988 invalidate();
989 }
990
Adam Cohenb5ba0972011-09-07 18:02:31 -0700991 protected void overScroll(float amount) {
992 dampedOverScroll(amount);
993 }
994
Tony Wickham1c9bf6a2017-11-17 10:25:27 -0800995
996 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -0800997 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -0800998 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700999 }
1000
Adam Cohenf9618852013-11-08 06:45:03 -08001001 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001002 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001003 mFreeScroll = freeScroll;
1004
Adam Cohenf9618852013-11-08 06:45:03 -08001005 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001006 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001007 } else if (wasFreeScroll) {
1008 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001009 }
1010
1011 setEnableOverscroll(!freeScroll);
1012 }
1013
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001014 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001015 mAllowOverScroll = enable;
1016 }
1017
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 @Override
1019 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001020 super.onTouchEvent(ev);
1021
Winson Chung45e1d6e2010-11-09 17:19:49 -08001022 // Skip touch handling if there are no pages to swipe
1023 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1024
Michael Jurkab8f06722010-10-10 15:58:46 -07001025 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001026
1027 final int action = ev.getAction();
1028
1029 switch (action & MotionEvent.ACTION_MASK) {
1030 case MotionEvent.ACTION_DOWN:
1031 /*
1032 * If being flinged and user touches, stop the fling. isFinished
1033 * will be false if being flinged.
1034 */
1035 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001036 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 }
1038
1039 // Remember where the motion event started
1040 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001041 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001042 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001043 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001045
Michael Jurka0142d492010-08-25 17:46:15 -07001046 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001047 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001048 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001049 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001050 break;
1051
1052 case MotionEvent.ACTION_MOVE:
1053 if (mTouchState == TOUCH_STATE_SCROLLING) {
1054 // Scroll to follow the motion event
1055 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001056
1057 if (pointerIndex == -1) return true;
1058
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001060 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001061
Adam Cohenaefd4e12011-02-14 16:39:38 -08001062 mTotalMotionX += Math.abs(deltaX);
1063
Winson Chungc0844aa2011-02-02 15:25:58 -08001064 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1065 // keep the remainder because we are actually testing if we've moved from the last
1066 // scrolled position (which is discrete).
1067 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001068 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001069 mLastMotionX = x;
1070 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 } else {
1072 awakenScrollBars();
1073 }
Adam Cohen564976a2010-10-13 18:52:07 -07001074 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 determineScrollingStart(ev);
1076 }
1077 break;
1078
1079 case MotionEvent.ACTION_UP:
1080 if (mTouchState == TOUCH_STATE_SCROLLING) {
1081 final int activePointerId = mActivePointerId;
1082 final int pointerIndex = ev.findPointerIndex(activePointerId);
1083 final float x = ev.getX(pointerIndex);
1084 final VelocityTracker velocityTracker = mVelocityTracker;
1085 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1086 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001087 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001088 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001089 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1090 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001091
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001092 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001093 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001094
Adam Cohenf358a4b2013-07-23 16:47:31 -07001095 if (!mFreeScroll) {
1096 // In the case that the page is moved far to one direction and then is flung
1097 // in the opposite direction, we use a threshold to determine whether we should
1098 // just return to the starting page, or if we should skip one further.
1099 boolean returnToOriginalPage = false;
1100 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1101 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1102 returnToOriginalPage = true;
1103 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001104
Adam Cohenf358a4b2013-07-23 16:47:31 -07001105 int finalPage;
1106 // We give flings precedence over large moves, which is why we short-circuit our
1107 // test for a large move if a fling has been registered. That is, a large
1108 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001109 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1110 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001111 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1112 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1113 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1114 snapToPageWithVelocity(finalPage, velocityX);
1115 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1116 (isFling && isVelocityXLeft)) &&
1117 mCurrentPage < getChildCount() - 1) {
1118 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1119 snapToPageWithVelocity(finalPage, velocityX);
1120 } else {
1121 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001122 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001123 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001124 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001125 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001126 }
1127
1128 float scaleX = getScaleX();
1129 int vX = (int) (-velocityX * scaleX);
1130 int initialScrollX = (int) (getScrollX() * scaleX);
1131
Adam Cohenf9618852013-11-08 06:45:03 -08001132 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001133 mScroller.fling(initialScrollX,
1134 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001135 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1136 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1137 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1138 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1139 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1140 && unscaledScrollX < lastPageScroll) {
1141 // Make sure we land directly on a page. If flinging past one of the ends,
1142 // don't change the velocity as it will get stopped at the end anyway.
1143 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1144 // Ensure the scroll/snap doesn't happen too fast;
1145 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1146 - mScroller.getDuration();
1147 if (extraScrollDuration > 0) {
1148 mScroller.extendDuration(extraScrollDuration);
1149 }
1150 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001151 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001154 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1155 // at this point we have not moved beyond the touch slop
1156 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1157 // we can just page
1158 int nextPage = Math.max(0, mCurrentPage - 1);
1159 if (nextPage != mCurrentPage) {
1160 snapToPage(nextPage);
1161 } else {
1162 snapToDestination();
1163 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001164 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 // at this point we have not moved beyond the touch slop
1166 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1167 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001168 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1169 if (nextPage != mCurrentPage) {
1170 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 } else {
1172 snapToDestination();
1173 }
1174 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001175
Adam Cohen7d30a372013-07-01 17:03:59 -07001176 // End any intermediate reordering states
1177 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 break;
1179
1180 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001181 if (mTouchState == TOUCH_STATE_SCROLLING) {
1182 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001183 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001184 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001185 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 break;
1187
1188 case MotionEvent.ACTION_POINTER_UP:
1189 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001190 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 break;
1192 }
1193
1194 return true;
1195 }
1196
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001197 protected boolean shouldFlingForVelocity(int velocityX) {
1198 return Math.abs(velocityX) > mFlingThresholdVelocity;
1199 }
1200
Adam Cohen7d30a372013-07-01 17:03:59 -07001201 private void resetTouchState() {
1202 releaseVelocityTracker();
Adam Cohen7d30a372013-07-01 17:03:59 -07001203 mTouchState = TOUCH_STATE_REST;
1204 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001205 }
1206
Adam Cohenc2d6e892014-10-16 09:49:24 -07001207 /**
1208 * Triggered by scrolling via touch
1209 */
1210 protected void onScrollInteractionBegin() {
1211 }
1212
1213 protected void onScrollInteractionEnd() {
1214 }
1215
Winson Chung185d7162011-02-28 13:47:29 -08001216 @Override
1217 public boolean onGenericMotionEvent(MotionEvent event) {
1218 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1219 switch (event.getAction()) {
1220 case MotionEvent.ACTION_SCROLL: {
1221 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1222 final float vscroll;
1223 final float hscroll;
1224 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1225 vscroll = 0;
1226 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1227 } else {
1228 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1229 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1230 }
1231 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001232 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001233 : (hscroll > 0 || vscroll > 0);
1234 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001235 scrollRight();
1236 } else {
1237 scrollLeft();
1238 }
1239 return true;
1240 }
1241 }
1242 }
1243 }
1244 return super.onGenericMotionEvent(event);
1245 }
1246
Michael Jurkab8f06722010-10-10 15:58:46 -07001247 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1248 if (mVelocityTracker == null) {
1249 mVelocityTracker = VelocityTracker.obtain();
1250 }
1251 mVelocityTracker.addMovement(ev);
1252 }
1253
1254 private void releaseVelocityTracker() {
1255 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001257 mVelocityTracker.recycle();
1258 mVelocityTracker = null;
1259 }
1260 }
1261
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 private void onSecondaryPointerUp(MotionEvent ev) {
1263 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1264 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1265 final int pointerId = ev.getPointerId(pointerIndex);
1266 if (pointerId == mActivePointerId) {
1267 // This was our active pointer going up. Choose a new
1268 // active pointer and adjust accordingly.
1269 // TODO: Make this decision more intelligent.
1270 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1271 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001272 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 mActivePointerId = ev.getPointerId(newPointerIndex);
1274 if (mVelocityTracker != null) {
1275 mVelocityTracker.clear();
1276 }
1277 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001278 }
1279
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 @Override
1281 public void requestChildFocus(View child, View focused) {
1282 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001283 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001284 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001285 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 }
1287 }
1288
Sunny Goyal228153d2018-01-04 15:35:22 -08001289 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001290 return getPageNearestToCenterOfScreen(getScrollX());
1291 }
1292
1293 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001294 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001295 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 final int childCount = getChildCount();
1298 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001299 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001300 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001302 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1304 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1305 minDistanceFromScreenCenter = distanceFromScreenCenter;
1306 minDistanceFromScreenCenterIndex = i;
1307 }
1308 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001309 return minDistanceFromScreenCenterIndex;
1310 }
1311
1312 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001313 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1314 }
1315
1316 protected boolean isInOverScroll() {
1317 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1318 }
1319
1320 protected int getPageSnapDuration() {
1321 if (isInOverScroll()) {
1322 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1323 }
1324 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 }
1326
Adam Cohene0f66b52010-11-23 15:06:07 -08001327 // We want the duration of the page snap animation to be influenced by the distance that
1328 // the screen has to travel, however, we don't want this duration to be effected in a
1329 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1330 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001331 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001332 f -= 0.5f; // center the values about 0.
1333 f *= 0.3f * Math.PI / 2.0f;
1334 return (float) Math.sin(f);
1335 }
1336
Winson Chung05a31ed2018-02-08 17:08:43 -08001337 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001338 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001339 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001340
Adam Cohenedb40762013-07-18 16:45:45 -07001341 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001342 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001343 int duration = 0;
1344
Sunny Goyal4d113a52015-05-27 10:05:28 -07001345 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001346 // If the velocity is low enough, then treat this more as an automatic page advance
1347 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001348 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001349 }
1350
1351 // Here we compute a "distance" that will be used in the computation of the overall
1352 // snap duration. This is a function of the actual distance that needs to be traveled;
1353 // we keep this value close to half screen size in order to reduce the variance in snap
1354 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001355 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001356 float distance = halfScreenSize + halfScreenSize *
1357 distanceInfluenceForSnapDuration(distanceRatio);
1358
1359 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001360 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001361
1362 // we want the page's snap velocity to approximately match the velocity at which the
1363 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001364 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1365 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001366
Winson Chung05a31ed2018-02-08 17:08:43 -08001367 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001368 }
1369
Winson Chung05a31ed2018-02-08 17:08:43 -08001370 public boolean snapToPage(int whichPage) {
1371 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 }
1373
Winson Chung05a31ed2018-02-08 17:08:43 -08001374 public boolean snapToPageImmediately(int whichPage) {
1375 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001376 }
1377
Winson Chung05a31ed2018-02-08 17:08:43 -08001378 public boolean snapToPage(int whichPage, int duration) {
1379 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001380 }
1381
Winson Chung05a31ed2018-02-08 17:08:43 -08001382 protected boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1383 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001384 }
1385
Winson Chung05a31ed2018-02-08 17:08:43 -08001386 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001387 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001388 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001389
Adam Cohenedb40762013-07-18 16:45:45 -07001390 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001391 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001392 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001393 }
1394
Winson Chung05a31ed2018-02-08 17:08:43 -08001395 protected boolean snapToPage(int whichPage, int delta, int duration) {
1396 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 }
Michael Jurka0142d492010-08-25 17:46:15 -07001398
Winson Chung05a31ed2018-02-08 17:08:43 -08001399 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001400 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001401 whichPage = validateNewPage(whichPage);
1402
Adam Cohen7d30a372013-07-01 17:03:59 -07001403 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001406 if (immediate) {
1407 duration = 0;
1408 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 duration = Math.abs(delta);
1410 }
1411
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001412 if (duration != 0) {
1413 pageBeginTransition();
1414 }
1415
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001417 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 }
Adam Cohenf9618852013-11-08 06:45:03 -08001419
1420 if (interpolator != null) {
1421 mScroller.setInterpolator(interpolator);
1422 } else {
1423 mScroller.setInterpolator(mDefaultInterpolator);
1424 }
1425
Sunny Goyalc86df472016-02-25 09:19:38 -08001426 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001427
Adam Cohen674531f2013-12-13 15:07:14 -08001428 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001429
1430 // Trigger a compute() to finish switching pages if necessary
1431 if (immediate) {
1432 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001433 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001434 }
1435
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001437 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 }
1439
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001441 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 }
1443
1444 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001445 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 }
1447
Winson Chung6a0f57d2011-06-29 20:10:49 -07001448 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001449 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001450 @Override
1451 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1452 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001453 info.setScrollable(getPageCount() > 1);
1454 if (getCurrentPage() < getPageCount() - 1) {
1455 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1456 }
1457 if (getCurrentPage() > 0) {
1458 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1459 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001460 info.setClassName(getClass().getName());
1461
1462 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1463 // Besides disabling the accessibility long-click, this also prevents this view from getting
1464 // accessibility focus.
1465 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001466 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001467 }
1468
1469 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001470 public void sendAccessibilityEvent(int eventType) {
1471 // Don't let the view send real scroll events.
1472 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1473 super.sendAccessibilityEvent(eventType);
1474 }
1475 }
1476
1477 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001478 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1479 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001480 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001481 }
1482
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001483 @Override
1484 public boolean performAccessibilityAction(int action, Bundle arguments) {
1485 if (super.performAccessibilityAction(action, arguments)) {
1486 return true;
1487 }
1488 switch (action) {
1489 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1490 if (getCurrentPage() < getPageCount() - 1) {
1491 scrollRight();
1492 return true;
1493 }
1494 } break;
1495 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1496 if (getCurrentPage() > 0) {
1497 scrollLeft();
1498 return true;
1499 }
1500 } break;
1501 }
1502 return false;
1503 }
1504
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001505 protected String getPageIndicatorDescription() {
1506 return getCurrentPageDescription();
1507 }
1508
Adam Cohen0ffac432013-07-10 11:19:26 -07001509 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001510 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001511 getNextPage() + 1, getChildCount());
1512 }
1513
Tony Mak2e564402018-02-27 17:19:34 +00001514 protected float getDownMotionX() {
1515 return mDownMotionX;
1516 }
1517
1518 protected float getDownMotionY() {
1519 return mDownMotionY;
1520 }
1521
Winson Chungd11265e2011-08-30 13:37:23 -07001522 @Override
1523 public boolean onHoverEvent(android.view.MotionEvent event) {
1524 return true;
1525 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001526}