blob: f55455b7efc53427b80cbb425ad8ac8a5ca53533 [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;
Adam Cohen7d30a372013-07-01 17:03:59 -070033import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080035import android.view.InputDevice;
36import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070037import android.view.MotionEvent;
38import android.view.VelocityTracker;
39import android.view.View;
40import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080041import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.view.ViewGroup;
43import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070044import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070045import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080047import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070048
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;
Adam Cohenf358a4b2013-07-23 16:47:31 -070089
Adam Cohen265b9a62011-12-07 14:37:18 -080090 protected int mFlingThresholdVelocity;
91 protected int mMinFlingVelocity;
92 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070095 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070096
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
Adam Cohen7d30a372013-07-01 17:03:59 -0700168 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700169 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700170 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700171 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700172 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 // This variable's scope is only for the duration of startReordering() and endReordering()
174 private boolean mReorderingStarted = false;
175 // This variable's scope is for the duration of startReordering() and after the zoomIn()
176 // animation after endReordering()
177 private boolean mIsReordering;
178 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700179 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700180 private int mPostReorderingPreZoomInRemainingAnimationCount;
181 private Runnable mPostReorderingPreZoomInRunnable;
182
Adam Cohen7d30a372013-07-01 17:03:59 -0700183 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700184 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700185 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700186 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700187
John Spurlock77e1f472013-09-11 10:09:51 -0400188 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700189 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400190
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 public PagedView(Context context) {
192 this(context, null);
193 }
194
195 public PagedView(Context context, AttributeSet attrs) {
196 this(context, attrs, 0);
197 }
198
199 public PagedView(Context context, AttributeSet attrs, int defStyle) {
200 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700201
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 TypedArray a = context.obtainStyledAttributes(attrs,
203 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700204 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 a.recycle();
206
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700208 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700209 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 }
211
212 /**
213 * Initializes various states for this workspace.
214 */
Michael Jurka0142d492010-08-25 17:46:15 -0700215 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800216 mScroller = new LauncherScroller(getContext());
217 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700218 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700219
220 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700221 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800223
Sunny Goyalcf25b522015-07-09 00:01:18 -0700224 float density = getResources().getDisplayMetrics().density;
225 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
226 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
227 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700228 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700229 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 }
231
Adam Cohenf9618852013-11-08 06:45:03 -0800232 protected void setDefaultInterpolator(Interpolator interpolator) {
233 mDefaultInterpolator = interpolator;
234 mScroller.setInterpolator(mDefaultInterpolator);
235 }
236
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700237 public void initParentViews(View parent) {
238 if (mPageIndicatorViewId > -1) {
239 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700240 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700241 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700242 }
243 }
244
Adam Cohen7d30a372013-07-01 17:03:59 -0700245 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700246 private float[] mapPointFromViewToParent(View v, float x, float y) {
247 sTmpPoint[0] = x;
248 sTmpPoint[1] = y;
249 v.getMatrix().mapPoints(sTmpPoint);
250 sTmpPoint[0] += v.getLeft();
251 sTmpPoint[1] += v.getTop();
252 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700253 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700254 private float[] mapPointFromParentToView(View v, float x, float y) {
255 sTmpPoint[0] = x - v.getLeft();
256 sTmpPoint[1] = y - v.getTop();
257 v.getMatrix().invert(sTmpInvMatrix);
258 sTmpInvMatrix.mapPoints(sTmpPoint);
259 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700260 }
261
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700262 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700263 if (mDragView != null) {
264 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
265 (mDragViewBaselineLeft - mDragView.getLeft());
266 float y = mLastMotionY - mDownMotionY;
267 mDragView.setTranslationX(x);
268 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700269
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
271 + x + ", " + y);
272 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700273 }
274
275 public void setMinScale(float f) {
276 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700277 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700278 requestLayout();
279 }
280
281 @Override
282 public void setScaleX(float scaleX) {
283 super.setScaleX(scaleX);
284 if (isReordering(true)) {
285 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
286 mLastMotionX = p[0];
287 mLastMotionY = p[1];
288 updateDragViewTranslationDuringDrag();
289 }
290 }
291
292 // Convenience methods to get the actual width/height of the PagedView (since it is measured
293 // to be larger to account for the minimum possible scale)
294 int getViewportWidth() {
295 return mViewport.width();
296 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800297 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700298 return mViewport.height();
299 }
300
301 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
302 // PagedView both horizontally and vertically
303 int getViewportOffsetX() {
304 return (getMeasuredWidth() - getViewportWidth()) / 2;
305 }
306
307 int getViewportOffsetY() {
308 return (getMeasuredHeight() - getViewportHeight()) / 2;
309 }
310
Hyunyoung Song77441692016-06-27 22:00:48 -0700311 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700312 return mPageIndicator;
313 }
314
Adam Cohen674531f2013-12-13 15:07:14 -0800315 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700316 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
317 * that the user was on before entering free scroll mode (e.g. the home screen page they
318 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
319 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700321 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700322 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700324
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700325 /**
326 * Returns the index of page to be shown immediately afterwards.
327 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700328 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700329 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
330 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700331
Adam Cohenf9c184a2016-01-15 16:47:43 -0800332 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 return getChildCount();
334 }
335
Sunny Goyal83a8f042015-05-19 12:52:12 -0700336 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 return getChildAt(index);
338 }
339
Adam Cohenae4f1552011-10-20 00:15:42 -0700340 protected int indexToPage(int index) {
341 return index;
342 }
343
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800345 * Updates the scroll of the current page immediately to its final scroll position. We use this
346 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
347 * the previous tab page.
348 */
349 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700350 // If the current page is invalid, just reset the scroll position to zero
351 int newX = 0;
352 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700353 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700354 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800355 scrollTo(newX, 0);
356 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700357 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800358 }
359
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700360 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700361 mScroller.abortAnimation();
362 // We need to clean up the next page here to avoid computeScrollHelper from
363 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700364 if (resetNextPage) {
365 mNextPage = INVALID_PAGE;
366 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700367 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700368
Tony Wickham8f7ead32016-04-07 18:46:44 -0700369 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700370 mScroller.forceFinished(true);
371 // We need to clean up the next page here to avoid computeScrollHelper from
372 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700373 if (resetNextPage) {
374 mNextPage = INVALID_PAGE;
375 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700376 }
377
Adam Cohen6f127a62014-06-12 14:54:41 -0700378 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700379 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700380 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700381 }
382
Chet Haasebc2f0822012-10-26 17:59:53 -0700383 /**
Winson Chung86f77532010-08-24 11:08:22 -0700384 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700385 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700386 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800387 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700388 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800389 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800390 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
391 // the default
392 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800393 return;
394 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700395 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700396 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700397 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700398 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800399 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 }
401
Winson Chung8c87cd82013-07-23 16:20:10 -0700402 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800403 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
404 * has settled.
405 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700406 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800407 updatePageIndicator();
408 }
409
410 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700411 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700412 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700413 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700414 if (!isReordering(false)) {
415 mPageIndicator.setActiveMarker(getNextPage());
416 }
Winson Chungd2be3812013-07-16 11:11:32 -0700417 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700419 protected void pageBeginTransition() {
420 if (!mIsPageInTransition) {
421 mIsPageInTransition = true;
422 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700423 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700424 }
425
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700426 protected void pageEndTransition() {
427 if (mIsPageInTransition) {
428 mIsPageInTransition = false;
429 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700430 }
Michael Jurka0142d492010-08-25 17:46:15 -0700431 }
432
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700433 protected boolean isPageInTransition() {
434 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700435 }
436
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700437 /**
438 * Called when the page starts moving as part of the scroll. Subclasses can override this
439 * to provide custom behavior during animation.
440 */
441 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700442 }
443
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700444 /**
445 * Called when the page ends moving as part of the scroll. Subclasses can override this
446 * to provide custom behavior during animation.
447 */
448 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700449 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700450 }
451
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 /**
Winson Chung86f77532010-08-24 11:08:22 -0700453 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 *
455 * @param l The listener used to respond to long clicks.
456 */
457 @Override
458 public void setOnLongClickListener(OnLongClickListener l) {
459 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700460 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700462 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 }
Adam Cohen1697b792013-09-17 19:08:21 -0700464 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 }
466
Sunny Goyalc86df472016-02-25 09:19:38 -0800467 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700468 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800469 }
470
Winson Chung321e9ee2010-08-09 13:37:56 -0700471 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800472 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800473 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800474 }
475
476 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700477 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700478 // In free scroll mode, we clamp the scrollX
479 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700480 // If the scroller is trying to move to a location beyond the maximum allowed
481 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700482 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700483 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700484 }
485
Sunny Goyal4d519f22017-10-24 10:32:40 -0700486 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700487 }
488
Adam Cohen8d769d62017-06-23 17:27:38 -0700489 mUnboundedScrollX = x;
490
Sunny Goyal70660032015-05-14 00:07:08 -0700491 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
492 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700493 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700494 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800495 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700496 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700497 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700498 overScroll(x - mMaxScrollX);
499 } else {
500 overScroll(x);
501 }
Adam Cohen68d73932010-11-15 10:50:58 -0800502 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700503 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700504 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800505 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700506 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700507 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700508 overScroll(x);
509 } else {
510 overScroll(x - mMaxScrollX);
511 }
Adam Cohen68d73932010-11-15 10:50:58 -0800512 }
513 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700514 if (mWasInOverscroll) {
515 overScroll(0);
516 mWasInOverscroll = false;
517 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700518 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800519 super.scrollTo(x, y);
520 }
521
Adam Cohen7d30a372013-07-01 17:03:59 -0700522 // Update the last motion events when scrolling
523 if (isReordering(true)) {
524 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
525 mLastMotionX = p[0];
526 mLastMotionY = p[1];
527 updateDragViewTranslationDuringDrag();
528 }
Michael Jurka0142d492010-08-25 17:46:15 -0700529 }
530
Adam Cohen53805212013-10-01 10:39:23 -0700531 private void sendScrollAccessibilityEvent() {
532 AccessibilityManager am =
533 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
534 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700535 if (mCurrentPage != getNextPage()) {
536 AccessibilityEvent ev =
537 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700538 ev.setScrollable(true);
539 ev.setScrollX(getScrollX());
540 ev.setScrollY(getScrollY());
541 ev.setMaxScrollX(mMaxScrollX);
542 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700543
Vadim Tryshevf4715972015-05-08 17:21:03 -0700544 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700545 }
Adam Cohen53805212013-10-01 10:39:23 -0700546 }
547 }
548
Michael Jurka0142d492010-08-25 17:46:15 -0700549 // we moved this functionality to a helper function so SmoothPagedView can reuse it
550 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800551 return computeScrollHelper(true);
552 }
553
554 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700556 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800557 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700558 || getScrollY() != mScroller.getCurrY()
559 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700560 float scaleX = mFreeScroll ? getScaleX() : 1f;
561 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
562 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700563 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800564 if (shouldInvalidate) {
565 invalidate();
566 }
Michael Jurka0142d492010-08-25 17:46:15 -0700567 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800568 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700569 sendScrollAccessibilityEvent();
570
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700571 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700572 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700573 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700574 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700575
Adam Cohen73aa9752010-11-24 16:26:58 -0800576 // We don't want to trigger a page end moving unless the page has settled
577 // and the user has stopped scrolling
578 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700579 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800580 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700581
Adam Cohen7d30a372013-07-01 17:03:59 -0700582 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700583 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700584 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700585 if (am.isEnabled()) {
586 // Notify the user when the page changes
587 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700588 }
Michael Jurka0142d492010-08-25 17:46:15 -0700589 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700590 }
Michael Jurka0142d492010-08-25 17:46:15 -0700591 return false;
592 }
593
594 @Override
595 public void computeScroll() {
596 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
598
Adam Cohen96d30a12013-07-16 18:13:21 -0700599 public static class LayoutParams extends ViewGroup.LayoutParams {
600 public boolean isFullScreenPage = false;
601
Sunny Goyal7c786f72016-06-01 14:08:21 -0700602 // If true, the start edge of the page snaps to the start edge of the viewport.
603 public boolean matchStartEdge = false;
604
Adam Cohen96d30a12013-07-16 18:13:21 -0700605 /**
606 * {@inheritDoc}
607 */
608 public LayoutParams(int width, int height) {
609 super(width, height);
610 }
611
Sunny Goyal41b22c02015-05-14 15:20:48 -0700612 public LayoutParams(Context context, AttributeSet attrs) {
613 super(context, attrs);
614 }
615
Adam Cohen96d30a12013-07-16 18:13:21 -0700616 public LayoutParams(ViewGroup.LayoutParams source) {
617 super(source);
618 }
619 }
620
Sunny Goyal41b22c02015-05-14 15:20:48 -0700621 @Override
622 public LayoutParams generateLayoutParams(AttributeSet attrs) {
623 return new LayoutParams(getContext(), attrs);
624 }
625
626 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700627 protected LayoutParams generateDefaultLayoutParams() {
628 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
629 }
630
Sunny Goyal41b22c02015-05-14 15:20:48 -0700631 @Override
632 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
633 return new LayoutParams(p);
634 }
635
636 @Override
637 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
638 return p instanceof LayoutParams;
639 }
640
Adam Cohenedb40762013-07-18 16:45:45 -0700641 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700642 LayoutParams lp = generateDefaultLayoutParams();
643 lp.isFullScreenPage = true;
644 super.addView(page, 0, lp);
645 }
646
Adam Cohen410f3cd2013-09-22 12:09:32 -0700647 public int getNormalChildHeight() {
648 return mNormalChildHeight;
649 }
650
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 @Override
652 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700653 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700654 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
655 return;
656 }
657
Adam Cohen7d30a372013-07-01 17:03:59 -0700658 // We measure the dimensions of the PagedView to be larger than the pages so that when we
659 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700660 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
661 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
662 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700663 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800664 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700665 // viewport, we can be at most one and a half screens offset once we scale down
666 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700667 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
668 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700669
Winson Chungc82d2622013-11-06 13:23:29 -0800670 int parentWidthSize = (int) (2f * maxSize);
671 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700672 int scaledWidthSize, scaledHeightSize;
673 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700674 scaledWidthSize = (int) (parentWidthSize / mMinScale);
675 scaledHeightSize = (int) (parentHeightSize / mMinScale);
676 } else {
677 scaledWidthSize = widthSize;
678 scaledHeightSize = heightSize;
679 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700680 mViewport.set(0, 0, widthSize, heightSize);
681
682 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
683 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
684 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 }
686
Winson Chung8aad6102012-05-11 16:27:49 -0700687 // Return early if we aren't given a proper dimension
688 if (widthSize <= 0 || heightSize <= 0) {
689 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
690 return;
691 }
692
Adam Lesinski6b879f02010-11-04 16:15:23 -0700693 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
694 * of the All apps view on XLarge displays to not take up more space then it needs. Width
695 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
696 * each page to have the same width.
697 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700698 final int verticalPadding = getPaddingTop() + getPaddingBottom();
699 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700700
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700701 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700702 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700703 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700704 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700705 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
706 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
707 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
708 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700709 final int childCount = getChildCount();
710 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700711 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700712 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100713 if (child.getVisibility() != GONE) {
714 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700715
Vladimir Marko2824b072013-10-04 16:42:17 +0100716 int childWidthMode;
717 int childHeightMode;
718 int childWidth;
719 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700720
Vladimir Marko2824b072013-10-04 16:42:17 +0100721 if (!lp.isFullScreenPage) {
722 if (lp.width == LayoutParams.WRAP_CONTENT) {
723 childWidthMode = MeasureSpec.AT_MOST;
724 } else {
725 childWidthMode = MeasureSpec.EXACTLY;
726 }
727
728 if (lp.height == LayoutParams.WRAP_CONTENT) {
729 childHeightMode = MeasureSpec.AT_MOST;
730 } else {
731 childHeightMode = MeasureSpec.EXACTLY;
732 }
733
Adam Cohen3b185e22013-10-29 14:45:58 -0700734 childWidth = getViewportWidth() - horizontalPadding
735 - mInsets.left - mInsets.right;
736 childHeight = getViewportHeight() - verticalPadding
737 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100738 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700739 } else {
740 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700741 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100742
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800743 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700744 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700745 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700746 if (referenceChildWidth == 0) {
747 referenceChildWidth = childWidth;
748 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700749
Vladimir Marko2824b072013-10-04 16:42:17 +0100750 final int childWidthMeasureSpec =
751 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
752 final int childHeightMeasureSpec =
753 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
754 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700755 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700756 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700757 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800758 }
759
Sunny Goyalcf25b522015-07-09 00:01:18 -0700760 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700761 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700762 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700763 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700764 return;
765 }
766
Winson Chung785d2eb2011-04-14 16:08:02 -0700767 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700769
Adam Cohen7d30a372013-07-01 17:03:59 -0700770 int offsetX = getViewportOffsetX();
771 int offsetY = getViewportOffsetY();
772
773 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700774 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700775
Sunny Goyal70660032015-05-14 00:07:08 -0700776 final int startIndex = mIsRtl ? childCount - 1 : 0;
777 final int endIndex = mIsRtl ? -1 : childCount;
778 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700779
Adam Cohen7d30a372013-07-01 17:03:59 -0700780 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700781
Adam Cohen3b185e22013-10-29 14:45:58 -0700782 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000783 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700784
Sunny Goyal6178f132016-07-11 17:30:03 -0700785 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700786 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
787 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700788 }
789
Adam Cohen0ffac432013-07-10 11:19:26 -0700790 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700791 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700793 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100794 int childTop;
795 if (lp.isFullScreenPage) {
796 childTop = offsetY;
797 } else {
798 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700799 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100800 }
801
Adam Cohen96d30a12013-07-16 18:13:21 -0700802 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700803 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800804
Winson Chung785d2eb2011-04-14 16:08:02 -0700805 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700806 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800807 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700808
Sunny Goyal6178f132016-07-11 17:30:03 -0700809 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700810 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000811
812 int pageGap = mPageSpacing;
813 int next = i + delta;
814 if (next != endIndex) {
815 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
816 } else {
817 nextLp = null;
818 }
819
820 // Prevent full screen pages from showing in the viewport
821 // when they are not the current page.
822 if (lp.isFullScreenPage) {
823 pageGap = getPaddingLeft();
824 } else if (nextLp != null && nextLp.isFullScreenPage) {
825 pageGap = getPaddingRight();
826 }
827
Sunny Goyala1fbd842015-05-20 13:40:27 -0700828 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 }
830 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700831
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700832 final LayoutTransition transition = getLayoutTransition();
833 // If the transition is running defer updating max scroll, as some empty pages could
834 // still be present, and a max scroll change could cause sudden jumps in scroll.
835 if (transition != null && transition.isRunning()) {
836 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
837
838 @Override
839 public void startTransition(LayoutTransition transition, ViewGroup container,
840 View view, int transitionType) { }
841
842 @Override
843 public void endTransition(LayoutTransition transition, ViewGroup container,
844 View view, int transitionType) {
845 // Wait until all transitions are complete.
846 if (!transition.isRunning()) {
847 transition.removeTransitionListener(this);
848 updateMaxScrollX();
849 }
850 }
851 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700852 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700853 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700854 }
855
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700856 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
857 updateCurrentPageScroll();
858 mFirstLayout = false;
859 }
860
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700861 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700862 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700863 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700864 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700865
866 if (isReordering(true)) {
867 updateDragViewTranslationDuringDrag();
868 }
Winson Chunge3193b92010-09-10 11:44:42 -0700869 }
870
Sunny Goyala1fbd842015-05-20 13:40:27 -0700871 protected int getChildGap() {
872 return 0;
873 }
874
Sunny Goyal4d519f22017-10-24 10:32:40 -0700875 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700876 mMaxScrollX = computeMaxScrollX();
877 }
878
879 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700880 int childCount = getChildCount();
881 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700882 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700883 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700884 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700885 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700886 }
887 }
888
Adam Cohen3b185e22013-10-29 14:45:58 -0700889 public void setPageSpacing(int pageSpacing) {
890 mPageSpacing = pageSpacing;
891 requestLayout();
892 }
893
Winson Chunge3193b92010-09-10 11:44:42 -0700894 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700895 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700896 // Update the page indicator, we don't update the page indicator as we
897 // add/remove pages
898 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700899 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700900 }
901
Adam Cohen2591f6a2011-10-25 14:36:40 -0700902 // This ensures that when children are added, they get the correct transforms / alphas
903 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700904 invalidate();
905 }
906
Michael Jurka8b805b12012-04-18 14:23:14 -0700907 @Override
908 public void onChildViewRemoved(View parent, View child) {
Tonya361c722017-07-04 09:43:06 -0700909 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700910 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700911 }
912
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700913 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700914 // Update the page indicator, we don't update the page indicator as we
915 // add/remove pages
916 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700917 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700918 }
919 }
920
921 @Override
922 public void removeView(View v) {
923 // XXX: We should find a better way to hook into this before the view
924 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700925 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700926 super.removeView(v);
927 }
928 @Override
929 public void removeViewInLayout(View v) {
930 // XXX: We should find a better way to hook into this before the view
931 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700932 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700933 super.removeViewInLayout(v);
934 }
935 @Override
936 public void removeViewAt(int index) {
937 // XXX: We should find a better way to hook into this before the view
938 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700939 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700940 super.removeViewAt(index);
941 }
942 @Override
943 public void removeAllViewsInLayout() {
944 // Update the page indicator, we don't update the page indicator as we
945 // add/remove pages
946 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700947 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700948 }
949
950 super.removeAllViewsInLayout();
951 }
952
Adam Cohen73894962011-10-31 13:17:17 -0700953 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700954 if (index < 0 || index > getChildCount() - 1) return 0;
955
Adam Cohenf698c6e2013-07-17 18:44:25 -0700956 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700957
Adam Cohenf698c6e2013-07-17 18:44:25 -0700958 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700959 }
960
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 @Override
962 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700963 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700964 if (page != mCurrentPage || !mScroller.isFinished()) {
965 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 return true;
967 }
968 return false;
969 }
970
971 @Override
972 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700973 int focusablePage;
974 if (mNextPage != INVALID_PAGE) {
975 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700977 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 }
Winson Chung86f77532010-08-24 11:08:22 -0700979 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700981 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 }
983 return false;
984 }
985
986 @Override
987 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800988 if (super.dispatchUnhandledMove(focused, direction)) {
989 return true;
990 }
991
992 if (mIsRtl) {
993 if (direction == View.FOCUS_LEFT) {
994 direction = View.FOCUS_RIGHT;
995 } else if (direction == View.FOCUS_RIGHT) {
996 direction = View.FOCUS_LEFT;
997 }
998 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001000 if (getCurrentPage() > 0) {
1001 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 return true;
1003 }
1004 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001005 if (getCurrentPage() < getPageCount() - 1) {
1006 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 return true;
1008 }
1009 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001010 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 }
1012
1013 @Override
1014 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001015 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1016 return;
1017 }
1018
Adam Cohen0ffac432013-07-10 11:19:26 -07001019 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001020 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001021 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 }
1023 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001024 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001025 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001026 }
1027 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001028 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001029 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 }
1031 }
1032 }
1033
1034 /**
1035 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001036 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 *
Winson Chung86f77532010-08-24 11:08:22 -07001038 * This happens when live folders requery, and if they're off page, they
1039 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 */
1041 @Override
1042 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001043 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 View v = focused;
1045 while (true) {
1046 if (v == current) {
1047 super.focusableViewAvailable(focused);
1048 return;
1049 }
1050 if (v == this) {
1051 return;
1052 }
1053 ViewParent parent = v.getParent();
1054 if (parent instanceof View) {
1055 v = (View)v.getParent();
1056 } else {
1057 return;
1058 }
1059 }
1060 }
1061
1062 /**
1063 * {@inheritDoc}
1064 */
1065 @Override
1066 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1067 if (disallowIntercept) {
1068 // We need to make sure to cancel our long press if
1069 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001070 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001071 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 }
1073 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1074 }
1075
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001076 /**
1077 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1078 */
1079 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001080 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001081 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001082 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001083 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001084 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001085 }
1086
1087 /**
1088 * Return true if a tap at (x, y) should trigger a flip to the next page.
1089 */
1090 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001091 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001092 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001093 }
1094 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001095 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001096 }
Winson Chung52aee602013-01-30 12:01:02 -08001097
Adam Cohen7d30a372013-07-01 17:03:59 -07001098 /** Returns whether x and y originated within the buffered viewport */
1099 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001100 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001101 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001102 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001103 }
1104
Winson Chung321e9ee2010-08-09 13:37:56 -07001105 @Override
1106 public boolean onInterceptTouchEvent(MotionEvent ev) {
1107 /*
1108 * This method JUST determines whether we want to intercept the motion.
1109 * If we return true, onTouchEvent will be called and we do the actual
1110 * scrolling there.
1111 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001112 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001113
Winson Chung45e1d6e2010-11-09 17:19:49 -08001114 // Skip touch handling if there are no pages to swipe
1115 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1116
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 /*
1118 * Shortcut the most recurring case: the user is in the dragging
1119 * state and he is moving his finger. We want to intercept this
1120 * motion.
1121 */
1122 final int action = ev.getAction();
1123 if ((action == MotionEvent.ACTION_MOVE) &&
1124 (mTouchState == TOUCH_STATE_SCROLLING)) {
1125 return true;
1126 }
1127
Winson Chung321e9ee2010-08-09 13:37:56 -07001128 switch (action & MotionEvent.ACTION_MASK) {
1129 case MotionEvent.ACTION_MOVE: {
1130 /*
1131 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1132 * whether the user has moved far enough from his original down touch.
1133 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001134 if (mActivePointerId != INVALID_POINTER) {
1135 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001136 }
1137 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1138 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1139 // i.e. fall through to the next case (don't break)
1140 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1141 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001142 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001143 }
1144
1145 case MotionEvent.ACTION_DOWN: {
1146 final float x = ev.getX();
1147 final float y = ev.getY();
1148 // Remember location of down touch
1149 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001150 mDownMotionY = y;
1151 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 mLastMotionX = x;
1153 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001154 float[] p = mapPointFromViewToParent(this, x, y);
1155 mParentDownMotionX = p[0];
1156 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001157 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001158 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001160
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 /*
1162 * If being flinged and user touches the screen, initiate drag;
1163 * otherwise don't. mScroller.isFinished should be false when
1164 * being flinged.
1165 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001166 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001167 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001168
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001169 if (finishedScrolling) {
1170 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001171 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001172 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001173 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001174 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001175 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001176 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1177 mTouchState = TOUCH_STATE_SCROLLING;
1178 } else {
1179 mTouchState = TOUCH_STATE_REST;
1180 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001181 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001182
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 break;
1184 }
1185
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001187 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001188 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 break;
1190
1191 case MotionEvent.ACTION_POINTER_UP:
1192 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001193 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 break;
1195 }
1196
1197 /*
1198 * The only time we want to intercept motion events is if we are in the
1199 * drag mode.
1200 */
1201 return mTouchState != TOUCH_STATE_REST;
1202 }
1203
Adam Cohenf8d28232011-02-01 21:47:00 -08001204 protected void determineScrollingStart(MotionEvent ev) {
1205 determineScrollingStart(ev, 1.0f);
1206 }
1207
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 /*
1209 * Determines if we should change the touch state to start scrolling after the
1210 * user moves their touch point too far.
1211 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001212 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001213 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001215 if (pointerIndex == -1) return;
1216
1217 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 final float x = ev.getX(pointerIndex);
1219 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001220 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1221
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001223
Adam Cohenf8d28232011-02-01 21:47:00 -08001224 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001226
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001227 if (xMoved) {
1228 // Scroll if the user moved far enough along the X axis
1229 mTouchState = TOUCH_STATE_SCROLLING;
1230 mTotalMotionX += Math.abs(mLastMotionX - x);
1231 mLastMotionX = x;
1232 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001233 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001234 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001235 // Stop listening for things like pinches.
1236 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001237 }
1238 }
1239
1240 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001241 // Try canceling the long press. It could also have been scheduled
1242 // by a distant descendant, so use the mAllowLongPress flag to block
1243 // everything
1244 final View currentPage = getPageAt(mCurrentPage);
1245 if (currentPage != null) {
1246 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 }
1248 }
1249
Adam Cohenb5ba0972011-09-07 18:02:31 -07001250 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001251 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001252
Adam Cohenedb40762013-07-18 16:45:45 -07001253 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001254 int count = getChildCount();
1255
1256 final int totalDistance;
1257
1258 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001259 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001260 adjacentPage = page - 1;
1261 }
1262
1263 if (adjacentPage < 0 || adjacentPage > count - 1) {
1264 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1265 } else {
1266 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1267 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001268
1269 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001270 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1271 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001272 return scrollProgress;
1273 }
1274
Adam Cohenedb40762013-07-18 16:45:45 -07001275 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001276 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001277 return 0;
1278 } else {
1279 return mPageScrolls[index];
1280 }
1281 }
1282
Adam Cohen564a2e72013-10-09 14:47:32 -07001283 // While layout transitions are occurring, a child's position may stray from its baseline
1284 // position. This method returns the magnitude of this stray at any given time.
1285 public int getLayoutTransitionOffsetForPage(int index) {
1286 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1287 return 0;
1288 } else {
1289 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001290
1291 int scrollOffset = 0;
1292 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1293 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001294 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001295 }
1296
Adam Cohen564a2e72013-10-09 14:47:32 -07001297 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1298 return (int) (child.getX() - baselineX);
1299 }
1300 }
1301
Adam Cohenb5ba0972011-09-07 18:02:31 -07001302 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001303 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001304
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001305 int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001306 if (amount < 0) {
1307 mOverScrollX = overScrollAmount;
1308 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001309 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001310 mOverScrollX = mMaxScrollX + overScrollAmount;
1311 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001312 }
1313 invalidate();
1314 }
1315
Adam Cohenb5ba0972011-09-07 18:02:31 -07001316 protected void overScroll(float amount) {
1317 dampedOverScroll(amount);
1318 }
1319
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001320 /**
1321 * return true if freescroll has been enabled, false otherwise
1322 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001323 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001324 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001325 }
1326
Sunny Goyalc99cb172017-10-19 16:15:09 -07001327 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001328 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001329 }
1330
Adam Cohenf9618852013-11-08 06:45:03 -08001331 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001332 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001333 mFreeScroll = freeScroll;
1334
Adam Cohenf9618852013-11-08 06:45:03 -08001335 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001336 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001337 } else if (wasFreeScroll) {
1338 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001339 }
1340
1341 setEnableOverscroll(!freeScroll);
1342 }
1343
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001344 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001345 mAllowOverScroll = enable;
1346 }
1347
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001348 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001349 if (mDragView != null) {
1350 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1351 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001352 int minDistance = Integer.MAX_VALUE;
1353 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001354 int maxPageNo = getChildCount() - 1;
1355 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001356 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001357 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001358 int d = Math.abs(dragX - pageX);
1359 if (d < minDistance) {
1360 minIndex = i;
1361 minDistance = d;
1362 }
1363 }
1364 return minIndex;
1365 }
1366 return -1;
1367 }
1368
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 @Override
1370 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001371 super.onTouchEvent(ev);
1372
Winson Chung45e1d6e2010-11-09 17:19:49 -08001373 // Skip touch handling if there are no pages to swipe
1374 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1375
Michael Jurkab8f06722010-10-10 15:58:46 -07001376 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001377
1378 final int action = ev.getAction();
1379
1380 switch (action & MotionEvent.ACTION_MASK) {
1381 case MotionEvent.ACTION_DOWN:
1382 /*
1383 * If being flinged and user touches, stop the fling. isFinished
1384 * will be false if being flinged.
1385 */
1386 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001387 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 }
1389
1390 // Remember where the motion event started
1391 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001392 mDownMotionY = mLastMotionY = ev.getY();
1393 mDownScrollX = getScrollX();
1394 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1395 mParentDownMotionX = p[0];
1396 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001397 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001398 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001400
Michael Jurka0142d492010-08-25 17:46:15 -07001401 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001402 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001403 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001404 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 break;
1406
1407 case MotionEvent.ACTION_MOVE:
1408 if (mTouchState == TOUCH_STATE_SCROLLING) {
1409 // Scroll to follow the motion event
1410 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001411
1412 if (pointerIndex == -1) return true;
1413
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001415 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001416
Adam Cohenaefd4e12011-02-14 16:39:38 -08001417 mTotalMotionX += Math.abs(deltaX);
1418
Winson Chungc0844aa2011-02-02 15:25:58 -08001419 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1420 // keep the remainder because we are actually testing if we've moved from the last
1421 // scrolled position (which is discrete).
1422 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001423 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001424 mLastMotionX = x;
1425 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 } else {
1427 awakenScrollBars();
1428 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001429 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1430 // Update the last motion position
1431 mLastMotionX = ev.getX();
1432 mLastMotionY = ev.getY();
1433
1434 // Update the parent down so that our zoom animations take this new movement into
1435 // account
1436 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1437 mParentDownMotionX = pt[0];
1438 mParentDownMotionY = pt[1];
1439 updateDragViewTranslationDuringDrag();
1440
1441 // Find the closest page to the touch point
1442 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001443
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1445 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1446 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1447 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1448
Adam Cohenf358a4b2013-07-23 16:47:31 -07001449 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001450 // Do not allow any page to be moved to 0th position.
1451 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001452 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001453 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1454 mSidePageHoverIndex = pageUnderPointIndex;
1455 mSidePageHoverRunnable = new Runnable() {
1456 @Override
1457 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001458 // Setup the scroll to the correct page before we swap the views
1459 snapToPage(pageUnderPointIndex);
1460
1461 // For each of the pages between the paged view and the drag view,
1462 // animate them from the previous position to the new position in
1463 // the layout (as a result of the drag view moving in the layout)
1464 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1465 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1466 dragViewIndex + 1 : pageUnderPointIndex;
1467 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1468 dragViewIndex - 1 : pageUnderPointIndex;
1469 for (int i = lowerIndex; i <= upperIndex; ++i) {
1470 View v = getChildAt(i);
1471 // dragViewIndex < pageUnderPointIndex, so after we remove the
1472 // drag view all subsequent views to pageUnderPointIndex will
1473 // shift down.
1474 int oldX = getViewportOffsetX() + getChildOffset(i);
1475 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1476
1477 // Animate the view translation from its old position to its new
1478 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001479 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001480 if (anim != null) {
1481 anim.cancel();
1482 }
1483
1484 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001485 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001486 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001487 anim.start();
1488 v.setTag(anim);
1489 }
1490
1491 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001492 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001493 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001494 if (mPageIndicator != null) {
1495 mPageIndicator.setActiveMarker(getNextPage());
1496 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001497 }
1498 };
1499 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1500 }
1501 } else {
1502 removeCallbacks(mSidePageHoverRunnable);
1503 mSidePageHoverIndex = -1;
1504 }
Adam Cohen564976a2010-10-13 18:52:07 -07001505 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 determineScrollingStart(ev);
1507 }
1508 break;
1509
1510 case MotionEvent.ACTION_UP:
1511 if (mTouchState == TOUCH_STATE_SCROLLING) {
1512 final int activePointerId = mActivePointerId;
1513 final int pointerIndex = ev.findPointerIndex(activePointerId);
1514 final float x = ev.getX(pointerIndex);
1515 final VelocityTracker velocityTracker = mVelocityTracker;
1516 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1517 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001518 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001519 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001520 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1521 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001522
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001523 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1524
Adam Cohen00481b32011-11-18 12:03:48 -08001525 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001526 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001527
Adam Cohenf358a4b2013-07-23 16:47:31 -07001528 if (!mFreeScroll) {
1529 // In the case that the page is moved far to one direction and then is flung
1530 // in the opposite direction, we use a threshold to determine whether we should
1531 // just return to the starting page, or if we should skip one further.
1532 boolean returnToOriginalPage = false;
1533 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1534 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1535 returnToOriginalPage = true;
1536 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001537
Adam Cohenf358a4b2013-07-23 16:47:31 -07001538 int finalPage;
1539 // We give flings precedence over large moves, which is why we short-circuit our
1540 // test for a large move if a fling has been registered. That is, a large
1541 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001542 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1543 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001544 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1545 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1546 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1547 snapToPageWithVelocity(finalPage, velocityX);
1548 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1549 (isFling && isVelocityXLeft)) &&
1550 mCurrentPage < getChildCount() - 1) {
1551 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1552 snapToPageWithVelocity(finalPage, velocityX);
1553 } else {
1554 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001555 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001556 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001557 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001558 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001559 }
1560
1561 float scaleX = getScaleX();
1562 int vX = (int) (-velocityX * scaleX);
1563 int initialScrollX = (int) (getScrollX() * scaleX);
1564
Adam Cohenf9618852013-11-08 06:45:03 -08001565 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001566 mScroller.fling(initialScrollX,
1567 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001568 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001569 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001571 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001572 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1573 // at this point we have not moved beyond the touch slop
1574 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1575 // we can just page
1576 int nextPage = Math.max(0, mCurrentPage - 1);
1577 if (nextPage != mCurrentPage) {
1578 snapToPage(nextPage);
1579 } else {
1580 snapToDestination();
1581 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001582 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001583 // at this point we have not moved beyond the touch slop
1584 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1585 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001586 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1587 if (nextPage != mCurrentPage) {
1588 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 } else {
1590 snapToDestination();
1591 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001592 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1593 // Update the last motion position
1594 mLastMotionX = ev.getX();
1595 mLastMotionY = ev.getY();
1596
1597 // Update the parent down so that our zoom animations take this new movement into
1598 // account
1599 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1600 mParentDownMotionX = pt[0];
1601 mParentDownMotionY = pt[1];
1602 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001603 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001604 if (!mCancelTap) {
1605 onUnhandledTap(ev);
1606 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001608
1609 // Remove the callback to wait for the side page hover timeout
1610 removeCallbacks(mSidePageHoverRunnable);
1611 // End any intermediate reordering states
1612 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001613 break;
1614
1615 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001616 if (mTouchState == TOUCH_STATE_SCROLLING) {
1617 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001618 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001619 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001620 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001621 break;
1622
1623 case MotionEvent.ACTION_POINTER_UP:
1624 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001625 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001626 break;
1627 }
1628
1629 return true;
1630 }
1631
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001632 protected boolean shouldFlingForVelocity(int velocityX) {
1633 return Math.abs(velocityX) > mFlingThresholdVelocity;
1634 }
1635
Adam Cohen7d30a372013-07-01 17:03:59 -07001636 private void resetTouchState() {
1637 releaseVelocityTracker();
1638 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001639 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001640 mTouchState = TOUCH_STATE_REST;
1641 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001642 }
1643
Adam Cohenc2d6e892014-10-16 09:49:24 -07001644 /**
1645 * Triggered by scrolling via touch
1646 */
1647 protected void onScrollInteractionBegin() {
1648 }
1649
1650 protected void onScrollInteractionEnd() {
1651 }
1652
Adam Cohen1697b792013-09-17 19:08:21 -07001653 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001654 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001655 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001656
Winson Chung185d7162011-02-28 13:47:29 -08001657 @Override
1658 public boolean onGenericMotionEvent(MotionEvent event) {
1659 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1660 switch (event.getAction()) {
1661 case MotionEvent.ACTION_SCROLL: {
1662 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1663 final float vscroll;
1664 final float hscroll;
1665 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1666 vscroll = 0;
1667 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1668 } else {
1669 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1670 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1671 }
1672 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001673 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001674 : (hscroll > 0 || vscroll > 0);
1675 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001676 scrollRight();
1677 } else {
1678 scrollLeft();
1679 }
1680 return true;
1681 }
1682 }
1683 }
1684 }
1685 return super.onGenericMotionEvent(event);
1686 }
1687
Michael Jurkab8f06722010-10-10 15:58:46 -07001688 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1689 if (mVelocityTracker == null) {
1690 mVelocityTracker = VelocityTracker.obtain();
1691 }
1692 mVelocityTracker.addMovement(ev);
1693 }
1694
1695 private void releaseVelocityTracker() {
1696 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001697 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001698 mVelocityTracker.recycle();
1699 mVelocityTracker = null;
1700 }
1701 }
1702
Winson Chung321e9ee2010-08-09 13:37:56 -07001703 private void onSecondaryPointerUp(MotionEvent ev) {
1704 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1705 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1706 final int pointerId = ev.getPointerId(pointerIndex);
1707 if (pointerId == mActivePointerId) {
1708 // This was our active pointer going up. Choose a new
1709 // active pointer and adjust accordingly.
1710 // TODO: Make this decision more intelligent.
1711 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1712 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1713 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001714 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001715 mActivePointerId = ev.getPointerId(newPointerIndex);
1716 if (mVelocityTracker != null) {
1717 mVelocityTracker.clear();
1718 }
1719 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001720 }
1721
Winson Chung321e9ee2010-08-09 13:37:56 -07001722 @Override
1723 public void requestChildFocus(View child, View focused) {
1724 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001725 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001726 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001727 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001728 }
1729 }
1730
Adam Cohend19d3ca2010-09-15 14:43:42 -07001731 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001732 return getPageNearestToCenterOfScreen(getScrollX());
1733 }
1734
1735 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1736 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001737 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001738 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001739 final int childCount = getChildCount();
1740 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001741 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001742 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001743 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001744 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001745 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1746 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1747 minDistanceFromScreenCenter = distanceFromScreenCenter;
1748 minDistanceFromScreenCenterIndex = i;
1749 }
1750 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001751 return minDistanceFromScreenCenterIndex;
1752 }
1753
1754 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001755 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1756 }
1757
1758 protected boolean isInOverScroll() {
1759 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1760 }
1761
1762 protected int getPageSnapDuration() {
1763 if (isInOverScroll()) {
1764 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1765 }
1766 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001767 }
1768
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001769 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001770 public ScrollInterpolator() {
1771 }
1772
1773 public float getInterpolation(float t) {
1774 t -= 1.0f;
1775 return t*t*t*t*t + 1;
1776 }
1777 }
1778
1779 // We want the duration of the page snap animation to be influenced by the distance that
1780 // the screen has to travel, however, we don't want this duration to be effected in a
1781 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1782 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001783 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001784 f -= 0.5f; // center the values about 0.
1785 f *= 0.3f * Math.PI / 2.0f;
1786 return (float) Math.sin(f);
1787 }
1788
Michael Jurka0142d492010-08-25 17:46:15 -07001789 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001790 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001791 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001792
Adam Cohenedb40762013-07-18 16:45:45 -07001793 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001794 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001795 int duration = 0;
1796
Sunny Goyal4d113a52015-05-27 10:05:28 -07001797 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001798 // If the velocity is low enough, then treat this more as an automatic page advance
1799 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001800 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001801 return;
1802 }
1803
1804 // Here we compute a "distance" that will be used in the computation of the overall
1805 // snap duration. This is a function of the actual distance that needs to be traveled;
1806 // we keep this value close to half screen size in order to reduce the variance in snap
1807 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001808 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001809 float distance = halfScreenSize + halfScreenSize *
1810 distanceInfluenceForSnapDuration(distanceRatio);
1811
1812 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001813 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001814
1815 // we want the page's snap velocity to approximately match the velocity at which the
1816 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001817 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1818 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001819
1820 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001821 }
1822
Sunny Goyal1740d902015-05-27 11:14:01 -07001823 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001824 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001825 }
1826
Adam Cohenf9c184a2016-01-15 16:47:43 -08001827 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001828 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 }
1830
Michael Jurka0142d492010-08-25 17:46:15 -07001831 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001832 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001833 }
1834
Adam Cohenf9618852013-11-08 06:45:03 -08001835 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1836 snapToPage(whichPage, duration, false, interpolator);
1837 }
1838
1839 protected void snapToPage(int whichPage, int duration, boolean immediate,
1840 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001841 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001842
Adam Cohenedb40762013-07-18 16:45:45 -07001843 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001844 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001845 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001846 }
1847
1848 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001849 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001850 }
Michael Jurka0142d492010-08-25 17:46:15 -07001851
Adam Cohenf9618852013-11-08 06:45:03 -08001852 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1853 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001854 whichPage = validateNewPage(whichPage);
1855
Adam Cohen7d30a372013-07-01 17:03:59 -07001856 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001857
Winson Chung321e9ee2010-08-09 13:37:56 -07001858 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001859 if (immediate) {
1860 duration = 0;
1861 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001862 duration = Math.abs(delta);
1863 }
1864
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001865 if (duration != 0) {
1866 pageBeginTransition();
1867 }
1868
Adam Cohenf358a4b2013-07-23 16:47:31 -07001869 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001870 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001871 }
Adam Cohenf9618852013-11-08 06:45:03 -08001872
1873 if (interpolator != null) {
1874 mScroller.setInterpolator(interpolator);
1875 } else {
1876 mScroller.setInterpolator(mDefaultInterpolator);
1877 }
1878
Sunny Goyalc86df472016-02-25 09:19:38 -08001879 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001880
Adam Cohen674531f2013-12-13 15:07:14 -08001881 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001882
1883 // Trigger a compute() to finish switching pages if necessary
1884 if (immediate) {
1885 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001886 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001887 }
1888
Winson Chung321e9ee2010-08-09 13:37:56 -07001889 invalidate();
1890 }
1891
Winson Chung321e9ee2010-08-09 13:37:56 -07001892 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001893 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001894 }
1895
1896 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001897 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001898 }
1899
Adam Cohendbdff6b2013-09-18 19:09:15 -07001900 @Override
1901 public boolean performLongClick() {
1902 mCancelTap = true;
1903 return super.performLongClick();
1904 }
1905
Winson Chung321e9ee2010-08-09 13:37:56 -07001906 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001907 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001908
1909 SavedState(Parcelable superState) {
1910 super(superState);
1911 }
1912
Adam Cohen091440a2015-03-18 14:16:05 -07001913 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001914 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001915 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001916 }
1917
1918 @Override
1919 public void writeToParcel(Parcel out, int flags) {
1920 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001921 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001922 }
1923
1924 public static final Parcelable.Creator<SavedState> CREATOR =
1925 new Parcelable.Creator<SavedState>() {
1926 public SavedState createFromParcel(Parcel in) {
1927 return new SavedState(in);
1928 }
1929
1930 public SavedState[] newArray(int size) {
1931 return new SavedState[size];
1932 }
1933 };
1934 }
1935
Adam Cohen7d30a372013-07-01 17:03:59 -07001936 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001937 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001938 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001939 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1940 new PropertyListBuilder()
1941 .scale(1)
1942 .translationX(0)
1943 .translationY(0)
1944 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001945 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001946 anim.addListener(new AnimatorListenerAdapter() {
1947 @Override
1948 public void onAnimationEnd(Animator animation) {
1949 onPostReorderingAnimationCompleted();
1950 }
1951 });
1952 anim.start();
1953 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001954 }
1955
Sunny Goyal1d08f702015-05-04 15:50:25 -07001956 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001957 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1958 mTouchState = TOUCH_STATE_REORDERING;
1959 mIsReordering = true;
1960
Adam Cohen7d30a372013-07-01 17:03:59 -07001961 // We must invalidate to trigger a redraw to update the layers such that the drag view
1962 // is always drawn on top
1963 invalidate();
1964 }
1965
Adam Cohen091440a2015-03-18 14:16:05 -07001966 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001967 // Trigger the callback when reordering has settled
1968 --mPostReorderingPreZoomInRemainingAnimationCount;
1969 if (mPostReorderingPreZoomInRunnable != null &&
1970 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1971 mPostReorderingPreZoomInRunnable.run();
1972 mPostReorderingPreZoomInRunnable = null;
1973 }
1974 }
1975
Sunny Goyal1d08f702015-05-04 15:50:25 -07001976 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001977 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001978 }
1979
Adam Cohenf358a4b2013-07-23 16:47:31 -07001980 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001981 int dragViewIndex = indexOfChild(v);
1982
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001983 // Do not allow the first page to be moved around
1984 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001985
Adam Cohen7d30a372013-07-01 17:03:59 -07001986 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001987 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001988 // Find the drag view under the pointer
1989 mDragView = getChildAt(dragViewIndex);
1990 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1991 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001992 mReorderingStarted = true;
1993
Adam Cohenf9618852013-11-08 06:45:03 -08001994 snapToPage(getPageNearestToCenterOfScreen());
1995 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001996 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001997 return true;
1998 }
1999 return false;
2000 }
2001
2002 boolean isReordering(boolean testTouchState) {
2003 boolean state = mIsReordering;
2004 if (testTouchState) {
2005 state &= (mTouchState == TOUCH_STATE_REORDERING);
2006 }
2007 return state;
2008 }
2009 void endReordering() {
2010 // For simplicity, we call endReordering sometimes even if reordering was never started.
2011 // In that case, we don't want to do anything.
2012 if (!mReorderingStarted) return;
2013 mReorderingStarted = false;
2014
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002015 mPostReorderingPreZoomInRunnable = new Runnable() {
2016 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002017 // If we haven't flung-to-delete the current child,
2018 // then we just animate the drag view back into position
2019 onEndReordering();
2020
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002021 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002022 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002023 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002024
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002025 mPostReorderingPreZoomInRemainingAnimationCount =
2026 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2027 // Snap to the current page
2028 snapToPage(indexOfChild(mDragView), 0);
2029 // Animate the drag view back to the front position
2030 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002031 }
2032
Winson Chung6a0f57d2011-06-29 20:10:49 -07002033 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002034 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002035 @Override
2036 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2037 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002038 info.setScrollable(getPageCount() > 1);
2039 if (getCurrentPage() < getPageCount() - 1) {
2040 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2041 }
2042 if (getCurrentPage() > 0) {
2043 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2044 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002045 info.setClassName(getClass().getName());
2046
2047 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2048 // Besides disabling the accessibility long-click, this also prevents this view from getting
2049 // accessibility focus.
2050 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002051 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002052 }
2053
2054 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002055 public void sendAccessibilityEvent(int eventType) {
2056 // Don't let the view send real scroll events.
2057 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2058 super.sendAccessibilityEvent(eventType);
2059 }
2060 }
2061
2062 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002063 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2064 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002065 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002066 }
2067
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002068 @Override
2069 public boolean performAccessibilityAction(int action, Bundle arguments) {
2070 if (super.performAccessibilityAction(action, arguments)) {
2071 return true;
2072 }
2073 switch (action) {
2074 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2075 if (getCurrentPage() < getPageCount() - 1) {
2076 scrollRight();
2077 return true;
2078 }
2079 } break;
2080 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2081 if (getCurrentPage() > 0) {
2082 scrollLeft();
2083 return true;
2084 }
2085 } break;
2086 }
2087 return false;
2088 }
2089
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002090 protected String getPageIndicatorDescription() {
2091 return getCurrentPageDescription();
2092 }
2093
Adam Cohen0ffac432013-07-10 11:19:26 -07002094 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002095 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002096 getNextPage() + 1, getChildCount());
2097 }
2098
Winson Chungd11265e2011-08-30 13:37:23 -07002099 @Override
2100 public boolean onHoverEvent(android.view.MotionEvent event) {
2101 return true;
2102 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002103}