blob: d39ec3ee3e2ede60d336c4498588c3c12573016f [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
Winson Chung228a0fa2011-01-26 22:14:13 -080022import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080023import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070024import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ObjectAnimator;
26import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070027import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070029import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070030import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080037import android.view.InputDevice;
38import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070039import android.view.MotionEvent;
40import android.view.VelocityTracker;
41import android.view.View;
42import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080043import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
47import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080048import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070050import com.android.launcher3.anim.Interpolators;
Sunny Goyal9e76f682017-02-13 12:13:43 -080051import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070053import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070055
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chung321e9ee2010-08-09 13:37:56 -070058/**
59 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070060 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070061 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080062public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070063 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070064 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070065 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Vadim Tryshevfedca432015-08-19 17:55:02 -070067 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080068 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Sunny Goyalb72d8b22017-07-14 00:02:27 -070070 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070071 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070072
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080074 // The page is moved more than halfway, automatically move to the next page on touch up.
75 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080076
Sunny Goyal8e2133b2015-05-06 13:39:07 -070077 private static final float MAX_SCROLL_PROGRESS = 1.0f;
78
Adam Cohen265b9a62011-12-07 14:37:18 -080079 // The following constants need to be scaled based on density. The scaled versions will be
80 // assigned to the corresponding member variables below.
81 private static final int FLING_THRESHOLD_VELOCITY = 500;
82 private static final int MIN_SNAP_VELOCITY = 1500;
83 private static final int MIN_FLING_VELOCITY = 250;
84
Adam Cohen21cd0022013-10-09 18:57:02 -070085 public static final int INVALID_RESTORE_PAGE = -1001;
86
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080088 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070089
Adam Cohen265b9a62011-12-07 14:37:18 -080090 protected int mFlingThresholdVelocity;
91 protected int mMinFlingVelocity;
92 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected boolean mFirstLayout = true;
95
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070098 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070099
Sunny Goyal4ffec482016-02-09 11:28:52 -0800100 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800102 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800103 protected LauncherScroller mScroller;
104 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800106 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Adam Cohen7d30a372013-07-01 17:03:59 -0700108 private float mParentDownMotionX;
109 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700111 private float mDownMotionY;
112 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700113 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700114 private float mLastMotionX;
115 private float mLastMotionXRemainder;
116 private float mLastMotionY;
117 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700118
Adam Cohendbdff6b2013-09-18 19:09:15 -0700119 private boolean mCancelTap;
120
Adam Cohenedb40762013-07-18 16:45:45 -0700121 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Michael Jurka0142d492010-08-25 17:46:15 -0700123 protected final static int TOUCH_STATE_REST = 0;
124 protected final static int TOUCH_STATE_SCROLLING = 1;
125 protected final static int TOUCH_STATE_PREV_PAGE = 2;
126 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700127 protected final static int TOUCH_STATE_REORDERING = 4;
128
Michael Jurka0142d492010-08-25 17:46:15 -0700129 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800135 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Michael Jurka5f1c5092010-09-03 14:15:02 -0700137 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700141 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700142
Sunny Goyal061380a2016-02-29 15:15:03 -0800143 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700144
Adam Cohen8d769d62017-06-23 17:27:38 -0700145 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
146 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
147 // the screens from continuing to translate beyond the normal bounds.
148 protected int mOverScrollX;
149
150 protected int mUnboundedScrollX;
151
Winson Chungd2be3812013-07-16 11:11:32 -0700152 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800154 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700155
156 // Reordering
157 // We use the min scale to determine how much to expand the actually PagedView measured
158 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700159 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700160 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700161 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
162
Sunny Goyalcf25b522015-07-09 00:01:18 -0700163 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700164 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700165 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700166 // This variable's scope is only for the duration of startReordering() and endReordering()
167 private boolean mReorderingStarted = false;
168 // This variable's scope is for the duration of startReordering() and after the zoomIn()
169 // animation after endReordering()
170 private boolean mIsReordering;
171 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700172 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 private int mPostReorderingPreZoomInRemainingAnimationCount;
174 private Runnable mPostReorderingPreZoomInRunnable;
175
Adam Cohen7d30a372013-07-01 17:03:59 -0700176 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700177 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700178 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700179 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700180
John Spurlock77e1f472013-09-11 10:09:51 -0400181 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800182 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400183
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800184 // Similar to the platform implementation of isLayoutValid();
185 protected boolean mIsLayoutValid;
186
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 public PagedView(Context context) {
188 this(context, null);
189 }
190
191 public PagedView(Context context, AttributeSet attrs) {
192 this(context, attrs, 0);
193 }
194
195 public PagedView(Context context, AttributeSet attrs, int defStyle) {
196 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700197
Adam Cohen9c4949e2010-10-05 12:27:22 -0700198 TypedArray a = context.obtainStyledAttributes(attrs,
199 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700200 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 a.recycle();
202
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700204 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700205 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 }
207
208 /**
209 * Initializes various states for this workspace.
210 */
Michael Jurka0142d492010-08-25 17:46:15 -0700211 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800212 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700213 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700214 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700215
216 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700217 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800219
Sunny Goyalcf25b522015-07-09 00:01:18 -0700220 float density = getResources().getDisplayMetrics().density;
221 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
222 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
223 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700224 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 }
226
Adam Cohenf9618852013-11-08 06:45:03 -0800227 protected void setDefaultInterpolator(Interpolator interpolator) {
228 mDefaultInterpolator = interpolator;
229 mScroller.setInterpolator(mDefaultInterpolator);
230 }
231
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700232 public void initParentViews(View parent) {
233 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800234 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700235 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800236 mPageIndicator.setPageDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700237 }
238 }
239
Adam Cohen7d30a372013-07-01 17:03:59 -0700240 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700241 private float[] mapPointFromViewToParent(View v, float x, float y) {
242 sTmpPoint[0] = x;
243 sTmpPoint[1] = y;
244 v.getMatrix().mapPoints(sTmpPoint);
245 sTmpPoint[0] += v.getLeft();
246 sTmpPoint[1] += v.getTop();
247 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700248 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700249 private float[] mapPointFromParentToView(View v, float x, float y) {
250 sTmpPoint[0] = x - v.getLeft();
251 sTmpPoint[1] = y - v.getTop();
252 v.getMatrix().invert(sTmpInvMatrix);
253 sTmpInvMatrix.mapPoints(sTmpPoint);
254 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700255 }
256
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700257 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700258 if (mDragView != null) {
259 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
260 (mDragViewBaselineLeft - mDragView.getLeft());
261 float y = mLastMotionY - mDownMotionY;
262 mDragView.setTranslationX(x);
263 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700264
Adam Cohenf358a4b2013-07-23 16:47:31 -0700265 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
266 + x + ", " + y);
267 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700268 }
269
Adam Cohen7d30a372013-07-01 17:03:59 -0700270 @Override
271 public void setScaleX(float scaleX) {
272 super.setScaleX(scaleX);
273 if (isReordering(true)) {
274 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
275 mLastMotionX = p[0];
276 mLastMotionY = p[1];
277 updateDragViewTranslationDuringDrag();
278 }
279 }
280
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800281 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700282 return mPageIndicator;
283 }
284
Adam Cohen674531f2013-12-13 15:07:14 -0800285 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700286 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
287 * that the user was on before entering free scroll mode (e.g. the home screen page they
288 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
289 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700291 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700292 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700294
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700295 /**
296 * Returns the index of page to be shown immediately afterwards.
297 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700298 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700299 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
300 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700301
Adam Cohenf9c184a2016-01-15 16:47:43 -0800302 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 return getChildCount();
304 }
305
Sunny Goyal83a8f042015-05-19 12:52:12 -0700306 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 return getChildAt(index);
308 }
309
Adam Cohenae4f1552011-10-20 00:15:42 -0700310 protected int indexToPage(int index) {
311 return index;
312 }
313
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800315 * Updates the scroll of the current page immediately to its final scroll position. We use this
316 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
317 * the previous tab page.
318 */
319 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700320 // If the current page is invalid, just reset the scroll position to zero
321 int newX = 0;
322 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700323 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700324 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800325 scrollTo(newX, 0);
326 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700327 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800328 }
329
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700330 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700331 mScroller.abortAnimation();
332 // We need to clean up the next page here to avoid computeScrollHelper from
333 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700334 if (resetNextPage) {
335 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800336 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700337 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700338 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700339
Tony Wickham8f7ead32016-04-07 18:46:44 -0700340 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700341 mScroller.forceFinished(true);
342 // We need to clean up the next page here to avoid computeScrollHelper from
343 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700344 if (resetNextPage) {
345 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800346 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700347 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700348 }
349
Adam Cohen6f127a62014-06-12 14:54:41 -0700350 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700351 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700352 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700353 }
354
Chet Haasebc2f0822012-10-26 17:59:53 -0700355 /**
Winson Chung86f77532010-08-24 11:08:22 -0700356 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700358 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800359 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700360 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800361 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800362 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
363 // the default
364 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800365 return;
366 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700367 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700368 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700369 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700370 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800371 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373
Winson Chung8c87cd82013-07-23 16:20:10 -0700374 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800375 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
376 * has settled.
377 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700378 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800379 updatePageIndicator();
380 }
381
382 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700383 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700384 if (mPageIndicator != null) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800385 mPageIndicator.setPageDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700386 if (!isReordering(false)) {
387 mPageIndicator.setActiveMarker(getNextPage());
388 }
Winson Chungd2be3812013-07-16 11:11:32 -0700389 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700391 protected void pageBeginTransition() {
392 if (!mIsPageInTransition) {
393 mIsPageInTransition = true;
394 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700395 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700396 }
397
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700398 protected void pageEndTransition() {
399 if (mIsPageInTransition) {
400 mIsPageInTransition = false;
401 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700402 }
Michael Jurka0142d492010-08-25 17:46:15 -0700403 }
404
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700405 protected boolean isPageInTransition() {
406 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700407 }
408
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700409 /**
410 * Called when the page starts moving as part of the scroll. Subclasses can override this
411 * to provide custom behavior during animation.
412 */
413 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700414 }
415
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700416 /**
417 * Called when the page ends moving as part of the scroll. Subclasses can override this
418 * to provide custom behavior during animation.
419 */
420 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700421 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700422 }
423
Winson Chung321e9ee2010-08-09 13:37:56 -0700424 /**
Winson Chung86f77532010-08-24 11:08:22 -0700425 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 *
427 * @param l The listener used to respond to long clicks.
428 */
429 @Override
430 public void setOnLongClickListener(OnLongClickListener l) {
431 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700432 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700434 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
Adam Cohen1697b792013-09-17 19:08:21 -0700436 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 }
438
Sunny Goyalc86df472016-02-25 09:19:38 -0800439 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700440 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800441 }
442
Winson Chung321e9ee2010-08-09 13:37:56 -0700443 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800444 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800445 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800446 }
447
448 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700449 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700450 // In free scroll mode, we clamp the scrollX
451 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700452 // If the scroller is trying to move to a location beyond the maximum allowed
453 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700454 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700455 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700456 }
457
Sunny Goyal4d519f22017-10-24 10:32:40 -0700458 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700459 }
460
Adam Cohen8d769d62017-06-23 17:27:38 -0700461 mUnboundedScrollX = x;
462
Sunny Goyal70660032015-05-14 00:07:08 -0700463 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
464 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700465 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700466 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800467 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700468 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700469 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700470 overScroll(x - mMaxScrollX);
471 } else {
472 overScroll(x);
473 }
Adam Cohen68d73932010-11-15 10:50:58 -0800474 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700475 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700476 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800477 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700478 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700479 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700480 overScroll(x);
481 } else {
482 overScroll(x - mMaxScrollX);
483 }
Adam Cohen68d73932010-11-15 10:50:58 -0800484 }
485 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700486 if (mWasInOverscroll) {
487 overScroll(0);
488 mWasInOverscroll = false;
489 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700490 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800491 super.scrollTo(x, y);
492 }
493
Adam Cohen7d30a372013-07-01 17:03:59 -0700494 // Update the last motion events when scrolling
495 if (isReordering(true)) {
496 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
497 mLastMotionX = p[0];
498 mLastMotionY = p[1];
499 updateDragViewTranslationDuringDrag();
500 }
Michael Jurka0142d492010-08-25 17:46:15 -0700501 }
502
Adam Cohen53805212013-10-01 10:39:23 -0700503 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800504 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700505 if (mCurrentPage != getNextPage()) {
506 AccessibilityEvent ev =
507 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700508 ev.setScrollable(true);
509 ev.setScrollX(getScrollX());
510 ev.setScrollY(getScrollY());
511 ev.setMaxScrollX(mMaxScrollX);
512 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700513
Vadim Tryshevf4715972015-05-08 17:21:03 -0700514 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700515 }
Adam Cohen53805212013-10-01 10:39:23 -0700516 }
517 }
518
Michael Jurka0142d492010-08-25 17:46:15 -0700519 // we moved this functionality to a helper function so SmoothPagedView can reuse it
520 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800521 return computeScrollHelper(true);
522 }
523
524 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700526 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800527 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700528 || getScrollY() != mScroller.getCurrY()
529 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700530 float scaleX = mFreeScroll ? getScaleX() : 1f;
531 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
532 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700533 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800534 if (shouldInvalidate) {
535 invalidate();
536 }
Michael Jurka0142d492010-08-25 17:46:15 -0700537 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800538 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700539 sendScrollAccessibilityEvent();
540
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700541 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700542 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700543 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700544 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700545
Adam Cohen73aa9752010-11-24 16:26:58 -0800546 // We don't want to trigger a page end moving unless the page has settled
547 // and the user has stopped scrolling
548 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700549 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800550 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700551
Adam Cohen7d30a372013-07-01 17:03:59 -0700552 onPostReorderingAnimationCompleted();
Sunny Goyald0030b02017-12-08 15:07:24 -0800553 if (isAccessibilityEnabled(getContext())) {
Adam Cohen53805212013-10-01 10:39:23 -0700554 // Notify the user when the page changes
555 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700556 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
Michael Jurka0142d492010-08-25 17:46:15 -0700558 return false;
559 }
560
561 @Override
562 public void computeScroll() {
563 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 }
565
Sunny Goyalce8809a2018-01-18 14:02:47 -0800566 public int getExpectedHeight() {
567 return getMeasuredHeight();
568 }
569
Adam Cohen410f3cd2013-09-22 12:09:32 -0700570 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800571 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800572 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700573 }
574
Sunny Goyalce8809a2018-01-18 14:02:47 -0800575 public int getExpectedWidth() {
576 return getMeasuredWidth();
577 }
578
Sunny Goyalbc683e92017-12-06 10:25:07 -0800579 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800580 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800581 - mInsets.left - mInsets.right;
582 }
583
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800585 public void requestLayout() {
586 mIsLayoutValid = false;
587 super.requestLayout();
588 }
589
590 @Override
591 public void forceLayout() {
592 mIsLayoutValid = false;
593 super.forceLayout();
594 }
595
596 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700598 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700599 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
600 return;
601 }
602
Adam Cohen7d30a372013-07-01 17:03:59 -0700603 // We measure the dimensions of the PagedView to be larger than the pages so that when we
604 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700605 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
606 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
607 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700608 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700609
Adam Cohen7d30a372013-07-01 17:03:59 -0700610 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
611 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
612 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
614
Winson Chung8aad6102012-05-11 16:27:49 -0700615 // Return early if we aren't given a proper dimension
616 if (widthSize <= 0 || heightSize <= 0) {
617 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
618 return;
619 }
620
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700622 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700623 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700624
Sunny Goyalac00cba2017-11-13 15:58:01 -0800625 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800626 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800627 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800628 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700629
Sunny Goyalac00cba2017-11-13 15:58:01 -0800630 // measureChildren takes accounts for content padding, we only need to care about extra
631 // space due to insets.
632 measureChildren(myWidthSpec, myHeightSpec);
633 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800634 }
635
Sunny Goyalcf25b522015-07-09 00:01:18 -0700636 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700638 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800639 mIsLayoutValid = true;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700640 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700641 return;
642 }
643
Winson Chung785d2eb2011-04-14 16:08:02 -0700644 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700646
Sunny Goyal70660032015-05-14 00:07:08 -0700647 final int startIndex = mIsRtl ? childCount - 1 : 0;
648 final int endIndex = mIsRtl ? -1 : childCount;
649 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700650
Adam Cohen7d30a372013-07-01 17:03:59 -0700651 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700652
Sunny Goyal228153d2018-01-04 15:35:22 -0800653 int scrollOffsetLeft = mInsets.left + getPaddingLeft();
654 int childLeft = scrollOffsetLeft;
655
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800656 boolean pageScrollChanged = false;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700657 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
658 mPageScrolls = new int[childCount];
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800659 pageScrollChanged = true;
Adam Cohenedb40762013-07-18 16:45:45 -0700660 }
661
Adam Cohen0ffac432013-07-10 11:19:26 -0700662 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700663 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700664 if (child.getVisibility() != View.GONE) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800665 int childTop = getPaddingTop() + mInsets.top;
666 childTop += (getMeasuredHeight() - mInsets.top - mInsets.bottom - verticalPadding
Sunny Goyalac00cba2017-11-13 15:58:01 -0800667 - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100668
Adam Cohen96d30a12013-07-16 18:13:21 -0700669 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700670 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800671
Winson Chung785d2eb2011-04-14 16:08:02 -0700672 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700673 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800674 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700675
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800676 final int pageScroll = childLeft - scrollOffsetLeft;
677 if (mPageScrolls[i] != pageScroll) {
678 pageScrollChanged = true;
679 mPageScrolls[i] = pageScroll;
680 }
Adam Cohen84a465a2013-11-11 18:49:56 +0000681
Sunny Goyalac00cba2017-11-13 15:58:01 -0800682 childLeft += childWidth + mPageSpacing + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700683 }
684 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700685
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700686 final LayoutTransition transition = getLayoutTransition();
687 // If the transition is running defer updating max scroll, as some empty pages could
688 // still be present, and a max scroll change could cause sudden jumps in scroll.
689 if (transition != null && transition.isRunning()) {
690 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
691
692 @Override
693 public void startTransition(LayoutTransition transition, ViewGroup container,
694 View view, int transitionType) { }
695
696 @Override
697 public void endTransition(LayoutTransition transition, ViewGroup container,
698 View view, int transitionType) {
699 // Wait until all transitions are complete.
700 if (!transition.isRunning()) {
701 transition.removeTransitionListener(this);
702 updateMaxScrollX();
703 }
704 }
705 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700706 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700707 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700708 }
709
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700710 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
711 updateCurrentPageScroll();
712 mFirstLayout = false;
713 }
714
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800715 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700716 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700717 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700718 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700719
720 if (isReordering(true)) {
721 updateDragViewTranslationDuringDrag();
722 }
Winson Chunge3193b92010-09-10 11:44:42 -0700723 }
724
Sunny Goyala1fbd842015-05-20 13:40:27 -0700725 protected int getChildGap() {
726 return 0;
727 }
728
Sunny Goyal4d519f22017-10-24 10:32:40 -0700729 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700730 mMaxScrollX = computeMaxScrollX();
731 }
732
733 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700734 int childCount = getChildCount();
735 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700736 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700737 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700738 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700739 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700740 }
741 }
742
Adam Cohen3b185e22013-10-29 14:45:58 -0700743 public void setPageSpacing(int pageSpacing) {
744 mPageSpacing = pageSpacing;
745 requestLayout();
746 }
747
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800748 private void dispatchPageCountChanged() {
749 if (mPageIndicator != null) {
750 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700751 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700752 // This ensures that when children are added, they get the correct transforms / alphas
753 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700754 invalidate();
755 }
756
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800758 public void onViewAdded(View child) {
759 dispatchPageCountChanged();
760 }
761
762 @Override
763 public void onViewRemoved(View child) {
Tonya361c722017-07-04 09:43:06 -0700764 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800765 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700766 }
767
Adam Cohen73894962011-10-31 13:17:17 -0700768 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700769 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800770 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700771 }
772
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 @Override
774 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700775 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700776 if (page != mCurrentPage || !mScroller.isFinished()) {
777 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 return true;
779 }
780 return false;
781 }
782
783 @Override
784 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700785 int focusablePage;
786 if (mNextPage != INVALID_PAGE) {
787 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700789 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 }
Winson Chung86f77532010-08-24 11:08:22 -0700791 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700793 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 }
795 return false;
796 }
797
798 @Override
799 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800800 if (super.dispatchUnhandledMove(focused, direction)) {
801 return true;
802 }
803
804 if (mIsRtl) {
805 if (direction == View.FOCUS_LEFT) {
806 direction = View.FOCUS_RIGHT;
807 } else if (direction == View.FOCUS_RIGHT) {
808 direction = View.FOCUS_LEFT;
809 }
810 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700812 if (getCurrentPage() > 0) {
813 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 return true;
815 }
816 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700817 if (getCurrentPage() < getPageCount() - 1) {
818 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 return true;
820 }
821 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800822 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 }
824
825 @Override
826 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700827 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
828 return;
829 }
830
Adam Cohen0ffac432013-07-10 11:19:26 -0700831 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700832 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700833 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 }
835 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700836 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700837 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700841 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843 }
844 }
845
846 /**
847 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700848 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 *
Winson Chung86f77532010-08-24 11:08:22 -0700850 * This happens when live folders requery, and if they're off page, they
851 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 */
853 @Override
854 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700855 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 View v = focused;
857 while (true) {
858 if (v == current) {
859 super.focusableViewAvailable(focused);
860 return;
861 }
862 if (v == this) {
863 return;
864 }
865 ViewParent parent = v.getParent();
866 if (parent instanceof View) {
867 v = (View)v.getParent();
868 } else {
869 return;
870 }
871 }
872 }
873
874 /**
875 * {@inheritDoc}
876 */
877 @Override
878 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
879 if (disallowIntercept) {
880 // We need to make sure to cancel our long press if
881 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700882 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700883 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
885 super.requestDisallowInterceptTouchEvent(disallowIntercept);
886 }
887
Adam Cohen7d30a372013-07-01 17:03:59 -0700888 /** Returns whether x and y originated within the buffered viewport */
889 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800890 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700891 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700892 }
893
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 @Override
895 public boolean onInterceptTouchEvent(MotionEvent ev) {
896 /*
897 * This method JUST determines whether we want to intercept the motion.
898 * If we return true, onTouchEvent will be called and we do the actual
899 * scrolling there.
900 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800901 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700902
Winson Chung45e1d6e2010-11-09 17:19:49 -0800903 // Skip touch handling if there are no pages to swipe
904 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
905
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 /*
907 * Shortcut the most recurring case: the user is in the dragging
908 * state and he is moving his finger. We want to intercept this
909 * motion.
910 */
911 final int action = ev.getAction();
912 if ((action == MotionEvent.ACTION_MOVE) &&
913 (mTouchState == TOUCH_STATE_SCROLLING)) {
914 return true;
915 }
916
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 switch (action & MotionEvent.ACTION_MASK) {
918 case MotionEvent.ACTION_MOVE: {
919 /*
920 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
921 * whether the user has moved far enough from his original down touch.
922 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700923 if (mActivePointerId != INVALID_POINTER) {
924 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700925 }
926 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
927 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
928 // i.e. fall through to the next case (don't break)
929 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
930 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700931 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 }
933
934 case MotionEvent.ACTION_DOWN: {
935 final float x = ev.getX();
936 final float y = ev.getY();
937 // Remember location of down touch
938 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700939 mDownMotionY = y;
940 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 mLastMotionX = x;
942 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -0700943 float[] p = mapPointFromViewToParent(this, x, y);
944 mParentDownMotionX = p[0];
945 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -0800946 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800947 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700949
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 /*
951 * If being flinged and user touches the screen, initiate drag;
952 * otherwise don't. mScroller.isFinished should be false when
953 * being flinged.
954 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700955 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000956 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800957
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700958 if (finishedScrolling) {
959 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800960 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000961 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700962 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800963 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700964 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700965 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
966 mTouchState = TOUCH_STATE_SCROLLING;
967 } else {
968 mTouchState = TOUCH_STATE_REST;
969 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700970 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700971
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 break;
973 }
974
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800976 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700977 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 break;
979
980 case MotionEvent.ACTION_POINTER_UP:
981 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800982 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 break;
984 }
985
986 /*
987 * The only time we want to intercept motion events is if we are in the
988 * drag mode.
989 */
990 return mTouchState != TOUCH_STATE_REST;
991 }
992
Adam Cohenf8d28232011-02-01 21:47:00 -0800993 protected void determineScrollingStart(MotionEvent ev) {
994 determineScrollingStart(ev, 1.0f);
995 }
996
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 /*
998 * Determines if we should change the touch state to start scrolling after the
999 * user moves their touch point too far.
1000 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001001 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001002 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001004 if (pointerIndex == -1) return;
1005
1006 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 final float x = ev.getX(pointerIndex);
1008 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001009 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1010
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001012
Adam Cohenf8d28232011-02-01 21:47:00 -08001013 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001014 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001015
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001016 if (xMoved) {
1017 // Scroll if the user moved far enough along the X axis
1018 mTouchState = TOUCH_STATE_SCROLLING;
1019 mTotalMotionX += Math.abs(mLastMotionX - x);
1020 mLastMotionX = x;
1021 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001022 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001023 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001024 // Stop listening for things like pinches.
1025 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001026 }
1027 }
1028
1029 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001030 // Try canceling the long press. It could also have been scheduled
1031 // by a distant descendant, so use the mAllowLongPress flag to block
1032 // everything
1033 final View currentPage = getPageAt(mCurrentPage);
1034 if (currentPage != null) {
1035 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 }
1037 }
1038
Adam Cohenb5ba0972011-09-07 18:02:31 -07001039 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001040 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001041
Adam Cohenedb40762013-07-18 16:45:45 -07001042 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001043 int count = getChildCount();
1044
1045 final int totalDistance;
1046
1047 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001048 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001049 adjacentPage = page - 1;
1050 }
1051
1052 if (adjacentPage < 0 || adjacentPage > count - 1) {
1053 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1054 } else {
1055 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1056 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001057
1058 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001059 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1060 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001061 return scrollProgress;
1062 }
1063
Adam Cohenedb40762013-07-18 16:45:45 -07001064 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001065 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001066 return 0;
1067 } else {
1068 return mPageScrolls[index];
1069 }
1070 }
1071
Adam Cohen564a2e72013-10-09 14:47:32 -07001072 // While layout transitions are occurring, a child's position may stray from its baseline
1073 // position. This method returns the magnitude of this stray at any given time.
1074 public int getLayoutTransitionOffsetForPage(int index) {
1075 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1076 return 0;
1077 } else {
1078 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001079
Sunny Goyal228153d2018-01-04 15:35:22 -08001080 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1081 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001082 return (int) (child.getX() - baselineX);
1083 }
1084 }
1085
Adam Cohenb5ba0972011-09-07 18:02:31 -07001086 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001087 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001088
Sunny Goyal228153d2018-01-04 15:35:22 -08001089 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001090 if (amount < 0) {
1091 mOverScrollX = overScrollAmount;
1092 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001093 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001094 mOverScrollX = mMaxScrollX + overScrollAmount;
1095 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001096 }
1097 invalidate();
1098 }
1099
Adam Cohenb5ba0972011-09-07 18:02:31 -07001100 protected void overScroll(float amount) {
1101 dampedOverScroll(amount);
1102 }
1103
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001104 /**
1105 * return true if freescroll has been enabled, false otherwise
1106 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001107 protected void enableFreeScroll() {
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001108 enableFreeScroll(false);
1109 }
1110
1111 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001112 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001113 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001114 }
1115
Sunny Goyalc99cb172017-10-19 16:15:09 -07001116 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001117 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001118 }
1119
Adam Cohenf9618852013-11-08 06:45:03 -08001120 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001121 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001122 mFreeScroll = freeScroll;
1123
Adam Cohenf9618852013-11-08 06:45:03 -08001124 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001125 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001126 } else if (wasFreeScroll) {
1127 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001128 }
1129
1130 setEnableOverscroll(!freeScroll);
1131 }
1132
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001133 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001134 mAllowOverScroll = enable;
1135 }
1136
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001137 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001138 if (mDragView != null) {
1139 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1140 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001141 int minDistance = Integer.MAX_VALUE;
1142 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001143 int maxPageNo = getChildCount() - 1;
1144 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001145 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001146 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001147 int d = Math.abs(dragX - pageX);
1148 if (d < minDistance) {
1149 minIndex = i;
1150 minDistance = d;
1151 }
1152 }
1153 return minIndex;
1154 }
1155 return -1;
1156 }
1157
Winson Chung321e9ee2010-08-09 13:37:56 -07001158 @Override
1159 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001160 super.onTouchEvent(ev);
1161
Winson Chung45e1d6e2010-11-09 17:19:49 -08001162 // Skip touch handling if there are no pages to swipe
1163 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1164
Michael Jurkab8f06722010-10-10 15:58:46 -07001165 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001166
1167 final int action = ev.getAction();
1168
1169 switch (action & MotionEvent.ACTION_MASK) {
1170 case MotionEvent.ACTION_DOWN:
1171 /*
1172 * If being flinged and user touches, stop the fling. isFinished
1173 * will be false if being flinged.
1174 */
1175 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001176 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 }
1178
1179 // Remember where the motion event started
1180 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001181 mDownMotionY = mLastMotionY = ev.getY();
1182 mDownScrollX = getScrollX();
1183 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1184 mParentDownMotionX = p[0];
1185 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001186 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001187 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001189
Michael Jurka0142d492010-08-25 17:46:15 -07001190 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001191 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001192 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001193 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 break;
1195
1196 case MotionEvent.ACTION_MOVE:
1197 if (mTouchState == TOUCH_STATE_SCROLLING) {
1198 // Scroll to follow the motion event
1199 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001200
1201 if (pointerIndex == -1) return true;
1202
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001204 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001205
Adam Cohenaefd4e12011-02-14 16:39:38 -08001206 mTotalMotionX += Math.abs(deltaX);
1207
Winson Chungc0844aa2011-02-02 15:25:58 -08001208 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1209 // keep the remainder because we are actually testing if we've moved from the last
1210 // scrolled position (which is discrete).
1211 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001212 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001213 mLastMotionX = x;
1214 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 } else {
1216 awakenScrollBars();
1217 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001218 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1219 // Update the last motion position
1220 mLastMotionX = ev.getX();
1221 mLastMotionY = ev.getY();
1222
1223 // Update the parent down so that our zoom animations take this new movement into
1224 // account
1225 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1226 mParentDownMotionX = pt[0];
1227 mParentDownMotionY = pt[1];
1228 updateDragViewTranslationDuringDrag();
1229
1230 // Find the closest page to the touch point
1231 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001232
Adam Cohen7d30a372013-07-01 17:03:59 -07001233 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1234 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1235 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1236 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1237
Adam Cohenf358a4b2013-07-23 16:47:31 -07001238 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001239 // Do not allow any page to be moved to 0th position.
1240 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001241 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001242 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1243 mSidePageHoverIndex = pageUnderPointIndex;
1244 mSidePageHoverRunnable = new Runnable() {
1245 @Override
1246 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001247 // Setup the scroll to the correct page before we swap the views
1248 snapToPage(pageUnderPointIndex);
1249
1250 // For each of the pages between the paged view and the drag view,
1251 // animate them from the previous position to the new position in
1252 // the layout (as a result of the drag view moving in the layout)
1253 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1254 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1255 dragViewIndex + 1 : pageUnderPointIndex;
1256 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1257 dragViewIndex - 1 : pageUnderPointIndex;
1258 for (int i = lowerIndex; i <= upperIndex; ++i) {
1259 View v = getChildAt(i);
1260 // dragViewIndex < pageUnderPointIndex, so after we remove the
1261 // drag view all subsequent views to pageUnderPointIndex will
1262 // shift down.
Sunny Goyal228153d2018-01-04 15:35:22 -08001263 int oldX = getChildOffset(i);
1264 int newX = getChildOffset(i + shiftDelta);
Adam Cohen7d30a372013-07-01 17:03:59 -07001265
1266 // Animate the view translation from its old position to its new
1267 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001268 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001269 if (anim != null) {
1270 anim.cancel();
1271 }
1272
1273 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001274 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001275 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001276 anim.start();
1277 v.setTag(anim);
1278 }
1279
1280 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001281 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001282 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001283 if (mPageIndicator != null) {
1284 mPageIndicator.setActiveMarker(getNextPage());
1285 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001286 }
1287 };
1288 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1289 }
1290 } else {
1291 removeCallbacks(mSidePageHoverRunnable);
1292 mSidePageHoverIndex = -1;
1293 }
Adam Cohen564976a2010-10-13 18:52:07 -07001294 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 determineScrollingStart(ev);
1296 }
1297 break;
1298
1299 case MotionEvent.ACTION_UP:
1300 if (mTouchState == TOUCH_STATE_SCROLLING) {
1301 final int activePointerId = mActivePointerId;
1302 final int pointerIndex = ev.findPointerIndex(activePointerId);
1303 final float x = ev.getX(pointerIndex);
1304 final VelocityTracker velocityTracker = mVelocityTracker;
1305 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1306 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001307 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001308 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001309 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1310 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001311
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001312 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001313 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001314
Adam Cohenf358a4b2013-07-23 16:47:31 -07001315 if (!mFreeScroll) {
1316 // In the case that the page is moved far to one direction and then is flung
1317 // in the opposite direction, we use a threshold to determine whether we should
1318 // just return to the starting page, or if we should skip one further.
1319 boolean returnToOriginalPage = false;
1320 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1321 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1322 returnToOriginalPage = true;
1323 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001324
Adam Cohenf358a4b2013-07-23 16:47:31 -07001325 int finalPage;
1326 // We give flings precedence over large moves, which is why we short-circuit our
1327 // test for a large move if a fling has been registered. That is, a large
1328 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001329 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1330 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001331 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1332 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1333 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1334 snapToPageWithVelocity(finalPage, velocityX);
1335 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1336 (isFling && isVelocityXLeft)) &&
1337 mCurrentPage < getChildCount() - 1) {
1338 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1339 snapToPageWithVelocity(finalPage, velocityX);
1340 } else {
1341 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001342 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001345 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001346 }
1347
1348 float scaleX = getScaleX();
1349 int vX = (int) (-velocityX * scaleX);
1350 int initialScrollX = (int) (getScrollX() * scaleX);
1351
Adam Cohenf9618852013-11-08 06:45:03 -08001352 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001353 mScroller.fling(initialScrollX,
1354 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001355 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1356 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1357 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1358 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1359 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1360 && unscaledScrollX < lastPageScroll) {
1361 // Make sure we land directly on a page. If flinging past one of the ends,
1362 // don't change the velocity as it will get stopped at the end anyway.
1363 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1364 // Ensure the scroll/snap doesn't happen too fast;
1365 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1366 - mScroller.getDuration();
1367 if (extraScrollDuration > 0) {
1368 mScroller.extendDuration(extraScrollDuration);
1369 }
1370 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001371 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001373 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001374 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1375 // at this point we have not moved beyond the touch slop
1376 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1377 // we can just page
1378 int nextPage = Math.max(0, mCurrentPage - 1);
1379 if (nextPage != mCurrentPage) {
1380 snapToPage(nextPage);
1381 } else {
1382 snapToDestination();
1383 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001384 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 // at this point we have not moved beyond the touch slop
1386 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1387 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001388 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1389 if (nextPage != mCurrentPage) {
1390 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 } else {
1392 snapToDestination();
1393 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001394 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1395 // Update the last motion position
1396 mLastMotionX = ev.getX();
1397 mLastMotionY = ev.getY();
1398
1399 // Update the parent down so that our zoom animations take this new movement into
1400 // account
1401 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1402 mParentDownMotionX = pt[0];
1403 mParentDownMotionY = pt[1];
1404 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001405 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001406 if (!mCancelTap) {
1407 onUnhandledTap(ev);
1408 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001410
1411 // Remove the callback to wait for the side page hover timeout
1412 removeCallbacks(mSidePageHoverRunnable);
1413 // End any intermediate reordering states
1414 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 break;
1416
1417 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001418 if (mTouchState == TOUCH_STATE_SCROLLING) {
1419 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001420 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001421 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001422 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001423 break;
1424
1425 case MotionEvent.ACTION_POINTER_UP:
1426 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001427 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 break;
1429 }
1430
1431 return true;
1432 }
1433
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001434 protected boolean shouldFlingForVelocity(int velocityX) {
1435 return Math.abs(velocityX) > mFlingThresholdVelocity;
1436 }
1437
Adam Cohen7d30a372013-07-01 17:03:59 -07001438 private void resetTouchState() {
1439 releaseVelocityTracker();
1440 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001441 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 mTouchState = TOUCH_STATE_REST;
1443 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 }
1445
Adam Cohenc2d6e892014-10-16 09:49:24 -07001446 /**
1447 * Triggered by scrolling via touch
1448 */
1449 protected void onScrollInteractionBegin() {
1450 }
1451
1452 protected void onScrollInteractionEnd() {
1453 }
1454
Adam Cohen1697b792013-09-17 19:08:21 -07001455 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001456 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001457 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001458
Winson Chung185d7162011-02-28 13:47:29 -08001459 @Override
1460 public boolean onGenericMotionEvent(MotionEvent event) {
1461 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1462 switch (event.getAction()) {
1463 case MotionEvent.ACTION_SCROLL: {
1464 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1465 final float vscroll;
1466 final float hscroll;
1467 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1468 vscroll = 0;
1469 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1470 } else {
1471 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1472 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1473 }
1474 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001475 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001476 : (hscroll > 0 || vscroll > 0);
1477 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001478 scrollRight();
1479 } else {
1480 scrollLeft();
1481 }
1482 return true;
1483 }
1484 }
1485 }
1486 }
1487 return super.onGenericMotionEvent(event);
1488 }
1489
Michael Jurkab8f06722010-10-10 15:58:46 -07001490 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1491 if (mVelocityTracker == null) {
1492 mVelocityTracker = VelocityTracker.obtain();
1493 }
1494 mVelocityTracker.addMovement(ev);
1495 }
1496
1497 private void releaseVelocityTracker() {
1498 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001499 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001500 mVelocityTracker.recycle();
1501 mVelocityTracker = null;
1502 }
1503 }
1504
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 private void onSecondaryPointerUp(MotionEvent ev) {
1506 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1507 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1508 final int pointerId = ev.getPointerId(pointerIndex);
1509 if (pointerId == mActivePointerId) {
1510 // This was our active pointer going up. Choose a new
1511 // active pointer and adjust accordingly.
1512 // TODO: Make this decision more intelligent.
1513 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1514 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1515 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001516 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001517 mActivePointerId = ev.getPointerId(newPointerIndex);
1518 if (mVelocityTracker != null) {
1519 mVelocityTracker.clear();
1520 }
1521 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001522 }
1523
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 @Override
1525 public void requestChildFocus(View child, View focused) {
1526 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001527 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001528 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001529 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 }
1531 }
1532
Sunny Goyal228153d2018-01-04 15:35:22 -08001533 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001534 return getPageNearestToCenterOfScreen(getScrollX());
1535 }
1536
1537 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001538 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001539 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001541 final int childCount = getChildCount();
1542 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001543 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001544 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001545 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001546 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001547 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1548 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1549 minDistanceFromScreenCenter = distanceFromScreenCenter;
1550 minDistanceFromScreenCenterIndex = i;
1551 }
1552 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001553 return minDistanceFromScreenCenterIndex;
1554 }
1555
1556 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001557 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1558 }
1559
1560 protected boolean isInOverScroll() {
1561 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1562 }
1563
1564 protected int getPageSnapDuration() {
1565 if (isInOverScroll()) {
1566 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1567 }
1568 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 }
1570
Adam Cohene0f66b52010-11-23 15:06:07 -08001571 // We want the duration of the page snap animation to be influenced by the distance that
1572 // the screen has to travel, however, we don't want this duration to be effected in a
1573 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1574 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001575 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001576 f -= 0.5f; // center the values about 0.
1577 f *= 0.3f * Math.PI / 2.0f;
1578 return (float) Math.sin(f);
1579 }
1580
Winson Chung05a31ed2018-02-08 17:08:43 -08001581 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001582 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001583 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001584
Adam Cohenedb40762013-07-18 16:45:45 -07001585 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001586 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001587 int duration = 0;
1588
Sunny Goyal4d113a52015-05-27 10:05:28 -07001589 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001590 // If the velocity is low enough, then treat this more as an automatic page advance
1591 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001592 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001593 }
1594
1595 // Here we compute a "distance" that will be used in the computation of the overall
1596 // snap duration. This is a function of the actual distance that needs to be traveled;
1597 // we keep this value close to half screen size in order to reduce the variance in snap
1598 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001599 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001600 float distance = halfScreenSize + halfScreenSize *
1601 distanceInfluenceForSnapDuration(distanceRatio);
1602
1603 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001604 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001605
1606 // we want the page's snap velocity to approximately match the velocity at which the
1607 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001608 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1609 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001610
Winson Chung05a31ed2018-02-08 17:08:43 -08001611 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001612 }
1613
Winson Chung05a31ed2018-02-08 17:08:43 -08001614 public boolean snapToPage(int whichPage) {
1615 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001616 }
1617
Winson Chung05a31ed2018-02-08 17:08:43 -08001618 public boolean snapToPageImmediately(int whichPage) {
1619 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001620 }
1621
Winson Chung05a31ed2018-02-08 17:08:43 -08001622 public boolean snapToPage(int whichPage, int duration) {
1623 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001624 }
1625
Winson Chung05a31ed2018-02-08 17:08:43 -08001626 protected boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1627 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001628 }
1629
Winson Chung05a31ed2018-02-08 17:08:43 -08001630 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001631 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001632 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001633
Adam Cohenedb40762013-07-18 16:45:45 -07001634 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001635 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001636 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001637 }
1638
Winson Chung05a31ed2018-02-08 17:08:43 -08001639 protected boolean snapToPage(int whichPage, int delta, int duration) {
1640 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001641 }
Michael Jurka0142d492010-08-25 17:46:15 -07001642
Winson Chung05a31ed2018-02-08 17:08:43 -08001643 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001644 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001645 whichPage = validateNewPage(whichPage);
1646
Adam Cohen7d30a372013-07-01 17:03:59 -07001647 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001648
Winson Chung321e9ee2010-08-09 13:37:56 -07001649 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001650 if (immediate) {
1651 duration = 0;
1652 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001653 duration = Math.abs(delta);
1654 }
1655
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001656 if (duration != 0) {
1657 pageBeginTransition();
1658 }
1659
Adam Cohenf358a4b2013-07-23 16:47:31 -07001660 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001661 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001662 }
Adam Cohenf9618852013-11-08 06:45:03 -08001663
1664 if (interpolator != null) {
1665 mScroller.setInterpolator(interpolator);
1666 } else {
1667 mScroller.setInterpolator(mDefaultInterpolator);
1668 }
1669
Sunny Goyalc86df472016-02-25 09:19:38 -08001670 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001671
Adam Cohen674531f2013-12-13 15:07:14 -08001672 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001673
1674 // Trigger a compute() to finish switching pages if necessary
1675 if (immediate) {
1676 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001677 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001678 }
1679
Winson Chung321e9ee2010-08-09 13:37:56 -07001680 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001681 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001682 }
1683
Winson Chung321e9ee2010-08-09 13:37:56 -07001684 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001685 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001686 }
1687
1688 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001689 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001690 }
1691
Adam Cohendbdff6b2013-09-18 19:09:15 -07001692 @Override
1693 public boolean performLongClick() {
1694 mCancelTap = true;
1695 return super.performLongClick();
1696 }
1697
Winson Chung321e9ee2010-08-09 13:37:56 -07001698 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001699 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001700
1701 SavedState(Parcelable superState) {
1702 super(superState);
1703 }
1704
Adam Cohen091440a2015-03-18 14:16:05 -07001705 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001706 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001707 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001708 }
1709
1710 @Override
1711 public void writeToParcel(Parcel out, int flags) {
1712 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001713 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001714 }
1715
1716 public static final Parcelable.Creator<SavedState> CREATOR =
1717 new Parcelable.Creator<SavedState>() {
1718 public SavedState createFromParcel(Parcel in) {
1719 return new SavedState(in);
1720 }
1721
1722 public SavedState[] newArray(int size) {
1723 return new SavedState[size];
1724 }
1725 };
1726 }
1727
Adam Cohen7d30a372013-07-01 17:03:59 -07001728 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001729 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001730 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001731 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1732 new PropertyListBuilder()
1733 .scale(1)
1734 .translationX(0)
1735 .translationY(0)
1736 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001737 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001738 anim.addListener(new AnimatorListenerAdapter() {
1739 @Override
1740 public void onAnimationEnd(Animator animation) {
1741 onPostReorderingAnimationCompleted();
1742 }
1743 });
1744 anim.start();
1745 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001746 }
1747
Sunny Goyal1d08f702015-05-04 15:50:25 -07001748 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001749 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1750 mTouchState = TOUCH_STATE_REORDERING;
1751 mIsReordering = true;
1752
Adam Cohen7d30a372013-07-01 17:03:59 -07001753 // We must invalidate to trigger a redraw to update the layers such that the drag view
1754 // is always drawn on top
1755 invalidate();
1756 }
1757
Adam Cohen091440a2015-03-18 14:16:05 -07001758 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001759 // Trigger the callback when reordering has settled
1760 --mPostReorderingPreZoomInRemainingAnimationCount;
1761 if (mPostReorderingPreZoomInRunnable != null &&
1762 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1763 mPostReorderingPreZoomInRunnable.run();
1764 mPostReorderingPreZoomInRunnable = null;
1765 }
1766 }
1767
Sunny Goyal1d08f702015-05-04 15:50:25 -07001768 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001769 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001770 }
1771
Adam Cohenf358a4b2013-07-23 16:47:31 -07001772 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001773 int dragViewIndex = indexOfChild(v);
1774
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001775 // Do not allow the first page to be moved around
1776 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001777
Adam Cohen7d30a372013-07-01 17:03:59 -07001778 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001779 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001780 // Find the drag view under the pointer
1781 mDragView = getChildAt(dragViewIndex);
1782 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1783 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001784 mReorderingStarted = true;
1785
Adam Cohenf9618852013-11-08 06:45:03 -08001786 snapToPage(getPageNearestToCenterOfScreen());
1787 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001788 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001789 return true;
1790 }
1791 return false;
1792 }
1793
1794 boolean isReordering(boolean testTouchState) {
1795 boolean state = mIsReordering;
1796 if (testTouchState) {
1797 state &= (mTouchState == TOUCH_STATE_REORDERING);
1798 }
1799 return state;
1800 }
1801 void endReordering() {
1802 // For simplicity, we call endReordering sometimes even if reordering was never started.
1803 // In that case, we don't want to do anything.
1804 if (!mReorderingStarted) return;
1805 mReorderingStarted = false;
1806
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001807 mPostReorderingPreZoomInRunnable = new Runnable() {
1808 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001809 // If we haven't flung-to-delete the current child,
1810 // then we just animate the drag view back into position
1811 onEndReordering();
1812
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001813 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001814 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001815 };
Adam Cohen7d30a372013-07-01 17:03:59 -07001816
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001817 mPostReorderingPreZoomInRemainingAnimationCount =
1818 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
1819 // Snap to the current page
1820 snapToPage(indexOfChild(mDragView), 0);
1821 // Animate the drag view back to the front position
1822 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001823 }
1824
Winson Chung6a0f57d2011-06-29 20:10:49 -07001825 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001826 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001827 @Override
1828 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1829 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001830 info.setScrollable(getPageCount() > 1);
1831 if (getCurrentPage() < getPageCount() - 1) {
1832 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1833 }
1834 if (getCurrentPage() > 0) {
1835 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1836 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001837 info.setClassName(getClass().getName());
1838
1839 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1840 // Besides disabling the accessibility long-click, this also prevents this view from getting
1841 // accessibility focus.
1842 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001843 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001844 }
1845
1846 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001847 public void sendAccessibilityEvent(int eventType) {
1848 // Don't let the view send real scroll events.
1849 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1850 super.sendAccessibilityEvent(eventType);
1851 }
1852 }
1853
1854 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001855 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1856 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001857 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001858 }
1859
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001860 @Override
1861 public boolean performAccessibilityAction(int action, Bundle arguments) {
1862 if (super.performAccessibilityAction(action, arguments)) {
1863 return true;
1864 }
1865 switch (action) {
1866 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1867 if (getCurrentPage() < getPageCount() - 1) {
1868 scrollRight();
1869 return true;
1870 }
1871 } break;
1872 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1873 if (getCurrentPage() > 0) {
1874 scrollLeft();
1875 return true;
1876 }
1877 } break;
1878 }
1879 return false;
1880 }
1881
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001882 protected String getPageIndicatorDescription() {
1883 return getCurrentPageDescription();
1884 }
1885
Adam Cohen0ffac432013-07-10 11:19:26 -07001886 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001887 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001888 getNextPage() + 1, getChildCount());
1889 }
1890
Tony Mak2e564402018-02-27 17:19:34 +00001891 protected float getDownMotionX() {
1892 return mDownMotionX;
1893 }
1894
1895 protected float getDownMotionY() {
1896 return mDownMotionY;
1897 }
1898
Winson Chungd11265e2011-08-30 13:37:23 -07001899 @Override
1900 public boolean onHoverEvent(android.view.MotionEvent event) {
1901 return true;
1902 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001903}