blob: bb137b0d6d83d2faa6131c09b78707577e380036 [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 */
Michael Jurka8b805b12012-04-18 14:23:14 -070062public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
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
Winson Chung86f77532010-08-24 11:08:22 -070067 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070068 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Vadim Tryshevfedca432015-08-19 17:55:02 -070070 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080071 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Sunny Goyalb72d8b22017-07-14 00:02:27 -070073 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070074 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070075
Adam Cohenb64cb5a2011-02-15 13:53:42 -080076 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080077 // The page is moved more than halfway, automatically move to the next page on touch up.
78 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080079
Sunny Goyal8e2133b2015-05-06 13:39:07 -070080 private static final float MAX_SCROLL_PROGRESS = 1.0f;
81
Adam Cohen265b9a62011-12-07 14:37:18 -080082 // The following constants need to be scaled based on density. The scaled versions will be
83 // assigned to the corresponding member variables below.
84 private static final int FLING_THRESHOLD_VELOCITY = 500;
85 private static final int MIN_SNAP_VELOCITY = 1500;
86 private static final int MIN_FLING_VELOCITY = 250;
87
Adam Cohen21cd0022013-10-09 18:57:02 -070088 public static final int INVALID_RESTORE_PAGE = -1001;
89
Adam Cohenf358a4b2013-07-23 16:47:31 -070090 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080091 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070092
Adam Cohen265b9a62011-12-07 14:37:18 -080093 protected int mFlingThresholdVelocity;
94 protected int mMinFlingVelocity;
95 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070096
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected boolean mFirstLayout = true;
98
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700101 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700102
Sunny Goyal4ffec482016-02-09 11:28:52 -0800103 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800105 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800106 protected LauncherScroller mScroller;
107 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800109 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Adam Cohen7d30a372013-07-01 17:03:59 -0700111 private float mParentDownMotionX;
112 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700114 private float mDownMotionY;
115 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700116 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700117 private float mLastMotionX;
118 private float mLastMotionXRemainder;
119 private float mLastMotionY;
120 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700121
Adam Cohendbdff6b2013-09-18 19:09:15 -0700122 private boolean mCancelTap;
123
Adam Cohenedb40762013-07-18 16:45:45 -0700124 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Michael Jurka0142d492010-08-25 17:46:15 -0700126 protected final static int TOUCH_STATE_REST = 0;
127 protected final static int TOUCH_STATE_SCROLLING = 1;
128 protected final static int TOUCH_STATE_PREV_PAGE = 2;
129 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700130 protected final static int TOUCH_STATE_REORDERING = 4;
131
Michael Jurka0142d492010-08-25 17:46:15 -0700132 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800138 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700144 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Sunny Goyal061380a2016-02-29 15:15:03 -0800146 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700147
Adam Cohen8d769d62017-06-23 17:27:38 -0700148 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
149 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
150 // the screens from continuing to translate beyond the normal bounds.
151 protected int mOverScrollX;
152
153 protected int mUnboundedScrollX;
154
Winson Chungd2be3812013-07-16 11:11:32 -0700155 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700157 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700158
159 // Reordering
160 // We use the min scale to determine how much to expand the actually PagedView measured
161 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700162 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700163 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700164 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
165
Sunny Goyalcf25b522015-07-09 00:01:18 -0700166 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700167 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 // This variable's scope is only for the duration of startReordering() and endReordering()
170 private boolean mReorderingStarted = false;
171 // This variable's scope is for the duration of startReordering() and after the zoomIn()
172 // animation after endReordering()
173 private boolean mIsReordering;
174 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700175 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700176 private int mPostReorderingPreZoomInRemainingAnimationCount;
177 private Runnable mPostReorderingPreZoomInRunnable;
178
Adam Cohen7d30a372013-07-01 17:03:59 -0700179 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700180 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700181 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700182 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700183
John Spurlock77e1f472013-09-11 10:09:51 -0400184 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700185 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400186
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800187 // Similar to the platform implementation of isLayoutValid();
188 protected boolean mIsLayoutValid;
189
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 public PagedView(Context context) {
191 this(context, null);
192 }
193
194 public PagedView(Context context, AttributeSet attrs) {
195 this(context, attrs, 0);
196 }
197
198 public PagedView(Context context, AttributeSet attrs, int defStyle) {
199 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700200
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 TypedArray a = context.obtainStyledAttributes(attrs,
202 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700203 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 a.recycle();
205
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700207 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700208 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 }
210
211 /**
212 * Initializes various states for this workspace.
213 */
Michael Jurka0142d492010-08-25 17:46:15 -0700214 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800215 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700216 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700217 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700218
219 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700220 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800222
Sunny Goyalcf25b522015-07-09 00:01:18 -0700223 float density = getResources().getDisplayMetrics().density;
224 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
225 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
226 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700227 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700228 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230
Adam Cohenf9618852013-11-08 06:45:03 -0800231 protected void setDefaultInterpolator(Interpolator interpolator) {
232 mDefaultInterpolator = interpolator;
233 mScroller.setInterpolator(mDefaultInterpolator);
234 }
235
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700236 public void initParentViews(View parent) {
237 if (mPageIndicatorViewId > -1) {
238 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700239 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700240 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700241 }
242 }
243
Adam Cohen7d30a372013-07-01 17:03:59 -0700244 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700245 private float[] mapPointFromViewToParent(View v, float x, float y) {
246 sTmpPoint[0] = x;
247 sTmpPoint[1] = y;
248 v.getMatrix().mapPoints(sTmpPoint);
249 sTmpPoint[0] += v.getLeft();
250 sTmpPoint[1] += v.getTop();
251 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700252 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700253 private float[] mapPointFromParentToView(View v, float x, float y) {
254 sTmpPoint[0] = x - v.getLeft();
255 sTmpPoint[1] = y - v.getTop();
256 v.getMatrix().invert(sTmpInvMatrix);
257 sTmpInvMatrix.mapPoints(sTmpPoint);
258 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700259 }
260
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700261 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262 if (mDragView != null) {
263 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
264 (mDragViewBaselineLeft - mDragView.getLeft());
265 float y = mLastMotionY - mDownMotionY;
266 mDragView.setTranslationX(x);
267 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700268
Adam Cohenf358a4b2013-07-23 16:47:31 -0700269 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
270 + x + ", " + y);
271 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700272 }
273
Adam Cohen7d30a372013-07-01 17:03:59 -0700274 @Override
275 public void setScaleX(float scaleX) {
276 super.setScaleX(scaleX);
277 if (isReordering(true)) {
278 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
279 mLastMotionX = p[0];
280 mLastMotionY = p[1];
281 updateDragViewTranslationDuringDrag();
282 }
283 }
284
Hyunyoung Song77441692016-06-27 22:00:48 -0700285 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700286 return mPageIndicator;
287 }
288
Adam Cohen674531f2013-12-13 15:07:14 -0800289 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700290 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
291 * that the user was on before entering free scroll mode (e.g. the home screen page they
292 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
293 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700295 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700296 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700298
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700299 /**
300 * Returns the index of page to be shown immediately afterwards.
301 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700302 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700303 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
304 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700305
Adam Cohenf9c184a2016-01-15 16:47:43 -0800306 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 return getChildCount();
308 }
309
Sunny Goyal83a8f042015-05-19 12:52:12 -0700310 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 return getChildAt(index);
312 }
313
Adam Cohenae4f1552011-10-20 00:15:42 -0700314 protected int indexToPage(int index) {
315 return index;
316 }
317
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800319 * Updates the scroll of the current page immediately to its final scroll position. We use this
320 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
321 * the previous tab page.
322 */
323 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700324 // If the current page is invalid, just reset the scroll position to zero
325 int newX = 0;
326 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700327 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700328 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800329 scrollTo(newX, 0);
330 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700331 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800332 }
333
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700334 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700335 mScroller.abortAnimation();
336 // We need to clean up the next page here to avoid computeScrollHelper from
337 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700338 if (resetNextPage) {
339 mNextPage = INVALID_PAGE;
340 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700341 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700342
Tony Wickham8f7ead32016-04-07 18:46:44 -0700343 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700344 mScroller.forceFinished(true);
345 // We need to clean up the next page here to avoid computeScrollHelper from
346 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700347 if (resetNextPage) {
348 mNextPage = INVALID_PAGE;
349 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700350 }
351
Adam Cohen6f127a62014-06-12 14:54:41 -0700352 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700353 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700354 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700355 }
356
Chet Haasebc2f0822012-10-26 17:59:53 -0700357 /**
Winson Chung86f77532010-08-24 11:08:22 -0700358 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700359 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700360 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800361 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700362 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800363 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800364 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
365 // the default
366 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800367 return;
368 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700369 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700370 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700371 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700372 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800373 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 }
375
Winson Chung8c87cd82013-07-23 16:20:10 -0700376 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800377 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
378 * has settled.
379 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700380 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800381 updatePageIndicator();
382 }
383
384 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700385 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700386 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700387 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700388 if (!isReordering(false)) {
389 mPageIndicator.setActiveMarker(getNextPage());
390 }
Winson Chungd2be3812013-07-16 11:11:32 -0700391 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700393 protected void pageBeginTransition() {
394 if (!mIsPageInTransition) {
395 mIsPageInTransition = true;
396 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700397 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700398 }
399
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700400 protected void pageEndTransition() {
401 if (mIsPageInTransition) {
402 mIsPageInTransition = false;
403 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700404 }
Michael Jurka0142d492010-08-25 17:46:15 -0700405 }
406
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700407 protected boolean isPageInTransition() {
408 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700409 }
410
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700411 /**
412 * Called when the page starts moving as part of the scroll. Subclasses can override this
413 * to provide custom behavior during animation.
414 */
415 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700416 }
417
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700418 /**
419 * Called when the page ends moving as part of the scroll. Subclasses can override this
420 * to provide custom behavior during animation.
421 */
422 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700423 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700424 }
425
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 /**
Winson Chung86f77532010-08-24 11:08:22 -0700427 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 *
429 * @param l The listener used to respond to long clicks.
430 */
431 @Override
432 public void setOnLongClickListener(OnLongClickListener l) {
433 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700434 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700436 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 }
Adam Cohen1697b792013-09-17 19:08:21 -0700438 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 }
440
Sunny Goyalc86df472016-02-25 09:19:38 -0800441 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700442 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800443 }
444
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800446 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800447 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800448 }
449
450 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700451 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700452 // In free scroll mode, we clamp the scrollX
453 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700454 // If the scroller is trying to move to a location beyond the maximum allowed
455 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700456 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700457 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700458 }
459
Sunny Goyal4d519f22017-10-24 10:32:40 -0700460 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700461 }
462
Adam Cohen8d769d62017-06-23 17:27:38 -0700463 mUnboundedScrollX = x;
464
Sunny Goyal70660032015-05-14 00:07:08 -0700465 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
466 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700467 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700468 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800469 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700470 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700471 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700472 overScroll(x - mMaxScrollX);
473 } else {
474 overScroll(x);
475 }
Adam Cohen68d73932010-11-15 10:50:58 -0800476 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700477 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700478 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800479 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700480 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700481 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700482 overScroll(x);
483 } else {
484 overScroll(x - mMaxScrollX);
485 }
Adam Cohen68d73932010-11-15 10:50:58 -0800486 }
487 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700488 if (mWasInOverscroll) {
489 overScroll(0);
490 mWasInOverscroll = false;
491 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700492 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800493 super.scrollTo(x, y);
494 }
495
Adam Cohen7d30a372013-07-01 17:03:59 -0700496 // Update the last motion events when scrolling
497 if (isReordering(true)) {
498 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
499 mLastMotionX = p[0];
500 mLastMotionY = p[1];
501 updateDragViewTranslationDuringDrag();
502 }
Michael Jurka0142d492010-08-25 17:46:15 -0700503 }
504
Adam Cohen53805212013-10-01 10:39:23 -0700505 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800506 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700507 if (mCurrentPage != getNextPage()) {
508 AccessibilityEvent ev =
509 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700510 ev.setScrollable(true);
511 ev.setScrollX(getScrollX());
512 ev.setScrollY(getScrollY());
513 ev.setMaxScrollX(mMaxScrollX);
514 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700515
Vadim Tryshevf4715972015-05-08 17:21:03 -0700516 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700517 }
Adam Cohen53805212013-10-01 10:39:23 -0700518 }
519 }
520
Michael Jurka0142d492010-08-25 17:46:15 -0700521 // we moved this functionality to a helper function so SmoothPagedView can reuse it
522 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800523 return computeScrollHelper(true);
524 }
525
526 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700528 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800529 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700530 || getScrollY() != mScroller.getCurrY()
531 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700532 float scaleX = mFreeScroll ? getScaleX() : 1f;
533 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
534 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700535 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800536 if (shouldInvalidate) {
537 invalidate();
538 }
Michael Jurka0142d492010-08-25 17:46:15 -0700539 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800540 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700541 sendScrollAccessibilityEvent();
542
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700543 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700544 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700545 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700546 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700547
Adam Cohen73aa9752010-11-24 16:26:58 -0800548 // We don't want to trigger a page end moving unless the page has settled
549 // and the user has stopped scrolling
550 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700551 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800552 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700553
Adam Cohen7d30a372013-07-01 17:03:59 -0700554 onPostReorderingAnimationCompleted();
Sunny Goyald0030b02017-12-08 15:07:24 -0800555 if (isAccessibilityEnabled(getContext())) {
Adam Cohen53805212013-10-01 10:39:23 -0700556 // Notify the user when the page changes
557 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700558 }
Michael Jurka0142d492010-08-25 17:46:15 -0700559 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 }
Michael Jurka0142d492010-08-25 17:46:15 -0700561 return false;
562 }
563
564 @Override
565 public void computeScroll() {
566 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 }
568
Sunny Goyalce8809a2018-01-18 14:02:47 -0800569 public int getExpectedHeight() {
570 return getMeasuredHeight();
571 }
572
Adam Cohen410f3cd2013-09-22 12:09:32 -0700573 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800574 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800575 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700576 }
577
Sunny Goyalce8809a2018-01-18 14:02:47 -0800578 public int getExpectedWidth() {
579 return getMeasuredWidth();
580 }
581
Sunny Goyalbc683e92017-12-06 10:25:07 -0800582 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800583 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800584 - mInsets.left - mInsets.right;
585 }
586
Winson Chung321e9ee2010-08-09 13:37:56 -0700587 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800588 public void requestLayout() {
589 mIsLayoutValid = false;
590 super.requestLayout();
591 }
592
593 @Override
594 public void forceLayout() {
595 mIsLayoutValid = false;
596 super.forceLayout();
597 }
598
599 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700601 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700602 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
603 return;
604 }
605
Adam Cohen7d30a372013-07-01 17:03:59 -0700606 // We measure the dimensions of the PagedView to be larger than the pages so that when we
607 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700608 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
609 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
610 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700611 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700612
Adam Cohen7d30a372013-07-01 17:03:59 -0700613 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
614 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
615 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 }
617
Winson Chung8aad6102012-05-11 16:27:49 -0700618 // Return early if we aren't given a proper dimension
619 if (widthSize <= 0 || heightSize <= 0) {
620 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
621 return;
622 }
623
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700625 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700626 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700627
Sunny Goyalac00cba2017-11-13 15:58:01 -0800628 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800629 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800630 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800631 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700632
Sunny Goyalac00cba2017-11-13 15:58:01 -0800633 // measureChildren takes accounts for content padding, we only need to care about extra
634 // space due to insets.
635 measureChildren(myWidthSpec, myHeightSpec);
636 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800637 }
638
Sunny Goyalcf25b522015-07-09 00:01:18 -0700639 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700641 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800642 mIsLayoutValid = true;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700643 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700644 return;
645 }
646
Winson Chung785d2eb2011-04-14 16:08:02 -0700647 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700649
Sunny Goyal70660032015-05-14 00:07:08 -0700650 final int startIndex = mIsRtl ? childCount - 1 : 0;
651 final int endIndex = mIsRtl ? -1 : childCount;
652 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700653
Adam Cohen7d30a372013-07-01 17:03:59 -0700654 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700655
Sunny Goyal228153d2018-01-04 15:35:22 -0800656 int scrollOffsetLeft = mInsets.left + getPaddingLeft();
657 int childLeft = scrollOffsetLeft;
658
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800659 boolean pageScrollChanged = false;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700660 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
661 mPageScrolls = new int[childCount];
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800662 pageScrollChanged = true;
Adam Cohenedb40762013-07-18 16:45:45 -0700663 }
664
Adam Cohen0ffac432013-07-10 11:19:26 -0700665 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700666 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 if (child.getVisibility() != View.GONE) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800668 int childTop = getPaddingTop() + mInsets.top;
669 childTop += (getMeasuredHeight() - mInsets.top - mInsets.bottom - verticalPadding
Sunny Goyalac00cba2017-11-13 15:58:01 -0800670 - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100671
Adam Cohen96d30a12013-07-16 18:13:21 -0700672 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700673 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800674
Winson Chung785d2eb2011-04-14 16:08:02 -0700675 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700676 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800677 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700678
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800679 final int pageScroll = childLeft - scrollOffsetLeft;
680 if (mPageScrolls[i] != pageScroll) {
681 pageScrollChanged = true;
682 mPageScrolls[i] = pageScroll;
683 }
Adam Cohen84a465a2013-11-11 18:49:56 +0000684
Sunny Goyalac00cba2017-11-13 15:58:01 -0800685 childLeft += childWidth + mPageSpacing + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 }
687 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700688
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700689 final LayoutTransition transition = getLayoutTransition();
690 // If the transition is running defer updating max scroll, as some empty pages could
691 // still be present, and a max scroll change could cause sudden jumps in scroll.
692 if (transition != null && transition.isRunning()) {
693 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
694
695 @Override
696 public void startTransition(LayoutTransition transition, ViewGroup container,
697 View view, int transitionType) { }
698
699 @Override
700 public void endTransition(LayoutTransition transition, ViewGroup container,
701 View view, int transitionType) {
702 // Wait until all transitions are complete.
703 if (!transition.isRunning()) {
704 transition.removeTransitionListener(this);
705 updateMaxScrollX();
706 }
707 }
708 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700709 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700710 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700711 }
712
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700713 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
714 updateCurrentPageScroll();
715 mFirstLayout = false;
716 }
717
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800718 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700719 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700720 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700721 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700722
723 if (isReordering(true)) {
724 updateDragViewTranslationDuringDrag();
725 }
Winson Chunge3193b92010-09-10 11:44:42 -0700726 }
727
Sunny Goyala1fbd842015-05-20 13:40:27 -0700728 protected int getChildGap() {
729 return 0;
730 }
731
Sunny Goyal4d519f22017-10-24 10:32:40 -0700732 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700733 mMaxScrollX = computeMaxScrollX();
734 }
735
736 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700737 int childCount = getChildCount();
738 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700739 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700740 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700741 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700742 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700743 }
744 }
745
Adam Cohen3b185e22013-10-29 14:45:58 -0700746 public void setPageSpacing(int pageSpacing) {
747 mPageSpacing = pageSpacing;
748 requestLayout();
749 }
750
Winson Chunge3193b92010-09-10 11:44:42 -0700751 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700753 // Update the page indicator, we don't update the page indicator as we
754 // add/remove pages
755 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700756 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700757 }
758
Adam Cohen2591f6a2011-10-25 14:36:40 -0700759 // This ensures that when children are added, they get the correct transforms / alphas
760 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700761 invalidate();
762 }
763
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 @Override
765 public void onChildViewRemoved(View parent, View child) {
Tonya361c722017-07-04 09:43:06 -0700766 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700767 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 }
769
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700770 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700771 // Update the page indicator, we don't update the page indicator as we
772 // add/remove pages
773 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700774 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700775 }
776 }
777
778 @Override
779 public void removeView(View v) {
780 // XXX: We should find a better way to hook into this before the view
781 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700782 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700783 super.removeView(v);
784 }
785 @Override
786 public void removeViewInLayout(View v) {
787 // XXX: We should find a better way to hook into this before the view
788 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700789 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700790 super.removeViewInLayout(v);
791 }
792 @Override
793 public void removeViewAt(int index) {
794 // XXX: We should find a better way to hook into this before the view
795 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700796 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700797 super.removeViewAt(index);
798 }
799 @Override
800 public void removeAllViewsInLayout() {
801 // Update the page indicator, we don't update the page indicator as we
802 // add/remove pages
803 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700804 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700805 }
806
807 super.removeAllViewsInLayout();
808 }
809
Adam Cohen73894962011-10-31 13:17:17 -0700810 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700811 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800812 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700813 }
814
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 @Override
816 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700817 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700818 if (page != mCurrentPage || !mScroller.isFinished()) {
819 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 return true;
821 }
822 return false;
823 }
824
825 @Override
826 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700827 int focusablePage;
828 if (mNextPage != INVALID_PAGE) {
829 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700831 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 }
Winson Chung86f77532010-08-24 11:08:22 -0700833 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700835 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 }
837 return false;
838 }
839
840 @Override
841 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800842 if (super.dispatchUnhandledMove(focused, direction)) {
843 return true;
844 }
845
846 if (mIsRtl) {
847 if (direction == View.FOCUS_LEFT) {
848 direction = View.FOCUS_RIGHT;
849 } else if (direction == View.FOCUS_RIGHT) {
850 direction = View.FOCUS_LEFT;
851 }
852 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700854 if (getCurrentPage() > 0) {
855 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 return true;
857 }
858 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 if (getCurrentPage() < getPageCount() - 1) {
860 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 return true;
862 }
863 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800864 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866
867 @Override
868 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700869 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
870 return;
871 }
872
Adam Cohen0ffac432013-07-10 11:19:26 -0700873 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700874 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700875 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 }
877 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700878 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700879 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 }
881 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700882 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700883 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
885 }
886 }
887
888 /**
889 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700890 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 *
Winson Chung86f77532010-08-24 11:08:22 -0700892 * This happens when live folders requery, and if they're off page, they
893 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 */
895 @Override
896 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700897 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 View v = focused;
899 while (true) {
900 if (v == current) {
901 super.focusableViewAvailable(focused);
902 return;
903 }
904 if (v == this) {
905 return;
906 }
907 ViewParent parent = v.getParent();
908 if (parent instanceof View) {
909 v = (View)v.getParent();
910 } else {
911 return;
912 }
913 }
914 }
915
916 /**
917 * {@inheritDoc}
918 */
919 @Override
920 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
921 if (disallowIntercept) {
922 // We need to make sure to cancel our long press if
923 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700924 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700925 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700926 }
927 super.requestDisallowInterceptTouchEvent(disallowIntercept);
928 }
929
Adam Cohen7d30a372013-07-01 17:03:59 -0700930 /** Returns whether x and y originated within the buffered viewport */
931 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800932 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700933 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700934 }
935
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 @Override
937 public boolean onInterceptTouchEvent(MotionEvent ev) {
938 /*
939 * This method JUST determines whether we want to intercept the motion.
940 * If we return true, onTouchEvent will be called and we do the actual
941 * scrolling there.
942 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800943 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700944
Winson Chung45e1d6e2010-11-09 17:19:49 -0800945 // Skip touch handling if there are no pages to swipe
946 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
947
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 /*
949 * Shortcut the most recurring case: the user is in the dragging
950 * state and he is moving his finger. We want to intercept this
951 * motion.
952 */
953 final int action = ev.getAction();
954 if ((action == MotionEvent.ACTION_MOVE) &&
955 (mTouchState == TOUCH_STATE_SCROLLING)) {
956 return true;
957 }
958
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 switch (action & MotionEvent.ACTION_MASK) {
960 case MotionEvent.ACTION_MOVE: {
961 /*
962 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
963 * whether the user has moved far enough from his original down touch.
964 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700965 if (mActivePointerId != INVALID_POINTER) {
966 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700967 }
968 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
969 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
970 // i.e. fall through to the next case (don't break)
971 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
972 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700973 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 }
975
976 case MotionEvent.ACTION_DOWN: {
977 final float x = ev.getX();
978 final float y = ev.getY();
979 // Remember location of down touch
980 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700981 mDownMotionY = y;
982 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 mLastMotionX = x;
984 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -0700985 float[] p = mapPointFromViewToParent(this, x, y);
986 mParentDownMotionX = p[0];
987 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -0800988 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800989 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700991
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 /*
993 * If being flinged and user touches the screen, initiate drag;
994 * otherwise don't. mScroller.isFinished should be false when
995 * being flinged.
996 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700997 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000998 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800999
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001000 if (finishedScrolling) {
1001 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001002 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001003 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001004 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001005 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001006 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001007 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1008 mTouchState = TOUCH_STATE_SCROLLING;
1009 } else {
1010 mTouchState = TOUCH_STATE_REST;
1011 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001012 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001013
Winson Chung321e9ee2010-08-09 13:37:56 -07001014 break;
1015 }
1016
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001018 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001019 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 break;
1021
1022 case MotionEvent.ACTION_POINTER_UP:
1023 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001024 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 break;
1026 }
1027
1028 /*
1029 * The only time we want to intercept motion events is if we are in the
1030 * drag mode.
1031 */
1032 return mTouchState != TOUCH_STATE_REST;
1033 }
1034
Adam Cohenf8d28232011-02-01 21:47:00 -08001035 protected void determineScrollingStart(MotionEvent ev) {
1036 determineScrollingStart(ev, 1.0f);
1037 }
1038
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 /*
1040 * Determines if we should change the touch state to start scrolling after the
1041 * user moves their touch point too far.
1042 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001043 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001044 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001046 if (pointerIndex == -1) return;
1047
1048 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 final float x = ev.getX(pointerIndex);
1050 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001051 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1052
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001054
Adam Cohenf8d28232011-02-01 21:47:00 -08001055 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001057
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001058 if (xMoved) {
1059 // Scroll if the user moved far enough along the X axis
1060 mTouchState = TOUCH_STATE_SCROLLING;
1061 mTotalMotionX += Math.abs(mLastMotionX - x);
1062 mLastMotionX = x;
1063 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001064 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001065 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001066 // Stop listening for things like pinches.
1067 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001068 }
1069 }
1070
1071 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001072 // Try canceling the long press. It could also have been scheduled
1073 // by a distant descendant, so use the mAllowLongPress flag to block
1074 // everything
1075 final View currentPage = getPageAt(mCurrentPage);
1076 if (currentPage != null) {
1077 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 }
1079 }
1080
Adam Cohenb5ba0972011-09-07 18:02:31 -07001081 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001082 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001083
Adam Cohenedb40762013-07-18 16:45:45 -07001084 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001085 int count = getChildCount();
1086
1087 final int totalDistance;
1088
1089 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001090 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001091 adjacentPage = page - 1;
1092 }
1093
1094 if (adjacentPage < 0 || adjacentPage > count - 1) {
1095 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1096 } else {
1097 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1098 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001099
1100 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001101 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1102 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001103 return scrollProgress;
1104 }
1105
Adam Cohenedb40762013-07-18 16:45:45 -07001106 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001107 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001108 return 0;
1109 } else {
1110 return mPageScrolls[index];
1111 }
1112 }
1113
Adam Cohen564a2e72013-10-09 14:47:32 -07001114 // While layout transitions are occurring, a child's position may stray from its baseline
1115 // position. This method returns the magnitude of this stray at any given time.
1116 public int getLayoutTransitionOffsetForPage(int index) {
1117 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1118 return 0;
1119 } else {
1120 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001121
Sunny Goyal228153d2018-01-04 15:35:22 -08001122 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1123 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001124 return (int) (child.getX() - baselineX);
1125 }
1126 }
1127
Adam Cohenb5ba0972011-09-07 18:02:31 -07001128 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001129 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001130
Sunny Goyal228153d2018-01-04 15:35:22 -08001131 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001132 if (amount < 0) {
1133 mOverScrollX = overScrollAmount;
1134 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001135 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001136 mOverScrollX = mMaxScrollX + overScrollAmount;
1137 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001138 }
1139 invalidate();
1140 }
1141
Adam Cohenb5ba0972011-09-07 18:02:31 -07001142 protected void overScroll(float amount) {
1143 dampedOverScroll(amount);
1144 }
1145
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001146 /**
1147 * return true if freescroll has been enabled, false otherwise
1148 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001149 protected void enableFreeScroll() {
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001150 enableFreeScroll(false);
1151 }
1152
1153 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001154 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001155 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001156 }
1157
Sunny Goyalc99cb172017-10-19 16:15:09 -07001158 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001159 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001160 }
1161
Adam Cohenf9618852013-11-08 06:45:03 -08001162 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001163 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001164 mFreeScroll = freeScroll;
1165
Adam Cohenf9618852013-11-08 06:45:03 -08001166 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001167 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001168 } else if (wasFreeScroll) {
1169 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001170 }
1171
1172 setEnableOverscroll(!freeScroll);
1173 }
1174
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001175 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001176 mAllowOverScroll = enable;
1177 }
1178
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001179 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001180 if (mDragView != null) {
1181 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1182 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001183 int minDistance = Integer.MAX_VALUE;
1184 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001185 int maxPageNo = getChildCount() - 1;
1186 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001187 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001188 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001189 int d = Math.abs(dragX - pageX);
1190 if (d < minDistance) {
1191 minIndex = i;
1192 minDistance = d;
1193 }
1194 }
1195 return minIndex;
1196 }
1197 return -1;
1198 }
1199
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 @Override
1201 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001202 super.onTouchEvent(ev);
1203
Winson Chung45e1d6e2010-11-09 17:19:49 -08001204 // Skip touch handling if there are no pages to swipe
1205 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1206
Michael Jurkab8f06722010-10-10 15:58:46 -07001207 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208
1209 final int action = ev.getAction();
1210
1211 switch (action & MotionEvent.ACTION_MASK) {
1212 case MotionEvent.ACTION_DOWN:
1213 /*
1214 * If being flinged and user touches, stop the fling. isFinished
1215 * will be false if being flinged.
1216 */
1217 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001218 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 }
1220
1221 // Remember where the motion event started
1222 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001223 mDownMotionY = mLastMotionY = ev.getY();
1224 mDownScrollX = getScrollX();
1225 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1226 mParentDownMotionX = p[0];
1227 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001228 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001229 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001231
Michael Jurka0142d492010-08-25 17:46:15 -07001232 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001233 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001234 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001235 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 break;
1237
1238 case MotionEvent.ACTION_MOVE:
1239 if (mTouchState == TOUCH_STATE_SCROLLING) {
1240 // Scroll to follow the motion event
1241 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001242
1243 if (pointerIndex == -1) return true;
1244
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001246 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001247
Adam Cohenaefd4e12011-02-14 16:39:38 -08001248 mTotalMotionX += Math.abs(deltaX);
1249
Winson Chungc0844aa2011-02-02 15:25:58 -08001250 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1251 // keep the remainder because we are actually testing if we've moved from the last
1252 // scrolled position (which is discrete).
1253 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001254 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001255 mLastMotionX = x;
1256 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 } else {
1258 awakenScrollBars();
1259 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001260 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1261 // Update the last motion position
1262 mLastMotionX = ev.getX();
1263 mLastMotionY = ev.getY();
1264
1265 // Update the parent down so that our zoom animations take this new movement into
1266 // account
1267 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1268 mParentDownMotionX = pt[0];
1269 mParentDownMotionY = pt[1];
1270 updateDragViewTranslationDuringDrag();
1271
1272 // Find the closest page to the touch point
1273 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001274
Adam Cohen7d30a372013-07-01 17:03:59 -07001275 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1276 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1277 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1278 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1279
Adam Cohenf358a4b2013-07-23 16:47:31 -07001280 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001281 // Do not allow any page to be moved to 0th position.
1282 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001283 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001284 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1285 mSidePageHoverIndex = pageUnderPointIndex;
1286 mSidePageHoverRunnable = new Runnable() {
1287 @Override
1288 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001289 // Setup the scroll to the correct page before we swap the views
1290 snapToPage(pageUnderPointIndex);
1291
1292 // For each of the pages between the paged view and the drag view,
1293 // animate them from the previous position to the new position in
1294 // the layout (as a result of the drag view moving in the layout)
1295 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1296 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1297 dragViewIndex + 1 : pageUnderPointIndex;
1298 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1299 dragViewIndex - 1 : pageUnderPointIndex;
1300 for (int i = lowerIndex; i <= upperIndex; ++i) {
1301 View v = getChildAt(i);
1302 // dragViewIndex < pageUnderPointIndex, so after we remove the
1303 // drag view all subsequent views to pageUnderPointIndex will
1304 // shift down.
Sunny Goyal228153d2018-01-04 15:35:22 -08001305 int oldX = getChildOffset(i);
1306 int newX = getChildOffset(i + shiftDelta);
Adam Cohen7d30a372013-07-01 17:03:59 -07001307
1308 // Animate the view translation from its old position to its new
1309 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001310 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001311 if (anim != null) {
1312 anim.cancel();
1313 }
1314
1315 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001316 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001318 anim.start();
1319 v.setTag(anim);
1320 }
1321
1322 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001323 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001324 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001325 if (mPageIndicator != null) {
1326 mPageIndicator.setActiveMarker(getNextPage());
1327 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001328 }
1329 };
1330 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1331 }
1332 } else {
1333 removeCallbacks(mSidePageHoverRunnable);
1334 mSidePageHoverIndex = -1;
1335 }
Adam Cohen564976a2010-10-13 18:52:07 -07001336 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 determineScrollingStart(ev);
1338 }
1339 break;
1340
1341 case MotionEvent.ACTION_UP:
1342 if (mTouchState == TOUCH_STATE_SCROLLING) {
1343 final int activePointerId = mActivePointerId;
1344 final int pointerIndex = ev.findPointerIndex(activePointerId);
1345 final float x = ev.getX(pointerIndex);
1346 final VelocityTracker velocityTracker = mVelocityTracker;
1347 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1348 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001349 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001350 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001351 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1352 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001353
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001354 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1355
Adam Cohen00481b32011-11-18 12:03:48 -08001356 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001357 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001358
Adam Cohenf358a4b2013-07-23 16:47:31 -07001359 if (!mFreeScroll) {
1360 // In the case that the page is moved far to one direction and then is flung
1361 // in the opposite direction, we use a threshold to determine whether we should
1362 // just return to the starting page, or if we should skip one further.
1363 boolean returnToOriginalPage = false;
1364 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1365 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1366 returnToOriginalPage = true;
1367 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001368
Adam Cohenf358a4b2013-07-23 16:47:31 -07001369 int finalPage;
1370 // We give flings precedence over large moves, which is why we short-circuit our
1371 // test for a large move if a fling has been registered. That is, a large
1372 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001373 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1374 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1376 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1377 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1378 snapToPageWithVelocity(finalPage, velocityX);
1379 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1380 (isFling && isVelocityXLeft)) &&
1381 mCurrentPage < getChildCount() - 1) {
1382 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1383 snapToPageWithVelocity(finalPage, velocityX);
1384 } else {
1385 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001386 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001389 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 }
1391
1392 float scaleX = getScaleX();
1393 int vX = (int) (-velocityX * scaleX);
1394 int initialScrollX = (int) (getScrollX() * scaleX);
1395
Adam Cohenf9618852013-11-08 06:45:03 -08001396 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 mScroller.fling(initialScrollX,
1398 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001399 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1400 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1401 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1402 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1403 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1404 && unscaledScrollX < lastPageScroll) {
1405 // Make sure we land directly on a page. If flinging past one of the ends,
1406 // don't change the velocity as it will get stopped at the end anyway.
1407 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1408 // Ensure the scroll/snap doesn't happen too fast;
1409 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1410 - mScroller.getDuration();
1411 if (extraScrollDuration > 0) {
1412 mScroller.extendDuration(extraScrollDuration);
1413 }
1414 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001415 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001417 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001418 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1419 // at this point we have not moved beyond the touch slop
1420 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1421 // we can just page
1422 int nextPage = Math.max(0, mCurrentPage - 1);
1423 if (nextPage != mCurrentPage) {
1424 snapToPage(nextPage);
1425 } else {
1426 snapToDestination();
1427 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001428 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 // at this point we have not moved beyond the touch slop
1430 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1431 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001432 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1433 if (nextPage != mCurrentPage) {
1434 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 } else {
1436 snapToDestination();
1437 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001438 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1439 // Update the last motion position
1440 mLastMotionX = ev.getX();
1441 mLastMotionY = ev.getY();
1442
1443 // Update the parent down so that our zoom animations take this new movement into
1444 // account
1445 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1446 mParentDownMotionX = pt[0];
1447 mParentDownMotionY = pt[1];
1448 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001449 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001450 if (!mCancelTap) {
1451 onUnhandledTap(ev);
1452 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001454
1455 // Remove the callback to wait for the side page hover timeout
1456 removeCallbacks(mSidePageHoverRunnable);
1457 // End any intermediate reordering states
1458 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 break;
1460
1461 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001462 if (mTouchState == TOUCH_STATE_SCROLLING) {
1463 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001464 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001465 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001466 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 break;
1468
1469 case MotionEvent.ACTION_POINTER_UP:
1470 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001471 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 break;
1473 }
1474
1475 return true;
1476 }
1477
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001478 protected boolean shouldFlingForVelocity(int velocityX) {
1479 return Math.abs(velocityX) > mFlingThresholdVelocity;
1480 }
1481
Adam Cohen7d30a372013-07-01 17:03:59 -07001482 private void resetTouchState() {
1483 releaseVelocityTracker();
1484 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001485 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001486 mTouchState = TOUCH_STATE_REST;
1487 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001488 }
1489
Adam Cohenc2d6e892014-10-16 09:49:24 -07001490 /**
1491 * Triggered by scrolling via touch
1492 */
1493 protected void onScrollInteractionBegin() {
1494 }
1495
1496 protected void onScrollInteractionEnd() {
1497 }
1498
Adam Cohen1697b792013-09-17 19:08:21 -07001499 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001500 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001501 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001502
Winson Chung185d7162011-02-28 13:47:29 -08001503 @Override
1504 public boolean onGenericMotionEvent(MotionEvent event) {
1505 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1506 switch (event.getAction()) {
1507 case MotionEvent.ACTION_SCROLL: {
1508 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1509 final float vscroll;
1510 final float hscroll;
1511 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1512 vscroll = 0;
1513 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1514 } else {
1515 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1516 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1517 }
1518 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001519 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001520 : (hscroll > 0 || vscroll > 0);
1521 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001522 scrollRight();
1523 } else {
1524 scrollLeft();
1525 }
1526 return true;
1527 }
1528 }
1529 }
1530 }
1531 return super.onGenericMotionEvent(event);
1532 }
1533
Michael Jurkab8f06722010-10-10 15:58:46 -07001534 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1535 if (mVelocityTracker == null) {
1536 mVelocityTracker = VelocityTracker.obtain();
1537 }
1538 mVelocityTracker.addMovement(ev);
1539 }
1540
1541 private void releaseVelocityTracker() {
1542 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001543 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001544 mVelocityTracker.recycle();
1545 mVelocityTracker = null;
1546 }
1547 }
1548
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 private void onSecondaryPointerUp(MotionEvent ev) {
1550 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1551 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1552 final int pointerId = ev.getPointerId(pointerIndex);
1553 if (pointerId == mActivePointerId) {
1554 // This was our active pointer going up. Choose a new
1555 // active pointer and adjust accordingly.
1556 // TODO: Make this decision more intelligent.
1557 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1558 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1559 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001560 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001561 mActivePointerId = ev.getPointerId(newPointerIndex);
1562 if (mVelocityTracker != null) {
1563 mVelocityTracker.clear();
1564 }
1565 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001566 }
1567
Winson Chung321e9ee2010-08-09 13:37:56 -07001568 @Override
1569 public void requestChildFocus(View child, View focused) {
1570 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001571 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001572 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001573 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001574 }
1575 }
1576
Sunny Goyal228153d2018-01-04 15:35:22 -08001577 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001578 return getPageNearestToCenterOfScreen(getScrollX());
1579 }
1580
1581 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001582 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001583 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001584 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001585 final int childCount = getChildCount();
1586 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001587 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001588 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001590 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001591 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1592 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1593 minDistanceFromScreenCenter = distanceFromScreenCenter;
1594 minDistanceFromScreenCenterIndex = i;
1595 }
1596 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001597 return minDistanceFromScreenCenterIndex;
1598 }
1599
1600 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001601 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1602 }
1603
1604 protected boolean isInOverScroll() {
1605 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1606 }
1607
1608 protected int getPageSnapDuration() {
1609 if (isInOverScroll()) {
1610 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1611 }
1612 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001613 }
1614
Adam Cohene0f66b52010-11-23 15:06:07 -08001615 // We want the duration of the page snap animation to be influenced by the distance that
1616 // the screen has to travel, however, we don't want this duration to be effected in a
1617 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1618 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001619 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001620 f -= 0.5f; // center the values about 0.
1621 f *= 0.3f * Math.PI / 2.0f;
1622 return (float) Math.sin(f);
1623 }
1624
Winson Chung05a31ed2018-02-08 17:08:43 -08001625 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001626 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001627 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001628
Adam Cohenedb40762013-07-18 16:45:45 -07001629 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001630 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001631 int duration = 0;
1632
Sunny Goyal4d113a52015-05-27 10:05:28 -07001633 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001634 // If the velocity is low enough, then treat this more as an automatic page advance
1635 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001636 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001637 }
1638
1639 // Here we compute a "distance" that will be used in the computation of the overall
1640 // snap duration. This is a function of the actual distance that needs to be traveled;
1641 // we keep this value close to half screen size in order to reduce the variance in snap
1642 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001643 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001644 float distance = halfScreenSize + halfScreenSize *
1645 distanceInfluenceForSnapDuration(distanceRatio);
1646
1647 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001648 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001649
1650 // we want the page's snap velocity to approximately match the velocity at which the
1651 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001652 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1653 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001654
Winson Chung05a31ed2018-02-08 17:08:43 -08001655 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001656 }
1657
Winson Chung05a31ed2018-02-08 17:08:43 -08001658 public boolean snapToPage(int whichPage) {
1659 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001660 }
1661
Winson Chung05a31ed2018-02-08 17:08:43 -08001662 public boolean snapToPageImmediately(int whichPage) {
1663 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 }
1665
Winson Chung05a31ed2018-02-08 17:08:43 -08001666 public boolean snapToPage(int whichPage, int duration) {
1667 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001668 }
1669
Winson Chung05a31ed2018-02-08 17:08:43 -08001670 protected boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1671 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001672 }
1673
Winson Chung05a31ed2018-02-08 17:08:43 -08001674 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001675 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001676 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001677
Adam Cohenedb40762013-07-18 16:45:45 -07001678 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001679 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001680 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001681 }
1682
Winson Chung05a31ed2018-02-08 17:08:43 -08001683 protected boolean snapToPage(int whichPage, int delta, int duration) {
1684 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001685 }
Michael Jurka0142d492010-08-25 17:46:15 -07001686
Winson Chung05a31ed2018-02-08 17:08:43 -08001687 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001688 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001689 whichPage = validateNewPage(whichPage);
1690
Adam Cohen7d30a372013-07-01 17:03:59 -07001691 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001692
Winson Chung321e9ee2010-08-09 13:37:56 -07001693 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001694 if (immediate) {
1695 duration = 0;
1696 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001697 duration = Math.abs(delta);
1698 }
1699
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001700 if (duration != 0) {
1701 pageBeginTransition();
1702 }
1703
Adam Cohenf358a4b2013-07-23 16:47:31 -07001704 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001705 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001706 }
Adam Cohenf9618852013-11-08 06:45:03 -08001707
1708 if (interpolator != null) {
1709 mScroller.setInterpolator(interpolator);
1710 } else {
1711 mScroller.setInterpolator(mDefaultInterpolator);
1712 }
1713
Sunny Goyalc86df472016-02-25 09:19:38 -08001714 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001715
Adam Cohen674531f2013-12-13 15:07:14 -08001716 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001717
1718 // Trigger a compute() to finish switching pages if necessary
1719 if (immediate) {
1720 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001721 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001722 }
1723
Winson Chung321e9ee2010-08-09 13:37:56 -07001724 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001725 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001726 }
1727
Winson Chung321e9ee2010-08-09 13:37:56 -07001728 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001729 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001730 }
1731
1732 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001733 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001734 }
1735
Adam Cohendbdff6b2013-09-18 19:09:15 -07001736 @Override
1737 public boolean performLongClick() {
1738 mCancelTap = true;
1739 return super.performLongClick();
1740 }
1741
Winson Chung321e9ee2010-08-09 13:37:56 -07001742 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001743 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001744
1745 SavedState(Parcelable superState) {
1746 super(superState);
1747 }
1748
Adam Cohen091440a2015-03-18 14:16:05 -07001749 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001750 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001751 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001752 }
1753
1754 @Override
1755 public void writeToParcel(Parcel out, int flags) {
1756 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001757 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001758 }
1759
1760 public static final Parcelable.Creator<SavedState> CREATOR =
1761 new Parcelable.Creator<SavedState>() {
1762 public SavedState createFromParcel(Parcel in) {
1763 return new SavedState(in);
1764 }
1765
1766 public SavedState[] newArray(int size) {
1767 return new SavedState[size];
1768 }
1769 };
1770 }
1771
Adam Cohen7d30a372013-07-01 17:03:59 -07001772 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001773 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001774 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001775 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1776 new PropertyListBuilder()
1777 .scale(1)
1778 .translationX(0)
1779 .translationY(0)
1780 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001781 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001782 anim.addListener(new AnimatorListenerAdapter() {
1783 @Override
1784 public void onAnimationEnd(Animator animation) {
1785 onPostReorderingAnimationCompleted();
1786 }
1787 });
1788 anim.start();
1789 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001790 }
1791
Sunny Goyal1d08f702015-05-04 15:50:25 -07001792 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001793 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1794 mTouchState = TOUCH_STATE_REORDERING;
1795 mIsReordering = true;
1796
Adam Cohen7d30a372013-07-01 17:03:59 -07001797 // We must invalidate to trigger a redraw to update the layers such that the drag view
1798 // is always drawn on top
1799 invalidate();
1800 }
1801
Adam Cohen091440a2015-03-18 14:16:05 -07001802 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001803 // Trigger the callback when reordering has settled
1804 --mPostReorderingPreZoomInRemainingAnimationCount;
1805 if (mPostReorderingPreZoomInRunnable != null &&
1806 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1807 mPostReorderingPreZoomInRunnable.run();
1808 mPostReorderingPreZoomInRunnable = null;
1809 }
1810 }
1811
Sunny Goyal1d08f702015-05-04 15:50:25 -07001812 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001813 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001814 }
1815
Adam Cohenf358a4b2013-07-23 16:47:31 -07001816 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001817 int dragViewIndex = indexOfChild(v);
1818
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001819 // Do not allow the first page to be moved around
1820 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001821
Adam Cohen7d30a372013-07-01 17:03:59 -07001822 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001823 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001824 // Find the drag view under the pointer
1825 mDragView = getChildAt(dragViewIndex);
1826 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1827 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001828 mReorderingStarted = true;
1829
Adam Cohenf9618852013-11-08 06:45:03 -08001830 snapToPage(getPageNearestToCenterOfScreen());
1831 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001832 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001833 return true;
1834 }
1835 return false;
1836 }
1837
1838 boolean isReordering(boolean testTouchState) {
1839 boolean state = mIsReordering;
1840 if (testTouchState) {
1841 state &= (mTouchState == TOUCH_STATE_REORDERING);
1842 }
1843 return state;
1844 }
1845 void endReordering() {
1846 // For simplicity, we call endReordering sometimes even if reordering was never started.
1847 // In that case, we don't want to do anything.
1848 if (!mReorderingStarted) return;
1849 mReorderingStarted = false;
1850
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001851 mPostReorderingPreZoomInRunnable = new Runnable() {
1852 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001853 // If we haven't flung-to-delete the current child,
1854 // then we just animate the drag view back into position
1855 onEndReordering();
1856
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001857 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001858 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001859 };
Adam Cohen7d30a372013-07-01 17:03:59 -07001860
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001861 mPostReorderingPreZoomInRemainingAnimationCount =
1862 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
1863 // Snap to the current page
1864 snapToPage(indexOfChild(mDragView), 0);
1865 // Animate the drag view back to the front position
1866 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001867 }
1868
Winson Chung6a0f57d2011-06-29 20:10:49 -07001869 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001870 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001871 @Override
1872 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1873 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001874 info.setScrollable(getPageCount() > 1);
1875 if (getCurrentPage() < getPageCount() - 1) {
1876 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1877 }
1878 if (getCurrentPage() > 0) {
1879 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1880 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001881 info.setClassName(getClass().getName());
1882
1883 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1884 // Besides disabling the accessibility long-click, this also prevents this view from getting
1885 // accessibility focus.
1886 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001887 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001888 }
1889
1890 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001891 public void sendAccessibilityEvent(int eventType) {
1892 // Don't let the view send real scroll events.
1893 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1894 super.sendAccessibilityEvent(eventType);
1895 }
1896 }
1897
1898 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001899 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1900 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001901 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001902 }
1903
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001904 @Override
1905 public boolean performAccessibilityAction(int action, Bundle arguments) {
1906 if (super.performAccessibilityAction(action, arguments)) {
1907 return true;
1908 }
1909 switch (action) {
1910 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1911 if (getCurrentPage() < getPageCount() - 1) {
1912 scrollRight();
1913 return true;
1914 }
1915 } break;
1916 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1917 if (getCurrentPage() > 0) {
1918 scrollLeft();
1919 return true;
1920 }
1921 } break;
1922 }
1923 return false;
1924 }
1925
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001926 protected String getPageIndicatorDescription() {
1927 return getCurrentPageDescription();
1928 }
1929
Adam Cohen0ffac432013-07-10 11:19:26 -07001930 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001931 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001932 getNextPage() + 1, getChildCount());
1933 }
1934
Winson Chungd11265e2011-08-30 13:37:23 -07001935 @Override
1936 public boolean onHoverEvent(android.view.MotionEvent event) {
1937 return true;
1938 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001939}