blob: 87595d91779ae90e706785327182de7e2fb4eab0 [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 // The viewport whether the pages are to be contained (the actual view may be larger than the
159 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800160 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700161 private Rect mViewport = new Rect();
162
163 // Reordering
164 // We use the min scale to determine how much to expand the actually PagedView measured
165 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700166 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700167 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700168 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
169
Sunny Goyalcf25b522015-07-09 00:01:18 -0700170 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700171 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700172 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 // This variable's scope is only for the duration of startReordering() and endReordering()
174 private boolean mReorderingStarted = false;
175 // This variable's scope is for the duration of startReordering() and after the zoomIn()
176 // animation after endReordering()
177 private boolean mIsReordering;
178 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700179 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700180 private int mPostReorderingPreZoomInRemainingAnimationCount;
181 private Runnable mPostReorderingPreZoomInRunnable;
182
Adam Cohen7d30a372013-07-01 17:03:59 -0700183 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700184 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700185 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700186 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700187
John Spurlock77e1f472013-09-11 10:09:51 -0400188 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700189 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400190
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 public PagedView(Context context) {
192 this(context, null);
193 }
194
195 public PagedView(Context context, AttributeSet attrs) {
196 this(context, attrs, 0);
197 }
198
199 public PagedView(Context context, AttributeSet attrs, int defStyle) {
200 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700201
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 TypedArray a = context.obtainStyledAttributes(attrs,
203 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700204 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 a.recycle();
206
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700208 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700209 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 }
211
212 /**
213 * Initializes various states for this workspace.
214 */
Michael Jurka0142d492010-08-25 17:46:15 -0700215 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800216 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700217 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700218 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700219
220 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700221 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800223
Sunny Goyalcf25b522015-07-09 00:01:18 -0700224 float density = getResources().getDisplayMetrics().density;
225 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
226 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
227 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700228 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700229 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 }
231
Adam Cohenf9618852013-11-08 06:45:03 -0800232 protected void setDefaultInterpolator(Interpolator interpolator) {
233 mDefaultInterpolator = interpolator;
234 mScroller.setInterpolator(mDefaultInterpolator);
235 }
236
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700237 public void initParentViews(View parent) {
238 if (mPageIndicatorViewId > -1) {
239 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700240 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700241 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700242 }
243 }
244
Adam Cohen7d30a372013-07-01 17:03:59 -0700245 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700246 private float[] mapPointFromViewToParent(View v, float x, float y) {
247 sTmpPoint[0] = x;
248 sTmpPoint[1] = y;
249 v.getMatrix().mapPoints(sTmpPoint);
250 sTmpPoint[0] += v.getLeft();
251 sTmpPoint[1] += v.getTop();
252 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700253 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700254 private float[] mapPointFromParentToView(View v, float x, float y) {
255 sTmpPoint[0] = x - v.getLeft();
256 sTmpPoint[1] = y - v.getTop();
257 v.getMatrix().invert(sTmpInvMatrix);
258 sTmpInvMatrix.mapPoints(sTmpPoint);
259 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700260 }
261
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700262 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700263 if (mDragView != null) {
264 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
265 (mDragViewBaselineLeft - mDragView.getLeft());
266 float y = mLastMotionY - mDownMotionY;
267 mDragView.setTranslationX(x);
268 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700269
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
271 + x + ", " + y);
272 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700273 }
274
Adam Cohen7d30a372013-07-01 17:03:59 -0700275 @Override
276 public void setScaleX(float scaleX) {
277 super.setScaleX(scaleX);
278 if (isReordering(true)) {
279 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
280 mLastMotionX = p[0];
281 mLastMotionY = p[1];
282 updateDragViewTranslationDuringDrag();
283 }
284 }
285
286 // Convenience methods to get the actual width/height of the PagedView (since it is measured
287 // to be larger to account for the minimum possible scale)
288 int getViewportWidth() {
289 return mViewport.width();
290 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800291 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700292 return mViewport.height();
293 }
294
295 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
296 // PagedView both horizontally and vertically
297 int getViewportOffsetX() {
298 return (getMeasuredWidth() - getViewportWidth()) / 2;
299 }
300
301 int getViewportOffsetY() {
302 return (getMeasuredHeight() - getViewportHeight()) / 2;
303 }
304
Hyunyoung Song77441692016-06-27 22:00:48 -0700305 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700306 return mPageIndicator;
307 }
308
Adam Cohen674531f2013-12-13 15:07:14 -0800309 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700310 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
311 * that the user was on before entering free scroll mode (e.g. the home screen page they
312 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
313 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700315 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700316 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700318
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700319 /**
320 * Returns the index of page to be shown immediately afterwards.
321 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700322 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700323 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
324 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700325
Adam Cohenf9c184a2016-01-15 16:47:43 -0800326 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 return getChildCount();
328 }
329
Sunny Goyal83a8f042015-05-19 12:52:12 -0700330 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 return getChildAt(index);
332 }
333
Adam Cohenae4f1552011-10-20 00:15:42 -0700334 protected int indexToPage(int index) {
335 return index;
336 }
337
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800339 * Updates the scroll of the current page immediately to its final scroll position. We use this
340 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
341 * the previous tab page.
342 */
343 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700344 // If the current page is invalid, just reset the scroll position to zero
345 int newX = 0;
346 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700347 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700348 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800349 scrollTo(newX, 0);
350 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700351 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800352 }
353
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700354 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700355 mScroller.abortAnimation();
356 // We need to clean up the next page here to avoid computeScrollHelper from
357 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700358 if (resetNextPage) {
359 mNextPage = INVALID_PAGE;
360 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700361 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700362
Tony Wickham8f7ead32016-04-07 18:46:44 -0700363 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700364 mScroller.forceFinished(true);
365 // We need to clean up the next page here to avoid computeScrollHelper from
366 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700367 if (resetNextPage) {
368 mNextPage = INVALID_PAGE;
369 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700370 }
371
Adam Cohen6f127a62014-06-12 14:54:41 -0700372 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700373 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700374 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700375 }
376
Chet Haasebc2f0822012-10-26 17:59:53 -0700377 /**
Winson Chung86f77532010-08-24 11:08:22 -0700378 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700380 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800381 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700382 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800383 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800384 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
385 // the default
386 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800387 return;
388 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700389 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700390 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700391 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700392 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800393 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 }
395
Winson Chung8c87cd82013-07-23 16:20:10 -0700396 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800397 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
398 * has settled.
399 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700400 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800401 updatePageIndicator();
402 }
403
404 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700405 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700406 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700407 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700408 if (!isReordering(false)) {
409 mPageIndicator.setActiveMarker(getNextPage());
410 }
Winson Chungd2be3812013-07-16 11:11:32 -0700411 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700413 protected void pageBeginTransition() {
414 if (!mIsPageInTransition) {
415 mIsPageInTransition = true;
416 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700417 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700418 }
419
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700420 protected void pageEndTransition() {
421 if (mIsPageInTransition) {
422 mIsPageInTransition = false;
423 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700424 }
Michael Jurka0142d492010-08-25 17:46:15 -0700425 }
426
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700427 protected boolean isPageInTransition() {
428 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700429 }
430
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700431 /**
432 * Called when the page starts moving as part of the scroll. Subclasses can override this
433 * to provide custom behavior during animation.
434 */
435 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700436 }
437
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700438 /**
439 * Called when the page ends moving as part of the scroll. Subclasses can override this
440 * to provide custom behavior during animation.
441 */
442 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700443 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700444 }
445
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 /**
Winson Chung86f77532010-08-24 11:08:22 -0700447 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 *
449 * @param l The listener used to respond to long clicks.
450 */
451 @Override
452 public void setOnLongClickListener(OnLongClickListener l) {
453 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700454 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700456 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 }
Adam Cohen1697b792013-09-17 19:08:21 -0700458 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 }
460
Sunny Goyalc86df472016-02-25 09:19:38 -0800461 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700462 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800463 }
464
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800466 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800467 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800468 }
469
470 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700471 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700472 // In free scroll mode, we clamp the scrollX
473 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700474 // If the scroller is trying to move to a location beyond the maximum allowed
475 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700476 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700477 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700478 }
479
Sunny Goyal4d519f22017-10-24 10:32:40 -0700480 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700481 }
482
Adam Cohen8d769d62017-06-23 17:27:38 -0700483 mUnboundedScrollX = x;
484
Sunny Goyal70660032015-05-14 00:07:08 -0700485 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
486 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700487 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700488 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800489 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700490 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700491 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700492 overScroll(x - mMaxScrollX);
493 } else {
494 overScroll(x);
495 }
Adam Cohen68d73932010-11-15 10:50:58 -0800496 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700497 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700498 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800499 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700501 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700502 overScroll(x);
503 } else {
504 overScroll(x - mMaxScrollX);
505 }
Adam Cohen68d73932010-11-15 10:50:58 -0800506 }
507 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700508 if (mWasInOverscroll) {
509 overScroll(0);
510 mWasInOverscroll = false;
511 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700512 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800513 super.scrollTo(x, y);
514 }
515
Adam Cohen7d30a372013-07-01 17:03:59 -0700516 // Update the last motion events when scrolling
517 if (isReordering(true)) {
518 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
519 mLastMotionX = p[0];
520 mLastMotionY = p[1];
521 updateDragViewTranslationDuringDrag();
522 }
Michael Jurka0142d492010-08-25 17:46:15 -0700523 }
524
Adam Cohen53805212013-10-01 10:39:23 -0700525 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800526 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700527 if (mCurrentPage != getNextPage()) {
528 AccessibilityEvent ev =
529 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700530 ev.setScrollable(true);
531 ev.setScrollX(getScrollX());
532 ev.setScrollY(getScrollY());
533 ev.setMaxScrollX(mMaxScrollX);
534 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700535
Vadim Tryshevf4715972015-05-08 17:21:03 -0700536 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700537 }
Adam Cohen53805212013-10-01 10:39:23 -0700538 }
539 }
540
Michael Jurka0142d492010-08-25 17:46:15 -0700541 // we moved this functionality to a helper function so SmoothPagedView can reuse it
542 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800543 return computeScrollHelper(true);
544 }
545
546 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700548 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800549 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700550 || getScrollY() != mScroller.getCurrY()
551 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700552 float scaleX = mFreeScroll ? getScaleX() : 1f;
553 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
554 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700555 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800556 if (shouldInvalidate) {
557 invalidate();
558 }
Michael Jurka0142d492010-08-25 17:46:15 -0700559 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800560 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700561 sendScrollAccessibilityEvent();
562
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700563 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700564 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700565 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700566 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700567
Adam Cohen73aa9752010-11-24 16:26:58 -0800568 // We don't want to trigger a page end moving unless the page has settled
569 // and the user has stopped scrolling
570 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700571 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800572 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700573
Adam Cohen7d30a372013-07-01 17:03:59 -0700574 onPostReorderingAnimationCompleted();
Sunny Goyald0030b02017-12-08 15:07:24 -0800575 if (isAccessibilityEnabled(getContext())) {
Adam Cohen53805212013-10-01 10:39:23 -0700576 // Notify the user when the page changes
577 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700578 }
Michael Jurka0142d492010-08-25 17:46:15 -0700579 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 }
Michael Jurka0142d492010-08-25 17:46:15 -0700581 return false;
582 }
583
584 @Override
585 public void computeScroll() {
586 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700587 }
588
Adam Cohen410f3cd2013-09-22 12:09:32 -0700589 public int getNormalChildHeight() {
Sunny Goyalac00cba2017-11-13 15:58:01 -0800590 return getViewportHeight() - getPaddingTop() - getPaddingBottom()
591 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700592 }
593
Sunny Goyalbc683e92017-12-06 10:25:07 -0800594 public int getNormalChildWidth() {
595 return getViewportWidth() - getPaddingLeft() - getPaddingRight()
596 - mInsets.left - mInsets.right;
597 }
598
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 @Override
600 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 mViewport.set(0, 0, widthSize, heightSize);
614
615 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
616 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
617 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 }
619
Winson Chung8aad6102012-05-11 16:27:49 -0700620 // Return early if we aren't given a proper dimension
621 if (widthSize <= 0 || heightSize <= 0) {
622 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
623 return;
624 }
625
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700627 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700628 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700629
Sunny Goyalac00cba2017-11-13 15:58:01 -0800630 int myWidthSpec = MeasureSpec.makeMeasureSpec(
631 getViewportWidth() - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
632 int myHeightSpec = MeasureSpec.makeMeasureSpec(
633 getViewportHeight() - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700634
Sunny Goyalac00cba2017-11-13 15:58:01 -0800635 // measureChildren takes accounts for content padding, we only need to care about extra
636 // space due to insets.
637 measureChildren(myWidthSpec, myHeightSpec);
638 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800639 }
640
Sunny Goyalcf25b522015-07-09 00:01:18 -0700641 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700642 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700643 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700644 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700645 return;
646 }
647
Winson Chung785d2eb2011-04-14 16:08:02 -0700648 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700650
Adam Cohen7d30a372013-07-01 17:03:59 -0700651 int offsetX = getViewportOffsetX();
652 int offsetY = getViewportOffsetY();
653
654 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700655 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700656
Sunny Goyal70660032015-05-14 00:07:08 -0700657 final int startIndex = mIsRtl ? childCount - 1 : 0;
658 final int endIndex = mIsRtl ? -1 : childCount;
659 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700660
Adam Cohen7d30a372013-07-01 17:03:59 -0700661 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700662
Sunny Goyalac00cba2017-11-13 15:58:01 -0800663 int childLeft = offsetX + getPaddingLeft();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700664 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
665 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700666 }
667
Adam Cohen0ffac432013-07-10 11:19:26 -0700668 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700669 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 if (child.getVisibility() != View.GONE) {
Sunny Goyalac00cba2017-11-13 15:58:01 -0800671 int childTop = offsetY + getPaddingTop() + mInsets.top;
672 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding
673 - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100674
Adam Cohen96d30a12013-07-16 18:13:21 -0700675 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700676 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800677
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700679 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800680 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700681
Sunny Goyalac00cba2017-11-13 15:58:01 -0800682 int scrollOffsetLeft = getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700683 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
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 Goyal8e2133b2015-05-06 13:39:07 -0700718 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
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;
812
Adam Cohenf698c6e2013-07-17 18:44:25 -0700813 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700814
Adam Cohenf698c6e2013-07-17 18:44:25 -0700815 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700816 }
817
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 @Override
819 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700820 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700821 if (page != mCurrentPage || !mScroller.isFinished()) {
822 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 return true;
824 }
825 return false;
826 }
827
828 @Override
829 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700830 int focusablePage;
831 if (mNextPage != INVALID_PAGE) {
832 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700834 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
Winson Chung86f77532010-08-24 11:08:22 -0700836 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700838 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 }
840 return false;
841 }
842
843 @Override
844 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800845 if (super.dispatchUnhandledMove(focused, direction)) {
846 return true;
847 }
848
849 if (mIsRtl) {
850 if (direction == View.FOCUS_LEFT) {
851 direction = View.FOCUS_RIGHT;
852 } else if (direction == View.FOCUS_RIGHT) {
853 direction = View.FOCUS_LEFT;
854 }
855 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700857 if (getCurrentPage() > 0) {
858 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 return true;
860 }
861 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700862 if (getCurrentPage() < getPageCount() - 1) {
863 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 return true;
865 }
866 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800867 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 }
869
870 @Override
871 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700872 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
873 return;
874 }
875
Adam Cohen0ffac432013-07-10 11:19:26 -0700876 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700877 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700878 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 }
880 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700881 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700882 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 }
884 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700885 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700886 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 }
888 }
889 }
890
891 /**
892 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700893 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 *
Winson Chung86f77532010-08-24 11:08:22 -0700895 * This happens when live folders requery, and if they're off page, they
896 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 */
898 @Override
899 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700900 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 View v = focused;
902 while (true) {
903 if (v == current) {
904 super.focusableViewAvailable(focused);
905 return;
906 }
907 if (v == this) {
908 return;
909 }
910 ViewParent parent = v.getParent();
911 if (parent instanceof View) {
912 v = (View)v.getParent();
913 } else {
914 return;
915 }
916 }
917 }
918
919 /**
920 * {@inheritDoc}
921 */
922 @Override
923 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
924 if (disallowIntercept) {
925 // We need to make sure to cancel our long press if
926 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700927 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700928 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 }
930 super.requestDisallowInterceptTouchEvent(disallowIntercept);
931 }
932
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800933 /**
934 * Return true if a tap at (x, y) should trigger a flip to the previous page.
935 */
936 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -0700937 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700938 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -0700939 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -0700940 }
Adam Cohen3b185e22013-10-29 14:45:58 -0700941 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800942 }
943
944 /**
945 * Return true if a tap at (x, y) should trigger a flip to the next page.
946 */
947 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -0700948 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700949 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -0700950 }
951 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -0700952 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -0700953 }
Winson Chung52aee602013-01-30 12:01:02 -0800954
Adam Cohen7d30a372013-07-01 17:03:59 -0700955 /** Returns whether x and y originated within the buffered viewport */
956 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700957 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -0700958 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700959 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700960 }
961
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 @Override
963 public boolean onInterceptTouchEvent(MotionEvent ev) {
964 /*
965 * This method JUST determines whether we want to intercept the motion.
966 * If we return true, onTouchEvent will be called and we do the actual
967 * scrolling there.
968 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800969 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700970
Winson Chung45e1d6e2010-11-09 17:19:49 -0800971 // Skip touch handling if there are no pages to swipe
972 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
973
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 /*
975 * Shortcut the most recurring case: the user is in the dragging
976 * state and he is moving his finger. We want to intercept this
977 * motion.
978 */
979 final int action = ev.getAction();
980 if ((action == MotionEvent.ACTION_MOVE) &&
981 (mTouchState == TOUCH_STATE_SCROLLING)) {
982 return true;
983 }
984
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 switch (action & MotionEvent.ACTION_MASK) {
986 case MotionEvent.ACTION_MOVE: {
987 /*
988 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
989 * whether the user has moved far enough from his original down touch.
990 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700991 if (mActivePointerId != INVALID_POINTER) {
992 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700993 }
994 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
995 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
996 // i.e. fall through to the next case (don't break)
997 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
998 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700999 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 }
1001
1002 case MotionEvent.ACTION_DOWN: {
1003 final float x = ev.getX();
1004 final float y = ev.getY();
1005 // Remember location of down touch
1006 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001007 mDownMotionY = y;
1008 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 mLastMotionX = x;
1010 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001011 float[] p = mapPointFromViewToParent(this, x, y);
1012 mParentDownMotionX = p[0];
1013 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001014 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001015 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001017
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 /*
1019 * If being flinged and user touches the screen, initiate drag;
1020 * otherwise don't. mScroller.isFinished should be false when
1021 * being flinged.
1022 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001023 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001024 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001025
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001026 if (finishedScrolling) {
1027 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001028 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001029 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001030 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001031 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001032 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001033 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1034 mTouchState = TOUCH_STATE_SCROLLING;
1035 } else {
1036 mTouchState = TOUCH_STATE_REST;
1037 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001038 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001039
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 break;
1041 }
1042
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001044 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001045 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 break;
1047
1048 case MotionEvent.ACTION_POINTER_UP:
1049 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001050 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 break;
1052 }
1053
1054 /*
1055 * The only time we want to intercept motion events is if we are in the
1056 * drag mode.
1057 */
1058 return mTouchState != TOUCH_STATE_REST;
1059 }
1060
Adam Cohenf8d28232011-02-01 21:47:00 -08001061 protected void determineScrollingStart(MotionEvent ev) {
1062 determineScrollingStart(ev, 1.0f);
1063 }
1064
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 /*
1066 * Determines if we should change the touch state to start scrolling after the
1067 * user moves their touch point too far.
1068 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001069 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001070 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001072 if (pointerIndex == -1) return;
1073
1074 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 final float x = ev.getX(pointerIndex);
1076 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001077 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1078
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001080
Adam Cohenf8d28232011-02-01 21:47:00 -08001081 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001083
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001084 if (xMoved) {
1085 // Scroll if the user moved far enough along the X axis
1086 mTouchState = TOUCH_STATE_SCROLLING;
1087 mTotalMotionX += Math.abs(mLastMotionX - x);
1088 mLastMotionX = x;
1089 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001090 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001091 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001092 // Stop listening for things like pinches.
1093 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001094 }
1095 }
1096
1097 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001098 // Try canceling the long press. It could also have been scheduled
1099 // by a distant descendant, so use the mAllowLongPress flag to block
1100 // everything
1101 final View currentPage = getPageAt(mCurrentPage);
1102 if (currentPage != null) {
1103 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001104 }
1105 }
1106
Adam Cohenb5ba0972011-09-07 18:02:31 -07001107 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001108 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001109
Adam Cohenedb40762013-07-18 16:45:45 -07001110 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001111 int count = getChildCount();
1112
1113 final int totalDistance;
1114
1115 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001116 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001117 adjacentPage = page - 1;
1118 }
1119
1120 if (adjacentPage < 0 || adjacentPage > count - 1) {
1121 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1122 } else {
1123 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1124 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001125
1126 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001127 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1128 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001129 return scrollProgress;
1130 }
1131
Adam Cohenedb40762013-07-18 16:45:45 -07001132 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001133 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001134 return 0;
1135 } else {
1136 return mPageScrolls[index];
1137 }
1138 }
1139
Adam Cohen564a2e72013-10-09 14:47:32 -07001140 // While layout transitions are occurring, a child's position may stray from its baseline
1141 // position. This method returns the magnitude of this stray at any given time.
1142 public int getLayoutTransitionOffsetForPage(int index) {
1143 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1144 return 0;
1145 } else {
1146 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001147
Sunny Goyalac00cba2017-11-13 15:58:01 -08001148 int scrollOffset = scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen564a2e72013-10-09 14:47:32 -07001149 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1150 return (int) (child.getX() - baselineX);
1151 }
1152 }
1153
Adam Cohenb5ba0972011-09-07 18:02:31 -07001154 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001155 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001156
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001157 int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001158 if (amount < 0) {
1159 mOverScrollX = overScrollAmount;
1160 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001161 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001162 mOverScrollX = mMaxScrollX + overScrollAmount;
1163 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001164 }
1165 invalidate();
1166 }
1167
Adam Cohenb5ba0972011-09-07 18:02:31 -07001168 protected void overScroll(float amount) {
1169 dampedOverScroll(amount);
1170 }
1171
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001172 /**
1173 * return true if freescroll has been enabled, false otherwise
1174 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001175 protected void enableFreeScroll() {
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001176 enableFreeScroll(false);
1177 }
1178
1179 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001180 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001181 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001182 }
1183
Sunny Goyalc99cb172017-10-19 16:15:09 -07001184 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001185 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001186 }
1187
Adam Cohenf9618852013-11-08 06:45:03 -08001188 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001189 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001190 mFreeScroll = freeScroll;
1191
Adam Cohenf9618852013-11-08 06:45:03 -08001192 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001193 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001194 } else if (wasFreeScroll) {
1195 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001196 }
1197
1198 setEnableOverscroll(!freeScroll);
1199 }
1200
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001201 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001202 mAllowOverScroll = enable;
1203 }
1204
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001205 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001206 if (mDragView != null) {
1207 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1208 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001209 int minDistance = Integer.MAX_VALUE;
1210 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001211 int maxPageNo = getChildCount() - 1;
1212 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001213 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001214 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001215 int d = Math.abs(dragX - pageX);
1216 if (d < minDistance) {
1217 minIndex = i;
1218 minDistance = d;
1219 }
1220 }
1221 return minIndex;
1222 }
1223 return -1;
1224 }
1225
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 @Override
1227 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001228 super.onTouchEvent(ev);
1229
Winson Chung45e1d6e2010-11-09 17:19:49 -08001230 // Skip touch handling if there are no pages to swipe
1231 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1232
Michael Jurkab8f06722010-10-10 15:58:46 -07001233 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234
1235 final int action = ev.getAction();
1236
1237 switch (action & MotionEvent.ACTION_MASK) {
1238 case MotionEvent.ACTION_DOWN:
1239 /*
1240 * If being flinged and user touches, stop the fling. isFinished
1241 * will be false if being flinged.
1242 */
1243 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001244 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 }
1246
1247 // Remember where the motion event started
1248 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001249 mDownMotionY = mLastMotionY = ev.getY();
1250 mDownScrollX = getScrollX();
1251 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1252 mParentDownMotionX = p[0];
1253 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001254 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001255 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001257
Michael Jurka0142d492010-08-25 17:46:15 -07001258 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001259 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001260 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001261 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 break;
1263
1264 case MotionEvent.ACTION_MOVE:
1265 if (mTouchState == TOUCH_STATE_SCROLLING) {
1266 // Scroll to follow the motion event
1267 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001268
1269 if (pointerIndex == -1) return true;
1270
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001272 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001273
Adam Cohenaefd4e12011-02-14 16:39:38 -08001274 mTotalMotionX += Math.abs(deltaX);
1275
Winson Chungc0844aa2011-02-02 15:25:58 -08001276 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1277 // keep the remainder because we are actually testing if we've moved from the last
1278 // scrolled position (which is discrete).
1279 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001280 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001281 mLastMotionX = x;
1282 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 } else {
1284 awakenScrollBars();
1285 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001286 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1287 // Update the last motion position
1288 mLastMotionX = ev.getX();
1289 mLastMotionY = ev.getY();
1290
1291 // Update the parent down so that our zoom animations take this new movement into
1292 // account
1293 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1294 mParentDownMotionX = pt[0];
1295 mParentDownMotionY = pt[1];
1296 updateDragViewTranslationDuringDrag();
1297
1298 // Find the closest page to the touch point
1299 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001300
Adam Cohen7d30a372013-07-01 17:03:59 -07001301 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1302 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1303 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1304 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1305
Adam Cohenf358a4b2013-07-23 16:47:31 -07001306 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001307 // Do not allow any page to be moved to 0th position.
1308 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001309 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001310 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1311 mSidePageHoverIndex = pageUnderPointIndex;
1312 mSidePageHoverRunnable = new Runnable() {
1313 @Override
1314 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001315 // Setup the scroll to the correct page before we swap the views
1316 snapToPage(pageUnderPointIndex);
1317
1318 // For each of the pages between the paged view and the drag view,
1319 // animate them from the previous position to the new position in
1320 // the layout (as a result of the drag view moving in the layout)
1321 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1322 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1323 dragViewIndex + 1 : pageUnderPointIndex;
1324 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1325 dragViewIndex - 1 : pageUnderPointIndex;
1326 for (int i = lowerIndex; i <= upperIndex; ++i) {
1327 View v = getChildAt(i);
1328 // dragViewIndex < pageUnderPointIndex, so after we remove the
1329 // drag view all subsequent views to pageUnderPointIndex will
1330 // shift down.
1331 int oldX = getViewportOffsetX() + getChildOffset(i);
1332 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1333
1334 // Animate the view translation from its old position to its new
1335 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001336 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001337 if (anim != null) {
1338 anim.cancel();
1339 }
1340
1341 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001342 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 anim.start();
1345 v.setTag(anim);
1346 }
1347
1348 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001349 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001350 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001351 if (mPageIndicator != null) {
1352 mPageIndicator.setActiveMarker(getNextPage());
1353 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001354 }
1355 };
1356 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1357 }
1358 } else {
1359 removeCallbacks(mSidePageHoverRunnable);
1360 mSidePageHoverIndex = -1;
1361 }
Adam Cohen564976a2010-10-13 18:52:07 -07001362 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 determineScrollingStart(ev);
1364 }
1365 break;
1366
1367 case MotionEvent.ACTION_UP:
1368 if (mTouchState == TOUCH_STATE_SCROLLING) {
1369 final int activePointerId = mActivePointerId;
1370 final int pointerIndex = ev.findPointerIndex(activePointerId);
1371 final float x = ev.getX(pointerIndex);
1372 final VelocityTracker velocityTracker = mVelocityTracker;
1373 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1374 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001375 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001376 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001377 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1378 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001379
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001380 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1381
Adam Cohen00481b32011-11-18 12:03:48 -08001382 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001383 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001384
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 if (!mFreeScroll) {
1386 // In the case that the page is moved far to one direction and then is flung
1387 // in the opposite direction, we use a threshold to determine whether we should
1388 // just return to the starting page, or if we should skip one further.
1389 boolean returnToOriginalPage = false;
1390 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1391 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1392 returnToOriginalPage = true;
1393 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001394
Adam Cohenf358a4b2013-07-23 16:47:31 -07001395 int finalPage;
1396 // We give flings precedence over large moves, which is why we short-circuit our
1397 // test for a large move if a fling has been registered. That is, a large
1398 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001399 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1400 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1402 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1403 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1404 snapToPageWithVelocity(finalPage, velocityX);
1405 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1406 (isFling && isVelocityXLeft)) &&
1407 mCurrentPage < getChildCount() - 1) {
1408 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1409 snapToPageWithVelocity(finalPage, velocityX);
1410 } else {
1411 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001415 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416 }
1417
1418 float scaleX = getScaleX();
1419 int vX = (int) (-velocityX * scaleX);
1420 int initialScrollX = (int) (getScrollX() * scaleX);
1421
Adam Cohenf9618852013-11-08 06:45:03 -08001422 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001423 mScroller.fling(initialScrollX,
1424 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001425 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1426 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1427 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1428 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1429 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1430 && unscaledScrollX < lastPageScroll) {
1431 // Make sure we land directly on a page. If flinging past one of the ends,
1432 // don't change the velocity as it will get stopped at the end anyway.
1433 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1434 // Ensure the scroll/snap doesn't happen too fast;
1435 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1436 - mScroller.getDuration();
1437 if (extraScrollDuration > 0) {
1438 mScroller.extendDuration(extraScrollDuration);
1439 }
1440 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001441 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001443 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001444 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1445 // at this point we have not moved beyond the touch slop
1446 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1447 // we can just page
1448 int nextPage = Math.max(0, mCurrentPage - 1);
1449 if (nextPage != mCurrentPage) {
1450 snapToPage(nextPage);
1451 } else {
1452 snapToDestination();
1453 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001454 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001455 // at this point we have not moved beyond the touch slop
1456 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1457 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001458 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1459 if (nextPage != mCurrentPage) {
1460 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 } else {
1462 snapToDestination();
1463 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001464 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1465 // Update the last motion position
1466 mLastMotionX = ev.getX();
1467 mLastMotionY = ev.getY();
1468
1469 // Update the parent down so that our zoom animations take this new movement into
1470 // account
1471 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1472 mParentDownMotionX = pt[0];
1473 mParentDownMotionY = pt[1];
1474 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001475 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001476 if (!mCancelTap) {
1477 onUnhandledTap(ev);
1478 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001479 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001480
1481 // Remove the callback to wait for the side page hover timeout
1482 removeCallbacks(mSidePageHoverRunnable);
1483 // End any intermediate reordering states
1484 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 break;
1486
1487 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001488 if (mTouchState == TOUCH_STATE_SCROLLING) {
1489 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001490 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001491 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001492 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 break;
1494
1495 case MotionEvent.ACTION_POINTER_UP:
1496 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001497 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 break;
1499 }
1500
1501 return true;
1502 }
1503
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001504 protected boolean shouldFlingForVelocity(int velocityX) {
1505 return Math.abs(velocityX) > mFlingThresholdVelocity;
1506 }
1507
Adam Cohen7d30a372013-07-01 17:03:59 -07001508 private void resetTouchState() {
1509 releaseVelocityTracker();
1510 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001511 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001512 mTouchState = TOUCH_STATE_REST;
1513 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 }
1515
Adam Cohenc2d6e892014-10-16 09:49:24 -07001516 /**
1517 * Triggered by scrolling via touch
1518 */
1519 protected void onScrollInteractionBegin() {
1520 }
1521
1522 protected void onScrollInteractionEnd() {
1523 }
1524
Adam Cohen1697b792013-09-17 19:08:21 -07001525 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001526 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001527 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001528
Winson Chung185d7162011-02-28 13:47:29 -08001529 @Override
1530 public boolean onGenericMotionEvent(MotionEvent event) {
1531 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1532 switch (event.getAction()) {
1533 case MotionEvent.ACTION_SCROLL: {
1534 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1535 final float vscroll;
1536 final float hscroll;
1537 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1538 vscroll = 0;
1539 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1540 } else {
1541 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1542 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1543 }
1544 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001545 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001546 : (hscroll > 0 || vscroll > 0);
1547 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001548 scrollRight();
1549 } else {
1550 scrollLeft();
1551 }
1552 return true;
1553 }
1554 }
1555 }
1556 }
1557 return super.onGenericMotionEvent(event);
1558 }
1559
Michael Jurkab8f06722010-10-10 15:58:46 -07001560 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1561 if (mVelocityTracker == null) {
1562 mVelocityTracker = VelocityTracker.obtain();
1563 }
1564 mVelocityTracker.addMovement(ev);
1565 }
1566
1567 private void releaseVelocityTracker() {
1568 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001569 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001570 mVelocityTracker.recycle();
1571 mVelocityTracker = null;
1572 }
1573 }
1574
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 private void onSecondaryPointerUp(MotionEvent ev) {
1576 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1577 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1578 final int pointerId = ev.getPointerId(pointerIndex);
1579 if (pointerId == mActivePointerId) {
1580 // This was our active pointer going up. Choose a new
1581 // active pointer and adjust accordingly.
1582 // TODO: Make this decision more intelligent.
1583 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1584 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1585 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001586 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001587 mActivePointerId = ev.getPointerId(newPointerIndex);
1588 if (mVelocityTracker != null) {
1589 mVelocityTracker.clear();
1590 }
1591 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001592 }
1593
Winson Chung321e9ee2010-08-09 13:37:56 -07001594 @Override
1595 public void requestChildFocus(View child, View focused) {
1596 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001597 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001598 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001599 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001600 }
1601 }
1602
Adam Cohend19d3ca2010-09-15 14:43:42 -07001603 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001604 return getPageNearestToCenterOfScreen(getScrollX());
1605 }
1606
1607 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1608 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001609 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001610 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001611 final int childCount = getChildCount();
1612 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001613 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001614 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001615 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001616 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1618 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1619 minDistanceFromScreenCenter = distanceFromScreenCenter;
1620 minDistanceFromScreenCenterIndex = i;
1621 }
1622 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001623 return minDistanceFromScreenCenterIndex;
1624 }
1625
1626 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001627 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1628 }
1629
1630 protected boolean isInOverScroll() {
1631 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1632 }
1633
1634 protected int getPageSnapDuration() {
1635 if (isInOverScroll()) {
1636 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1637 }
1638 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001639 }
1640
Adam Cohene0f66b52010-11-23 15:06:07 -08001641 // We want the duration of the page snap animation to be influenced by the distance that
1642 // the screen has to travel, however, we don't want this duration to be effected in a
1643 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1644 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001645 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001646 f -= 0.5f; // center the values about 0.
1647 f *= 0.3f * Math.PI / 2.0f;
1648 return (float) Math.sin(f);
1649 }
1650
Michael Jurka0142d492010-08-25 17:46:15 -07001651 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001652 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001653 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001654
Adam Cohenedb40762013-07-18 16:45:45 -07001655 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001656 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001657 int duration = 0;
1658
Sunny Goyal4d113a52015-05-27 10:05:28 -07001659 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001660 // If the velocity is low enough, then treat this more as an automatic page advance
1661 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001662 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001663 return;
1664 }
1665
1666 // Here we compute a "distance" that will be used in the computation of the overall
1667 // snap duration. This is a function of the actual distance that needs to be traveled;
1668 // we keep this value close to half screen size in order to reduce the variance in snap
1669 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001670 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001671 float distance = halfScreenSize + halfScreenSize *
1672 distanceInfluenceForSnapDuration(distanceRatio);
1673
1674 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001675 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001676
1677 // we want the page's snap velocity to approximately match the velocity at which the
1678 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001679 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1680 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001681
1682 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001683 }
1684
Sunny Goyal1740d902015-05-27 11:14:01 -07001685 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001686 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001687 }
1688
Adam Cohenf9c184a2016-01-15 16:47:43 -08001689 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001690 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001691 }
1692
Tony Wickham07b1d672018-01-04 17:16:05 -08001693 public void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001694 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001695 }
1696
Adam Cohenf9618852013-11-08 06:45:03 -08001697 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1698 snapToPage(whichPage, duration, false, interpolator);
1699 }
1700
1701 protected void snapToPage(int whichPage, int duration, boolean immediate,
1702 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001703 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001704
Adam Cohenedb40762013-07-18 16:45:45 -07001705 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001706 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001707 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001708 }
1709
1710 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001711 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001712 }
Michael Jurka0142d492010-08-25 17:46:15 -07001713
Adam Cohenf9618852013-11-08 06:45:03 -08001714 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1715 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001716 whichPage = validateNewPage(whichPage);
1717
Adam Cohen7d30a372013-07-01 17:03:59 -07001718 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001719
Winson Chung321e9ee2010-08-09 13:37:56 -07001720 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001721 if (immediate) {
1722 duration = 0;
1723 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001724 duration = Math.abs(delta);
1725 }
1726
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001727 if (duration != 0) {
1728 pageBeginTransition();
1729 }
1730
Adam Cohenf358a4b2013-07-23 16:47:31 -07001731 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001732 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001733 }
Adam Cohenf9618852013-11-08 06:45:03 -08001734
1735 if (interpolator != null) {
1736 mScroller.setInterpolator(interpolator);
1737 } else {
1738 mScroller.setInterpolator(mDefaultInterpolator);
1739 }
1740
Sunny Goyalc86df472016-02-25 09:19:38 -08001741 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001742
Adam Cohen674531f2013-12-13 15:07:14 -08001743 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001744
1745 // Trigger a compute() to finish switching pages if necessary
1746 if (immediate) {
1747 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001748 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001749 }
1750
Winson Chung321e9ee2010-08-09 13:37:56 -07001751 invalidate();
1752 }
1753
Winson Chung321e9ee2010-08-09 13:37:56 -07001754 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001755 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001756 }
1757
1758 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001759 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001760 }
1761
Adam Cohendbdff6b2013-09-18 19:09:15 -07001762 @Override
1763 public boolean performLongClick() {
1764 mCancelTap = true;
1765 return super.performLongClick();
1766 }
1767
Winson Chung321e9ee2010-08-09 13:37:56 -07001768 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001769 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001770
1771 SavedState(Parcelable superState) {
1772 super(superState);
1773 }
1774
Adam Cohen091440a2015-03-18 14:16:05 -07001775 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001776 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001777 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001778 }
1779
1780 @Override
1781 public void writeToParcel(Parcel out, int flags) {
1782 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001783 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001784 }
1785
1786 public static final Parcelable.Creator<SavedState> CREATOR =
1787 new Parcelable.Creator<SavedState>() {
1788 public SavedState createFromParcel(Parcel in) {
1789 return new SavedState(in);
1790 }
1791
1792 public SavedState[] newArray(int size) {
1793 return new SavedState[size];
1794 }
1795 };
1796 }
1797
Adam Cohen7d30a372013-07-01 17:03:59 -07001798 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001799 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001800 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001801 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1802 new PropertyListBuilder()
1803 .scale(1)
1804 .translationX(0)
1805 .translationY(0)
1806 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001807 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001808 anim.addListener(new AnimatorListenerAdapter() {
1809 @Override
1810 public void onAnimationEnd(Animator animation) {
1811 onPostReorderingAnimationCompleted();
1812 }
1813 });
1814 anim.start();
1815 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001816 }
1817
Sunny Goyal1d08f702015-05-04 15:50:25 -07001818 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001819 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1820 mTouchState = TOUCH_STATE_REORDERING;
1821 mIsReordering = true;
1822
Adam Cohen7d30a372013-07-01 17:03:59 -07001823 // We must invalidate to trigger a redraw to update the layers such that the drag view
1824 // is always drawn on top
1825 invalidate();
1826 }
1827
Adam Cohen091440a2015-03-18 14:16:05 -07001828 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 // Trigger the callback when reordering has settled
1830 --mPostReorderingPreZoomInRemainingAnimationCount;
1831 if (mPostReorderingPreZoomInRunnable != null &&
1832 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1833 mPostReorderingPreZoomInRunnable.run();
1834 mPostReorderingPreZoomInRunnable = null;
1835 }
1836 }
1837
Sunny Goyal1d08f702015-05-04 15:50:25 -07001838 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001839 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001840 }
1841
Adam Cohenf358a4b2013-07-23 16:47:31 -07001842 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001843 int dragViewIndex = indexOfChild(v);
1844
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001845 // Do not allow the first page to be moved around
1846 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001847
Adam Cohen7d30a372013-07-01 17:03:59 -07001848 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001849 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001850 // Find the drag view under the pointer
1851 mDragView = getChildAt(dragViewIndex);
1852 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1853 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001854 mReorderingStarted = true;
1855
Adam Cohenf9618852013-11-08 06:45:03 -08001856 snapToPage(getPageNearestToCenterOfScreen());
1857 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001858 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001859 return true;
1860 }
1861 return false;
1862 }
1863
1864 boolean isReordering(boolean testTouchState) {
1865 boolean state = mIsReordering;
1866 if (testTouchState) {
1867 state &= (mTouchState == TOUCH_STATE_REORDERING);
1868 }
1869 return state;
1870 }
1871 void endReordering() {
1872 // For simplicity, we call endReordering sometimes even if reordering was never started.
1873 // In that case, we don't want to do anything.
1874 if (!mReorderingStarted) return;
1875 mReorderingStarted = false;
1876
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001877 mPostReorderingPreZoomInRunnable = new Runnable() {
1878 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001879 // If we haven't flung-to-delete the current child,
1880 // then we just animate the drag view back into position
1881 onEndReordering();
1882
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001883 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001884 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001885 };
Adam Cohen7d30a372013-07-01 17:03:59 -07001886
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001887 mPostReorderingPreZoomInRemainingAnimationCount =
1888 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
1889 // Snap to the current page
1890 snapToPage(indexOfChild(mDragView), 0);
1891 // Animate the drag view back to the front position
1892 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001893 }
1894
Winson Chung6a0f57d2011-06-29 20:10:49 -07001895 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001896 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001897 @Override
1898 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1899 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001900 info.setScrollable(getPageCount() > 1);
1901 if (getCurrentPage() < getPageCount() - 1) {
1902 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1903 }
1904 if (getCurrentPage() > 0) {
1905 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1906 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001907 info.setClassName(getClass().getName());
1908
1909 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1910 // Besides disabling the accessibility long-click, this also prevents this view from getting
1911 // accessibility focus.
1912 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001913 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001914 }
1915
1916 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001917 public void sendAccessibilityEvent(int eventType) {
1918 // Don't let the view send real scroll events.
1919 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1920 super.sendAccessibilityEvent(eventType);
1921 }
1922 }
1923
1924 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001925 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1926 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001927 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001928 }
1929
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001930 @Override
1931 public boolean performAccessibilityAction(int action, Bundle arguments) {
1932 if (super.performAccessibilityAction(action, arguments)) {
1933 return true;
1934 }
1935 switch (action) {
1936 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1937 if (getCurrentPage() < getPageCount() - 1) {
1938 scrollRight();
1939 return true;
1940 }
1941 } break;
1942 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1943 if (getCurrentPage() > 0) {
1944 scrollLeft();
1945 return true;
1946 }
1947 } break;
1948 }
1949 return false;
1950 }
1951
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001952 protected String getPageIndicatorDescription() {
1953 return getCurrentPageDescription();
1954 }
1955
Adam Cohen0ffac432013-07-10 11:19:26 -07001956 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001957 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001958 getNextPage() + 1, getChildCount());
1959 }
1960
Winson Chungd11265e2011-08-30 13:37:23 -07001961 @Override
1962 public boolean onHoverEvent(android.view.MotionEvent event) {
1963 return true;
1964 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001965}