blob: b23568e9e943062305113fac520ca00c5a40b9c1 [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
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070027import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070029import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.os.Parcel;
31import android.os.Parcelable;
32import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080034import android.view.InputDevice;
35import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.view.MotionEvent;
37import android.view.VelocityTracker;
38import android.view.View;
39import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080040import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.ViewGroup;
42import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070043import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070044import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070045import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080046import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070047
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070048import com.android.launcher3.anim.Interpolators;
Sunny Goyal9e76f682017-02-13 12:13:43 -080049import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070050import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070051import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Vadim Tryshevfedca432015-08-19 17:55:02 -070068 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Sunny Goyalb72d8b22017-07-14 00:02:27 -070071 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070072 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070073
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080075 // The page is moved more than halfway, automatically move to the next page on touch up.
76 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080077
Sunny Goyal8e2133b2015-05-06 13:39:07 -070078 private static final float MAX_SCROLL_PROGRESS = 1.0f;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 // The following constants need to be scaled based on density. The scaled versions will be
81 // assigned to the corresponding member variables below.
82 private static final int FLING_THRESHOLD_VELOCITY = 500;
83 private static final int MIN_SNAP_VELOCITY = 1500;
84 private static final int MIN_FLING_VELOCITY = 250;
85
Adam Cohen21cd0022013-10-09 18:57:02 -070086 public static final int INVALID_RESTORE_PAGE = -1001;
87
Adam Cohenf358a4b2013-07-23 16:47:31 -070088 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080089 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070090
Adam Cohen265b9a62011-12-07 14:37:18 -080091 protected int mFlingThresholdVelocity;
92 protected int mMinFlingVelocity;
93 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected boolean mFirstLayout = true;
96
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070099 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800103 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800104 protected LauncherScroller mScroller;
105 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mParentDownMotionX;
110 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mDownMotionY;
113 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700114 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700115 private float mLastMotionX;
116 private float mLastMotionXRemainder;
117 private float mLastMotionY;
118 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700119
Adam Cohendbdff6b2013-09-18 19:09:15 -0700120 private boolean mCancelTap;
121
Adam Cohenedb40762013-07-18 16:45:45 -0700122 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Michael Jurka0142d492010-08-25 17:46:15 -0700124 protected final static int TOUCH_STATE_REST = 0;
125 protected final static int TOUCH_STATE_SCROLLING = 1;
126 protected final static int TOUCH_STATE_PREV_PAGE = 2;
127 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700128 protected final static int TOUCH_STATE_REORDERING = 4;
129
Michael Jurka0142d492010-08-25 17:46:15 -0700130 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800131
Michael Jurka0142d492010-08-25 17:46:15 -0700132 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800136 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka5f1c5092010-09-03 14:15:02 -0700138 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700142 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700143
Sunny Goyal061380a2016-02-29 15:15:03 -0800144 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700145
Adam Cohen8d769d62017-06-23 17:27:38 -0700146 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
147 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
148 // the screens from continuing to translate beyond the normal bounds.
149 protected int mOverScrollX;
150
151 protected int mUnboundedScrollX;
152
Winson Chungd2be3812013-07-16 11:11:32 -0700153 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700155 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700156 // The viewport whether the pages are to be contained (the actual view may be larger than the
157 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800158 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700159 private Rect mViewport = new Rect();
160
161 // Reordering
162 // We use the min scale to determine how much to expand the actually PagedView measured
163 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700164 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700165 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700166 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
167
Sunny Goyalcf25b522015-07-09 00:01:18 -0700168 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700171 // This variable's scope is only for the duration of startReordering() and endReordering()
172 private boolean mReorderingStarted = false;
173 // This variable's scope is for the duration of startReordering() and after the zoomIn()
174 // animation after endReordering()
175 private boolean mIsReordering;
176 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700177 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 private int mPostReorderingPreZoomInRemainingAnimationCount;
179 private Runnable mPostReorderingPreZoomInRunnable;
180
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700182 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700183 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700184 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700185
John Spurlock77e1f472013-09-11 10:09:51 -0400186 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700187 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 public PagedView(Context context) {
190 this(context, null);
191 }
192
193 public PagedView(Context context, AttributeSet attrs) {
194 this(context, attrs, 0);
195 }
196
197 public PagedView(Context context, AttributeSet attrs, int defStyle) {
198 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700199
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700202 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 a.recycle();
204
Winson Chung321e9ee2010-08-09 13:37:56 -0700205 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700206 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700207 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 }
209
210 /**
211 * Initializes various states for this workspace.
212 */
Michael Jurka0142d492010-08-25 17:46:15 -0700213 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800214 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700215 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700216 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700217
218 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700219 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800221
Sunny Goyalcf25b522015-07-09 00:01:18 -0700222 float density = getResources().getDisplayMetrics().density;
223 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
224 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
225 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700226 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700227 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
Adam Cohenf9618852013-11-08 06:45:03 -0800230 protected void setDefaultInterpolator(Interpolator interpolator) {
231 mDefaultInterpolator = interpolator;
232 mScroller.setInterpolator(mDefaultInterpolator);
233 }
234
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700235 public void initParentViews(View parent) {
236 if (mPageIndicatorViewId > -1) {
237 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700238 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700239 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700240 }
241 }
242
Adam Cohen7d30a372013-07-01 17:03:59 -0700243 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700244 private float[] mapPointFromViewToParent(View v, float x, float y) {
245 sTmpPoint[0] = x;
246 sTmpPoint[1] = y;
247 v.getMatrix().mapPoints(sTmpPoint);
248 sTmpPoint[0] += v.getLeft();
249 sTmpPoint[1] += v.getTop();
250 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700251 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700252 private float[] mapPointFromParentToView(View v, float x, float y) {
253 sTmpPoint[0] = x - v.getLeft();
254 sTmpPoint[1] = y - v.getTop();
255 v.getMatrix().invert(sTmpInvMatrix);
256 sTmpInvMatrix.mapPoints(sTmpPoint);
257 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700258 }
259
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700260 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700261 if (mDragView != null) {
262 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
263 (mDragViewBaselineLeft - mDragView.getLeft());
264 float y = mLastMotionY - mDownMotionY;
265 mDragView.setTranslationX(x);
266 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700267
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
269 + x + ", " + y);
270 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700271 }
272
Adam Cohen7d30a372013-07-01 17:03:59 -0700273 @Override
274 public void setScaleX(float scaleX) {
275 super.setScaleX(scaleX);
276 if (isReordering(true)) {
277 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
278 mLastMotionX = p[0];
279 mLastMotionY = p[1];
280 updateDragViewTranslationDuringDrag();
281 }
282 }
283
284 // Convenience methods to get the actual width/height of the PagedView (since it is measured
285 // to be larger to account for the minimum possible scale)
286 int getViewportWidth() {
287 return mViewport.width();
288 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800289 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700290 return mViewport.height();
291 }
292
293 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
294 // PagedView both horizontally and vertically
295 int getViewportOffsetX() {
296 return (getMeasuredWidth() - getViewportWidth()) / 2;
297 }
298
299 int getViewportOffsetY() {
300 return (getMeasuredHeight() - getViewportHeight()) / 2;
301 }
302
Hyunyoung Song77441692016-06-27 22:00:48 -0700303 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700304 return mPageIndicator;
305 }
306
Adam Cohen674531f2013-12-13 15:07:14 -0800307 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700308 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
309 * that the user was on before entering free scroll mode (e.g. the home screen page they
310 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
311 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700313 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700314 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700316
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700317 /**
318 * Returns the index of page to be shown immediately afterwards.
319 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700320 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700321 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
322 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700323
Adam Cohenf9c184a2016-01-15 16:47:43 -0800324 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 return getChildCount();
326 }
327
Sunny Goyal83a8f042015-05-19 12:52:12 -0700328 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 return getChildAt(index);
330 }
331
Adam Cohenae4f1552011-10-20 00:15:42 -0700332 protected int indexToPage(int index) {
333 return index;
334 }
335
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800337 * Updates the scroll of the current page immediately to its final scroll position. We use this
338 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
339 * the previous tab page.
340 */
341 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700342 // If the current page is invalid, just reset the scroll position to zero
343 int newX = 0;
344 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700345 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700346 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800347 scrollTo(newX, 0);
348 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700349 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800350 }
351
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700352 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700353 mScroller.abortAnimation();
354 // We need to clean up the next page here to avoid computeScrollHelper from
355 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700356 if (resetNextPage) {
357 mNextPage = INVALID_PAGE;
358 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700359 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700360
Tony Wickham8f7ead32016-04-07 18:46:44 -0700361 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700362 mScroller.forceFinished(true);
363 // We need to clean up the next page here to avoid computeScrollHelper from
364 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700365 if (resetNextPage) {
366 mNextPage = INVALID_PAGE;
367 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700368 }
369
Adam Cohen6f127a62014-06-12 14:54:41 -0700370 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700371 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700372 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700373 }
374
Chet Haasebc2f0822012-10-26 17:59:53 -0700375 /**
Winson Chung86f77532010-08-24 11:08:22 -0700376 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700378 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800379 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700380 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800381 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800382 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
383 // the default
384 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800385 return;
386 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700387 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700388 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700389 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700390 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800391 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 }
393
Winson Chung8c87cd82013-07-23 16:20:10 -0700394 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800395 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
396 * has settled.
397 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700398 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800399 updatePageIndicator();
400 }
401
402 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700403 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700404 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700405 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700406 if (!isReordering(false)) {
407 mPageIndicator.setActiveMarker(getNextPage());
408 }
Winson Chungd2be3812013-07-16 11:11:32 -0700409 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700411 protected void pageBeginTransition() {
412 if (!mIsPageInTransition) {
413 mIsPageInTransition = true;
414 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700415 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700416 }
417
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700418 protected void pageEndTransition() {
419 if (mIsPageInTransition) {
420 mIsPageInTransition = false;
421 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700422 }
Michael Jurka0142d492010-08-25 17:46:15 -0700423 }
424
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700425 protected boolean isPageInTransition() {
426 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700427 }
428
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700429 /**
430 * Called when the page starts moving as part of the scroll. Subclasses can override this
431 * to provide custom behavior during animation.
432 */
433 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700434 }
435
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700436 /**
437 * Called when the page ends moving as part of the scroll. Subclasses can override this
438 * to provide custom behavior during animation.
439 */
440 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700441 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700442 }
443
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 /**
Winson Chung86f77532010-08-24 11:08:22 -0700445 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 *
447 * @param l The listener used to respond to long clicks.
448 */
449 @Override
450 public void setOnLongClickListener(OnLongClickListener l) {
451 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700452 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700454 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 }
Adam Cohen1697b792013-09-17 19:08:21 -0700456 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 }
458
Sunny Goyalc86df472016-02-25 09:19:38 -0800459 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700460 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800461 }
462
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800464 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800465 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800466 }
467
468 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700469 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700470 // In free scroll mode, we clamp the scrollX
471 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700472 // If the scroller is trying to move to a location beyond the maximum allowed
473 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700474 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700475 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700476 }
477
Sunny Goyal4d519f22017-10-24 10:32:40 -0700478 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700479 }
480
Adam Cohen8d769d62017-06-23 17:27:38 -0700481 mUnboundedScrollX = x;
482
Sunny Goyal70660032015-05-14 00:07:08 -0700483 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
484 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700485 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700486 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800487 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700488 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700489 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700490 overScroll(x - mMaxScrollX);
491 } else {
492 overScroll(x);
493 }
Adam Cohen68d73932010-11-15 10:50:58 -0800494 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700495 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700496 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800497 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700498 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700499 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700500 overScroll(x);
501 } else {
502 overScroll(x - mMaxScrollX);
503 }
Adam Cohen68d73932010-11-15 10:50:58 -0800504 }
505 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700506 if (mWasInOverscroll) {
507 overScroll(0);
508 mWasInOverscroll = false;
509 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700510 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800511 super.scrollTo(x, y);
512 }
513
Adam Cohen7d30a372013-07-01 17:03:59 -0700514 // Update the last motion events when scrolling
515 if (isReordering(true)) {
516 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
517 mLastMotionX = p[0];
518 mLastMotionY = p[1];
519 updateDragViewTranslationDuringDrag();
520 }
Michael Jurka0142d492010-08-25 17:46:15 -0700521 }
522
Adam Cohen53805212013-10-01 10:39:23 -0700523 private void sendScrollAccessibilityEvent() {
524 AccessibilityManager am =
525 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
526 if (am.isEnabled()) {
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();
Adam Cohen53805212013-10-01 10:39:23 -0700575 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700576 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700577 if (am.isEnabled()) {
578 // Notify the user when the page changes
579 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700580 }
Michael Jurka0142d492010-08-25 17:46:15 -0700581 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700582 }
Michael Jurka0142d492010-08-25 17:46:15 -0700583 return false;
584 }
585
586 @Override
587 public void computeScroll() {
588 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 }
590
Adam Cohen410f3cd2013-09-22 12:09:32 -0700591 public int getNormalChildHeight() {
Sunny Goyalac00cba2017-11-13 15:58:01 -0800592 return getViewportHeight() - getPaddingTop() - getPaddingBottom()
593 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700594 }
595
Sunny Goyalbc683e92017-12-06 10:25:07 -0800596 public int getNormalChildWidth() {
597 return getViewportWidth() - getPaddingLeft() - getPaddingRight()
598 - mInsets.left - mInsets.right;
599 }
600
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 @Override
602 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700603 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700604 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
605 return;
606 }
607
Adam Cohen7d30a372013-07-01 17:03:59 -0700608 // We measure the dimensions of the PagedView to be larger than the pages so that when we
609 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700610 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
611 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
612 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700613 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700614
Adam Cohen7d30a372013-07-01 17:03:59 -0700615 mViewport.set(0, 0, widthSize, heightSize);
616
617 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
618 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
619 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 }
621
Winson Chung8aad6102012-05-11 16:27:49 -0700622 // Return early if we aren't given a proper dimension
623 if (widthSize <= 0 || heightSize <= 0) {
624 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
625 return;
626 }
627
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700629 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700630 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700631
Sunny Goyalac00cba2017-11-13 15:58:01 -0800632 int myWidthSpec = MeasureSpec.makeMeasureSpec(
633 getViewportWidth() - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
634 int myHeightSpec = MeasureSpec.makeMeasureSpec(
635 getViewportHeight() - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700636
Sunny Goyalac00cba2017-11-13 15:58:01 -0800637 // measureChildren takes accounts for content padding, we only need to care about extra
638 // space due to insets.
639 measureChildren(myWidthSpec, myHeightSpec);
640 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800641 }
642
Sunny Goyalcf25b522015-07-09 00:01:18 -0700643 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700645 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700646 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700647 return;
648 }
649
Winson Chung785d2eb2011-04-14 16:08:02 -0700650 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700652
Adam Cohen7d30a372013-07-01 17:03:59 -0700653 int offsetX = getViewportOffsetX();
654 int offsetY = getViewportOffsetY();
655
656 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700657 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700658
Sunny Goyal70660032015-05-14 00:07:08 -0700659 final int startIndex = mIsRtl ? childCount - 1 : 0;
660 final int endIndex = mIsRtl ? -1 : childCount;
661 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700662
Adam Cohen7d30a372013-07-01 17:03:59 -0700663 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700664
Sunny Goyalac00cba2017-11-13 15:58:01 -0800665 int childLeft = offsetX + getPaddingLeft();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700666 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
667 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700668 }
669
Adam Cohen0ffac432013-07-10 11:19:26 -0700670 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700671 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 if (child.getVisibility() != View.GONE) {
Sunny Goyalac00cba2017-11-13 15:58:01 -0800673 int childTop = offsetY + getPaddingTop() + mInsets.top;
674 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding
675 - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100676
Adam Cohen96d30a12013-07-16 18:13:21 -0700677 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700678 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800679
Winson Chung785d2eb2011-04-14 16:08:02 -0700680 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700681 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800682 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700683
Sunny Goyalac00cba2017-11-13 15:58:01 -0800684 int scrollOffsetLeft = getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700685 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000686
Sunny Goyalac00cba2017-11-13 15:58:01 -0800687 childLeft += childWidth + mPageSpacing + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 }
689 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700690
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700691 final LayoutTransition transition = getLayoutTransition();
692 // If the transition is running defer updating max scroll, as some empty pages could
693 // still be present, and a max scroll change could cause sudden jumps in scroll.
694 if (transition != null && transition.isRunning()) {
695 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
696
697 @Override
698 public void startTransition(LayoutTransition transition, ViewGroup container,
699 View view, int transitionType) { }
700
701 @Override
702 public void endTransition(LayoutTransition transition, ViewGroup container,
703 View view, int transitionType) {
704 // Wait until all transitions are complete.
705 if (!transition.isRunning()) {
706 transition.removeTransitionListener(this);
707 updateMaxScrollX();
708 }
709 }
710 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700711 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700712 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700713 }
714
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700715 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
716 updateCurrentPageScroll();
717 mFirstLayout = false;
718 }
719
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700720 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700721 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700722 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700723 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700724
725 if (isReordering(true)) {
726 updateDragViewTranslationDuringDrag();
727 }
Winson Chunge3193b92010-09-10 11:44:42 -0700728 }
729
Sunny Goyala1fbd842015-05-20 13:40:27 -0700730 protected int getChildGap() {
731 return 0;
732 }
733
Sunny Goyal4d519f22017-10-24 10:32:40 -0700734 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700735 mMaxScrollX = computeMaxScrollX();
736 }
737
738 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700739 int childCount = getChildCount();
740 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700741 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700742 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700743 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700744 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700745 }
746 }
747
Adam Cohen3b185e22013-10-29 14:45:58 -0700748 public void setPageSpacing(int pageSpacing) {
749 mPageSpacing = pageSpacing;
750 requestLayout();
751 }
752
Winson Chunge3193b92010-09-10 11:44:42 -0700753 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700755 // Update the page indicator, we don't update the page indicator as we
756 // add/remove pages
757 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700758 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700759 }
760
Adam Cohen2591f6a2011-10-25 14:36:40 -0700761 // This ensures that when children are added, they get the correct transforms / alphas
762 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700763 invalidate();
764 }
765
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 @Override
767 public void onChildViewRemoved(View parent, View child) {
Tonya361c722017-07-04 09:43:06 -0700768 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700769 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 }
771
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700772 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700773 // Update the page indicator, we don't update the page indicator as we
774 // add/remove pages
775 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700776 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700777 }
778 }
779
780 @Override
781 public void removeView(View v) {
782 // XXX: We should find a better way to hook into this before the view
783 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700784 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700785 super.removeView(v);
786 }
787 @Override
788 public void removeViewInLayout(View v) {
789 // XXX: We should find a better way to hook into this before the view
790 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700791 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700792 super.removeViewInLayout(v);
793 }
794 @Override
795 public void removeViewAt(int index) {
796 // XXX: We should find a better way to hook into this before the view
797 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700798 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700799 super.removeViewAt(index);
800 }
801 @Override
802 public void removeAllViewsInLayout() {
803 // Update the page indicator, we don't update the page indicator as we
804 // add/remove pages
805 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700806 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700807 }
808
809 super.removeAllViewsInLayout();
810 }
811
Adam Cohen73894962011-10-31 13:17:17 -0700812 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700813 if (index < 0 || index > getChildCount() - 1) return 0;
814
Adam Cohenf698c6e2013-07-17 18:44:25 -0700815 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700816
Adam Cohenf698c6e2013-07-17 18:44:25 -0700817 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700818 }
819
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 @Override
821 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700822 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700823 if (page != mCurrentPage || !mScroller.isFinished()) {
824 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700825 return true;
826 }
827 return false;
828 }
829
830 @Override
831 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700832 int focusablePage;
833 if (mNextPage != INVALID_PAGE) {
834 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700836 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 }
Winson Chung86f77532010-08-24 11:08:22 -0700838 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700840 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 }
842 return false;
843 }
844
845 @Override
846 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800847 if (super.dispatchUnhandledMove(focused, direction)) {
848 return true;
849 }
850
851 if (mIsRtl) {
852 if (direction == View.FOCUS_LEFT) {
853 direction = View.FOCUS_RIGHT;
854 } else if (direction == View.FOCUS_RIGHT) {
855 direction = View.FOCUS_LEFT;
856 }
857 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 if (getCurrentPage() > 0) {
860 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 return true;
862 }
863 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700864 if (getCurrentPage() < getPageCount() - 1) {
865 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 return true;
867 }
868 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800869 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 }
871
872 @Override
873 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700874 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
875 return;
876 }
877
Adam Cohen0ffac432013-07-10 11:19:26 -0700878 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700879 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700880 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 }
882 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700883 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700884 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 }
886 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700887 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700888 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 }
890 }
891 }
892
893 /**
894 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700895 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 *
Winson Chung86f77532010-08-24 11:08:22 -0700897 * This happens when live folders requery, and if they're off page, they
898 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 */
900 @Override
901 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700902 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 View v = focused;
904 while (true) {
905 if (v == current) {
906 super.focusableViewAvailable(focused);
907 return;
908 }
909 if (v == this) {
910 return;
911 }
912 ViewParent parent = v.getParent();
913 if (parent instanceof View) {
914 v = (View)v.getParent();
915 } else {
916 return;
917 }
918 }
919 }
920
921 /**
922 * {@inheritDoc}
923 */
924 @Override
925 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
926 if (disallowIntercept) {
927 // We need to make sure to cancel our long press if
928 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700929 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700930 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 }
932 super.requestDisallowInterceptTouchEvent(disallowIntercept);
933 }
934
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800935 /**
936 * Return true if a tap at (x, y) should trigger a flip to the previous page.
937 */
938 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -0700939 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700940 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -0700941 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -0700942 }
Adam Cohen3b185e22013-10-29 14:45:58 -0700943 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800944 }
945
946 /**
947 * Return true if a tap at (x, y) should trigger a flip to the next page.
948 */
949 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -0700950 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700951 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -0700952 }
953 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -0700954 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -0700955 }
Winson Chung52aee602013-01-30 12:01:02 -0800956
Adam Cohen7d30a372013-07-01 17:03:59 -0700957 /** Returns whether x and y originated within the buffered viewport */
958 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700959 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -0700960 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700961 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700962 }
963
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 @Override
965 public boolean onInterceptTouchEvent(MotionEvent ev) {
966 /*
967 * This method JUST determines whether we want to intercept the motion.
968 * If we return true, onTouchEvent will be called and we do the actual
969 * scrolling there.
970 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800971 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700972
Winson Chung45e1d6e2010-11-09 17:19:49 -0800973 // Skip touch handling if there are no pages to swipe
974 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
975
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 /*
977 * Shortcut the most recurring case: the user is in the dragging
978 * state and he is moving his finger. We want to intercept this
979 * motion.
980 */
981 final int action = ev.getAction();
982 if ((action == MotionEvent.ACTION_MOVE) &&
983 (mTouchState == TOUCH_STATE_SCROLLING)) {
984 return true;
985 }
986
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 switch (action & MotionEvent.ACTION_MASK) {
988 case MotionEvent.ACTION_MOVE: {
989 /*
990 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
991 * whether the user has moved far enough from his original down touch.
992 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700993 if (mActivePointerId != INVALID_POINTER) {
994 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700995 }
996 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
997 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
998 // i.e. fall through to the next case (don't break)
999 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1000 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001001 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 }
1003
1004 case MotionEvent.ACTION_DOWN: {
1005 final float x = ev.getX();
1006 final float y = ev.getY();
1007 // Remember location of down touch
1008 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001009 mDownMotionY = y;
1010 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 mLastMotionX = x;
1012 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001013 float[] p = mapPointFromViewToParent(this, x, y);
1014 mParentDownMotionX = p[0];
1015 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001016 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001017 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001019
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 /*
1021 * If being flinged and user touches the screen, initiate drag;
1022 * otherwise don't. mScroller.isFinished should be false when
1023 * being flinged.
1024 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001025 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001026 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001027
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001028 if (finishedScrolling) {
1029 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001030 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001031 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001032 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001033 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001034 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001035 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1036 mTouchState = TOUCH_STATE_SCROLLING;
1037 } else {
1038 mTouchState = TOUCH_STATE_REST;
1039 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001040 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001041
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 break;
1043 }
1044
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001046 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001047 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 break;
1049
1050 case MotionEvent.ACTION_POINTER_UP:
1051 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001052 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 break;
1054 }
1055
1056 /*
1057 * The only time we want to intercept motion events is if we are in the
1058 * drag mode.
1059 */
1060 return mTouchState != TOUCH_STATE_REST;
1061 }
1062
Adam Cohenf8d28232011-02-01 21:47:00 -08001063 protected void determineScrollingStart(MotionEvent ev) {
1064 determineScrollingStart(ev, 1.0f);
1065 }
1066
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 /*
1068 * Determines if we should change the touch state to start scrolling after the
1069 * user moves their touch point too far.
1070 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001071 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001072 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001074 if (pointerIndex == -1) return;
1075
1076 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001077 final float x = ev.getX(pointerIndex);
1078 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001079 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1080
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001082
Adam Cohenf8d28232011-02-01 21:47:00 -08001083 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001085
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001086 if (xMoved) {
1087 // Scroll if the user moved far enough along the X axis
1088 mTouchState = TOUCH_STATE_SCROLLING;
1089 mTotalMotionX += Math.abs(mLastMotionX - x);
1090 mLastMotionX = x;
1091 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001092 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001093 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001094 // Stop listening for things like pinches.
1095 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001096 }
1097 }
1098
1099 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001100 // Try canceling the long press. It could also have been scheduled
1101 // by a distant descendant, so use the mAllowLongPress flag to block
1102 // everything
1103 final View currentPage = getPageAt(mCurrentPage);
1104 if (currentPage != null) {
1105 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 }
1107 }
1108
Adam Cohenb5ba0972011-09-07 18:02:31 -07001109 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001110 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001111
Adam Cohenedb40762013-07-18 16:45:45 -07001112 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001113 int count = getChildCount();
1114
1115 final int totalDistance;
1116
1117 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001118 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001119 adjacentPage = page - 1;
1120 }
1121
1122 if (adjacentPage < 0 || adjacentPage > count - 1) {
1123 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1124 } else {
1125 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1126 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001127
1128 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001129 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1130 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001131 return scrollProgress;
1132 }
1133
Adam Cohenedb40762013-07-18 16:45:45 -07001134 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001135 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001136 return 0;
1137 } else {
1138 return mPageScrolls[index];
1139 }
1140 }
1141
Adam Cohen564a2e72013-10-09 14:47:32 -07001142 // While layout transitions are occurring, a child's position may stray from its baseline
1143 // position. This method returns the magnitude of this stray at any given time.
1144 public int getLayoutTransitionOffsetForPage(int index) {
1145 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1146 return 0;
1147 } else {
1148 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001149
Sunny Goyalac00cba2017-11-13 15:58:01 -08001150 int scrollOffset = scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen564a2e72013-10-09 14:47:32 -07001151 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1152 return (int) (child.getX() - baselineX);
1153 }
1154 }
1155
Adam Cohenb5ba0972011-09-07 18:02:31 -07001156 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001157 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001158
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001159 int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001160 if (amount < 0) {
1161 mOverScrollX = overScrollAmount;
1162 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001163 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001164 mOverScrollX = mMaxScrollX + overScrollAmount;
1165 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001166 }
1167 invalidate();
1168 }
1169
Adam Cohenb5ba0972011-09-07 18:02:31 -07001170 protected void overScroll(float amount) {
1171 dampedOverScroll(amount);
1172 }
1173
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001174 /**
1175 * return true if freescroll has been enabled, false otherwise
1176 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001177 protected void enableFreeScroll() {
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001178 enableFreeScroll(false);
1179 }
1180
1181 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001182 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001183 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001184 }
1185
Sunny Goyalc99cb172017-10-19 16:15:09 -07001186 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001187 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001188 }
1189
Adam Cohenf9618852013-11-08 06:45:03 -08001190 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001191 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001192 mFreeScroll = freeScroll;
1193
Adam Cohenf9618852013-11-08 06:45:03 -08001194 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001195 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001196 } else if (wasFreeScroll) {
1197 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001198 }
1199
1200 setEnableOverscroll(!freeScroll);
1201 }
1202
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001203 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001204 mAllowOverScroll = enable;
1205 }
1206
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001207 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001208 if (mDragView != null) {
1209 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1210 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001211 int minDistance = Integer.MAX_VALUE;
1212 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001213 int maxPageNo = getChildCount() - 1;
1214 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001215 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001216 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001217 int d = Math.abs(dragX - pageX);
1218 if (d < minDistance) {
1219 minIndex = i;
1220 minDistance = d;
1221 }
1222 }
1223 return minIndex;
1224 }
1225 return -1;
1226 }
1227
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 @Override
1229 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001230 super.onTouchEvent(ev);
1231
Winson Chung45e1d6e2010-11-09 17:19:49 -08001232 // Skip touch handling if there are no pages to swipe
1233 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1234
Michael Jurkab8f06722010-10-10 15:58:46 -07001235 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001236
1237 final int action = ev.getAction();
1238
1239 switch (action & MotionEvent.ACTION_MASK) {
1240 case MotionEvent.ACTION_DOWN:
1241 /*
1242 * If being flinged and user touches, stop the fling. isFinished
1243 * will be false if being flinged.
1244 */
1245 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001246 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 }
1248
1249 // Remember where the motion event started
1250 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001251 mDownMotionY = mLastMotionY = ev.getY();
1252 mDownScrollX = getScrollX();
1253 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1254 mParentDownMotionX = p[0];
1255 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001256 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001257 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001259
Michael Jurka0142d492010-08-25 17:46:15 -07001260 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001261 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001262 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001263 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 break;
1265
1266 case MotionEvent.ACTION_MOVE:
1267 if (mTouchState == TOUCH_STATE_SCROLLING) {
1268 // Scroll to follow the motion event
1269 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001270
1271 if (pointerIndex == -1) return true;
1272
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001274 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001275
Adam Cohenaefd4e12011-02-14 16:39:38 -08001276 mTotalMotionX += Math.abs(deltaX);
1277
Winson Chungc0844aa2011-02-02 15:25:58 -08001278 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1279 // keep the remainder because we are actually testing if we've moved from the last
1280 // scrolled position (which is discrete).
1281 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001282 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001283 mLastMotionX = x;
1284 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 } else {
1286 awakenScrollBars();
1287 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001288 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1289 // Update the last motion position
1290 mLastMotionX = ev.getX();
1291 mLastMotionY = ev.getY();
1292
1293 // Update the parent down so that our zoom animations take this new movement into
1294 // account
1295 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1296 mParentDownMotionX = pt[0];
1297 mParentDownMotionY = pt[1];
1298 updateDragViewTranslationDuringDrag();
1299
1300 // Find the closest page to the touch point
1301 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001302
Adam Cohen7d30a372013-07-01 17:03:59 -07001303 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1304 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1305 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1306 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1307
Adam Cohenf358a4b2013-07-23 16:47:31 -07001308 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001309 // Do not allow any page to be moved to 0th position.
1310 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001311 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001312 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1313 mSidePageHoverIndex = pageUnderPointIndex;
1314 mSidePageHoverRunnable = new Runnable() {
1315 @Override
1316 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 // Setup the scroll to the correct page before we swap the views
1318 snapToPage(pageUnderPointIndex);
1319
1320 // For each of the pages between the paged view and the drag view,
1321 // animate them from the previous position to the new position in
1322 // the layout (as a result of the drag view moving in the layout)
1323 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1324 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1325 dragViewIndex + 1 : pageUnderPointIndex;
1326 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1327 dragViewIndex - 1 : pageUnderPointIndex;
1328 for (int i = lowerIndex; i <= upperIndex; ++i) {
1329 View v = getChildAt(i);
1330 // dragViewIndex < pageUnderPointIndex, so after we remove the
1331 // drag view all subsequent views to pageUnderPointIndex will
1332 // shift down.
1333 int oldX = getViewportOffsetX() + getChildOffset(i);
1334 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1335
1336 // Animate the view translation from its old position to its new
1337 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001338 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001339 if (anim != null) {
1340 anim.cancel();
1341 }
1342
1343 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001344 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001345 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001346 anim.start();
1347 v.setTag(anim);
1348 }
1349
1350 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001351 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001352 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001353 if (mPageIndicator != null) {
1354 mPageIndicator.setActiveMarker(getNextPage());
1355 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001356 }
1357 };
1358 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1359 }
1360 } else {
1361 removeCallbacks(mSidePageHoverRunnable);
1362 mSidePageHoverIndex = -1;
1363 }
Adam Cohen564976a2010-10-13 18:52:07 -07001364 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 determineScrollingStart(ev);
1366 }
1367 break;
1368
1369 case MotionEvent.ACTION_UP:
1370 if (mTouchState == TOUCH_STATE_SCROLLING) {
1371 final int activePointerId = mActivePointerId;
1372 final int pointerIndex = ev.findPointerIndex(activePointerId);
1373 final float x = ev.getX(pointerIndex);
1374 final VelocityTracker velocityTracker = mVelocityTracker;
1375 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1376 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001377 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001378 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001379 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1380 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001381
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001382 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1383
Adam Cohen00481b32011-11-18 12:03:48 -08001384 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001385 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001386
Adam Cohenf358a4b2013-07-23 16:47:31 -07001387 if (!mFreeScroll) {
1388 // In the case that the page is moved far to one direction and then is flung
1389 // in the opposite direction, we use a threshold to determine whether we should
1390 // just return to the starting page, or if we should skip one further.
1391 boolean returnToOriginalPage = false;
1392 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1393 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1394 returnToOriginalPage = true;
1395 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001396
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 int finalPage;
1398 // We give flings precedence over large moves, which is why we short-circuit our
1399 // test for a large move if a fling has been registered. That is, a large
1400 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001401 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1402 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1404 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1405 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1406 snapToPageWithVelocity(finalPage, velocityX);
1407 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1408 (isFling && isVelocityXLeft)) &&
1409 mCurrentPage < getChildCount() - 1) {
1410 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1411 snapToPageWithVelocity(finalPage, velocityX);
1412 } else {
1413 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001417 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 }
1419
1420 float scaleX = getScaleX();
1421 int vX = (int) (-velocityX * scaleX);
1422 int initialScrollX = (int) (getScrollX() * scaleX);
1423
Adam Cohenf9618852013-11-08 06:45:03 -08001424 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 mScroller.fling(initialScrollX,
1426 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001427 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1428 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1429 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1430 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1431 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1432 && unscaledScrollX < lastPageScroll) {
1433 // Make sure we land directly on a page. If flinging past one of the ends,
1434 // don't change the velocity as it will get stopped at the end anyway.
1435 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1436 // Ensure the scroll/snap doesn't happen too fast;
1437 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1438 - mScroller.getDuration();
1439 if (extraScrollDuration > 0) {
1440 mScroller.extendDuration(extraScrollDuration);
1441 }
1442 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001443 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001445 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001446 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1447 // at this point we have not moved beyond the touch slop
1448 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1449 // we can just page
1450 int nextPage = Math.max(0, mCurrentPage - 1);
1451 if (nextPage != mCurrentPage) {
1452 snapToPage(nextPage);
1453 } else {
1454 snapToDestination();
1455 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001456 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 // at this point we have not moved beyond the touch slop
1458 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1459 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001460 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1461 if (nextPage != mCurrentPage) {
1462 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 } else {
1464 snapToDestination();
1465 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001466 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1467 // Update the last motion position
1468 mLastMotionX = ev.getX();
1469 mLastMotionY = ev.getY();
1470
1471 // Update the parent down so that our zoom animations take this new movement into
1472 // account
1473 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1474 mParentDownMotionX = pt[0];
1475 mParentDownMotionY = pt[1];
1476 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001477 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001478 if (!mCancelTap) {
1479 onUnhandledTap(ev);
1480 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001482
1483 // Remove the callback to wait for the side page hover timeout
1484 removeCallbacks(mSidePageHoverRunnable);
1485 // End any intermediate reordering states
1486 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 break;
1488
1489 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001490 if (mTouchState == TOUCH_STATE_SCROLLING) {
1491 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001492 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001493 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 break;
1496
1497 case MotionEvent.ACTION_POINTER_UP:
1498 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001499 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 break;
1501 }
1502
1503 return true;
1504 }
1505
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001506 protected boolean shouldFlingForVelocity(int velocityX) {
1507 return Math.abs(velocityX) > mFlingThresholdVelocity;
1508 }
1509
Adam Cohen7d30a372013-07-01 17:03:59 -07001510 private void resetTouchState() {
1511 releaseVelocityTracker();
1512 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001513 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 mTouchState = TOUCH_STATE_REST;
1515 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001516 }
1517
Adam Cohenc2d6e892014-10-16 09:49:24 -07001518 /**
1519 * Triggered by scrolling via touch
1520 */
1521 protected void onScrollInteractionBegin() {
1522 }
1523
1524 protected void onScrollInteractionEnd() {
1525 }
1526
Adam Cohen1697b792013-09-17 19:08:21 -07001527 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001528 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001529 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001530
Winson Chung185d7162011-02-28 13:47:29 -08001531 @Override
1532 public boolean onGenericMotionEvent(MotionEvent event) {
1533 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1534 switch (event.getAction()) {
1535 case MotionEvent.ACTION_SCROLL: {
1536 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1537 final float vscroll;
1538 final float hscroll;
1539 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1540 vscroll = 0;
1541 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1542 } else {
1543 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1544 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1545 }
1546 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001547 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001548 : (hscroll > 0 || vscroll > 0);
1549 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001550 scrollRight();
1551 } else {
1552 scrollLeft();
1553 }
1554 return true;
1555 }
1556 }
1557 }
1558 }
1559 return super.onGenericMotionEvent(event);
1560 }
1561
Michael Jurkab8f06722010-10-10 15:58:46 -07001562 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1563 if (mVelocityTracker == null) {
1564 mVelocityTracker = VelocityTracker.obtain();
1565 }
1566 mVelocityTracker.addMovement(ev);
1567 }
1568
1569 private void releaseVelocityTracker() {
1570 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001571 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001572 mVelocityTracker.recycle();
1573 mVelocityTracker = null;
1574 }
1575 }
1576
Winson Chung321e9ee2010-08-09 13:37:56 -07001577 private void onSecondaryPointerUp(MotionEvent ev) {
1578 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1579 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1580 final int pointerId = ev.getPointerId(pointerIndex);
1581 if (pointerId == mActivePointerId) {
1582 // This was our active pointer going up. Choose a new
1583 // active pointer and adjust accordingly.
1584 // TODO: Make this decision more intelligent.
1585 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1586 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1587 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001588 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 mActivePointerId = ev.getPointerId(newPointerIndex);
1590 if (mVelocityTracker != null) {
1591 mVelocityTracker.clear();
1592 }
1593 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001594 }
1595
Winson Chung321e9ee2010-08-09 13:37:56 -07001596 @Override
1597 public void requestChildFocus(View child, View focused) {
1598 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001599 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001600 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001601 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001602 }
1603 }
1604
Adam Cohend19d3ca2010-09-15 14:43:42 -07001605 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001606 return getPageNearestToCenterOfScreen(getScrollX());
1607 }
1608
1609 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1610 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001611 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001612 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001613 final int childCount = getChildCount();
1614 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001615 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001616 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001618 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001619 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1620 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1621 minDistanceFromScreenCenter = distanceFromScreenCenter;
1622 minDistanceFromScreenCenterIndex = i;
1623 }
1624 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001625 return minDistanceFromScreenCenterIndex;
1626 }
1627
1628 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001629 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1630 }
1631
1632 protected boolean isInOverScroll() {
1633 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1634 }
1635
1636 protected int getPageSnapDuration() {
1637 if (isInOverScroll()) {
1638 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1639 }
1640 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001641 }
1642
Adam Cohene0f66b52010-11-23 15:06:07 -08001643 // We want the duration of the page snap animation to be influenced by the distance that
1644 // the screen has to travel, however, we don't want this duration to be effected in a
1645 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1646 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001647 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001648 f -= 0.5f; // center the values about 0.
1649 f *= 0.3f * Math.PI / 2.0f;
1650 return (float) Math.sin(f);
1651 }
1652
Michael Jurka0142d492010-08-25 17:46:15 -07001653 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001654 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001655 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001656
Adam Cohenedb40762013-07-18 16:45:45 -07001657 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001658 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001659 int duration = 0;
1660
Sunny Goyal4d113a52015-05-27 10:05:28 -07001661 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001662 // If the velocity is low enough, then treat this more as an automatic page advance
1663 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001664 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001665 return;
1666 }
1667
1668 // Here we compute a "distance" that will be used in the computation of the overall
1669 // snap duration. This is a function of the actual distance that needs to be traveled;
1670 // we keep this value close to half screen size in order to reduce the variance in snap
1671 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001672 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001673 float distance = halfScreenSize + halfScreenSize *
1674 distanceInfluenceForSnapDuration(distanceRatio);
1675
1676 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001677 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001678
1679 // we want the page's snap velocity to approximately match the velocity at which the
1680 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001681 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1682 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001683
1684 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001685 }
1686
Sunny Goyal1740d902015-05-27 11:14:01 -07001687 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001688 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001689 }
1690
Adam Cohenf9c184a2016-01-15 16:47:43 -08001691 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001692 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001693 }
1694
Michael Jurka0142d492010-08-25 17:46:15 -07001695 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001696 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001697 }
1698
Adam Cohenf9618852013-11-08 06:45:03 -08001699 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1700 snapToPage(whichPage, duration, false, interpolator);
1701 }
1702
1703 protected void snapToPage(int whichPage, int duration, boolean immediate,
1704 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001705 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001706
Adam Cohenedb40762013-07-18 16:45:45 -07001707 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001708 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001709 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001710 }
1711
1712 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001713 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001714 }
Michael Jurka0142d492010-08-25 17:46:15 -07001715
Adam Cohenf9618852013-11-08 06:45:03 -08001716 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1717 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001718 whichPage = validateNewPage(whichPage);
1719
Adam Cohen7d30a372013-07-01 17:03:59 -07001720 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001721
Winson Chung321e9ee2010-08-09 13:37:56 -07001722 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001723 if (immediate) {
1724 duration = 0;
1725 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001726 duration = Math.abs(delta);
1727 }
1728
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001729 if (duration != 0) {
1730 pageBeginTransition();
1731 }
1732
Adam Cohenf358a4b2013-07-23 16:47:31 -07001733 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001734 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001735 }
Adam Cohenf9618852013-11-08 06:45:03 -08001736
1737 if (interpolator != null) {
1738 mScroller.setInterpolator(interpolator);
1739 } else {
1740 mScroller.setInterpolator(mDefaultInterpolator);
1741 }
1742
Sunny Goyalc86df472016-02-25 09:19:38 -08001743 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001744
Adam Cohen674531f2013-12-13 15:07:14 -08001745 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001746
1747 // Trigger a compute() to finish switching pages if necessary
1748 if (immediate) {
1749 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001750 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001751 }
1752
Winson Chung321e9ee2010-08-09 13:37:56 -07001753 invalidate();
1754 }
1755
Winson Chung321e9ee2010-08-09 13:37:56 -07001756 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001757 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001758 }
1759
1760 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001761 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001762 }
1763
Adam Cohendbdff6b2013-09-18 19:09:15 -07001764 @Override
1765 public boolean performLongClick() {
1766 mCancelTap = true;
1767 return super.performLongClick();
1768 }
1769
Winson Chung321e9ee2010-08-09 13:37:56 -07001770 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001771 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001772
1773 SavedState(Parcelable superState) {
1774 super(superState);
1775 }
1776
Adam Cohen091440a2015-03-18 14:16:05 -07001777 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001778 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001779 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001780 }
1781
1782 @Override
1783 public void writeToParcel(Parcel out, int flags) {
1784 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001785 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001786 }
1787
1788 public static final Parcelable.Creator<SavedState> CREATOR =
1789 new Parcelable.Creator<SavedState>() {
1790 public SavedState createFromParcel(Parcel in) {
1791 return new SavedState(in);
1792 }
1793
1794 public SavedState[] newArray(int size) {
1795 return new SavedState[size];
1796 }
1797 };
1798 }
1799
Adam Cohen7d30a372013-07-01 17:03:59 -07001800 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001801 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001802 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001803 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1804 new PropertyListBuilder()
1805 .scale(1)
1806 .translationX(0)
1807 .translationY(0)
1808 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001809 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001810 anim.addListener(new AnimatorListenerAdapter() {
1811 @Override
1812 public void onAnimationEnd(Animator animation) {
1813 onPostReorderingAnimationCompleted();
1814 }
1815 });
1816 anim.start();
1817 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001818 }
1819
Sunny Goyal1d08f702015-05-04 15:50:25 -07001820 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001821 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1822 mTouchState = TOUCH_STATE_REORDERING;
1823 mIsReordering = true;
1824
Adam Cohen7d30a372013-07-01 17:03:59 -07001825 // We must invalidate to trigger a redraw to update the layers such that the drag view
1826 // is always drawn on top
1827 invalidate();
1828 }
1829
Adam Cohen091440a2015-03-18 14:16:05 -07001830 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001831 // Trigger the callback when reordering has settled
1832 --mPostReorderingPreZoomInRemainingAnimationCount;
1833 if (mPostReorderingPreZoomInRunnable != null &&
1834 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1835 mPostReorderingPreZoomInRunnable.run();
1836 mPostReorderingPreZoomInRunnable = null;
1837 }
1838 }
1839
Sunny Goyal1d08f702015-05-04 15:50:25 -07001840 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001841 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001842 }
1843
Adam Cohenf358a4b2013-07-23 16:47:31 -07001844 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001845 int dragViewIndex = indexOfChild(v);
1846
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001847 // Do not allow the first page to be moved around
1848 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001849
Adam Cohen7d30a372013-07-01 17:03:59 -07001850 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001851 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001852 // Find the drag view under the pointer
1853 mDragView = getChildAt(dragViewIndex);
1854 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1855 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001856 mReorderingStarted = true;
1857
Adam Cohenf9618852013-11-08 06:45:03 -08001858 snapToPage(getPageNearestToCenterOfScreen());
1859 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001860 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001861 return true;
1862 }
1863 return false;
1864 }
1865
1866 boolean isReordering(boolean testTouchState) {
1867 boolean state = mIsReordering;
1868 if (testTouchState) {
1869 state &= (mTouchState == TOUCH_STATE_REORDERING);
1870 }
1871 return state;
1872 }
1873 void endReordering() {
1874 // For simplicity, we call endReordering sometimes even if reordering was never started.
1875 // In that case, we don't want to do anything.
1876 if (!mReorderingStarted) return;
1877 mReorderingStarted = false;
1878
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001879 mPostReorderingPreZoomInRunnable = new Runnable() {
1880 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001881 // If we haven't flung-to-delete the current child,
1882 // then we just animate the drag view back into position
1883 onEndReordering();
1884
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001885 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001886 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001887 };
Adam Cohen7d30a372013-07-01 17:03:59 -07001888
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001889 mPostReorderingPreZoomInRemainingAnimationCount =
1890 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
1891 // Snap to the current page
1892 snapToPage(indexOfChild(mDragView), 0);
1893 // Animate the drag view back to the front position
1894 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001895 }
1896
Winson Chung6a0f57d2011-06-29 20:10:49 -07001897 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001898 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001899 @Override
1900 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1901 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001902 info.setScrollable(getPageCount() > 1);
1903 if (getCurrentPage() < getPageCount() - 1) {
1904 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1905 }
1906 if (getCurrentPage() > 0) {
1907 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1908 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001909 info.setClassName(getClass().getName());
1910
1911 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1912 // Besides disabling the accessibility long-click, this also prevents this view from getting
1913 // accessibility focus.
1914 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001915 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001916 }
1917
1918 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001919 public void sendAccessibilityEvent(int eventType) {
1920 // Don't let the view send real scroll events.
1921 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1922 super.sendAccessibilityEvent(eventType);
1923 }
1924 }
1925
1926 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001927 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1928 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001929 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001930 }
1931
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001932 @Override
1933 public boolean performAccessibilityAction(int action, Bundle arguments) {
1934 if (super.performAccessibilityAction(action, arguments)) {
1935 return true;
1936 }
1937 switch (action) {
1938 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1939 if (getCurrentPage() < getPageCount() - 1) {
1940 scrollRight();
1941 return true;
1942 }
1943 } break;
1944 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1945 if (getCurrentPage() > 0) {
1946 scrollLeft();
1947 return true;
1948 }
1949 } break;
1950 }
1951 return false;
1952 }
1953
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001954 protected String getPageIndicatorDescription() {
1955 return getCurrentPageDescription();
1956 }
1957
Adam Cohen0ffac432013-07-10 11:19:26 -07001958 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001959 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001960 getNextPage() + 1, getChildCount());
1961 }
1962
Winson Chungd11265e2011-08-30 13:37:23 -07001963 @Override
1964 public boolean onHoverEvent(android.view.MotionEvent event) {
1965 return true;
1966 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001967}