blob: 62b581f83eefd7520049e5f8c7c6b4bb1cd2272d [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyald0030b02017-12-08 15:07:24 -080019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070027import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070029import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070030import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.ViewGroup;
41import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070045import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070046
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070047import com.android.launcher3.anim.Interpolators;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070048import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070050import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070051import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
Winson Chung6a0f57d2011-06-29 20:10:49 -070053import java.util.ArrayList;
54
Winson Chung321e9ee2010-08-09 13:37:56 -070055/**
56 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070057 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070058 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080059public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070060 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070061 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070062
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070064 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Vadim Tryshevfedca432015-08-19 17:55:02 -070066 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080067 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Sunny Goyalb72d8b22017-07-14 00:02:27 -070069 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070070 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070071
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080073 // The page is moved more than halfway, automatically move to the next page on touch up.
74 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Sunny Goyal8e2133b2015-05-06 13:39:07 -070076 private static final float MAX_SCROLL_PROGRESS = 1.0f;
77
Adam Cohen265b9a62011-12-07 14:37:18 -080078 // The following constants need to be scaled based on density. The scaled versions will be
79 // assigned to the corresponding member variables below.
80 private static final int FLING_THRESHOLD_VELOCITY = 500;
81 private static final int MIN_SNAP_VELOCITY = 1500;
82 private static final int MIN_FLING_VELOCITY = 250;
83
Adam Cohen21cd0022013-10-09 18:57:02 -070084 public static final int INVALID_RESTORE_PAGE = -1001;
85
Adam Cohenf358a4b2013-07-23 16:47:31 -070086 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080087 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070088
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
94
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -070097
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800100 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800101 protected LauncherScroller mScroller;
102 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800104 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
106 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700107 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700108 private float mLastMotionX;
109 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700110 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700111
Vadim Trysheve628c472018-05-16 14:28:18 -0700112 protected int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Michael Jurka0142d492010-08-25 17:46:15 -0700114 protected final static int TOUCH_STATE_REST = 0;
115 protected final static int TOUCH_STATE_SCROLLING = 1;
116 protected final static int TOUCH_STATE_PREV_PAGE = 2;
117 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700118
Michael Jurka0142d492010-08-25 17:46:15 -0700119 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800120
Michael Jurka7426c422010-11-11 15:23:47 -0800121 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800123 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka5f1c5092010-09-03 14:15:02 -0700125 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Michael Jurka5f1c5092010-09-03 14:15:02 -0700127 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700129 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700130
Sunny Goyal061380a2016-02-29 15:15:03 -0800131 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700132
Adam Cohen8d769d62017-06-23 17:27:38 -0700133 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
134 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
135 // the screens from continuing to translate beyond the normal bounds.
136 protected int mOverScrollX;
137
138 protected int mUnboundedScrollX;
139
Winson Chungd2be3812013-07-16 11:11:32 -0700140 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700141 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800142 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700143
Adam Cohen7d30a372013-07-01 17:03:59 -0700144 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700145 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700146 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700147 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700148
John Spurlock77e1f472013-09-11 10:09:51 -0400149 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800150 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400151
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800152 // Similar to the platform implementation of isLayoutValid();
153 protected boolean mIsLayoutValid;
154
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700155 private int[] mTmpIntPair = new int[2];
156
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 public PagedView(Context context) {
158 this(context, null);
159 }
160
161 public PagedView(Context context, AttributeSet attrs) {
162 this(context, attrs, 0);
163 }
164
165 public PagedView(Context context, AttributeSet attrs, int defStyle) {
166 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700167
Adam Cohen9c4949e2010-10-05 12:27:22 -0700168 TypedArray a = context.obtainStyledAttributes(attrs,
169 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700170 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700171 a.recycle();
172
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700174 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700175 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700176 }
177
178 /**
179 * Initializes various states for this workspace.
180 */
Michael Jurka0142d492010-08-25 17:46:15 -0700181 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800182 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700183 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700184 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700185
186 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700187 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700188 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800189
Sunny Goyalcf25b522015-07-09 00:01:18 -0700190 float density = getResources().getDisplayMetrics().density;
191 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
192 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
193 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700194
195 if (Utilities.ATLEAST_OREO) {
196 setDefaultFocusHighlightEnabled(false);
197 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 }
199
Adam Cohenf9618852013-11-08 06:45:03 -0800200 protected void setDefaultInterpolator(Interpolator interpolator) {
201 mDefaultInterpolator = interpolator;
202 mScroller.setInterpolator(mDefaultInterpolator);
203 }
204
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700205 public void initParentViews(View parent) {
206 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800207 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700208 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700209 }
210 }
211
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800212 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700213 return mPageIndicator;
214 }
215
Adam Cohen674531f2013-12-13 15:07:14 -0800216 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700217 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
218 * that the user was on before entering free scroll mode (e.g. the home screen page they
219 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
220 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700222 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700223 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700225
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700226 /**
227 * Returns the index of page to be shown immediately afterwards.
228 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700229 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700230 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
231 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700232
Adam Cohenf9c184a2016-01-15 16:47:43 -0800233 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 return getChildCount();
235 }
236
Sunny Goyal83a8f042015-05-19 12:52:12 -0700237 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 return getChildAt(index);
239 }
240
Adam Cohenae4f1552011-10-20 00:15:42 -0700241 protected int indexToPage(int index) {
242 return index;
243 }
244
Vadim Tryshev3154cd12018-05-10 15:42:42 -0700245 protected void scrollAndForceFinish(int scrollX) {
246 scrollTo(scrollX, 0);
247 mScroller.setFinalX(scrollX);
248 forceFinishScroller(true);
249 }
250
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800252 * Updates the scroll of the current page immediately to its final scroll position. We use this
253 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
254 * the previous tab page.
255 */
256 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700257 // If the current page is invalid, just reset the scroll position to zero
258 int newX = 0;
259 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700260 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700261 }
Vadim Tryshev3154cd12018-05-10 15:42:42 -0700262 scrollAndForceFinish(newX);
Winson Chungbbc60d82010-11-11 16:34:41 -0800263 }
264
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700265 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700266 mScroller.abortAnimation();
267 // We need to clean up the next page here to avoid computeScrollHelper from
268 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700269 if (resetNextPage) {
270 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800271 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700272 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700273 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700274
Tony Wickham8f7ead32016-04-07 18:46:44 -0700275 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700276 mScroller.forceFinished(true);
277 // We need to clean up the next page here to avoid computeScrollHelper from
278 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700279 if (resetNextPage) {
280 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800281 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700282 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700283 }
284
Adam Cohen6f127a62014-06-12 14:54:41 -0700285 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700287 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700288 }
289
Chet Haasebc2f0822012-10-26 17:59:53 -0700290 /**
Winson Chung86f77532010-08-24 11:08:22 -0700291 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700293 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800294 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700295 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800296 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800297 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
298 // the default
299 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800300 return;
301 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700302 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700303 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700304 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700305 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800306 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 }
308
Winson Chung8c87cd82013-07-23 16:20:10 -0700309 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800310 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
311 * has settled.
312 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700313 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800314 updatePageIndicator();
315 }
316
317 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700318 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000319 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700320 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700322 protected void pageBeginTransition() {
323 if (!mIsPageInTransition) {
324 mIsPageInTransition = true;
325 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700326 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700327 }
328
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700329 protected void pageEndTransition() {
330 if (mIsPageInTransition) {
331 mIsPageInTransition = false;
332 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700333 }
Michael Jurka0142d492010-08-25 17:46:15 -0700334 }
335
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700336 protected boolean isPageInTransition() {
337 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700338 }
339
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700340 /**
341 * Called when the page starts moving as part of the scroll. Subclasses can override this
342 * to provide custom behavior during animation.
343 */
344 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700345 }
346
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700347 /**
348 * Called when the page ends moving as part of the scroll. Subclasses can override this
349 * to provide custom behavior during animation.
350 */
351 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700352 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700353 }
354
Sunny Goyalc86df472016-02-25 09:19:38 -0800355 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700356 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800357 }
358
Winson Chung321e9ee2010-08-09 13:37:56 -0700359 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800360 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800361 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800362 }
363
364 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700365 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700366 // In free scroll mode, we clamp the scrollX
367 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700368 // If the scroller is trying to move to a location beyond the maximum allowed
369 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700370 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700371 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700372 }
373
Sunny Goyal4d519f22017-10-24 10:32:40 -0700374 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700375 }
376
Adam Cohen8d769d62017-06-23 17:27:38 -0700377 mUnboundedScrollX = x;
378
Sunny Goyal70660032015-05-14 00:07:08 -0700379 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
380 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700381 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700382 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800383 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700384 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700385 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700386 overScroll(x - mMaxScrollX);
387 } else {
388 overScroll(x);
389 }
Adam Cohen68d73932010-11-15 10:50:58 -0800390 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700391 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700392 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800393 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700394 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700395 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700396 overScroll(x);
397 } else {
398 overScroll(x - mMaxScrollX);
399 }
Adam Cohen68d73932010-11-15 10:50:58 -0800400 }
401 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700402 if (mWasInOverscroll) {
403 overScroll(0);
404 mWasInOverscroll = false;
405 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700406 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800407 super.scrollTo(x, y);
408 }
Michael Jurka0142d492010-08-25 17:46:15 -0700409 }
410
Adam Cohen53805212013-10-01 10:39:23 -0700411 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800412 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700413 if (mCurrentPage != getNextPage()) {
414 AccessibilityEvent ev =
415 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700416 ev.setScrollable(true);
417 ev.setScrollX(getScrollX());
418 ev.setScrollY(getScrollY());
419 ev.setMaxScrollX(mMaxScrollX);
420 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700421
Vadim Tryshevf4715972015-05-08 17:21:03 -0700422 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700423 }
Adam Cohen53805212013-10-01 10:39:23 -0700424 }
425 }
426
Michael Jurka0142d492010-08-25 17:46:15 -0700427 // we moved this functionality to a helper function so SmoothPagedView can reuse it
428 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800429 return computeScrollHelper(true);
430 }
431
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700432 protected void announcePageForAccessibility() {
433 if (isAccessibilityEnabled(getContext())) {
434 // Notify the user when the page changes
435 announceForAccessibility(getCurrentPageDescription());
436 }
437 }
438
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800439 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700440 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700441 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800442 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700443 || getScrollY() != mScroller.getCurrY()
444 || mOverScrollX != mScroller.getCurrX()) {
Sunny Goyal7f28fd52018-05-22 16:24:40 -0700445 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700446 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800447 if (shouldInvalidate) {
448 invalidate();
449 }
Michael Jurka0142d492010-08-25 17:46:15 -0700450 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800451 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700452 sendScrollAccessibilityEvent();
453
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700454 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700455 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700456 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700457 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700458
Adam Cohen73aa9752010-11-24 16:26:58 -0800459 // We don't want to trigger a page end moving unless the page has settled
460 // and the user has stopped scrolling
461 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700462 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800463 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700464
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700465 if (canAnnouncePageDescription()) {
466 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700467 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700468 }
Michael Jurka0142d492010-08-25 17:46:15 -0700469 return false;
470 }
471
472 @Override
473 public void computeScroll() {
474 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
476
Sunny Goyalce8809a2018-01-18 14:02:47 -0800477 public int getExpectedHeight() {
478 return getMeasuredHeight();
479 }
480
Adam Cohen410f3cd2013-09-22 12:09:32 -0700481 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800482 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800483 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700484 }
485
Sunny Goyalce8809a2018-01-18 14:02:47 -0800486 public int getExpectedWidth() {
487 return getMeasuredWidth();
488 }
489
Sunny Goyalbc683e92017-12-06 10:25:07 -0800490 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800491 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800492 - mInsets.left - mInsets.right;
493 }
494
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800496 public void requestLayout() {
497 mIsLayoutValid = false;
498 super.requestLayout();
499 }
500
501 @Override
502 public void forceLayout() {
503 mIsLayoutValid = false;
504 super.forceLayout();
505 }
506
507 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700509 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700510 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
511 return;
512 }
513
Adam Cohen7d30a372013-07-01 17:03:59 -0700514 // We measure the dimensions of the PagedView to be larger than the pages so that when we
515 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700516 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
517 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
518 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700519 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700520
Adam Cohen7d30a372013-07-01 17:03:59 -0700521 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
522 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
523 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525
Winson Chung8aad6102012-05-11 16:27:49 -0700526 // Return early if we aren't given a proper dimension
527 if (widthSize <= 0 || heightSize <= 0) {
528 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
529 return;
530 }
531
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700533 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700534 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700535
Sunny Goyalac00cba2017-11-13 15:58:01 -0800536 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800537 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800538 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800539 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700540
Sunny Goyalac00cba2017-11-13 15:58:01 -0800541 // measureChildren takes accounts for content padding, we only need to care about extra
542 // space due to insets.
543 measureChildren(myWidthSpec, myHeightSpec);
544 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800545 }
546
Vadim Tryshev3154cd12018-05-10 15:42:42 -0700547 protected void restoreScrollOnLayout() {
548 setCurrentPage(getNextPage());
549 }
550
Sunny Goyalcf25b522015-07-09 00:01:18 -0700551 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700553 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800554 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700555 final int childCount = getChildCount();
556 boolean pageScrollChanged = false;
557 if (mPageScrolls == null || childCount != mPageScrolls.length) {
558 mPageScrolls = new int[childCount];
559 pageScrollChanged = true;
560 }
561
562 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700563 return;
564 }
565
Winson Chung785d2eb2011-04-14 16:08:02 -0700566 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700567
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700568 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
569 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700571
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700572 final LayoutTransition transition = getLayoutTransition();
573 // If the transition is running defer updating max scroll, as some empty pages could
574 // still be present, and a max scroll change could cause sudden jumps in scroll.
575 if (transition != null && transition.isRunning()) {
576 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
577
578 @Override
579 public void startTransition(LayoutTransition transition, ViewGroup container,
580 View view, int transitionType) { }
581
582 @Override
583 public void endTransition(LayoutTransition transition, ViewGroup container,
584 View view, int transitionType) {
585 // Wait until all transitions are complete.
586 if (!transition.isRunning()) {
587 transition.removeTransitionListener(this);
588 updateMaxScrollX();
589 }
590 }
591 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700592 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700593 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700594 }
595
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700596 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
597 updateCurrentPageScroll();
598 mFirstLayout = false;
599 }
600
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800601 if (mScroller.isFinished() && pageScrollChanged) {
Vadim Tryshev3154cd12018-05-10 15:42:42 -0700602 restoreScrollOnLayout();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700603 }
Winson Chunge3193b92010-09-10 11:44:42 -0700604 }
605
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700606 /**
607 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
608 * of {@code outPageScrolls} should be same as the the childCount
609 *
610 */
611 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
612 ComputePageScrollsLogic scrollLogic) {
613 final int childCount = getChildCount();
614
615 final int startIndex = mIsRtl ? childCount - 1 : 0;
616 final int endIndex = mIsRtl ? -1 : childCount;
617 final int delta = mIsRtl ? -1 : 1;
618
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700619 final int verticalCenter = (getPaddingTop() + getMeasuredHeight() + mInsets.top
620 - mInsets.bottom - getPaddingBottom()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700621
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700622 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700623 boolean pageScrollChanged = false;
624
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700625 for (int i = startIndex, childLeft = scrollOffsetLeft + offsetForPageScrolls();
626 i != endIndex;
627 i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700628 final View child = getPageAt(i);
629 if (scrollLogic.shouldIncludeView(child)) {
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700630 final int childTop = verticalCenter - child.getMeasuredHeight() / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700631 final int childWidth = child.getMeasuredWidth();
632
633 if (layoutChildren) {
634 final int childHeight = child.getMeasuredHeight();
635 child.layout(childLeft, childTop,
636 childLeft + child.getMeasuredWidth(), childTop + childHeight);
637 }
638
639 final int pageScroll = childLeft - scrollOffsetLeft;
640 if (outPageScrolls[i] != pageScroll) {
641 pageScrollChanged = true;
642 outPageScrolls[i] = pageScroll;
643 }
644
645 childLeft += childWidth + mPageSpacing + getChildGap();
646 }
647 }
648 return pageScrollChanged;
649 }
650
Sunny Goyala1fbd842015-05-20 13:40:27 -0700651 protected int getChildGap() {
652 return 0;
653 }
654
Sunny Goyal4d519f22017-10-24 10:32:40 -0700655 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700656 mMaxScrollX = computeMaxScrollX();
657 }
658
659 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700660 int childCount = getChildCount();
661 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700662 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700663 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700664 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700665 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700666 }
667 }
668
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700669 protected int offsetForPageScrolls() {
670 return 0;
671 }
672
Adam Cohen3b185e22013-10-29 14:45:58 -0700673 public void setPageSpacing(int pageSpacing) {
674 mPageSpacing = pageSpacing;
675 requestLayout();
676 }
677
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800678 private void dispatchPageCountChanged() {
679 if (mPageIndicator != null) {
680 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700681 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700682 // This ensures that when children are added, they get the correct transforms / alphas
683 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700684 invalidate();
685 }
686
Michael Jurka8b805b12012-04-18 14:23:14 -0700687 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800688 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800689 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800690 dispatchPageCountChanged();
691 }
692
693 @Override
694 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800695 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700696 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800697 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700698 }
699
Adam Cohen73894962011-10-31 13:17:17 -0700700 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700701 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800702 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700703 }
704
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 @Override
706 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700707 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700708 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700709 if (immediate) {
710 setCurrentPage(page);
711 } else {
712 snapToPage(page);
713 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 return true;
715 }
716 return false;
717 }
718
719 @Override
720 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700721 int focusablePage;
722 if (mNextPage != INVALID_PAGE) {
723 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700725 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 }
Winson Chung86f77532010-08-24 11:08:22 -0700727 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700728 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700729 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 }
731 return false;
732 }
733
734 @Override
735 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800736 if (super.dispatchUnhandledMove(focused, direction)) {
737 return true;
738 }
739
740 if (mIsRtl) {
741 if (direction == View.FOCUS_LEFT) {
742 direction = View.FOCUS_RIGHT;
743 } else if (direction == View.FOCUS_RIGHT) {
744 direction = View.FOCUS_LEFT;
745 }
746 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700748 if (getCurrentPage() > 0) {
749 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 return true;
751 }
752 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700753 if (getCurrentPage() < getPageCount() - 1) {
754 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 return true;
756 }
757 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800758 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 }
760
761 @Override
762 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700763 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
764 return;
765 }
766
Adam Cohen0ffac432013-07-10 11:19:26 -0700767 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700768 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700769 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 }
771 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700772 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700773 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 }
775 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700776 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700777 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 }
779 }
780 }
781
782 /**
783 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700784 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 *
Winson Chung86f77532010-08-24 11:08:22 -0700786 * This happens when live folders requery, and if they're off page, they
787 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 */
789 @Override
790 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700791 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 View v = focused;
793 while (true) {
794 if (v == current) {
795 super.focusableViewAvailable(focused);
796 return;
797 }
798 if (v == this) {
799 return;
800 }
801 ViewParent parent = v.getParent();
802 if (parent instanceof View) {
803 v = (View)v.getParent();
804 } else {
805 return;
806 }
807 }
808 }
809
810 /**
811 * {@inheritDoc}
812 */
813 @Override
814 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
815 if (disallowIntercept) {
816 // We need to make sure to cancel our long press if
817 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700818 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700819 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 }
821 super.requestDisallowInterceptTouchEvent(disallowIntercept);
822 }
823
Adam Cohen7d30a372013-07-01 17:03:59 -0700824 /** Returns whether x and y originated within the buffered viewport */
825 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800826 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700827 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700828 }
829
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 @Override
831 public boolean onInterceptTouchEvent(MotionEvent ev) {
832 /*
833 * This method JUST determines whether we want to intercept the motion.
834 * If we return true, onTouchEvent will be called and we do the actual
835 * scrolling there.
836 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800837 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700838
Winson Chung45e1d6e2010-11-09 17:19:49 -0800839 // Skip touch handling if there are no pages to swipe
840 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
841
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 /*
843 * Shortcut the most recurring case: the user is in the dragging
844 * state and he is moving his finger. We want to intercept this
845 * motion.
846 */
847 final int action = ev.getAction();
848 if ((action == MotionEvent.ACTION_MOVE) &&
849 (mTouchState == TOUCH_STATE_SCROLLING)) {
850 return true;
851 }
852
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 switch (action & MotionEvent.ACTION_MASK) {
854 case MotionEvent.ACTION_MOVE: {
855 /*
856 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
857 * whether the user has moved far enough from his original down touch.
858 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700859 if (mActivePointerId != INVALID_POINTER) {
860 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700861 }
862 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
863 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
864 // i.e. fall through to the next case (don't break)
865 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
866 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700867 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 }
869
870 case MotionEvent.ACTION_DOWN: {
871 final float x = ev.getX();
872 final float y = ev.getY();
873 // Remember location of down touch
874 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700875 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800877 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800878 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700880
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 /*
882 * If being flinged and user touches the screen, initiate drag;
883 * otherwise don't. mScroller.isFinished should be false when
884 * being flinged.
885 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700886 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000887 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800888
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700889 if (finishedScrolling) {
890 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800891 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000892 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700893 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800894 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700895 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700896 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
897 mTouchState = TOUCH_STATE_SCROLLING;
898 } else {
899 mTouchState = TOUCH_STATE_REST;
900 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700901 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700902
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 break;
904 }
905
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800907 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700908 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 break;
910
911 case MotionEvent.ACTION_POINTER_UP:
912 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800913 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 break;
915 }
916
917 /*
918 * The only time we want to intercept motion events is if we are in the
919 * drag mode.
920 */
921 return mTouchState != TOUCH_STATE_REST;
922 }
923
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800924 public boolean isHandlingTouch() {
925 return mTouchState != TOUCH_STATE_REST;
926 }
927
Adam Cohenf8d28232011-02-01 21:47:00 -0800928 protected void determineScrollingStart(MotionEvent ev) {
929 determineScrollingStart(ev, 1.0f);
930 }
931
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 /*
933 * Determines if we should change the touch state to start scrolling after the
934 * user moves their touch point too far.
935 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800936 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700937 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700939 if (pointerIndex == -1) return;
940
941 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 final float x = ev.getX(pointerIndex);
943 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -0700944 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
945
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947
Adam Cohenf8d28232011-02-01 21:47:00 -0800948 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700950
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700951 if (xMoved) {
952 // Scroll if the user moved far enough along the X axis
953 mTouchState = TOUCH_STATE_SCROLLING;
954 mTotalMotionX += Math.abs(mLastMotionX - x);
955 mLastMotionX = x;
956 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700957 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700958 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800959 // Stop listening for things like pinches.
960 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800961 }
962 }
963
964 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700965 // Try canceling the long press. It could also have been scheduled
966 // by a distant descendant, so use the mAllowLongPress flag to block
967 // everything
968 final View currentPage = getPageAt(mCurrentPage);
969 if (currentPage != null) {
970 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 }
972 }
973
Adam Cohenb5ba0972011-09-07 18:02:31 -0700974 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800975 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700976
Adam Cohenedb40762013-07-18 16:45:45 -0700977 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700978 int count = getChildCount();
979
980 final int totalDistance;
981
982 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700983 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700984 adjacentPage = page - 1;
985 }
986
987 if (adjacentPage < 0 || adjacentPage > count - 1) {
988 totalDistance = v.getMeasuredWidth() + mPageSpacing;
989 } else {
990 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
991 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700992
993 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700994 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
995 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700996 return scrollProgress;
997 }
998
Adam Cohenedb40762013-07-18 16:45:45 -0700999 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001000 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001001 return 0;
1002 } else {
1003 return mPageScrolls[index];
1004 }
1005 }
1006
Adam Cohen564a2e72013-10-09 14:47:32 -07001007 // While layout transitions are occurring, a child's position may stray from its baseline
1008 // position. This method returns the magnitude of this stray at any given time.
1009 public int getLayoutTransitionOffsetForPage(int index) {
1010 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1011 return 0;
1012 } else {
1013 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001014
Sunny Goyal228153d2018-01-04 15:35:22 -08001015 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1016 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001017 return (int) (child.getX() - baselineX);
1018 }
1019 }
1020
Adam Cohenb5ba0972011-09-07 18:02:31 -07001021 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001022 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001023
Sunny Goyal228153d2018-01-04 15:35:22 -08001024 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001025 if (amount < 0) {
1026 mOverScrollX = overScrollAmount;
1027 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001028 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001029 mOverScrollX = mMaxScrollX + overScrollAmount;
1030 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001031 }
1032 invalidate();
1033 }
1034
Adam Cohenb5ba0972011-09-07 18:02:31 -07001035 protected void overScroll(float amount) {
1036 dampedOverScroll(amount);
1037 }
1038
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001039
1040 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001041 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001042 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001043 }
1044
Adam Cohenf9618852013-11-08 06:45:03 -08001045 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001046 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001047 mFreeScroll = freeScroll;
1048
Adam Cohenf9618852013-11-08 06:45:03 -08001049 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001050 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001051 } else if (wasFreeScroll) {
1052 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001053 }
1054
1055 setEnableOverscroll(!freeScroll);
1056 }
1057
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001058 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001059 mAllowOverScroll = enable;
1060 }
1061
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 @Override
1063 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001064 super.onTouchEvent(ev);
1065
Winson Chung45e1d6e2010-11-09 17:19:49 -08001066 // Skip touch handling if there are no pages to swipe
1067 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1068
Michael Jurkab8f06722010-10-10 15:58:46 -07001069 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001070
1071 final int action = ev.getAction();
1072
1073 switch (action & MotionEvent.ACTION_MASK) {
1074 case MotionEvent.ACTION_DOWN:
1075 /*
1076 * If being flinged and user touches, stop the fling. isFinished
1077 * will be false if being flinged.
1078 */
1079 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001080 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 }
1082
1083 // Remember where the motion event started
1084 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001085 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001086 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001087 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001088 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001089
Michael Jurka0142d492010-08-25 17:46:15 -07001090 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001091 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001092 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001093 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 break;
1095
1096 case MotionEvent.ACTION_MOVE:
1097 if (mTouchState == TOUCH_STATE_SCROLLING) {
1098 // Scroll to follow the motion event
1099 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001100
1101 if (pointerIndex == -1) return true;
1102
Winson Chung321e9ee2010-08-09 13:37:56 -07001103 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001104 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001105
Adam Cohenaefd4e12011-02-14 16:39:38 -08001106 mTotalMotionX += Math.abs(deltaX);
1107
Winson Chungc0844aa2011-02-02 15:25:58 -08001108 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1109 // keep the remainder because we are actually testing if we've moved from the last
1110 // scrolled position (which is discrete).
1111 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001112 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001113 mLastMotionX = x;
1114 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 } else {
1116 awakenScrollBars();
1117 }
Adam Cohen564976a2010-10-13 18:52:07 -07001118 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 determineScrollingStart(ev);
1120 }
1121 break;
1122
1123 case MotionEvent.ACTION_UP:
1124 if (mTouchState == TOUCH_STATE_SCROLLING) {
1125 final int activePointerId = mActivePointerId;
1126 final int pointerIndex = ev.findPointerIndex(activePointerId);
1127 final float x = ev.getX(pointerIndex);
1128 final VelocityTracker velocityTracker = mVelocityTracker;
1129 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1130 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001131 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001132 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001133 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1134 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001135
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001136 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001137 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001138
Adam Cohenf358a4b2013-07-23 16:47:31 -07001139 if (!mFreeScroll) {
1140 // In the case that the page is moved far to one direction and then is flung
1141 // in the opposite direction, we use a threshold to determine whether we should
1142 // just return to the starting page, or if we should skip one further.
1143 boolean returnToOriginalPage = false;
1144 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1145 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1146 returnToOriginalPage = true;
1147 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001148
Adam Cohenf358a4b2013-07-23 16:47:31 -07001149 int finalPage;
1150 // We give flings precedence over large moves, which is why we short-circuit our
1151 // test for a large move if a fling has been registered. That is, a large
1152 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001153 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1154 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001155 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1156 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1157 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1158 snapToPageWithVelocity(finalPage, velocityX);
1159 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1160 (isFling && isVelocityXLeft)) &&
1161 mCurrentPage < getChildCount() - 1) {
1162 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1163 snapToPageWithVelocity(finalPage, velocityX);
1164 } else {
1165 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001166 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001168 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001169 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001170 }
1171
1172 float scaleX = getScaleX();
1173 int vX = (int) (-velocityX * scaleX);
1174 int initialScrollX = (int) (getScrollX() * scaleX);
1175
Adam Cohenf9618852013-11-08 06:45:03 -08001176 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001177 mScroller.fling(initialScrollX,
1178 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001179 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1180 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1181 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1182 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
Vadim Trysheva9283be2018-05-01 19:22:37 -07001183 if (mSettleOnPageInFreeScroll && unscaledScrollX > 0
1184 && unscaledScrollX < mMaxScrollX) {
1185 // If scrolling ends in the half of the added space that is closer to the
1186 // end, settle to the end. Otherwise snap to the nearest page.
1187 // If flinging past one of the ends, don't change the velocity as it will
1188 // get stopped at the end anyway.
1189 final int finalX = unscaledScrollX < firstPageScroll / 2 ?
1190 0 :
1191 unscaledScrollX > (lastPageScroll + mMaxScrollX) / 2 ?
1192 mMaxScrollX :
1193 getScrollForPage(mNextPage);
1194
1195 mScroller.setFinalX((int) (finalX * getScaleX()));
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001196 // Ensure the scroll/snap doesn't happen too fast;
1197 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1198 - mScroller.getDuration();
1199 if (extraScrollDuration > 0) {
1200 mScroller.extendDuration(extraScrollDuration);
1201 }
1202 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001203 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001205 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001206 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1207 // at this point we have not moved beyond the touch slop
1208 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1209 // we can just page
1210 int nextPage = Math.max(0, mCurrentPage - 1);
1211 if (nextPage != mCurrentPage) {
1212 snapToPage(nextPage);
1213 } else {
1214 snapToDestination();
1215 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001216 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 // at this point we have not moved beyond the touch slop
1218 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1219 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001220 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1221 if (nextPage != mCurrentPage) {
1222 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 } else {
1224 snapToDestination();
1225 }
1226 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001227
Adam Cohen7d30a372013-07-01 17:03:59 -07001228 // End any intermediate reordering states
1229 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 break;
1231
1232 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001233 if (mTouchState == TOUCH_STATE_SCROLLING) {
1234 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001235 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001236 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001237 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 break;
1239
1240 case MotionEvent.ACTION_POINTER_UP:
1241 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001242 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 break;
1244 }
1245
1246 return true;
1247 }
1248
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001249 protected boolean shouldFlingForVelocity(int velocityX) {
1250 return Math.abs(velocityX) > mFlingThresholdVelocity;
1251 }
1252
Adam Cohen7d30a372013-07-01 17:03:59 -07001253 private void resetTouchState() {
1254 releaseVelocityTracker();
Adam Cohen7d30a372013-07-01 17:03:59 -07001255 mTouchState = TOUCH_STATE_REST;
1256 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001257 }
1258
Adam Cohenc2d6e892014-10-16 09:49:24 -07001259 /**
1260 * Triggered by scrolling via touch
1261 */
1262 protected void onScrollInteractionBegin() {
1263 }
1264
1265 protected void onScrollInteractionEnd() {
1266 }
1267
Winson Chung185d7162011-02-28 13:47:29 -08001268 @Override
1269 public boolean onGenericMotionEvent(MotionEvent event) {
1270 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1271 switch (event.getAction()) {
1272 case MotionEvent.ACTION_SCROLL: {
1273 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1274 final float vscroll;
1275 final float hscroll;
1276 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1277 vscroll = 0;
1278 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1279 } else {
1280 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1281 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1282 }
1283 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001284 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001285 : (hscroll > 0 || vscroll > 0);
1286 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001287 scrollRight();
1288 } else {
1289 scrollLeft();
1290 }
1291 return true;
1292 }
1293 }
1294 }
1295 }
1296 return super.onGenericMotionEvent(event);
1297 }
1298
Michael Jurkab8f06722010-10-10 15:58:46 -07001299 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1300 if (mVelocityTracker == null) {
1301 mVelocityTracker = VelocityTracker.obtain();
1302 }
1303 mVelocityTracker.addMovement(ev);
1304 }
1305
1306 private void releaseVelocityTracker() {
1307 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001308 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001309 mVelocityTracker.recycle();
1310 mVelocityTracker = null;
1311 }
1312 }
1313
Winson Chung321e9ee2010-08-09 13:37:56 -07001314 private void onSecondaryPointerUp(MotionEvent ev) {
1315 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1316 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1317 final int pointerId = ev.getPointerId(pointerIndex);
1318 if (pointerId == mActivePointerId) {
1319 // This was our active pointer going up. Choose a new
1320 // active pointer and adjust accordingly.
1321 // TODO: Make this decision more intelligent.
1322 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1323 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001324 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 mActivePointerId = ev.getPointerId(newPointerIndex);
1326 if (mVelocityTracker != null) {
1327 mVelocityTracker.clear();
1328 }
1329 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001330 }
1331
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 @Override
1333 public void requestChildFocus(View child, View focused) {
1334 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001335 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001336 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001337 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 }
1339 }
1340
Sunny Goyal228153d2018-01-04 15:35:22 -08001341 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001342 return getPageNearestToCenterOfScreen(getScrollX());
1343 }
1344
1345 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001346 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001347 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 final int childCount = getChildCount();
1350 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001351 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001352 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001354 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1356 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1357 minDistanceFromScreenCenter = distanceFromScreenCenter;
1358 minDistanceFromScreenCenterIndex = i;
1359 }
1360 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001361 return minDistanceFromScreenCenterIndex;
1362 }
1363
1364 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001365 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1366 }
1367
1368 protected boolean isInOverScroll() {
1369 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1370 }
1371
1372 protected int getPageSnapDuration() {
1373 if (isInOverScroll()) {
1374 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1375 }
1376 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 }
1378
Adam Cohene0f66b52010-11-23 15:06:07 -08001379 // We want the duration of the page snap animation to be influenced by the distance that
1380 // the screen has to travel, however, we don't want this duration to be effected in a
1381 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1382 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001383 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001384 f -= 0.5f; // center the values about 0.
1385 f *= 0.3f * Math.PI / 2.0f;
1386 return (float) Math.sin(f);
1387 }
1388
Winson Chung05a31ed2018-02-08 17:08:43 -08001389 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001390 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001391 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001392
Adam Cohenedb40762013-07-18 16:45:45 -07001393 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001394 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001395 int duration = 0;
1396
Sunny Goyal4d113a52015-05-27 10:05:28 -07001397 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001398 // If the velocity is low enough, then treat this more as an automatic page advance
1399 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001400 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001401 }
1402
1403 // Here we compute a "distance" that will be used in the computation of the overall
1404 // snap duration. This is a function of the actual distance that needs to be traveled;
1405 // we keep this value close to half screen size in order to reduce the variance in snap
1406 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001407 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001408 float distance = halfScreenSize + halfScreenSize *
1409 distanceInfluenceForSnapDuration(distanceRatio);
1410
1411 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001412 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001413
1414 // we want the page's snap velocity to approximately match the velocity at which the
1415 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001416 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1417 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001418
Winson Chung05a31ed2018-02-08 17:08:43 -08001419 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001420 }
1421
Winson Chung05a31ed2018-02-08 17:08:43 -08001422 public boolean snapToPage(int whichPage) {
1423 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 }
1425
Winson Chung05a31ed2018-02-08 17:08:43 -08001426 public boolean snapToPageImmediately(int whichPage) {
1427 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001428 }
1429
Winson Chung05a31ed2018-02-08 17:08:43 -08001430 public boolean snapToPage(int whichPage, int duration) {
1431 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001432 }
1433
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001434 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001435 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001436 }
1437
Winson Chung05a31ed2018-02-08 17:08:43 -08001438 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001439 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001440 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001441
Adam Cohenedb40762013-07-18 16:45:45 -07001442 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001443 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001444 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001445 }
1446
Winson Chung05a31ed2018-02-08 17:08:43 -08001447 protected boolean snapToPage(int whichPage, int delta, int duration) {
1448 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001449 }
Michael Jurka0142d492010-08-25 17:46:15 -07001450
Winson Chung05a31ed2018-02-08 17:08:43 -08001451 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001452 TimeInterpolator interpolator) {
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001453
1454 if (FeatureFlags.IS_DOGFOOD_BUILD) {
1455 duration *= Settings.System.getFloat(getContext().getContentResolver(),
1456 Settings.System.WINDOW_ANIMATION_SCALE, 1);
1457 }
1458
Adam Cohen6f127a62014-06-12 14:54:41 -07001459 whichPage = validateNewPage(whichPage);
1460
Adam Cohen7d30a372013-07-01 17:03:59 -07001461 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001462
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001464 if (immediate) {
1465 duration = 0;
1466 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 duration = Math.abs(delta);
1468 }
1469
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001470 if (duration != 0) {
1471 pageBeginTransition();
1472 }
1473
Adam Cohenf358a4b2013-07-23 16:47:31 -07001474 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001475 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001476 }
Adam Cohenf9618852013-11-08 06:45:03 -08001477
1478 if (interpolator != null) {
1479 mScroller.setInterpolator(interpolator);
1480 } else {
1481 mScroller.setInterpolator(mDefaultInterpolator);
1482 }
1483
Sunny Goyalc86df472016-02-25 09:19:38 -08001484 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001485
Adam Cohen674531f2013-12-13 15:07:14 -08001486 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001487
1488 // Trigger a compute() to finish switching pages if necessary
1489 if (immediate) {
1490 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001491 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001492 }
1493
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001495 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 }
1497
Vadim Tryshev98913d02018-05-18 18:41:34 -07001498 public boolean scrollLeft() {
1499 if (getNextPage() > 0) {
1500 snapToPage(getNextPage() - 1);
1501 return true;
1502 }
1503 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 }
1505
Vadim Tryshev98913d02018-05-18 18:41:34 -07001506 public boolean scrollRight() {
1507 if (getNextPage() < getChildCount() - 1) {
1508 snapToPage(getNextPage() + 1);
1509 return true;
1510 }
1511 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 }
1513
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001514 @Override
1515 public CharSequence getAccessibilityClassName() {
1516 // Some accessibility services have special logic for ScrollView. Since we provide same
1517 // accessibility info as ScrollView, inform the service to handle use the same way.
1518 return ScrollView.class.getName();
1519 }
1520
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001521 protected boolean isPageOrderFlipped() {
1522 return false;
1523 }
1524
Winson Chung6a0f57d2011-06-29 20:10:49 -07001525 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001526 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001527 @Override
1528 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1529 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001530 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001531 info.setScrollable(getPageCount() > 1);
1532 if (getCurrentPage() < getPageCount() - 1) {
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001533 info.addAction(pagesFlipped ? AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
1534 : AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001535 }
1536 if (getCurrentPage() > 0) {
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001537 info.addAction(pagesFlipped ? AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
1538 : AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001539 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001540
1541 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1542 // Besides disabling the accessibility long-click, this also prevents this view from getting
1543 // accessibility focus.
1544 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001545 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001546 }
1547
1548 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001549 public void sendAccessibilityEvent(int eventType) {
1550 // Don't let the view send real scroll events.
1551 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1552 super.sendAccessibilityEvent(eventType);
1553 }
1554 }
1555
1556 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001557 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1558 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001559 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001560 }
1561
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001562 @Override
1563 public boolean performAccessibilityAction(int action, Bundle arguments) {
1564 if (super.performAccessibilityAction(action, arguments)) {
1565 return true;
1566 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001567 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001568 switch (action) {
1569 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001570 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001571 return true;
1572 }
1573 } break;
1574 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001575 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001576 return true;
1577 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001578 }
1579 break;
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001580 }
1581 return false;
1582 }
1583
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001584 protected boolean canAnnouncePageDescription() {
1585 return true;
1586 }
1587
Adam Cohen0ffac432013-07-10 11:19:26 -07001588 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001589 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001590 getNextPage() + 1, getChildCount());
1591 }
1592
Tony Mak2e564402018-02-27 17:19:34 +00001593 protected float getDownMotionX() {
1594 return mDownMotionX;
1595 }
1596
1597 protected float getDownMotionY() {
1598 return mDownMotionY;
1599 }
1600
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001601 protected interface ComputePageScrollsLogic {
1602
1603 boolean shouldIncludeView(View view);
1604 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001605
1606 public int[] getVisibleChildrenRange() {
1607 float visibleLeft = 0;
1608 float visibleRight = visibleLeft + getMeasuredWidth();
1609 float scaleX = getScaleX();
1610 if (scaleX < 1 && scaleX > 0) {
1611 float mid = getMeasuredWidth() / 2;
1612 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1613 visibleRight = mid + ((visibleRight - mid) / scaleX);
1614 }
1615
1616 int leftChild = -1;
1617 int rightChild = -1;
1618 final int childCount = getChildCount();
1619 for (int i = 0; i < childCount; i++) {
1620 final View child = getPageAt(i);
1621
1622 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1623 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1624 if (leftChild == -1) {
1625 leftChild = i;
1626 }
1627 rightChild = i;
1628 }
1629 }
1630 mTmpIntPair[0] = leftChild;
1631 mTmpIntPair[1] = rightChild;
1632 return mTmpIntPair;
1633 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001634}