blob: 9eff84b0e364085e389609d6670318f0d6948991 [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;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070059
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070061 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Vadim Tryshevfedca432015-08-19 17:55:02 -070063 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080064 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Sunny Goyalb72d8b22017-07-14 00:02:27 -070066 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070067 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070068
Adam Cohenb64cb5a2011-02-15 13:53:42 -080069 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080070 // The page is moved more than halfway, automatically move to the next page on touch up.
71 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080072
Sunny Goyal8e2133b2015-05-06 13:39:07 -070073 private static final float MAX_SCROLL_PROGRESS = 1.0f;
74
Adam Cohen265b9a62011-12-07 14:37:18 -080075 // The following constants need to be scaled based on density. The scaled versions will be
76 // assigned to the corresponding member variables below.
77 private static final int FLING_THRESHOLD_VELOCITY = 500;
78 private static final int MIN_SNAP_VELOCITY = 1500;
79 private static final int MIN_FLING_VELOCITY = 250;
80
Adam Cohen21cd0022013-10-09 18:57:02 -070081 public static final int INVALID_RESTORE_PAGE = -1001;
82
Adam Cohenf358a4b2013-07-23 16:47:31 -070083 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080084 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070085
Adam Cohen265b9a62011-12-07 14:37:18 -080086 protected int mFlingThresholdVelocity;
87 protected int mMinFlingVelocity;
88 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070089
Michael Jurka0142d492010-08-25 17:46:15 -070090 protected boolean mFirstLayout = true;
91
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070094 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070095
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -080098 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -080099 protected LauncherScroller mScroller;
100 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800102 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
104 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700105 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700106 private float mLastMotionX;
107 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700108 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700109
110 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected final static int TOUCH_STATE_REST = 0;
113 protected final static int TOUCH_STATE_SCROLLING = 1;
114 protected final static int TOUCH_STATE_PREV_PAGE = 2;
115 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700116
Michael Jurka0142d492010-08-25 17:46:15 -0700117 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800118
Michael Jurka7426c422010-11-11 15:23:47 -0800119 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800121 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Michael Jurka5f1c5092010-09-03 14:15:02 -0700123 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka5f1c5092010-09-03 14:15:02 -0700125 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700127 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700128
Sunny Goyal061380a2016-02-29 15:15:03 -0800129 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700130
Adam Cohen8d769d62017-06-23 17:27:38 -0700131 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
132 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
133 // the screens from continuing to translate beyond the normal bounds.
134 protected int mOverScrollX;
135
136 protected int mUnboundedScrollX;
137
Winson Chungd2be3812013-07-16 11:11:32 -0700138 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800140 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700141
Adam Cohen7d30a372013-07-01 17:03:59 -0700142 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700143 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700144 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700145 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700146
John Spurlock77e1f472013-09-11 10:09:51 -0400147 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800148 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400149
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800150 // Similar to the platform implementation of isLayoutValid();
151 protected boolean mIsLayoutValid;
152
Winson Chung321e9ee2010-08-09 13:37:56 -0700153 public PagedView(Context context) {
154 this(context, null);
155 }
156
157 public PagedView(Context context, AttributeSet attrs) {
158 this(context, attrs, 0);
159 }
160
161 public PagedView(Context context, AttributeSet attrs, int defStyle) {
162 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700163
Adam Cohen9c4949e2010-10-05 12:27:22 -0700164 TypedArray a = context.obtainStyledAttributes(attrs,
165 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700166 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700167 a.recycle();
168
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700170 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700171 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700172 }
173
174 /**
175 * Initializes various states for this workspace.
176 */
Michael Jurka0142d492010-08-25 17:46:15 -0700177 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800178 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700179 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700180 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700181
182 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700183 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800185
Sunny Goyalcf25b522015-07-09 00:01:18 -0700186 float density = getResources().getDisplayMetrics().density;
187 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
188 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
189 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700190 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Adam Cohenf9618852013-11-08 06:45:03 -0800193 protected void setDefaultInterpolator(Interpolator interpolator) {
194 mDefaultInterpolator = interpolator;
195 mScroller.setInterpolator(mDefaultInterpolator);
196 }
197
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700198 public void initParentViews(View parent) {
199 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800200 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700201 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800202 mPageIndicator.setPageDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700203 }
204 }
205
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800206 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700207 return mPageIndicator;
208 }
209
Adam Cohen674531f2013-12-13 15:07:14 -0800210 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700211 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
212 * that the user was on before entering free scroll mode (e.g. the home screen page they
213 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
214 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700216 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700217 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700219
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700220 /**
221 * Returns the index of page to be shown immediately afterwards.
222 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700223 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700224 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
225 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700226
Adam Cohenf9c184a2016-01-15 16:47:43 -0800227 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 return getChildCount();
229 }
230
Sunny Goyal83a8f042015-05-19 12:52:12 -0700231 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 return getChildAt(index);
233 }
234
Adam Cohenae4f1552011-10-20 00:15:42 -0700235 protected int indexToPage(int index) {
236 return index;
237 }
238
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800240 * Updates the scroll of the current page immediately to its final scroll position. We use this
241 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
242 * the previous tab page.
243 */
244 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700245 // If the current page is invalid, just reset the scroll position to zero
246 int newX = 0;
247 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700248 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700249 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800250 scrollTo(newX, 0);
251 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700252 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800253 }
254
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700255 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700256 mScroller.abortAnimation();
257 // We need to clean up the next page here to avoid computeScrollHelper from
258 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700259 if (resetNextPage) {
260 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800261 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700262 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700263 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700264
Tony Wickham8f7ead32016-04-07 18:46:44 -0700265 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700266 mScroller.forceFinished(true);
267 // We need to clean up the next page here to avoid computeScrollHelper from
268 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700269 if (resetNextPage) {
270 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800271 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700272 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700273 }
274
Adam Cohen6f127a62014-06-12 14:54:41 -0700275 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700276 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700277 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700278 }
279
Chet Haasebc2f0822012-10-26 17:59:53 -0700280 /**
Winson Chung86f77532010-08-24 11:08:22 -0700281 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700283 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800284 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700285 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800286 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800287 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
288 // the default
289 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800290 return;
291 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700292 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700293 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700294 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700295 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800296 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 }
298
Winson Chung8c87cd82013-07-23 16:20:10 -0700299 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800300 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
301 * has settled.
302 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700303 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800304 updatePageIndicator();
305 }
306
307 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700308 if (mPageIndicator != null) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800309 mPageIndicator.setPageDescription(getPageIndicatorDescription());
Tony Mak4f521af2018-03-15 16:37:34 +0000310 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700311 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700313 protected void pageBeginTransition() {
314 if (!mIsPageInTransition) {
315 mIsPageInTransition = true;
316 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700317 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700318 }
319
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700320 protected void pageEndTransition() {
321 if (mIsPageInTransition) {
322 mIsPageInTransition = false;
323 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700324 }
Michael Jurka0142d492010-08-25 17:46:15 -0700325 }
326
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700327 protected boolean isPageInTransition() {
328 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700329 }
330
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700331 /**
332 * Called when the page starts moving as part of the scroll. Subclasses can override this
333 * to provide custom behavior during animation.
334 */
335 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700336 }
337
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700338 /**
339 * Called when the page ends moving as part of the scroll. Subclasses can override this
340 * to provide custom behavior during animation.
341 */
342 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700343 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700344 }
345
Sunny Goyalc86df472016-02-25 09:19:38 -0800346 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700347 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800348 }
349
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800351 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800352 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800353 }
354
355 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700356 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700357 // In free scroll mode, we clamp the scrollX
358 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700359 // If the scroller is trying to move to a location beyond the maximum allowed
360 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700361 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700362 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700363 }
364
Sunny Goyal4d519f22017-10-24 10:32:40 -0700365 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700366 }
367
Adam Cohen8d769d62017-06-23 17:27:38 -0700368 mUnboundedScrollX = x;
369
Sunny Goyal70660032015-05-14 00:07:08 -0700370 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
371 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700372 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700373 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800374 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700375 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700376 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700377 overScroll(x - mMaxScrollX);
378 } else {
379 overScroll(x);
380 }
Adam Cohen68d73932010-11-15 10:50:58 -0800381 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700382 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700383 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800384 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700385 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700386 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700387 overScroll(x);
388 } else {
389 overScroll(x - mMaxScrollX);
390 }
Adam Cohen68d73932010-11-15 10:50:58 -0800391 }
392 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700393 if (mWasInOverscroll) {
394 overScroll(0);
395 mWasInOverscroll = false;
396 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700397 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800398 super.scrollTo(x, y);
399 }
Michael Jurka0142d492010-08-25 17:46:15 -0700400 }
401
Adam Cohen53805212013-10-01 10:39:23 -0700402 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800403 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700404 if (mCurrentPage != getNextPage()) {
405 AccessibilityEvent ev =
406 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700407 ev.setScrollable(true);
408 ev.setScrollX(getScrollX());
409 ev.setScrollY(getScrollY());
410 ev.setMaxScrollX(mMaxScrollX);
411 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700412
Vadim Tryshevf4715972015-05-08 17:21:03 -0700413 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700414 }
Adam Cohen53805212013-10-01 10:39:23 -0700415 }
416 }
417
Michael Jurka0142d492010-08-25 17:46:15 -0700418 // we moved this functionality to a helper function so SmoothPagedView can reuse it
419 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800420 return computeScrollHelper(true);
421 }
422
423 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700424 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700425 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800426 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700427 || getScrollY() != mScroller.getCurrY()
428 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700429 float scaleX = mFreeScroll ? getScaleX() : 1f;
430 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
431 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700432 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800433 if (shouldInvalidate) {
434 invalidate();
435 }
Michael Jurka0142d492010-08-25 17:46:15 -0700436 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800437 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700438 sendScrollAccessibilityEvent();
439
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700440 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700441 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700442 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700443 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700444
Adam Cohen73aa9752010-11-24 16:26:58 -0800445 // We don't want to trigger a page end moving unless the page has settled
446 // and the user has stopped scrolling
447 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700448 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800449 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700450
Sunny Goyald0030b02017-12-08 15:07:24 -0800451 if (isAccessibilityEnabled(getContext())) {
Adam Cohen53805212013-10-01 10:39:23 -0700452 // Notify the user when the page changes
453 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700454 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 }
Michael Jurka0142d492010-08-25 17:46:15 -0700456 return false;
457 }
458
459 @Override
460 public void computeScroll() {
461 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 }
463
Sunny Goyalce8809a2018-01-18 14:02:47 -0800464 public int getExpectedHeight() {
465 return getMeasuredHeight();
466 }
467
Adam Cohen410f3cd2013-09-22 12:09:32 -0700468 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800469 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800470 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700471 }
472
Sunny Goyalce8809a2018-01-18 14:02:47 -0800473 public int getExpectedWidth() {
474 return getMeasuredWidth();
475 }
476
Sunny Goyalbc683e92017-12-06 10:25:07 -0800477 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800478 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800479 - mInsets.left - mInsets.right;
480 }
481
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800483 public void requestLayout() {
484 mIsLayoutValid = false;
485 super.requestLayout();
486 }
487
488 @Override
489 public void forceLayout() {
490 mIsLayoutValid = false;
491 super.forceLayout();
492 }
493
494 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700496 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700497 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
498 return;
499 }
500
Adam Cohen7d30a372013-07-01 17:03:59 -0700501 // We measure the dimensions of the PagedView to be larger than the pages so that when we
502 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700503 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
504 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
505 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700506 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700507
Adam Cohen7d30a372013-07-01 17:03:59 -0700508 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
509 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
510 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700511 }
512
Winson Chung8aad6102012-05-11 16:27:49 -0700513 // Return early if we aren't given a proper dimension
514 if (widthSize <= 0 || heightSize <= 0) {
515 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
516 return;
517 }
518
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700520 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700521 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700522
Sunny Goyalac00cba2017-11-13 15:58:01 -0800523 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800524 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800525 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800526 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700527
Sunny Goyalac00cba2017-11-13 15:58:01 -0800528 // measureChildren takes accounts for content padding, we only need to care about extra
529 // space due to insets.
530 measureChildren(myWidthSpec, myHeightSpec);
531 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800532 }
533
Sunny Goyalcf25b522015-07-09 00:01:18 -0700534 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700536 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800537 mIsLayoutValid = true;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700538 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700539 return;
540 }
541
Winson Chung785d2eb2011-04-14 16:08:02 -0700542 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700544
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800545 boolean pageScrollChanged = false;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700546 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
547 mPageScrolls = new int[childCount];
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800548 pageScrollChanged = true;
Adam Cohenedb40762013-07-18 16:45:45 -0700549 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700550 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
551 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700553
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700554 final LayoutTransition transition = getLayoutTransition();
555 // If the transition is running defer updating max scroll, as some empty pages could
556 // still be present, and a max scroll change could cause sudden jumps in scroll.
557 if (transition != null && transition.isRunning()) {
558 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
559
560 @Override
561 public void startTransition(LayoutTransition transition, ViewGroup container,
562 View view, int transitionType) { }
563
564 @Override
565 public void endTransition(LayoutTransition transition, ViewGroup container,
566 View view, int transitionType) {
567 // Wait until all transitions are complete.
568 if (!transition.isRunning()) {
569 transition.removeTransitionListener(this);
570 updateMaxScrollX();
571 }
572 }
573 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700574 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700575 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700576 }
577
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700578 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
579 updateCurrentPageScroll();
580 mFirstLayout = false;
581 }
582
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800583 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700584 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700585 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700586 mChildCountOnLastLayout = childCount;
Winson Chunge3193b92010-09-10 11:44:42 -0700587 }
588
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700589 /**
590 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
591 * of {@code outPageScrolls} should be same as the the childCount
592 *
593 */
594 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
595 ComputePageScrollsLogic scrollLogic) {
596 final int childCount = getChildCount();
597
598 final int startIndex = mIsRtl ? childCount - 1 : 0;
599 final int endIndex = mIsRtl ? -1 : childCount;
600 final int delta = mIsRtl ? -1 : 1;
601
602 int verticalPadding = getPaddingTop() + getPaddingBottom();
603
604 int scrollOffsetLeft = mInsets.left + getPaddingLeft();
605 int childLeft = scrollOffsetLeft;
606 boolean pageScrollChanged = false;
607
608 for (int i = startIndex; i != endIndex; i += delta) {
609 final View child = getPageAt(i);
610 if (scrollLogic.shouldIncludeView(child)) {
611 int childTop = getPaddingTop() + mInsets.top;
612 childTop += (getMeasuredHeight() - mInsets.top - mInsets.bottom - verticalPadding
613 - child.getMeasuredHeight()) / 2;
614 final int childWidth = child.getMeasuredWidth();
615
616 if (layoutChildren) {
617 final int childHeight = child.getMeasuredHeight();
618 child.layout(childLeft, childTop,
619 childLeft + child.getMeasuredWidth(), childTop + childHeight);
620 }
621
622 final int pageScroll = childLeft - scrollOffsetLeft;
623 if (outPageScrolls[i] != pageScroll) {
624 pageScrollChanged = true;
625 outPageScrolls[i] = pageScroll;
626 }
627
628 childLeft += childWidth + mPageSpacing + getChildGap();
629 }
630 }
631 return pageScrollChanged;
632 }
633
Sunny Goyala1fbd842015-05-20 13:40:27 -0700634 protected int getChildGap() {
635 return 0;
636 }
637
Sunny Goyal4d519f22017-10-24 10:32:40 -0700638 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700639 mMaxScrollX = computeMaxScrollX();
640 }
641
642 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700643 int childCount = getChildCount();
644 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700645 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700646 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700647 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700648 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700649 }
650 }
651
Adam Cohen3b185e22013-10-29 14:45:58 -0700652 public void setPageSpacing(int pageSpacing) {
653 mPageSpacing = pageSpacing;
654 requestLayout();
655 }
656
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800657 private void dispatchPageCountChanged() {
658 if (mPageIndicator != null) {
659 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700660 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700661 // This ensures that when children are added, they get the correct transforms / alphas
662 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700663 invalidate();
664 }
665
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800667 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800668 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800669 dispatchPageCountChanged();
670 }
671
672 @Override
673 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800674 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700675 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800676 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700677 }
678
Adam Cohen73894962011-10-31 13:17:17 -0700679 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700680 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800681 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700682 }
683
Winson Chung321e9ee2010-08-09 13:37:56 -0700684 @Override
685 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700686 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700687 if (page != mCurrentPage || !mScroller.isFinished()) {
688 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 return true;
690 }
691 return false;
692 }
693
694 @Override
695 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700696 int focusablePage;
697 if (mNextPage != INVALID_PAGE) {
698 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700700 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 }
Winson Chung86f77532010-08-24 11:08:22 -0700702 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700704 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 }
706 return false;
707 }
708
709 @Override
710 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800711 if (super.dispatchUnhandledMove(focused, direction)) {
712 return true;
713 }
714
715 if (mIsRtl) {
716 if (direction == View.FOCUS_LEFT) {
717 direction = View.FOCUS_RIGHT;
718 } else if (direction == View.FOCUS_RIGHT) {
719 direction = View.FOCUS_LEFT;
720 }
721 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700723 if (getCurrentPage() > 0) {
724 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 return true;
726 }
727 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700728 if (getCurrentPage() < getPageCount() - 1) {
729 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 return true;
731 }
732 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800733 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 }
735
736 @Override
737 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700738 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
739 return;
740 }
741
Adam Cohen0ffac432013-07-10 11:19:26 -0700742 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700743 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700744 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 }
746 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700747 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700748 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700749 }
750 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700751 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700752 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 }
754 }
755 }
756
757 /**
758 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700759 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 *
Winson Chung86f77532010-08-24 11:08:22 -0700761 * This happens when live folders requery, and if they're off page, they
762 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 */
764 @Override
765 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700766 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 View v = focused;
768 while (true) {
769 if (v == current) {
770 super.focusableViewAvailable(focused);
771 return;
772 }
773 if (v == this) {
774 return;
775 }
776 ViewParent parent = v.getParent();
777 if (parent instanceof View) {
778 v = (View)v.getParent();
779 } else {
780 return;
781 }
782 }
783 }
784
785 /**
786 * {@inheritDoc}
787 */
788 @Override
789 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
790 if (disallowIntercept) {
791 // We need to make sure to cancel our long press if
792 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700793 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700794 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 }
796 super.requestDisallowInterceptTouchEvent(disallowIntercept);
797 }
798
Adam Cohen7d30a372013-07-01 17:03:59 -0700799 /** Returns whether x and y originated within the buffered viewport */
800 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800801 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700802 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700803 }
804
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 @Override
806 public boolean onInterceptTouchEvent(MotionEvent ev) {
807 /*
808 * This method JUST determines whether we want to intercept the motion.
809 * If we return true, onTouchEvent will be called and we do the actual
810 * scrolling there.
811 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800812 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700813
Winson Chung45e1d6e2010-11-09 17:19:49 -0800814 // Skip touch handling if there are no pages to swipe
815 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
816
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 /*
818 * Shortcut the most recurring case: the user is in the dragging
819 * state and he is moving his finger. We want to intercept this
820 * motion.
821 */
822 final int action = ev.getAction();
823 if ((action == MotionEvent.ACTION_MOVE) &&
824 (mTouchState == TOUCH_STATE_SCROLLING)) {
825 return true;
826 }
827
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 switch (action & MotionEvent.ACTION_MASK) {
829 case MotionEvent.ACTION_MOVE: {
830 /*
831 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
832 * whether the user has moved far enough from his original down touch.
833 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700834 if (mActivePointerId != INVALID_POINTER) {
835 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700836 }
837 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
838 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
839 // i.e. fall through to the next case (don't break)
840 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
841 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700842 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844
845 case MotionEvent.ACTION_DOWN: {
846 final float x = ev.getX();
847 final float y = ev.getY();
848 // Remember location of down touch
849 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700850 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800852 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800853 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700855
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 /*
857 * If being flinged and user touches the screen, initiate drag;
858 * otherwise don't. mScroller.isFinished should be false when
859 * being flinged.
860 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700861 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000862 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800863
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700864 if (finishedScrolling) {
865 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800866 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000867 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700868 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800869 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700870 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700871 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
872 mTouchState = TOUCH_STATE_SCROLLING;
873 } else {
874 mTouchState = TOUCH_STATE_REST;
875 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700876 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700877
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 break;
879 }
880
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800882 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700883 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 break;
885
886 case MotionEvent.ACTION_POINTER_UP:
887 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800888 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 break;
890 }
891
892 /*
893 * The only time we want to intercept motion events is if we are in the
894 * drag mode.
895 */
896 return mTouchState != TOUCH_STATE_REST;
897 }
898
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800899 public boolean isHandlingTouch() {
900 return mTouchState != TOUCH_STATE_REST;
901 }
902
Adam Cohenf8d28232011-02-01 21:47:00 -0800903 protected void determineScrollingStart(MotionEvent ev) {
904 determineScrollingStart(ev, 1.0f);
905 }
906
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 /*
908 * Determines if we should change the touch state to start scrolling after the
909 * user moves their touch point too far.
910 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800911 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700912 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700913 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700914 if (pointerIndex == -1) return;
915
916 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 final float x = ev.getX(pointerIndex);
918 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -0700919 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
920
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700922
Adam Cohenf8d28232011-02-01 21:47:00 -0800923 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700925
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700926 if (xMoved) {
927 // Scroll if the user moved far enough along the X axis
928 mTouchState = TOUCH_STATE_SCROLLING;
929 mTotalMotionX += Math.abs(mLastMotionX - x);
930 mLastMotionX = x;
931 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700932 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700933 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800934 // Stop listening for things like pinches.
935 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800936 }
937 }
938
939 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700940 // Try canceling the long press. It could also have been scheduled
941 // by a distant descendant, so use the mAllowLongPress flag to block
942 // everything
943 final View currentPage = getPageAt(mCurrentPage);
944 if (currentPage != null) {
945 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 }
947 }
948
Adam Cohenb5ba0972011-09-07 18:02:31 -0700949 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800950 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700951
Adam Cohenedb40762013-07-18 16:45:45 -0700952 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700953 int count = getChildCount();
954
955 final int totalDistance;
956
957 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700958 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700959 adjacentPage = page - 1;
960 }
961
962 if (adjacentPage < 0 || adjacentPage > count - 1) {
963 totalDistance = v.getMeasuredWidth() + mPageSpacing;
964 } else {
965 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
966 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700967
968 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700969 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
970 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700971 return scrollProgress;
972 }
973
Adam Cohenedb40762013-07-18 16:45:45 -0700974 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700975 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -0700976 return 0;
977 } else {
978 return mPageScrolls[index];
979 }
980 }
981
Adam Cohen564a2e72013-10-09 14:47:32 -0700982 // While layout transitions are occurring, a child's position may stray from its baseline
983 // position. This method returns the magnitude of this stray at any given time.
984 public int getLayoutTransitionOffsetForPage(int index) {
985 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
986 return 0;
987 } else {
988 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -0700989
Sunny Goyal228153d2018-01-04 15:35:22 -0800990 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
991 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -0700992 return (int) (child.getX() - baselineX);
993 }
994 }
995
Adam Cohenb5ba0972011-09-07 18:02:31 -0700996 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -0700997 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -0700998
Sunny Goyal228153d2018-01-04 15:35:22 -0800999 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001000 if (amount < 0) {
1001 mOverScrollX = overScrollAmount;
1002 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001003 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001004 mOverScrollX = mMaxScrollX + overScrollAmount;
1005 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001006 }
1007 invalidate();
1008 }
1009
Adam Cohenb5ba0972011-09-07 18:02:31 -07001010 protected void overScroll(float amount) {
1011 dampedOverScroll(amount);
1012 }
1013
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001014
1015 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001016 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001017 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001018 }
1019
Adam Cohenf9618852013-11-08 06:45:03 -08001020 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001021 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001022 mFreeScroll = freeScroll;
1023
Adam Cohenf9618852013-11-08 06:45:03 -08001024 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001025 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001026 } else if (wasFreeScroll) {
1027 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001028 }
1029
1030 setEnableOverscroll(!freeScroll);
1031 }
1032
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001033 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001034 mAllowOverScroll = enable;
1035 }
1036
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 @Override
1038 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001039 super.onTouchEvent(ev);
1040
Winson Chung45e1d6e2010-11-09 17:19:49 -08001041 // Skip touch handling if there are no pages to swipe
1042 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1043
Michael Jurkab8f06722010-10-10 15:58:46 -07001044 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001045
1046 final int action = ev.getAction();
1047
1048 switch (action & MotionEvent.ACTION_MASK) {
1049 case MotionEvent.ACTION_DOWN:
1050 /*
1051 * If being flinged and user touches, stop the fling. isFinished
1052 * will be false if being flinged.
1053 */
1054 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001055 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 }
1057
1058 // Remember where the motion event started
1059 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001060 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001061 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001062 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001064
Michael Jurka0142d492010-08-25 17:46:15 -07001065 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001066 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001067 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001068 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 break;
1070
1071 case MotionEvent.ACTION_MOVE:
1072 if (mTouchState == TOUCH_STATE_SCROLLING) {
1073 // Scroll to follow the motion event
1074 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001075
1076 if (pointerIndex == -1) return true;
1077
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001079 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001080
Adam Cohenaefd4e12011-02-14 16:39:38 -08001081 mTotalMotionX += Math.abs(deltaX);
1082
Winson Chungc0844aa2011-02-02 15:25:58 -08001083 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1084 // keep the remainder because we are actually testing if we've moved from the last
1085 // scrolled position (which is discrete).
1086 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001087 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001088 mLastMotionX = x;
1089 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001090 } else {
1091 awakenScrollBars();
1092 }
Adam Cohen564976a2010-10-13 18:52:07 -07001093 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 determineScrollingStart(ev);
1095 }
1096 break;
1097
1098 case MotionEvent.ACTION_UP:
1099 if (mTouchState == TOUCH_STATE_SCROLLING) {
1100 final int activePointerId = mActivePointerId;
1101 final int pointerIndex = ev.findPointerIndex(activePointerId);
1102 final float x = ev.getX(pointerIndex);
1103 final VelocityTracker velocityTracker = mVelocityTracker;
1104 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1105 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001106 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001107 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001108 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1109 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001110
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001111 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001112 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001113
Adam Cohenf358a4b2013-07-23 16:47:31 -07001114 if (!mFreeScroll) {
1115 // In the case that the page is moved far to one direction and then is flung
1116 // in the opposite direction, we use a threshold to determine whether we should
1117 // just return to the starting page, or if we should skip one further.
1118 boolean returnToOriginalPage = false;
1119 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1120 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1121 returnToOriginalPage = true;
1122 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001123
Adam Cohenf358a4b2013-07-23 16:47:31 -07001124 int finalPage;
1125 // We give flings precedence over large moves, which is why we short-circuit our
1126 // test for a large move if a fling has been registered. That is, a large
1127 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001128 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1129 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001130 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1131 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1132 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1133 snapToPageWithVelocity(finalPage, velocityX);
1134 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1135 (isFling && isVelocityXLeft)) &&
1136 mCurrentPage < getChildCount() - 1) {
1137 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1138 snapToPageWithVelocity(finalPage, velocityX);
1139 } else {
1140 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001141 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001143 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001144 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001145 }
1146
1147 float scaleX = getScaleX();
1148 int vX = (int) (-velocityX * scaleX);
1149 int initialScrollX = (int) (getScrollX() * scaleX);
1150
Adam Cohenf9618852013-11-08 06:45:03 -08001151 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001152 mScroller.fling(initialScrollX,
1153 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001154 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1155 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1156 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1157 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1158 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1159 && unscaledScrollX < lastPageScroll) {
1160 // Make sure we land directly on a page. If flinging past one of the ends,
1161 // don't change the velocity as it will get stopped at the end anyway.
1162 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1163 // Ensure the scroll/snap doesn't happen too fast;
1164 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1165 - mScroller.getDuration();
1166 if (extraScrollDuration > 0) {
1167 mScroller.extendDuration(extraScrollDuration);
1168 }
1169 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001170 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001173 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1174 // at this point we have not moved beyond the touch slop
1175 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1176 // we can just page
1177 int nextPage = Math.max(0, mCurrentPage - 1);
1178 if (nextPage != mCurrentPage) {
1179 snapToPage(nextPage);
1180 } else {
1181 snapToDestination();
1182 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001183 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 // at this point we have not moved beyond the touch slop
1185 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1186 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001187 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1188 if (nextPage != mCurrentPage) {
1189 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 } else {
1191 snapToDestination();
1192 }
1193 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001194
Adam Cohen7d30a372013-07-01 17:03:59 -07001195 // End any intermediate reordering states
1196 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 break;
1198
1199 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001200 if (mTouchState == TOUCH_STATE_SCROLLING) {
1201 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001202 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001203 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001204 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 break;
1206
1207 case MotionEvent.ACTION_POINTER_UP:
1208 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001209 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 break;
1211 }
1212
1213 return true;
1214 }
1215
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001216 protected boolean shouldFlingForVelocity(int velocityX) {
1217 return Math.abs(velocityX) > mFlingThresholdVelocity;
1218 }
1219
Adam Cohen7d30a372013-07-01 17:03:59 -07001220 private void resetTouchState() {
1221 releaseVelocityTracker();
Adam Cohen7d30a372013-07-01 17:03:59 -07001222 mTouchState = TOUCH_STATE_REST;
1223 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001224 }
1225
Adam Cohenc2d6e892014-10-16 09:49:24 -07001226 /**
1227 * Triggered by scrolling via touch
1228 */
1229 protected void onScrollInteractionBegin() {
1230 }
1231
1232 protected void onScrollInteractionEnd() {
1233 }
1234
Winson Chung185d7162011-02-28 13:47:29 -08001235 @Override
1236 public boolean onGenericMotionEvent(MotionEvent event) {
1237 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1238 switch (event.getAction()) {
1239 case MotionEvent.ACTION_SCROLL: {
1240 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1241 final float vscroll;
1242 final float hscroll;
1243 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1244 vscroll = 0;
1245 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1246 } else {
1247 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1248 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1249 }
1250 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001251 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001252 : (hscroll > 0 || vscroll > 0);
1253 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001254 scrollRight();
1255 } else {
1256 scrollLeft();
1257 }
1258 return true;
1259 }
1260 }
1261 }
1262 }
1263 return super.onGenericMotionEvent(event);
1264 }
1265
Michael Jurkab8f06722010-10-10 15:58:46 -07001266 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1267 if (mVelocityTracker == null) {
1268 mVelocityTracker = VelocityTracker.obtain();
1269 }
1270 mVelocityTracker.addMovement(ev);
1271 }
1272
1273 private void releaseVelocityTracker() {
1274 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001275 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001276 mVelocityTracker.recycle();
1277 mVelocityTracker = null;
1278 }
1279 }
1280
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 private void onSecondaryPointerUp(MotionEvent ev) {
1282 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1283 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1284 final int pointerId = ev.getPointerId(pointerIndex);
1285 if (pointerId == mActivePointerId) {
1286 // This was our active pointer going up. Choose a new
1287 // active pointer and adjust accordingly.
1288 // TODO: Make this decision more intelligent.
1289 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1290 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001291 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001292 mActivePointerId = ev.getPointerId(newPointerIndex);
1293 if (mVelocityTracker != null) {
1294 mVelocityTracker.clear();
1295 }
1296 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001297 }
1298
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 @Override
1300 public void requestChildFocus(View child, View focused) {
1301 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001302 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001303 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001304 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 }
1306 }
1307
Sunny Goyal228153d2018-01-04 15:35:22 -08001308 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001309 return getPageNearestToCenterOfScreen(getScrollX());
1310 }
1311
1312 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001313 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001314 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 final int childCount = getChildCount();
1317 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001318 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001319 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001321 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1323 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1324 minDistanceFromScreenCenter = distanceFromScreenCenter;
1325 minDistanceFromScreenCenterIndex = i;
1326 }
1327 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001328 return minDistanceFromScreenCenterIndex;
1329 }
1330
1331 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001332 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1333 }
1334
1335 protected boolean isInOverScroll() {
1336 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1337 }
1338
1339 protected int getPageSnapDuration() {
1340 if (isInOverScroll()) {
1341 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1342 }
1343 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 }
1345
Adam Cohene0f66b52010-11-23 15:06:07 -08001346 // We want the duration of the page snap animation to be influenced by the distance that
1347 // the screen has to travel, however, we don't want this duration to be effected in a
1348 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1349 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001350 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001351 f -= 0.5f; // center the values about 0.
1352 f *= 0.3f * Math.PI / 2.0f;
1353 return (float) Math.sin(f);
1354 }
1355
Winson Chung05a31ed2018-02-08 17:08:43 -08001356 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001357 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001358 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001359
Adam Cohenedb40762013-07-18 16:45:45 -07001360 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001361 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001362 int duration = 0;
1363
Sunny Goyal4d113a52015-05-27 10:05:28 -07001364 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001365 // If the velocity is low enough, then treat this more as an automatic page advance
1366 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001367 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001368 }
1369
1370 // Here we compute a "distance" that will be used in the computation of the overall
1371 // snap duration. This is a function of the actual distance that needs to be traveled;
1372 // we keep this value close to half screen size in order to reduce the variance in snap
1373 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001374 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001375 float distance = halfScreenSize + halfScreenSize *
1376 distanceInfluenceForSnapDuration(distanceRatio);
1377
1378 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001379 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001380
1381 // we want the page's snap velocity to approximately match the velocity at which the
1382 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001383 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1384 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001385
Winson Chung05a31ed2018-02-08 17:08:43 -08001386 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001387 }
1388
Winson Chung05a31ed2018-02-08 17:08:43 -08001389 public boolean snapToPage(int whichPage) {
1390 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 }
1392
Winson Chung05a31ed2018-02-08 17:08:43 -08001393 public boolean snapToPageImmediately(int whichPage) {
1394 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 }
1396
Winson Chung05a31ed2018-02-08 17:08:43 -08001397 public boolean snapToPage(int whichPage, int duration) {
1398 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001399 }
1400
Winson Chung05a31ed2018-02-08 17:08:43 -08001401 protected boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1402 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001403 }
1404
Winson Chung05a31ed2018-02-08 17:08:43 -08001405 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001406 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001407 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001408
Adam Cohenedb40762013-07-18 16:45:45 -07001409 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001410 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001411 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001412 }
1413
Winson Chung05a31ed2018-02-08 17:08:43 -08001414 protected boolean snapToPage(int whichPage, int delta, int duration) {
1415 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001416 }
Michael Jurka0142d492010-08-25 17:46:15 -07001417
Winson Chung05a31ed2018-02-08 17:08:43 -08001418 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001419 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001420 whichPage = validateNewPage(whichPage);
1421
Adam Cohen7d30a372013-07-01 17:03:59 -07001422 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001425 if (immediate) {
1426 duration = 0;
1427 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 duration = Math.abs(delta);
1429 }
1430
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001431 if (duration != 0) {
1432 pageBeginTransition();
1433 }
1434
Adam Cohenf358a4b2013-07-23 16:47:31 -07001435 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001436 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001437 }
Adam Cohenf9618852013-11-08 06:45:03 -08001438
1439 if (interpolator != null) {
1440 mScroller.setInterpolator(interpolator);
1441 } else {
1442 mScroller.setInterpolator(mDefaultInterpolator);
1443 }
1444
Sunny Goyalc86df472016-02-25 09:19:38 -08001445 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001446
Adam Cohen674531f2013-12-13 15:07:14 -08001447 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001448
1449 // Trigger a compute() to finish switching pages if necessary
1450 if (immediate) {
1451 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001452 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001453 }
1454
Winson Chung321e9ee2010-08-09 13:37:56 -07001455 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001456 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 }
1458
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001460 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 }
1462
1463 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001464 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 }
1466
Winson Chung6a0f57d2011-06-29 20:10:49 -07001467 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001468 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001469 @Override
1470 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1471 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001472 info.setScrollable(getPageCount() > 1);
1473 if (getCurrentPage() < getPageCount() - 1) {
1474 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1475 }
1476 if (getCurrentPage() > 0) {
1477 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1478 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001479 info.setClassName(getClass().getName());
1480
1481 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1482 // Besides disabling the accessibility long-click, this also prevents this view from getting
1483 // accessibility focus.
1484 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001485 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001486 }
1487
1488 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001489 public void sendAccessibilityEvent(int eventType) {
1490 // Don't let the view send real scroll events.
1491 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1492 super.sendAccessibilityEvent(eventType);
1493 }
1494 }
1495
1496 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001497 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1498 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001499 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001500 }
1501
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001502 @Override
1503 public boolean performAccessibilityAction(int action, Bundle arguments) {
1504 if (super.performAccessibilityAction(action, arguments)) {
1505 return true;
1506 }
1507 switch (action) {
1508 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1509 if (getCurrentPage() < getPageCount() - 1) {
1510 scrollRight();
1511 return true;
1512 }
1513 } break;
1514 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1515 if (getCurrentPage() > 0) {
1516 scrollLeft();
1517 return true;
1518 }
1519 } break;
1520 }
1521 return false;
1522 }
1523
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001524 protected String getPageIndicatorDescription() {
1525 return getCurrentPageDescription();
1526 }
1527
Adam Cohen0ffac432013-07-10 11:19:26 -07001528 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001529 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001530 getNextPage() + 1, getChildCount());
1531 }
1532
Tony Mak2e564402018-02-27 17:19:34 +00001533 protected float getDownMotionX() {
1534 return mDownMotionX;
1535 }
1536
1537 protected float getDownMotionY() {
1538 return mDownMotionY;
1539 }
1540
Winson Chungd11265e2011-08-30 13:37:23 -07001541 @Override
1542 public boolean onHoverEvent(android.view.MotionEvent event) {
1543 return true;
1544 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001545
1546 protected interface ComputePageScrollsLogic {
1547
1548 boolean shouldIncludeView(View view);
1549 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001550}