blob: 4240a30b4ffb4910c7cc7f1d5483e551ef17546f [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 Goyal5bc6b6f2017-10-26 15:36:10 -070049import com.android.launcher3.anim.Interpolators;
Sunny Goyal9e76f682017-02-13 12:13:43 -080050import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070052import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070054
Winson Chung6a0f57d2011-06-29 20:10:49 -070055import java.util.ArrayList;
56
Winson Chung321e9ee2010-08-09 13:37:56 -070057/**
58 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070059 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070060 */
Michael Jurka8b805b12012-04-18 14:23:14 -070061public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070062 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070063 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Winson Chung86f77532010-08-24 11:08:22 -070066 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070067 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Vadim Tryshevfedca432015-08-19 17:55:02 -070069 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070070 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070071
Sunny Goyalb72d8b22017-07-14 00:02:27 -070072 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070073 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070074
Adam Cohenb64cb5a2011-02-15 13:53:42 -080075 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080076 // The page is moved more than halfway, automatically move to the next page on touch up.
77 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080078
Sunny Goyal8e2133b2015-05-06 13:39:07 -070079 private static final float MAX_SCROLL_PROGRESS = 1.0f;
80
Adam Cohen265b9a62011-12-07 14:37:18 -080081 // The following constants need to be scaled based on density. The scaled versions will be
82 // assigned to the corresponding member variables below.
83 private static final int FLING_THRESHOLD_VELOCITY = 500;
84 private static final int MIN_SNAP_VELOCITY = 1500;
85 private static final int MIN_FLING_VELOCITY = 250;
86
Adam Cohen21cd0022013-10-09 18:57:02 -070087 public static final int INVALID_RESTORE_PAGE = -1001;
88
Adam Cohenf358a4b2013-07-23 16:47:31 -070089 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070090
Adam Cohen265b9a62011-12-07 14:37:18 -080091 protected int mFlingThresholdVelocity;
92 protected int mMinFlingVelocity;
93 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070096 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070097
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700100 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700101
Sunny Goyal4ffec482016-02-09 11:28:52 -0800102 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800104 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800105 protected LauncherScroller mScroller;
106 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Adam Cohen7d30a372013-07-01 17:03:59 -0700110 private float mParentDownMotionX;
111 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700113 private float mDownMotionY;
114 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700115 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700116 private float mLastMotionX;
117 private float mLastMotionXRemainder;
118 private float mLastMotionY;
119 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700120
Adam Cohendbdff6b2013-09-18 19:09:15 -0700121 private boolean mCancelTap;
122
Adam Cohenedb40762013-07-18 16:45:45 -0700123 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected final static int TOUCH_STATE_REST = 0;
126 protected final static int TOUCH_STATE_SCROLLING = 1;
127 protected final static int TOUCH_STATE_PREV_PAGE = 2;
128 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 protected final static int TOUCH_STATE_REORDERING = 4;
130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700143 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700144
Sunny Goyal061380a2016-02-29 15:15:03 -0800145 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700146
Adam Cohen8d769d62017-06-23 17:27:38 -0700147 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
148 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
149 // the screens from continuing to translate beyond the normal bounds.
150 protected int mOverScrollX;
151
152 protected int mUnboundedScrollX;
153
Winson Chungd2be3812013-07-16 11:11:32 -0700154 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700156 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700157 // The viewport whether the pages are to be contained (the actual view may be larger than the
158 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800159 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700160 private Rect mViewport = new Rect();
161
162 // Reordering
163 // We use the min scale to determine how much to expand the actually PagedView measured
164 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700165 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700166 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700167 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
168
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700170 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700171 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700173 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700174 // This variable's scope is only for the duration of startReordering() and endReordering()
175 private boolean mReorderingStarted = false;
176 // This variable's scope is for the duration of startReordering() and after the zoomIn()
177 // animation after endReordering()
178 private boolean mIsReordering;
179 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700180 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 private int mPostReorderingPreZoomInRemainingAnimationCount;
182 private Runnable mPostReorderingPreZoomInRunnable;
183
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700185 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700186 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700187 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700188
John Spurlock77e1f472013-09-11 10:09:51 -0400189 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700190 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400191
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 public PagedView(Context context) {
193 this(context, null);
194 }
195
196 public PagedView(Context context, AttributeSet attrs) {
197 this(context, attrs, 0);
198 }
199
200 public PagedView(Context context, AttributeSet attrs, int defStyle) {
201 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700202
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 TypedArray a = context.obtainStyledAttributes(attrs,
204 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700205 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 a.recycle();
207
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700209 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700210 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 }
212
213 /**
214 * Initializes various states for this workspace.
215 */
Michael Jurka0142d492010-08-25 17:46:15 -0700216 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800217 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700218 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700219 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700220
221 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700222 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800224
Sunny Goyalcf25b522015-07-09 00:01:18 -0700225 float density = getResources().getDisplayMetrics().density;
226 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
227 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
228 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700229 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700230 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 }
232
Adam Cohenf9618852013-11-08 06:45:03 -0800233 protected void setDefaultInterpolator(Interpolator interpolator) {
234 mDefaultInterpolator = interpolator;
235 mScroller.setInterpolator(mDefaultInterpolator);
236 }
237
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700238 public void initParentViews(View parent) {
239 if (mPageIndicatorViewId > -1) {
240 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700241 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700242 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700243 }
244 }
245
Adam Cohen7d30a372013-07-01 17:03:59 -0700246 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700247 private float[] mapPointFromViewToParent(View v, float x, float y) {
248 sTmpPoint[0] = x;
249 sTmpPoint[1] = y;
250 v.getMatrix().mapPoints(sTmpPoint);
251 sTmpPoint[0] += v.getLeft();
252 sTmpPoint[1] += v.getTop();
253 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700254 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700255 private float[] mapPointFromParentToView(View v, float x, float y) {
256 sTmpPoint[0] = x - v.getLeft();
257 sTmpPoint[1] = y - v.getTop();
258 v.getMatrix().invert(sTmpInvMatrix);
259 sTmpInvMatrix.mapPoints(sTmpPoint);
260 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700261 }
262
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700263 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264 if (mDragView != null) {
265 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
266 (mDragViewBaselineLeft - mDragView.getLeft());
267 float y = mLastMotionY - mDownMotionY;
268 mDragView.setTranslationX(x);
269 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700270
Adam Cohenf358a4b2013-07-23 16:47:31 -0700271 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
272 + x + ", " + y);
273 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700274 }
275
276 public void setMinScale(float f) {
277 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700278 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700279 requestLayout();
280 }
281
282 @Override
283 public void setScaleX(float scaleX) {
284 super.setScaleX(scaleX);
285 if (isReordering(true)) {
286 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
287 mLastMotionX = p[0];
288 mLastMotionY = p[1];
289 updateDragViewTranslationDuringDrag();
290 }
291 }
292
293 // Convenience methods to get the actual width/height of the PagedView (since it is measured
294 // to be larger to account for the minimum possible scale)
295 int getViewportWidth() {
296 return mViewport.width();
297 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800298 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700299 return mViewport.height();
300 }
301
302 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
303 // PagedView both horizontally and vertically
304 int getViewportOffsetX() {
305 return (getMeasuredWidth() - getViewportWidth()) / 2;
306 }
307
308 int getViewportOffsetY() {
309 return (getMeasuredHeight() - getViewportHeight()) / 2;
310 }
311
Hyunyoung Song77441692016-06-27 22:00:48 -0700312 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700313 return mPageIndicator;
314 }
315
Adam Cohen674531f2013-12-13 15:07:14 -0800316 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700317 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
318 * that the user was on before entering free scroll mode (e.g. the home screen page they
319 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
320 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700322 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700323 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700325
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700326 /**
327 * Returns the index of page to be shown immediately afterwards.
328 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700329 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700330 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
331 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700332
Adam Cohenf9c184a2016-01-15 16:47:43 -0800333 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 return getChildCount();
335 }
336
Sunny Goyal83a8f042015-05-19 12:52:12 -0700337 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 return getChildAt(index);
339 }
340
Adam Cohenae4f1552011-10-20 00:15:42 -0700341 protected int indexToPage(int index) {
342 return index;
343 }
344
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800346 * Updates the scroll of the current page immediately to its final scroll position. We use this
347 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
348 * the previous tab page.
349 */
350 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700351 // If the current page is invalid, just reset the scroll position to zero
352 int newX = 0;
353 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700354 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700355 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800356 scrollTo(newX, 0);
357 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700358 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800359 }
360
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700361 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700362 mScroller.abortAnimation();
363 // We need to clean up the next page here to avoid computeScrollHelper from
364 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700365 if (resetNextPage) {
366 mNextPage = INVALID_PAGE;
367 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700368 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700369
Tony Wickham8f7ead32016-04-07 18:46:44 -0700370 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700371 mScroller.forceFinished(true);
372 // We need to clean up the next page here to avoid computeScrollHelper from
373 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700374 if (resetNextPage) {
375 mNextPage = INVALID_PAGE;
376 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700377 }
378
Adam Cohen6f127a62014-06-12 14:54:41 -0700379 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700380 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700381 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700382 }
383
Chet Haasebc2f0822012-10-26 17:59:53 -0700384 /**
Winson Chung86f77532010-08-24 11:08:22 -0700385 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700387 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800388 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700389 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800390 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800391 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
392 // the default
393 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800394 return;
395 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700396 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700397 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700398 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700399 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800400 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 }
402
Winson Chung8c87cd82013-07-23 16:20:10 -0700403 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800404 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
405 * has settled.
406 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700407 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800408 updatePageIndicator();
409 }
410
411 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700412 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700413 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700414 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700415 if (!isReordering(false)) {
416 mPageIndicator.setActiveMarker(getNextPage());
417 }
Winson Chungd2be3812013-07-16 11:11:32 -0700418 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700420 protected void pageBeginTransition() {
421 if (!mIsPageInTransition) {
422 mIsPageInTransition = true;
423 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700424 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700425 }
426
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700427 protected void pageEndTransition() {
428 if (mIsPageInTransition) {
429 mIsPageInTransition = false;
430 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700431 }
Michael Jurka0142d492010-08-25 17:46:15 -0700432 }
433
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700434 protected boolean isPageInTransition() {
435 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700436 }
437
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700438 /**
439 * Called when the page starts moving as part of the scroll. Subclasses can override this
440 * to provide custom behavior during animation.
441 */
442 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700443 }
444
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700445 /**
446 * Called when the page ends moving as part of the scroll. Subclasses can override this
447 * to provide custom behavior during animation.
448 */
449 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700450 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700451 }
452
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 /**
Winson Chung86f77532010-08-24 11:08:22 -0700454 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 *
456 * @param l The listener used to respond to long clicks.
457 */
458 @Override
459 public void setOnLongClickListener(OnLongClickListener l) {
460 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700461 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700463 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 }
Adam Cohen1697b792013-09-17 19:08:21 -0700465 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 }
467
Sunny Goyalc86df472016-02-25 09:19:38 -0800468 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700469 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800470 }
471
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800473 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800474 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800475 }
476
477 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700478 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700479 // In free scroll mode, we clamp the scrollX
480 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700481 // If the scroller is trying to move to a location beyond the maximum allowed
482 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700483 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700484 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700485 }
486
Sunny Goyal4d519f22017-10-24 10:32:40 -0700487 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700488 }
489
Adam Cohen8d769d62017-06-23 17:27:38 -0700490 mUnboundedScrollX = x;
491
Sunny Goyal70660032015-05-14 00:07:08 -0700492 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
493 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700494 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700495 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800496 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700498 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700499 overScroll(x - mMaxScrollX);
500 } else {
501 overScroll(x);
502 }
Adam Cohen68d73932010-11-15 10:50:58 -0800503 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700504 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700505 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800506 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700507 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700508 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700509 overScroll(x);
510 } else {
511 overScroll(x - mMaxScrollX);
512 }
Adam Cohen68d73932010-11-15 10:50:58 -0800513 }
514 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700515 if (mWasInOverscroll) {
516 overScroll(0);
517 mWasInOverscroll = false;
518 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700519 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800520 super.scrollTo(x, y);
521 }
522
Adam Cohen7d30a372013-07-01 17:03:59 -0700523 // Update the last motion events when scrolling
524 if (isReordering(true)) {
525 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
526 mLastMotionX = p[0];
527 mLastMotionY = p[1];
528 updateDragViewTranslationDuringDrag();
529 }
Michael Jurka0142d492010-08-25 17:46:15 -0700530 }
531
Adam Cohen53805212013-10-01 10:39:23 -0700532 private void sendScrollAccessibilityEvent() {
533 AccessibilityManager am =
534 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
535 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700536 if (mCurrentPage != getNextPage()) {
537 AccessibilityEvent ev =
538 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700539 ev.setScrollable(true);
540 ev.setScrollX(getScrollX());
541 ev.setScrollY(getScrollY());
542 ev.setMaxScrollX(mMaxScrollX);
543 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700544
Vadim Tryshevf4715972015-05-08 17:21:03 -0700545 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700546 }
Adam Cohen53805212013-10-01 10:39:23 -0700547 }
548 }
549
Michael Jurka0142d492010-08-25 17:46:15 -0700550 // we moved this functionality to a helper function so SmoothPagedView can reuse it
551 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800552 return computeScrollHelper(true);
553 }
554
555 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700557 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800558 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700559 || getScrollY() != mScroller.getCurrY()
560 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700561 float scaleX = mFreeScroll ? getScaleX() : 1f;
562 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
563 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700564 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800565 if (shouldInvalidate) {
566 invalidate();
567 }
Michael Jurka0142d492010-08-25 17:46:15 -0700568 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800569 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700570 sendScrollAccessibilityEvent();
571
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700572 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700573 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700574 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700575 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700576
Adam Cohen73aa9752010-11-24 16:26:58 -0800577 // We don't want to trigger a page end moving unless the page has settled
578 // and the user has stopped scrolling
579 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700580 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800581 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700582
Adam Cohen7d30a372013-07-01 17:03:59 -0700583 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700584 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700585 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700586 if (am.isEnabled()) {
587 // Notify the user when the page changes
588 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700589 }
Michael Jurka0142d492010-08-25 17:46:15 -0700590 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700591 }
Michael Jurka0142d492010-08-25 17:46:15 -0700592 return false;
593 }
594
595 @Override
596 public void computeScroll() {
597 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
599
Adam Cohen96d30a12013-07-16 18:13:21 -0700600 public static class LayoutParams extends ViewGroup.LayoutParams {
601 public boolean isFullScreenPage = false;
602
Sunny Goyal7c786f72016-06-01 14:08:21 -0700603 // If true, the start edge of the page snaps to the start edge of the viewport.
604 public boolean matchStartEdge = false;
605
Adam Cohen96d30a12013-07-16 18:13:21 -0700606 /**
607 * {@inheritDoc}
608 */
609 public LayoutParams(int width, int height) {
610 super(width, height);
611 }
612
Sunny Goyal41b22c02015-05-14 15:20:48 -0700613 public LayoutParams(Context context, AttributeSet attrs) {
614 super(context, attrs);
615 }
616
Adam Cohen96d30a12013-07-16 18:13:21 -0700617 public LayoutParams(ViewGroup.LayoutParams source) {
618 super(source);
619 }
620 }
621
Sunny Goyal41b22c02015-05-14 15:20:48 -0700622 @Override
623 public LayoutParams generateLayoutParams(AttributeSet attrs) {
624 return new LayoutParams(getContext(), attrs);
625 }
626
627 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700628 protected LayoutParams generateDefaultLayoutParams() {
629 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
630 }
631
Sunny Goyal41b22c02015-05-14 15:20:48 -0700632 @Override
633 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
634 return new LayoutParams(p);
635 }
636
637 @Override
638 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
639 return p instanceof LayoutParams;
640 }
641
Adam Cohenedb40762013-07-18 16:45:45 -0700642 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700643 LayoutParams lp = generateDefaultLayoutParams();
644 lp.isFullScreenPage = true;
645 super.addView(page, 0, lp);
646 }
647
Adam Cohen410f3cd2013-09-22 12:09:32 -0700648 public int getNormalChildHeight() {
649 return mNormalChildHeight;
650 }
651
Winson Chung321e9ee2010-08-09 13:37:56 -0700652 @Override
653 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700654 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700655 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
656 return;
657 }
658
Adam Cohen7d30a372013-07-01 17:03:59 -0700659 // We measure the dimensions of the PagedView to be larger than the pages so that when we
660 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700661 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
662 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
663 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700664 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800665 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700666 // viewport, we can be at most one and a half screens offset once we scale down
667 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700668 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
669 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700670
Winson Chungc82d2622013-11-06 13:23:29 -0800671 int parentWidthSize = (int) (2f * maxSize);
672 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700673 int scaledWidthSize, scaledHeightSize;
674 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700675 scaledWidthSize = (int) (parentWidthSize / mMinScale);
676 scaledHeightSize = (int) (parentHeightSize / mMinScale);
677 } else {
678 scaledWidthSize = widthSize;
679 scaledHeightSize = heightSize;
680 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700681 mViewport.set(0, 0, widthSize, heightSize);
682
683 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
684 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
685 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 }
687
Winson Chung8aad6102012-05-11 16:27:49 -0700688 // Return early if we aren't given a proper dimension
689 if (widthSize <= 0 || heightSize <= 0) {
690 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
691 return;
692 }
693
Adam Lesinski6b879f02010-11-04 16:15:23 -0700694 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
695 * of the All apps view on XLarge displays to not take up more space then it needs. Width
696 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
697 * each page to have the same width.
698 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 final int verticalPadding = getPaddingTop() + getPaddingBottom();
700 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700701
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700702 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700704 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700706 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
707 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
708 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
709 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 final int childCount = getChildCount();
711 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700712 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700713 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100714 if (child.getVisibility() != GONE) {
715 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700716
Vladimir Marko2824b072013-10-04 16:42:17 +0100717 int childWidthMode;
718 int childHeightMode;
719 int childWidth;
720 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700721
Vladimir Marko2824b072013-10-04 16:42:17 +0100722 if (!lp.isFullScreenPage) {
723 if (lp.width == LayoutParams.WRAP_CONTENT) {
724 childWidthMode = MeasureSpec.AT_MOST;
725 } else {
726 childWidthMode = MeasureSpec.EXACTLY;
727 }
728
729 if (lp.height == LayoutParams.WRAP_CONTENT) {
730 childHeightMode = MeasureSpec.AT_MOST;
731 } else {
732 childHeightMode = MeasureSpec.EXACTLY;
733 }
734
Adam Cohen3b185e22013-10-29 14:45:58 -0700735 childWidth = getViewportWidth() - horizontalPadding
736 - mInsets.left - mInsets.right;
737 childHeight = getViewportHeight() - verticalPadding
738 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100739 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700740 } else {
741 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700742 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100743
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800744 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700745 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700746 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700747 if (referenceChildWidth == 0) {
748 referenceChildWidth = childWidth;
749 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700750
Vladimir Marko2824b072013-10-04 16:42:17 +0100751 final int childWidthMeasureSpec =
752 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
753 final int childHeightMeasureSpec =
754 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
755 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700756 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700757 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700758 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800759 }
760
Sunny Goyalcf25b522015-07-09 00:01:18 -0700761 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700763 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700764 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700765 return;
766 }
767
Winson Chung785d2eb2011-04-14 16:08:02 -0700768 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700770
Adam Cohen7d30a372013-07-01 17:03:59 -0700771 int offsetX = getViewportOffsetX();
772 int offsetY = getViewportOffsetY();
773
774 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700775 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700776
Sunny Goyal70660032015-05-14 00:07:08 -0700777 final int startIndex = mIsRtl ? childCount - 1 : 0;
778 final int endIndex = mIsRtl ? -1 : childCount;
779 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700780
Adam Cohen7d30a372013-07-01 17:03:59 -0700781 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700782
Adam Cohen3b185e22013-10-29 14:45:58 -0700783 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000784 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700785
Sunny Goyal6178f132016-07-11 17:30:03 -0700786 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700787 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
788 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700789 }
790
Adam Cohen0ffac432013-07-10 11:19:26 -0700791 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700792 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700794 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100795 int childTop;
796 if (lp.isFullScreenPage) {
797 childTop = offsetY;
798 } else {
799 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700800 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100801 }
802
Adam Cohen96d30a12013-07-16 18:13:21 -0700803 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700804 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800805
Winson Chung785d2eb2011-04-14 16:08:02 -0700806 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700807 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800808 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700809
Sunny Goyal6178f132016-07-11 17:30:03 -0700810 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700811 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000812
813 int pageGap = mPageSpacing;
814 int next = i + delta;
815 if (next != endIndex) {
816 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
817 } else {
818 nextLp = null;
819 }
820
821 // Prevent full screen pages from showing in the viewport
822 // when they are not the current page.
823 if (lp.isFullScreenPage) {
824 pageGap = getPaddingLeft();
825 } else if (nextLp != null && nextLp.isFullScreenPage) {
826 pageGap = getPaddingRight();
827 }
828
Sunny Goyala1fbd842015-05-20 13:40:27 -0700829 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 }
831 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700832
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700833 final LayoutTransition transition = getLayoutTransition();
834 // If the transition is running defer updating max scroll, as some empty pages could
835 // still be present, and a max scroll change could cause sudden jumps in scroll.
836 if (transition != null && transition.isRunning()) {
837 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
838
839 @Override
840 public void startTransition(LayoutTransition transition, ViewGroup container,
841 View view, int transitionType) { }
842
843 @Override
844 public void endTransition(LayoutTransition transition, ViewGroup container,
845 View view, int transitionType) {
846 // Wait until all transitions are complete.
847 if (!transition.isRunning()) {
848 transition.removeTransitionListener(this);
849 updateMaxScrollX();
850 }
851 }
852 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700853 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700854 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700855 }
856
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700857 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
858 updateCurrentPageScroll();
859 mFirstLayout = false;
860 }
861
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700862 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700863 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700864 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700865 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700866
867 if (isReordering(true)) {
868 updateDragViewTranslationDuringDrag();
869 }
Winson Chunge3193b92010-09-10 11:44:42 -0700870 }
871
Sunny Goyala1fbd842015-05-20 13:40:27 -0700872 protected int getChildGap() {
873 return 0;
874 }
875
Sunny Goyal4d519f22017-10-24 10:32:40 -0700876 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700877 mMaxScrollX = computeMaxScrollX();
878 }
879
880 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700881 int childCount = getChildCount();
882 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700883 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700884 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700885 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700886 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700887 }
888 }
889
Adam Cohen3b185e22013-10-29 14:45:58 -0700890 public void setPageSpacing(int pageSpacing) {
891 mPageSpacing = pageSpacing;
892 requestLayout();
893 }
894
Winson Chunge3193b92010-09-10 11:44:42 -0700895 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700896 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700897 // Update the page indicator, we don't update the page indicator as we
898 // add/remove pages
899 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700900 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700901 }
902
Adam Cohen2591f6a2011-10-25 14:36:40 -0700903 // This ensures that when children are added, they get the correct transforms / alphas
904 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700905 invalidate();
906 }
907
Michael Jurka8b805b12012-04-18 14:23:14 -0700908 @Override
909 public void onChildViewRemoved(View parent, View child) {
Tonya361c722017-07-04 09:43:06 -0700910 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700911 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700912 }
913
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700914 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700915 // Update the page indicator, we don't update the page indicator as we
916 // add/remove pages
917 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700918 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700919 }
920 }
921
922 @Override
923 public void removeView(View v) {
924 // XXX: We should find a better way to hook into this before the view
925 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700926 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700927 super.removeView(v);
928 }
929 @Override
930 public void removeViewInLayout(View v) {
931 // XXX: We should find a better way to hook into this before the view
932 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700933 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700934 super.removeViewInLayout(v);
935 }
936 @Override
937 public void removeViewAt(int index) {
938 // XXX: We should find a better way to hook into this before the view
939 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700940 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700941 super.removeViewAt(index);
942 }
943 @Override
944 public void removeAllViewsInLayout() {
945 // Update the page indicator, we don't update the page indicator as we
946 // add/remove pages
947 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700948 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700949 }
950
951 super.removeAllViewsInLayout();
952 }
953
Adam Cohen73894962011-10-31 13:17:17 -0700954 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700955 if (index < 0 || index > getChildCount() - 1) return 0;
956
Adam Cohenf698c6e2013-07-17 18:44:25 -0700957 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700958
Adam Cohenf698c6e2013-07-17 18:44:25 -0700959 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700960 }
961
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 @Override
963 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700964 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700965 if (page != mCurrentPage || !mScroller.isFinished()) {
966 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 return true;
968 }
969 return false;
970 }
971
972 @Override
973 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700974 int focusablePage;
975 if (mNextPage != INVALID_PAGE) {
976 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700978 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 }
Winson Chung86f77532010-08-24 11:08:22 -0700980 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700982 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 }
984 return false;
985 }
986
987 @Override
988 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800989 if (super.dispatchUnhandledMove(focused, direction)) {
990 return true;
991 }
992
993 if (mIsRtl) {
994 if (direction == View.FOCUS_LEFT) {
995 direction = View.FOCUS_RIGHT;
996 } else if (direction == View.FOCUS_RIGHT) {
997 direction = View.FOCUS_LEFT;
998 }
999 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001001 if (getCurrentPage() > 0) {
1002 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 return true;
1004 }
1005 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001006 if (getCurrentPage() < getPageCount() - 1) {
1007 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 return true;
1009 }
1010 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001011 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 }
1013
1014 @Override
1015 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001016 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1017 return;
1018 }
1019
Adam Cohen0ffac432013-07-10 11:19:26 -07001020 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001021 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001022 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 }
1024 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001025 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001026 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 }
1028 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001029 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001030 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 }
1032 }
1033 }
1034
1035 /**
1036 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001037 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 *
Winson Chung86f77532010-08-24 11:08:22 -07001039 * This happens when live folders requery, and if they're off page, they
1040 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001041 */
1042 @Override
1043 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001044 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 View v = focused;
1046 while (true) {
1047 if (v == current) {
1048 super.focusableViewAvailable(focused);
1049 return;
1050 }
1051 if (v == this) {
1052 return;
1053 }
1054 ViewParent parent = v.getParent();
1055 if (parent instanceof View) {
1056 v = (View)v.getParent();
1057 } else {
1058 return;
1059 }
1060 }
1061 }
1062
1063 /**
1064 * {@inheritDoc}
1065 */
1066 @Override
1067 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1068 if (disallowIntercept) {
1069 // We need to make sure to cancel our long press if
1070 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001071 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001072 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1075 }
1076
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001077 /**
1078 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1079 */
1080 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001081 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001082 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001083 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001084 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001085 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001086 }
1087
1088 /**
1089 * Return true if a tap at (x, y) should trigger a flip to the next page.
1090 */
1091 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001092 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001093 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001094 }
1095 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001096 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001097 }
Winson Chung52aee602013-01-30 12:01:02 -08001098
Adam Cohen7d30a372013-07-01 17:03:59 -07001099 /** Returns whether x and y originated within the buffered viewport */
1100 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001101 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001102 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001103 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001104 }
1105
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 @Override
1107 public boolean onInterceptTouchEvent(MotionEvent ev) {
1108 /*
1109 * This method JUST determines whether we want to intercept the motion.
1110 * If we return true, onTouchEvent will be called and we do the actual
1111 * scrolling there.
1112 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001113 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001114
Winson Chung45e1d6e2010-11-09 17:19:49 -08001115 // Skip touch handling if there are no pages to swipe
1116 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1117
Winson Chung321e9ee2010-08-09 13:37:56 -07001118 /*
1119 * Shortcut the most recurring case: the user is in the dragging
1120 * state and he is moving his finger. We want to intercept this
1121 * motion.
1122 */
1123 final int action = ev.getAction();
1124 if ((action == MotionEvent.ACTION_MOVE) &&
1125 (mTouchState == TOUCH_STATE_SCROLLING)) {
1126 return true;
1127 }
1128
Winson Chung321e9ee2010-08-09 13:37:56 -07001129 switch (action & MotionEvent.ACTION_MASK) {
1130 case MotionEvent.ACTION_MOVE: {
1131 /*
1132 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1133 * whether the user has moved far enough from his original down touch.
1134 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001135 if (mActivePointerId != INVALID_POINTER) {
1136 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001137 }
1138 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1139 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1140 // i.e. fall through to the next case (don't break)
1141 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1142 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001143 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001144 }
1145
1146 case MotionEvent.ACTION_DOWN: {
1147 final float x = ev.getX();
1148 final float y = ev.getY();
1149 // Remember location of down touch
1150 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001151 mDownMotionY = y;
1152 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 mLastMotionX = x;
1154 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001155 float[] p = mapPointFromViewToParent(this, x, y);
1156 mParentDownMotionX = p[0];
1157 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001158 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001159 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001161
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 /*
1163 * If being flinged and user touches the screen, initiate drag;
1164 * otherwise don't. mScroller.isFinished should be false when
1165 * being flinged.
1166 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001167 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001168 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001169
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001170 if (finishedScrolling) {
1171 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001172 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001173 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001174 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001175 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001176 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001177 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1178 mTouchState = TOUCH_STATE_SCROLLING;
1179 } else {
1180 mTouchState = TOUCH_STATE_REST;
1181 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001182 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001183
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 break;
1185 }
1186
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001188 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001189 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 break;
1191
1192 case MotionEvent.ACTION_POINTER_UP:
1193 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001194 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 break;
1196 }
1197
1198 /*
1199 * The only time we want to intercept motion events is if we are in the
1200 * drag mode.
1201 */
1202 return mTouchState != TOUCH_STATE_REST;
1203 }
1204
Adam Cohenf8d28232011-02-01 21:47:00 -08001205 protected void determineScrollingStart(MotionEvent ev) {
1206 determineScrollingStart(ev, 1.0f);
1207 }
1208
Winson Chung321e9ee2010-08-09 13:37:56 -07001209 /*
1210 * Determines if we should change the touch state to start scrolling after the
1211 * user moves their touch point too far.
1212 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001213 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001214 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001216 if (pointerIndex == -1) return;
1217
1218 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 final float x = ev.getX(pointerIndex);
1220 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001221 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1222
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001224
Adam Cohenf8d28232011-02-01 21:47:00 -08001225 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001227
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001228 if (xMoved) {
1229 // Scroll if the user moved far enough along the X axis
1230 mTouchState = TOUCH_STATE_SCROLLING;
1231 mTotalMotionX += Math.abs(mLastMotionX - x);
1232 mLastMotionX = x;
1233 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001234 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001235 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001236 // Stop listening for things like pinches.
1237 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001238 }
1239 }
1240
1241 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001242 // Try canceling the long press. It could also have been scheduled
1243 // by a distant descendant, so use the mAllowLongPress flag to block
1244 // everything
1245 final View currentPage = getPageAt(mCurrentPage);
1246 if (currentPage != null) {
1247 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 }
1249 }
1250
Adam Cohenb5ba0972011-09-07 18:02:31 -07001251 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001252 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001253
Adam Cohenedb40762013-07-18 16:45:45 -07001254 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001255 int count = getChildCount();
1256
1257 final int totalDistance;
1258
1259 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001260 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001261 adjacentPage = page - 1;
1262 }
1263
1264 if (adjacentPage < 0 || adjacentPage > count - 1) {
1265 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1266 } else {
1267 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1268 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001269
1270 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001271 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1272 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001273 return scrollProgress;
1274 }
1275
Adam Cohenedb40762013-07-18 16:45:45 -07001276 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001277 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001278 return 0;
1279 } else {
1280 return mPageScrolls[index];
1281 }
1282 }
1283
Adam Cohen564a2e72013-10-09 14:47:32 -07001284 // While layout transitions are occurring, a child's position may stray from its baseline
1285 // position. This method returns the magnitude of this stray at any given time.
1286 public int getLayoutTransitionOffsetForPage(int index) {
1287 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1288 return 0;
1289 } else {
1290 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001291
1292 int scrollOffset = 0;
1293 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1294 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001295 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001296 }
1297
Adam Cohen564a2e72013-10-09 14:47:32 -07001298 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1299 return (int) (child.getX() - baselineX);
1300 }
1301 }
1302
Adam Cohenb5ba0972011-09-07 18:02:31 -07001303 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001304 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001305
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001306 int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001307 if (amount < 0) {
1308 mOverScrollX = overScrollAmount;
1309 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001310 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001311 mOverScrollX = mMaxScrollX + overScrollAmount;
1312 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001313 }
1314 invalidate();
1315 }
1316
Adam Cohenb5ba0972011-09-07 18:02:31 -07001317 protected void overScroll(float amount) {
1318 dampedOverScroll(amount);
1319 }
1320
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001321 /**
1322 * return true if freescroll has been enabled, false otherwise
1323 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001324 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001325 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001326 }
1327
Sunny Goyalc99cb172017-10-19 16:15:09 -07001328 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001329 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001330 }
1331
Adam Cohenf9618852013-11-08 06:45:03 -08001332 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001333 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001334 mFreeScroll = freeScroll;
1335
Adam Cohenf9618852013-11-08 06:45:03 -08001336 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001337 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001338 } else if (wasFreeScroll) {
1339 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001340 }
1341
1342 setEnableOverscroll(!freeScroll);
1343 }
1344
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001345 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001346 mAllowOverScroll = enable;
1347 }
1348
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001349 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350 if (mDragView != null) {
1351 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1352 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001353 int minDistance = Integer.MAX_VALUE;
1354 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001355 int maxPageNo = getChildCount() - 1;
1356 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001357 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001358 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001359 int d = Math.abs(dragX - pageX);
1360 if (d < minDistance) {
1361 minIndex = i;
1362 minDistance = d;
1363 }
1364 }
1365 return minIndex;
1366 }
1367 return -1;
1368 }
1369
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 @Override
1371 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001372 super.onTouchEvent(ev);
1373
Winson Chung45e1d6e2010-11-09 17:19:49 -08001374 // Skip touch handling if there are no pages to swipe
1375 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1376
Michael Jurkab8f06722010-10-10 15:58:46 -07001377 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001378
1379 final int action = ev.getAction();
1380
1381 switch (action & MotionEvent.ACTION_MASK) {
1382 case MotionEvent.ACTION_DOWN:
1383 /*
1384 * If being flinged and user touches, stop the fling. isFinished
1385 * will be false if being flinged.
1386 */
1387 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001388 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 }
1390
1391 // Remember where the motion event started
1392 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001393 mDownMotionY = mLastMotionY = ev.getY();
1394 mDownScrollX = getScrollX();
1395 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1396 mParentDownMotionX = p[0];
1397 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001398 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001399 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001401
Michael Jurka0142d492010-08-25 17:46:15 -07001402 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001403 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001404 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001405 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001406 break;
1407
1408 case MotionEvent.ACTION_MOVE:
1409 if (mTouchState == TOUCH_STATE_SCROLLING) {
1410 // Scroll to follow the motion event
1411 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001412
1413 if (pointerIndex == -1) return true;
1414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001416 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001417
Adam Cohenaefd4e12011-02-14 16:39:38 -08001418 mTotalMotionX += Math.abs(deltaX);
1419
Winson Chungc0844aa2011-02-02 15:25:58 -08001420 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1421 // keep the remainder because we are actually testing if we've moved from the last
1422 // scrolled position (which is discrete).
1423 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001424 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001425 mLastMotionX = x;
1426 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 } else {
1428 awakenScrollBars();
1429 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001430 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1431 // Update the last motion position
1432 mLastMotionX = ev.getX();
1433 mLastMotionY = ev.getY();
1434
1435 // Update the parent down so that our zoom animations take this new movement into
1436 // account
1437 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1438 mParentDownMotionX = pt[0];
1439 mParentDownMotionY = pt[1];
1440 updateDragViewTranslationDuringDrag();
1441
1442 // Find the closest page to the touch point
1443 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001444
Adam Cohen7d30a372013-07-01 17:03:59 -07001445 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1446 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1447 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1448 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1449
Adam Cohenf358a4b2013-07-23 16:47:31 -07001450 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001451 // Do not allow any page to be moved to 0th position.
1452 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001453 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001454 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1455 mSidePageHoverIndex = pageUnderPointIndex;
1456 mSidePageHoverRunnable = new Runnable() {
1457 @Override
1458 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001459 // Setup the scroll to the correct page before we swap the views
1460 snapToPage(pageUnderPointIndex);
1461
1462 // For each of the pages between the paged view and the drag view,
1463 // animate them from the previous position to the new position in
1464 // the layout (as a result of the drag view moving in the layout)
1465 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1466 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1467 dragViewIndex + 1 : pageUnderPointIndex;
1468 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1469 dragViewIndex - 1 : pageUnderPointIndex;
1470 for (int i = lowerIndex; i <= upperIndex; ++i) {
1471 View v = getChildAt(i);
1472 // dragViewIndex < pageUnderPointIndex, so after we remove the
1473 // drag view all subsequent views to pageUnderPointIndex will
1474 // shift down.
1475 int oldX = getViewportOffsetX() + getChildOffset(i);
1476 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1477
1478 // Animate the view translation from its old position to its new
1479 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001480 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001481 if (anim != null) {
1482 anim.cancel();
1483 }
1484
1485 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001486 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001487 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001488 anim.start();
1489 v.setTag(anim);
1490 }
1491
1492 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001493 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001495 if (mPageIndicator != null) {
1496 mPageIndicator.setActiveMarker(getNextPage());
1497 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001498 }
1499 };
1500 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1501 }
1502 } else {
1503 removeCallbacks(mSidePageHoverRunnable);
1504 mSidePageHoverIndex = -1;
1505 }
Adam Cohen564976a2010-10-13 18:52:07 -07001506 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 determineScrollingStart(ev);
1508 }
1509 break;
1510
1511 case MotionEvent.ACTION_UP:
1512 if (mTouchState == TOUCH_STATE_SCROLLING) {
1513 final int activePointerId = mActivePointerId;
1514 final int pointerIndex = ev.findPointerIndex(activePointerId);
1515 final float x = ev.getX(pointerIndex);
1516 final VelocityTracker velocityTracker = mVelocityTracker;
1517 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1518 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001519 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001520 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001521 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1522 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001523
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001524 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1525
Adam Cohen00481b32011-11-18 12:03:48 -08001526 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001527 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001528
Adam Cohenf358a4b2013-07-23 16:47:31 -07001529 if (!mFreeScroll) {
1530 // In the case that the page is moved far to one direction and then is flung
1531 // in the opposite direction, we use a threshold to determine whether we should
1532 // just return to the starting page, or if we should skip one further.
1533 boolean returnToOriginalPage = false;
1534 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1535 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1536 returnToOriginalPage = true;
1537 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001538
Adam Cohenf358a4b2013-07-23 16:47:31 -07001539 int finalPage;
1540 // We give flings precedence over large moves, which is why we short-circuit our
1541 // test for a large move if a fling has been registered. That is, a large
1542 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001543 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1544 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001545 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1546 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1547 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1548 snapToPageWithVelocity(finalPage, velocityX);
1549 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1550 (isFling && isVelocityXLeft)) &&
1551 mCurrentPage < getChildCount() - 1) {
1552 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1553 snapToPageWithVelocity(finalPage, velocityX);
1554 } else {
1555 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001556 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001558 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001559 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001560 }
1561
1562 float scaleX = getScaleX();
1563 int vX = (int) (-velocityX * scaleX);
1564 int initialScrollX = (int) (getScrollX() * scaleX);
1565
Adam Cohenf9618852013-11-08 06:45:03 -08001566 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001567 mScroller.fling(initialScrollX,
1568 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001569 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001570 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001572 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001573 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1574 // at this point we have not moved beyond the touch slop
1575 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1576 // we can just page
1577 int nextPage = Math.max(0, mCurrentPage - 1);
1578 if (nextPage != mCurrentPage) {
1579 snapToPage(nextPage);
1580 } else {
1581 snapToDestination();
1582 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001583 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001584 // at this point we have not moved beyond the touch slop
1585 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1586 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001587 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1588 if (nextPage != mCurrentPage) {
1589 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 } else {
1591 snapToDestination();
1592 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001593 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1594 // Update the last motion position
1595 mLastMotionX = ev.getX();
1596 mLastMotionY = ev.getY();
1597
1598 // Update the parent down so that our zoom animations take this new movement into
1599 // account
1600 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1601 mParentDownMotionX = pt[0];
1602 mParentDownMotionY = pt[1];
1603 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001604 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001605 if (!mCancelTap) {
1606 onUnhandledTap(ev);
1607 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001608 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001609
1610 // Remove the callback to wait for the side page hover timeout
1611 removeCallbacks(mSidePageHoverRunnable);
1612 // End any intermediate reordering states
1613 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001614 break;
1615
1616 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001617 if (mTouchState == TOUCH_STATE_SCROLLING) {
1618 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001619 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001620 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001621 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001622 break;
1623
1624 case MotionEvent.ACTION_POINTER_UP:
1625 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001626 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001627 break;
1628 }
1629
1630 return true;
1631 }
1632
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001633 protected boolean shouldFlingForVelocity(int velocityX) {
1634 return Math.abs(velocityX) > mFlingThresholdVelocity;
1635 }
1636
Adam Cohen7d30a372013-07-01 17:03:59 -07001637 private void resetTouchState() {
1638 releaseVelocityTracker();
1639 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001640 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001641 mTouchState = TOUCH_STATE_REST;
1642 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001643 }
1644
Adam Cohenc2d6e892014-10-16 09:49:24 -07001645 /**
1646 * Triggered by scrolling via touch
1647 */
1648 protected void onScrollInteractionBegin() {
1649 }
1650
1651 protected void onScrollInteractionEnd() {
1652 }
1653
Adam Cohen1697b792013-09-17 19:08:21 -07001654 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001655 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001656 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001657
Winson Chung185d7162011-02-28 13:47:29 -08001658 @Override
1659 public boolean onGenericMotionEvent(MotionEvent event) {
1660 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1661 switch (event.getAction()) {
1662 case MotionEvent.ACTION_SCROLL: {
1663 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1664 final float vscroll;
1665 final float hscroll;
1666 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1667 vscroll = 0;
1668 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1669 } else {
1670 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1671 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1672 }
1673 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001674 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001675 : (hscroll > 0 || vscroll > 0);
1676 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001677 scrollRight();
1678 } else {
1679 scrollLeft();
1680 }
1681 return true;
1682 }
1683 }
1684 }
1685 }
1686 return super.onGenericMotionEvent(event);
1687 }
1688
Michael Jurkab8f06722010-10-10 15:58:46 -07001689 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1690 if (mVelocityTracker == null) {
1691 mVelocityTracker = VelocityTracker.obtain();
1692 }
1693 mVelocityTracker.addMovement(ev);
1694 }
1695
1696 private void releaseVelocityTracker() {
1697 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001698 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001699 mVelocityTracker.recycle();
1700 mVelocityTracker = null;
1701 }
1702 }
1703
Winson Chung321e9ee2010-08-09 13:37:56 -07001704 private void onSecondaryPointerUp(MotionEvent ev) {
1705 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1706 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1707 final int pointerId = ev.getPointerId(pointerIndex);
1708 if (pointerId == mActivePointerId) {
1709 // This was our active pointer going up. Choose a new
1710 // active pointer and adjust accordingly.
1711 // TODO: Make this decision more intelligent.
1712 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1713 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1714 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001715 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001716 mActivePointerId = ev.getPointerId(newPointerIndex);
1717 if (mVelocityTracker != null) {
1718 mVelocityTracker.clear();
1719 }
1720 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001721 }
1722
Winson Chung321e9ee2010-08-09 13:37:56 -07001723 @Override
1724 public void requestChildFocus(View child, View focused) {
1725 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001726 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001727 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001728 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001729 }
1730 }
1731
Adam Cohend19d3ca2010-09-15 14:43:42 -07001732 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001733 return getPageNearestToCenterOfScreen(getScrollX());
1734 }
1735
1736 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1737 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001738 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001739 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001740 final int childCount = getChildCount();
1741 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001742 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001743 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001744 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001745 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001746 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1747 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1748 minDistanceFromScreenCenter = distanceFromScreenCenter;
1749 minDistanceFromScreenCenterIndex = i;
1750 }
1751 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001752 return minDistanceFromScreenCenterIndex;
1753 }
1754
1755 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001756 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1757 }
1758
1759 protected boolean isInOverScroll() {
1760 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1761 }
1762
1763 protected int getPageSnapDuration() {
1764 if (isInOverScroll()) {
1765 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1766 }
1767 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001768 }
1769
Adam Cohene0f66b52010-11-23 15:06:07 -08001770 // We want the duration of the page snap animation to be influenced by the distance that
1771 // the screen has to travel, however, we don't want this duration to be effected in a
1772 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1773 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001774 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001775 f -= 0.5f; // center the values about 0.
1776 f *= 0.3f * Math.PI / 2.0f;
1777 return (float) Math.sin(f);
1778 }
1779
Michael Jurka0142d492010-08-25 17:46:15 -07001780 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001781 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001782 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001783
Adam Cohenedb40762013-07-18 16:45:45 -07001784 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001785 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001786 int duration = 0;
1787
Sunny Goyal4d113a52015-05-27 10:05:28 -07001788 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001789 // If the velocity is low enough, then treat this more as an automatic page advance
1790 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001791 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001792 return;
1793 }
1794
1795 // Here we compute a "distance" that will be used in the computation of the overall
1796 // snap duration. This is a function of the actual distance that needs to be traveled;
1797 // we keep this value close to half screen size in order to reduce the variance in snap
1798 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001799 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001800 float distance = halfScreenSize + halfScreenSize *
1801 distanceInfluenceForSnapDuration(distanceRatio);
1802
1803 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001804 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001805
1806 // we want the page's snap velocity to approximately match the velocity at which the
1807 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001808 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1809 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001810
1811 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001812 }
1813
Sunny Goyal1740d902015-05-27 11:14:01 -07001814 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001815 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001816 }
1817
Adam Cohenf9c184a2016-01-15 16:47:43 -08001818 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001819 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001820 }
1821
Michael Jurka0142d492010-08-25 17:46:15 -07001822 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001823 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001824 }
1825
Adam Cohenf9618852013-11-08 06:45:03 -08001826 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1827 snapToPage(whichPage, duration, false, interpolator);
1828 }
1829
1830 protected void snapToPage(int whichPage, int duration, boolean immediate,
1831 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001832 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001833
Adam Cohenedb40762013-07-18 16:45:45 -07001834 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001835 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001836 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001837 }
1838
1839 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001840 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001841 }
Michael Jurka0142d492010-08-25 17:46:15 -07001842
Adam Cohenf9618852013-11-08 06:45:03 -08001843 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1844 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001845 whichPage = validateNewPage(whichPage);
1846
Adam Cohen7d30a372013-07-01 17:03:59 -07001847 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001848
Winson Chung321e9ee2010-08-09 13:37:56 -07001849 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001850 if (immediate) {
1851 duration = 0;
1852 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001853 duration = Math.abs(delta);
1854 }
1855
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001856 if (duration != 0) {
1857 pageBeginTransition();
1858 }
1859
Adam Cohenf358a4b2013-07-23 16:47:31 -07001860 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001861 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001862 }
Adam Cohenf9618852013-11-08 06:45:03 -08001863
1864 if (interpolator != null) {
1865 mScroller.setInterpolator(interpolator);
1866 } else {
1867 mScroller.setInterpolator(mDefaultInterpolator);
1868 }
1869
Sunny Goyalc86df472016-02-25 09:19:38 -08001870 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001871
Adam Cohen674531f2013-12-13 15:07:14 -08001872 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001873
1874 // Trigger a compute() to finish switching pages if necessary
1875 if (immediate) {
1876 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001877 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001878 }
1879
Winson Chung321e9ee2010-08-09 13:37:56 -07001880 invalidate();
1881 }
1882
Winson Chung321e9ee2010-08-09 13:37:56 -07001883 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001884 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001885 }
1886
1887 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001888 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001889 }
1890
Adam Cohendbdff6b2013-09-18 19:09:15 -07001891 @Override
1892 public boolean performLongClick() {
1893 mCancelTap = true;
1894 return super.performLongClick();
1895 }
1896
Winson Chung321e9ee2010-08-09 13:37:56 -07001897 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001898 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001899
1900 SavedState(Parcelable superState) {
1901 super(superState);
1902 }
1903
Adam Cohen091440a2015-03-18 14:16:05 -07001904 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001905 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001906 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001907 }
1908
1909 @Override
1910 public void writeToParcel(Parcel out, int flags) {
1911 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001912 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001913 }
1914
1915 public static final Parcelable.Creator<SavedState> CREATOR =
1916 new Parcelable.Creator<SavedState>() {
1917 public SavedState createFromParcel(Parcel in) {
1918 return new SavedState(in);
1919 }
1920
1921 public SavedState[] newArray(int size) {
1922 return new SavedState[size];
1923 }
1924 };
1925 }
1926
Adam Cohen7d30a372013-07-01 17:03:59 -07001927 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001928 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001929 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001930 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1931 new PropertyListBuilder()
1932 .scale(1)
1933 .translationX(0)
1934 .translationY(0)
1935 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001936 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001937 anim.addListener(new AnimatorListenerAdapter() {
1938 @Override
1939 public void onAnimationEnd(Animator animation) {
1940 onPostReorderingAnimationCompleted();
1941 }
1942 });
1943 anim.start();
1944 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001945 }
1946
Sunny Goyal1d08f702015-05-04 15:50:25 -07001947 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001948 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1949 mTouchState = TOUCH_STATE_REORDERING;
1950 mIsReordering = true;
1951
Adam Cohen7d30a372013-07-01 17:03:59 -07001952 // We must invalidate to trigger a redraw to update the layers such that the drag view
1953 // is always drawn on top
1954 invalidate();
1955 }
1956
Adam Cohen091440a2015-03-18 14:16:05 -07001957 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001958 // Trigger the callback when reordering has settled
1959 --mPostReorderingPreZoomInRemainingAnimationCount;
1960 if (mPostReorderingPreZoomInRunnable != null &&
1961 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1962 mPostReorderingPreZoomInRunnable.run();
1963 mPostReorderingPreZoomInRunnable = null;
1964 }
1965 }
1966
Sunny Goyal1d08f702015-05-04 15:50:25 -07001967 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001968 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001969 }
1970
Adam Cohenf358a4b2013-07-23 16:47:31 -07001971 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001972 int dragViewIndex = indexOfChild(v);
1973
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001974 // Do not allow the first page to be moved around
1975 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001976
Adam Cohen7d30a372013-07-01 17:03:59 -07001977 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001978 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001979 // Find the drag view under the pointer
1980 mDragView = getChildAt(dragViewIndex);
1981 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1982 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001983 mReorderingStarted = true;
1984
Adam Cohenf9618852013-11-08 06:45:03 -08001985 snapToPage(getPageNearestToCenterOfScreen());
1986 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001987 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001988 return true;
1989 }
1990 return false;
1991 }
1992
1993 boolean isReordering(boolean testTouchState) {
1994 boolean state = mIsReordering;
1995 if (testTouchState) {
1996 state &= (mTouchState == TOUCH_STATE_REORDERING);
1997 }
1998 return state;
1999 }
2000 void endReordering() {
2001 // For simplicity, we call endReordering sometimes even if reordering was never started.
2002 // In that case, we don't want to do anything.
2003 if (!mReorderingStarted) return;
2004 mReorderingStarted = false;
2005
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002006 mPostReorderingPreZoomInRunnable = new Runnable() {
2007 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002008 // If we haven't flung-to-delete the current child,
2009 // then we just animate the drag view back into position
2010 onEndReordering();
2011
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002012 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002013 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002014 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002015
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002016 mPostReorderingPreZoomInRemainingAnimationCount =
2017 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2018 // Snap to the current page
2019 snapToPage(indexOfChild(mDragView), 0);
2020 // Animate the drag view back to the front position
2021 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002022 }
2023
Winson Chung6a0f57d2011-06-29 20:10:49 -07002024 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002025 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002026 @Override
2027 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2028 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002029 info.setScrollable(getPageCount() > 1);
2030 if (getCurrentPage() < getPageCount() - 1) {
2031 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2032 }
2033 if (getCurrentPage() > 0) {
2034 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2035 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002036 info.setClassName(getClass().getName());
2037
2038 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2039 // Besides disabling the accessibility long-click, this also prevents this view from getting
2040 // accessibility focus.
2041 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002042 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002043 }
2044
2045 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002046 public void sendAccessibilityEvent(int eventType) {
2047 // Don't let the view send real scroll events.
2048 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2049 super.sendAccessibilityEvent(eventType);
2050 }
2051 }
2052
2053 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002054 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2055 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002056 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002057 }
2058
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002059 @Override
2060 public boolean performAccessibilityAction(int action, Bundle arguments) {
2061 if (super.performAccessibilityAction(action, arguments)) {
2062 return true;
2063 }
2064 switch (action) {
2065 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2066 if (getCurrentPage() < getPageCount() - 1) {
2067 scrollRight();
2068 return true;
2069 }
2070 } break;
2071 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2072 if (getCurrentPage() > 0) {
2073 scrollLeft();
2074 return true;
2075 }
2076 } break;
2077 }
2078 return false;
2079 }
2080
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002081 protected String getPageIndicatorDescription() {
2082 return getCurrentPageDescription();
2083 }
2084
Adam Cohen0ffac432013-07-10 11:19:26 -07002085 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002086 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002087 getNextPage() + 1, getChildCount());
2088 }
2089
Winson Chungd11265e2011-08-30 13:37:23 -07002090 @Override
2091 public boolean onHoverEvent(android.view.MotionEvent event) {
2092 return true;
2093 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002094}