blob: ed225c9ecec0dcd37ee819dc7cc4b5e54c75ebb2 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070027import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070029import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.os.Parcel;
31import android.os.Parcelable;
32import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070033import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080035import android.view.InputDevice;
36import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070037import android.view.MotionEvent;
38import android.view.VelocityTracker;
39import android.view.View;
40import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080041import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.view.ViewGroup;
43import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070044import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070045import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080047import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070048
Sunny Goyal9e76f682017-02-13 12:13:43 -080049import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070050import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080051import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Vadim Tryshevfedca432015-08-19 17:55:02 -070068 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Adam Cohen8d769d62017-06-23 17:27:38 -070071 // Overscroll constants
72 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
73 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
74 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
75
Adam Cohenb64cb5a2011-02-15 13:53:42 -080076 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080077 // The page is moved more than halfway, automatically move to the next page on touch up.
78 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080079
Sunny Goyal8e2133b2015-05-06 13:39:07 -070080 private static final float MAX_SCROLL_PROGRESS = 1.0f;
81
Adam Cohen265b9a62011-12-07 14:37:18 -080082 // The following constants need to be scaled based on density. The scaled versions will be
83 // assigned to the corresponding member variables below.
84 private static final int FLING_THRESHOLD_VELOCITY = 500;
85 private static final int MIN_SNAP_VELOCITY = 1500;
86 private static final int MIN_FLING_VELOCITY = 250;
87
Adam Cohen21cd0022013-10-09 18:57:02 -070088 public static final int INVALID_RESTORE_PAGE = -1001;
89
Adam Cohenf358a4b2013-07-23 16:47:31 -070090 private boolean mFreeScroll = false;
91 private int mFreeScrollMinScrollX = -1;
92 private int mFreeScrollMaxScrollX = -1;
93
Adam Cohen265b9a62011-12-07 14:37:18 -080094 protected int mFlingThresholdVelocity;
95 protected int mMinFlingVelocity;
96 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070099 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700103 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800107 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800108 protected LauncherScroller mScroller;
109 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Adam Cohen7d30a372013-07-01 17:03:59 -0700113 private float mParentDownMotionX;
114 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700116 private float mDownMotionY;
117 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700118 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700119 private float mLastMotionX;
120 private float mLastMotionXRemainder;
121 private float mLastMotionY;
122 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700123
Adam Cohendbdff6b2013-09-18 19:09:15 -0700124 private boolean mCancelTap;
125
Adam Cohenedb40762013-07-18 16:45:45 -0700126 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka0142d492010-08-25 17:46:15 -0700128 protected final static int TOUCH_STATE_REST = 0;
129 protected final static int TOUCH_STATE_SCROLLING = 1;
130 protected final static int TOUCH_STATE_PREV_PAGE = 2;
131 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 protected final static int TOUCH_STATE_REORDERING = 4;
133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800135
Michael Jurka0142d492010-08-25 17:46:15 -0700136 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka7426c422010-11-11 15:23:47 -0800138 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800140 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800141 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka5f1c5092010-09-03 14:15:02 -0700145 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700147 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148
Sunny Goyal061380a2016-02-29 15:15:03 -0800149 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700150
Adam Cohen8d769d62017-06-23 17:27:38 -0700151 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
152 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
153 // the screens from continuing to translate beyond the normal bounds.
154 protected int mOverScrollX;
155
156 protected int mUnboundedScrollX;
157
Winson Chungd2be3812013-07-16 11:11:32 -0700158 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700159 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700160 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700161 // The viewport whether the pages are to be contained (the actual view may be larger than the
162 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800163 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700164 private Rect mViewport = new Rect();
165
166 // Reordering
167 // We use the min scale to determine how much to expand the actually PagedView measured
168 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700169 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700170 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700171 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
172
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700174 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700175 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700176 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 // This variable's scope is only for the duration of startReordering() and endReordering()
179 private boolean mReorderingStarted = false;
180 // This variable's scope is for the duration of startReordering() and after the zoomIn()
181 // animation after endReordering()
182 private boolean mIsReordering;
183 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700184 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700185 private int mPostReorderingPreZoomInRemainingAnimationCount;
186 private Runnable mPostReorderingPreZoomInRunnable;
187
Adam Cohen7d30a372013-07-01 17:03:59 -0700188 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700189 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700190 private static final float[] sTmpPoint = new float[2];
191 private static final int[] sTmpIntPoint = new int[2];
192 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700193
John Spurlock77e1f472013-09-11 10:09:51 -0400194 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700195 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400196
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 public PagedView(Context context) {
198 this(context, null);
199 }
200
201 public PagedView(Context context, AttributeSet attrs) {
202 this(context, attrs, 0);
203 }
204
205 public PagedView(Context context, AttributeSet attrs, int defStyle) {
206 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700207
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 TypedArray a = context.obtainStyledAttributes(attrs,
209 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700210 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 a.recycle();
212
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700214 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700215 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 }
217
218 /**
219 * Initializes various states for this workspace.
220 */
Michael Jurka0142d492010-08-25 17:46:15 -0700221 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800222 mScroller = new LauncherScroller(getContext());
223 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700224 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700225
226 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700227 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800229
Sunny Goyalcf25b522015-07-09 00:01:18 -0700230 float density = getResources().getDisplayMetrics().density;
231 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
232 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
233 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700234 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700235 setWillNotDraw(false);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700236
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800237 int edgeEffectColor = Themes.getAttrColor(getContext(), android.R.attr.colorEdgeEffect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 }
239
Adam Cohenf9618852013-11-08 06:45:03 -0800240 protected void setDefaultInterpolator(Interpolator interpolator) {
241 mDefaultInterpolator = interpolator;
242 mScroller.setInterpolator(mDefaultInterpolator);
243 }
244
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700245 public void initParentViews(View parent) {
246 if (mPageIndicatorViewId > -1) {
247 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700248 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700249 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700250 }
251 }
252
Adam Cohen7d30a372013-07-01 17:03:59 -0700253 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700254 private float[] mapPointFromViewToParent(View v, float x, float y) {
255 sTmpPoint[0] = x;
256 sTmpPoint[1] = y;
257 v.getMatrix().mapPoints(sTmpPoint);
258 sTmpPoint[0] += v.getLeft();
259 sTmpPoint[1] += v.getTop();
260 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700261 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700262 private float[] mapPointFromParentToView(View v, float x, float y) {
263 sTmpPoint[0] = x - v.getLeft();
264 sTmpPoint[1] = y - v.getTop();
265 v.getMatrix().invert(sTmpInvMatrix);
266 sTmpInvMatrix.mapPoints(sTmpPoint);
267 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700268 }
269
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700270 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700271 if (mDragView != null) {
272 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
273 (mDragViewBaselineLeft - mDragView.getLeft());
274 float y = mLastMotionY - mDownMotionY;
275 mDragView.setTranslationX(x);
276 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700277
Adam Cohenf358a4b2013-07-23 16:47:31 -0700278 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
279 + x + ", " + y);
280 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700281 }
282
283 public void setMinScale(float f) {
284 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700285 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700286 requestLayout();
287 }
288
289 @Override
290 public void setScaleX(float scaleX) {
291 super.setScaleX(scaleX);
292 if (isReordering(true)) {
293 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
294 mLastMotionX = p[0];
295 mLastMotionY = p[1];
296 updateDragViewTranslationDuringDrag();
297 }
298 }
299
300 // Convenience methods to get the actual width/height of the PagedView (since it is measured
301 // to be larger to account for the minimum possible scale)
302 int getViewportWidth() {
303 return mViewport.width();
304 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800305 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700306 return mViewport.height();
307 }
308
309 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
310 // PagedView both horizontally and vertically
311 int getViewportOffsetX() {
312 return (getMeasuredWidth() - getViewportWidth()) / 2;
313 }
314
315 int getViewportOffsetY() {
316 return (getMeasuredHeight() - getViewportHeight()) / 2;
317 }
318
Hyunyoung Song77441692016-06-27 22:00:48 -0700319 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700320 return mPageIndicator;
321 }
322
Adam Cohen674531f2013-12-13 15:07:14 -0800323 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700324 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
325 * that the user was on before entering free scroll mode (e.g. the home screen page they
326 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
327 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700329 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700330 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700332
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700333 /**
334 * Returns the index of page to be shown immediately afterwards.
335 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700336 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700337 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
338 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700339
Adam Cohenf9c184a2016-01-15 16:47:43 -0800340 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 return getChildCount();
342 }
343
Sunny Goyal83a8f042015-05-19 12:52:12 -0700344 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 return getChildAt(index);
346 }
347
Adam Cohenae4f1552011-10-20 00:15:42 -0700348 protected int indexToPage(int index) {
349 return index;
350 }
351
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800353 * Updates the scroll of the current page immediately to its final scroll position. We use this
354 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
355 * the previous tab page.
356 */
357 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700358 // If the current page is invalid, just reset the scroll position to zero
359 int newX = 0;
360 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700361 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700362 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800363 scrollTo(newX, 0);
364 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700365 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800366 }
367
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700368 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700369 mScroller.abortAnimation();
370 // We need to clean up the next page here to avoid computeScrollHelper from
371 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700372 if (resetNextPage) {
373 mNextPage = INVALID_PAGE;
374 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700375 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700376
Tony Wickham8f7ead32016-04-07 18:46:44 -0700377 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700378 mScroller.forceFinished(true);
379 // We need to clean up the next page here to avoid computeScrollHelper from
380 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700381 if (resetNextPage) {
382 mNextPage = INVALID_PAGE;
383 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700384 }
385
Adam Cohen6f127a62014-06-12 14:54:41 -0700386 private int validateNewPage(int newPage) {
387 int validatedPage = newPage;
388 // When in free scroll mode, we need to clamp to the free scroll page range.
389 if (mFreeScroll) {
390 getFreeScrollPageRange(mTempVisiblePagesRange);
391 validatedPage = Math.max(mTempVisiblePagesRange[0],
392 Math.min(newPage, mTempVisiblePagesRange[1]));
393 }
394 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700395 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700396 return validatedPage;
397 }
398
Chet Haasebc2f0822012-10-26 17:59:53 -0700399 /**
Winson Chung86f77532010-08-24 11:08:22 -0700400 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700402 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800403 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700404 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800405 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800406 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
407 // the default
408 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800409 return;
410 }
Adam Cohen6f127a62014-06-12 14:54:41 -0700411 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700412 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700413 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800414 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700415 }
416
Winson Chung8c87cd82013-07-23 16:20:10 -0700417 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800418 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
419 * has settled.
420 */
Michael Jurka0142d492010-08-25 17:46:15 -0700421 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800422 updatePageIndicator();
423 }
424
425 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700426 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700427 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700428 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700429 if (!isReordering(false)) {
430 mPageIndicator.setActiveMarker(getNextPage());
431 }
Winson Chungd2be3812013-07-16 11:11:32 -0700432 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700434 protected void pageBeginTransition() {
435 if (!mIsPageInTransition) {
436 mIsPageInTransition = true;
437 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700438 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700439 }
440
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700441 protected void pageEndTransition() {
442 if (mIsPageInTransition) {
443 mIsPageInTransition = false;
444 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700445 }
Michael Jurka0142d492010-08-25 17:46:15 -0700446 }
447
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700448 protected boolean isPageInTransition() {
449 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700450 }
451
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700452 /**
453 * Called when the page starts moving as part of the scroll. Subclasses can override this
454 * to provide custom behavior during animation.
455 */
456 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700457 }
458
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700459 /**
460 * Called when the page ends moving as part of the scroll. Subclasses can override this
461 * to provide custom behavior during animation.
462 */
463 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700464 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700465 }
466
Winson Chung321e9ee2010-08-09 13:37:56 -0700467 /**
Winson Chung86f77532010-08-24 11:08:22 -0700468 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700469 *
470 * @param l The listener used to respond to long clicks.
471 */
472 @Override
473 public void setOnLongClickListener(OnLongClickListener l) {
474 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700475 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700477 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
Adam Cohen1697b792013-09-17 19:08:21 -0700479 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 }
481
Sunny Goyalc86df472016-02-25 09:19:38 -0800482 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700483 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800484 }
485
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800487 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800488 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800489 }
490
491 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700492 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700493 // In free scroll mode, we clamp the scrollX
494 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700495 // If the scroller is trying to move to a location beyond the maximum allowed
496 // in the free scroll mode, we make sure to end the scroll operation.
497 if (!mScroller.isFinished() &&
498 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700499 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700500 }
501
Adam Cohenf358a4b2013-07-23 16:47:31 -0700502 x = Math.min(x, mFreeScrollMaxScrollX);
503 x = Math.max(x, mFreeScrollMinScrollX);
504 }
505
Adam Cohen8d769d62017-06-23 17:27:38 -0700506 mUnboundedScrollX = x;
507
Sunny Goyal70660032015-05-14 00:07:08 -0700508 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
509 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700510 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700511 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800512 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700513 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700514 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700515 overScroll(x - mMaxScrollX);
516 } else {
517 overScroll(x);
518 }
Adam Cohen68d73932010-11-15 10:50:58 -0800519 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700520 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700521 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800522 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700523 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700524 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700525 overScroll(x);
526 } else {
527 overScroll(x - mMaxScrollX);
528 }
Adam Cohen68d73932010-11-15 10:50:58 -0800529 }
530 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700531 if (mWasInOverscroll) {
532 overScroll(0);
533 mWasInOverscroll = false;
534 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700535 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800536 super.scrollTo(x, y);
537 }
538
Adam Cohen7d30a372013-07-01 17:03:59 -0700539 // Update the last motion events when scrolling
540 if (isReordering(true)) {
541 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
542 mLastMotionX = p[0];
543 mLastMotionY = p[1];
544 updateDragViewTranslationDuringDrag();
545 }
Michael Jurka0142d492010-08-25 17:46:15 -0700546 }
547
Adam Cohen53805212013-10-01 10:39:23 -0700548 private void sendScrollAccessibilityEvent() {
549 AccessibilityManager am =
550 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
551 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700552 if (mCurrentPage != getNextPage()) {
553 AccessibilityEvent ev =
554 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700555 ev.setScrollable(true);
556 ev.setScrollX(getScrollX());
557 ev.setScrollY(getScrollY());
558 ev.setMaxScrollX(mMaxScrollX);
559 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700560
Vadim Tryshevf4715972015-05-08 17:21:03 -0700561 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700562 }
Adam Cohen53805212013-10-01 10:39:23 -0700563 }
564 }
565
Michael Jurka0142d492010-08-25 17:46:15 -0700566 // we moved this functionality to a helper function so SmoothPagedView can reuse it
567 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800568 return computeScrollHelper(true);
569 }
570
571 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700573 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800574 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700575 || getScrollY() != mScroller.getCurrY()
576 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700577 float scaleX = mFreeScroll ? getScaleX() : 1f;
578 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
579 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700580 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800581 if (shouldInvalidate) {
582 invalidate();
583 }
Michael Jurka0142d492010-08-25 17:46:15 -0700584 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800585 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700586 sendScrollAccessibilityEvent();
587
Adam Cohen6f127a62014-06-12 14:54:41 -0700588 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700589 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700590 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700591
Adam Cohen73aa9752010-11-24 16:26:58 -0800592 // We don't want to trigger a page end moving unless the page has settled
593 // and the user has stopped scrolling
594 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700595 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800596 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700597
Adam Cohen7d30a372013-07-01 17:03:59 -0700598 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700599 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700600 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700601 if (am.isEnabled()) {
602 // Notify the user when the page changes
603 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700604 }
Michael Jurka0142d492010-08-25 17:46:15 -0700605 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700606 }
Michael Jurka0142d492010-08-25 17:46:15 -0700607 return false;
608 }
609
610 @Override
611 public void computeScroll() {
612 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
614
Adam Cohen96d30a12013-07-16 18:13:21 -0700615 public static class LayoutParams extends ViewGroup.LayoutParams {
616 public boolean isFullScreenPage = false;
617
Sunny Goyal7c786f72016-06-01 14:08:21 -0700618 // If true, the start edge of the page snaps to the start edge of the viewport.
619 public boolean matchStartEdge = false;
620
Adam Cohen96d30a12013-07-16 18:13:21 -0700621 /**
622 * {@inheritDoc}
623 */
624 public LayoutParams(int width, int height) {
625 super(width, height);
626 }
627
Sunny Goyal41b22c02015-05-14 15:20:48 -0700628 public LayoutParams(Context context, AttributeSet attrs) {
629 super(context, attrs);
630 }
631
Adam Cohen96d30a12013-07-16 18:13:21 -0700632 public LayoutParams(ViewGroup.LayoutParams source) {
633 super(source);
634 }
635 }
636
Sunny Goyal41b22c02015-05-14 15:20:48 -0700637 @Override
638 public LayoutParams generateLayoutParams(AttributeSet attrs) {
639 return new LayoutParams(getContext(), attrs);
640 }
641
642 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700643 protected LayoutParams generateDefaultLayoutParams() {
644 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
645 }
646
Sunny Goyal41b22c02015-05-14 15:20:48 -0700647 @Override
648 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
649 return new LayoutParams(p);
650 }
651
652 @Override
653 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
654 return p instanceof LayoutParams;
655 }
656
Adam Cohenedb40762013-07-18 16:45:45 -0700657 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700658 LayoutParams lp = generateDefaultLayoutParams();
659 lp.isFullScreenPage = true;
660 super.addView(page, 0, lp);
661 }
662
Adam Cohen410f3cd2013-09-22 12:09:32 -0700663 public int getNormalChildHeight() {
664 return mNormalChildHeight;
665 }
666
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 @Override
668 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700669 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700670 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
671 return;
672 }
673
Adam Cohen7d30a372013-07-01 17:03:59 -0700674 // We measure the dimensions of the PagedView to be larger than the pages so that when we
675 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700676 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
677 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
678 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700679 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800680 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700681 // viewport, we can be at most one and a half screens offset once we scale down
682 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700683 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
684 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700685
Winson Chungc82d2622013-11-06 13:23:29 -0800686 int parentWidthSize = (int) (2f * maxSize);
687 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700688 int scaledWidthSize, scaledHeightSize;
689 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700690 scaledWidthSize = (int) (parentWidthSize / mMinScale);
691 scaledHeightSize = (int) (parentHeightSize / mMinScale);
692 } else {
693 scaledWidthSize = widthSize;
694 scaledHeightSize = heightSize;
695 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700696 mViewport.set(0, 0, widthSize, heightSize);
697
698 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
699 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
700 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 }
702
Winson Chung8aad6102012-05-11 16:27:49 -0700703 // Return early if we aren't given a proper dimension
704 if (widthSize <= 0 || heightSize <= 0) {
705 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
706 return;
707 }
708
Adam Lesinski6b879f02010-11-04 16:15:23 -0700709 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
710 * of the All apps view on XLarge displays to not take up more space then it needs. Width
711 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
712 * each page to have the same width.
713 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int verticalPadding = getPaddingTop() + getPaddingBottom();
715 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700716
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700717 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700719 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700720 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700721 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
722 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
723 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
724 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 final int childCount = getChildCount();
726 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700727 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700728 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100729 if (child.getVisibility() != GONE) {
730 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700731
Vladimir Marko2824b072013-10-04 16:42:17 +0100732 int childWidthMode;
733 int childHeightMode;
734 int childWidth;
735 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700736
Vladimir Marko2824b072013-10-04 16:42:17 +0100737 if (!lp.isFullScreenPage) {
738 if (lp.width == LayoutParams.WRAP_CONTENT) {
739 childWidthMode = MeasureSpec.AT_MOST;
740 } else {
741 childWidthMode = MeasureSpec.EXACTLY;
742 }
743
744 if (lp.height == LayoutParams.WRAP_CONTENT) {
745 childHeightMode = MeasureSpec.AT_MOST;
746 } else {
747 childHeightMode = MeasureSpec.EXACTLY;
748 }
749
Adam Cohen3b185e22013-10-29 14:45:58 -0700750 childWidth = getViewportWidth() - horizontalPadding
751 - mInsets.left - mInsets.right;
752 childHeight = getViewportHeight() - verticalPadding
753 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100754 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700755 } else {
756 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700757 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100758
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800759 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700760 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700761 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700762 if (referenceChildWidth == 0) {
763 referenceChildWidth = childWidth;
764 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700765
Vladimir Marko2824b072013-10-04 16:42:17 +0100766 final int childWidthMeasureSpec =
767 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
768 final int childHeightMeasureSpec =
769 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
770 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700771 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700772 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700773 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800774 }
775
Sunny Goyalcf25b522015-07-09 00:01:18 -0700776 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700778 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700779 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700780 return;
781 }
782
Winson Chung785d2eb2011-04-14 16:08:02 -0700783 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700785
Adam Cohen7d30a372013-07-01 17:03:59 -0700786 int offsetX = getViewportOffsetX();
787 int offsetY = getViewportOffsetY();
788
789 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700790 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700791
Sunny Goyal70660032015-05-14 00:07:08 -0700792 final int startIndex = mIsRtl ? childCount - 1 : 0;
793 final int endIndex = mIsRtl ? -1 : childCount;
794 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700795
Adam Cohen7d30a372013-07-01 17:03:59 -0700796 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700797
Adam Cohen3b185e22013-10-29 14:45:58 -0700798 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000799 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700800
Sunny Goyal6178f132016-07-11 17:30:03 -0700801 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700802 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
803 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700804 }
805
Adam Cohen0ffac432013-07-10 11:19:26 -0700806 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700807 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700809 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100810 int childTop;
811 if (lp.isFullScreenPage) {
812 childTop = offsetY;
813 } else {
814 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700815 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100816 }
817
Adam Cohen96d30a12013-07-16 18:13:21 -0700818 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700819 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800820
Winson Chung785d2eb2011-04-14 16:08:02 -0700821 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700822 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800823 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700824
Sunny Goyal6178f132016-07-11 17:30:03 -0700825 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700826 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000827
828 int pageGap = mPageSpacing;
829 int next = i + delta;
830 if (next != endIndex) {
831 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
832 } else {
833 nextLp = null;
834 }
835
836 // Prevent full screen pages from showing in the viewport
837 // when they are not the current page.
838 if (lp.isFullScreenPage) {
839 pageGap = getPaddingLeft();
840 } else if (nextLp != null && nextLp.isFullScreenPage) {
841 pageGap = getPaddingRight();
842 }
843
Sunny Goyala1fbd842015-05-20 13:40:27 -0700844 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 }
846 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700847
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700848 final LayoutTransition transition = getLayoutTransition();
849 // If the transition is running defer updating max scroll, as some empty pages could
850 // still be present, and a max scroll change could cause sudden jumps in scroll.
851 if (transition != null && transition.isRunning()) {
852 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
853
854 @Override
855 public void startTransition(LayoutTransition transition, ViewGroup container,
856 View view, int transitionType) { }
857
858 @Override
859 public void endTransition(LayoutTransition transition, ViewGroup container,
860 View view, int transitionType) {
861 // Wait until all transitions are complete.
862 if (!transition.isRunning()) {
863 transition.removeTransitionListener(this);
864 updateMaxScrollX();
865 }
866 }
867 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700868 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700869 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700870 }
871
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700872 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
873 updateCurrentPageScroll();
874 mFirstLayout = false;
875 }
876
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700877 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700878 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700879 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700880 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700881
882 if (isReordering(true)) {
883 updateDragViewTranslationDuringDrag();
884 }
Winson Chunge3193b92010-09-10 11:44:42 -0700885 }
886
Sunny Goyala1fbd842015-05-20 13:40:27 -0700887 protected int getChildGap() {
888 return 0;
889 }
890
Sunny Goyal316490e2015-06-02 09:38:28 -0700891 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700892 mMaxScrollX = computeMaxScrollX();
893 }
894
895 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700896 int childCount = getChildCount();
897 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700898 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700899 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700900 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700901 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700902 }
903 }
904
Adam Cohen3b185e22013-10-29 14:45:58 -0700905 public void setPageSpacing(int pageSpacing) {
906 mPageSpacing = pageSpacing;
907 requestLayout();
908 }
909
Winson Chunge3193b92010-09-10 11:44:42 -0700910 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700911 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700912 // Update the page indicator, we don't update the page indicator as we
913 // add/remove pages
914 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700915 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700916 }
917
Adam Cohen2591f6a2011-10-25 14:36:40 -0700918 // This ensures that when children are added, they get the correct transforms / alphas
919 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700920 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700921 invalidate();
922 }
923
Michael Jurka8b805b12012-04-18 14:23:14 -0700924 @Override
925 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700926 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700927 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700928 }
929
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700930 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700931 // Update the page indicator, we don't update the page indicator as we
932 // add/remove pages
933 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700934 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700935 }
936 }
937
938 @Override
939 public void removeView(View v) {
940 // XXX: We should find a better way to hook into this before the view
941 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700942 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700943 super.removeView(v);
944 }
945 @Override
946 public void removeViewInLayout(View v) {
947 // XXX: We should find a better way to hook into this before the view
948 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700949 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700950 super.removeViewInLayout(v);
951 }
952 @Override
953 public void removeViewAt(int index) {
954 // XXX: We should find a better way to hook into this before the view
955 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700956 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700957 super.removeViewAt(index);
958 }
959 @Override
960 public void removeAllViewsInLayout() {
961 // Update the page indicator, we don't update the page indicator as we
962 // add/remove pages
963 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700964 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700965 }
966
967 super.removeAllViewsInLayout();
968 }
969
Adam Cohen73894962011-10-31 13:17:17 -0700970 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700971 if (index < 0 || index > getChildCount() - 1) return 0;
972
Adam Cohenf698c6e2013-07-17 18:44:25 -0700973 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700974
Adam Cohenf698c6e2013-07-17 18:44:25 -0700975 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700976 }
977
Adam Cohen6f127a62014-06-12 14:54:41 -0700978 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700979 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700980 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700981 }
982
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 @Override
984 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700985 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700986 if (page != mCurrentPage || !mScroller.isFinished()) {
987 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 return true;
989 }
990 return false;
991 }
992
993 @Override
994 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700995 int focusablePage;
996 if (mNextPage != INVALID_PAGE) {
997 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700999 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 }
Winson Chung86f77532010-08-24 11:08:22 -07001001 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001003 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 }
1005 return false;
1006 }
1007
1008 @Override
1009 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001010 if (super.dispatchUnhandledMove(focused, direction)) {
1011 return true;
1012 }
1013
1014 if (mIsRtl) {
1015 if (direction == View.FOCUS_LEFT) {
1016 direction = View.FOCUS_RIGHT;
1017 } else if (direction == View.FOCUS_RIGHT) {
1018 direction = View.FOCUS_LEFT;
1019 }
1020 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001022 if (getCurrentPage() > 0) {
1023 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 return true;
1025 }
1026 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001027 if (getCurrentPage() < getPageCount() - 1) {
1028 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 return true;
1030 }
1031 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001032 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 }
1034
1035 @Override
1036 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001037 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1038 return;
1039 }
1040
Adam Cohen0ffac432013-07-10 11:19:26 -07001041 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001042 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001043 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 }
1045 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001046 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001047 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 }
1049 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001050 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001051 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 }
1053 }
1054 }
1055
1056 /**
1057 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001058 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 *
Winson Chung86f77532010-08-24 11:08:22 -07001060 * This happens when live folders requery, and if they're off page, they
1061 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 */
1063 @Override
1064 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001065 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 View v = focused;
1067 while (true) {
1068 if (v == current) {
1069 super.focusableViewAvailable(focused);
1070 return;
1071 }
1072 if (v == this) {
1073 return;
1074 }
1075 ViewParent parent = v.getParent();
1076 if (parent instanceof View) {
1077 v = (View)v.getParent();
1078 } else {
1079 return;
1080 }
1081 }
1082 }
1083
1084 /**
1085 * {@inheritDoc}
1086 */
1087 @Override
1088 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1089 if (disallowIntercept) {
1090 // We need to make sure to cancel our long press if
1091 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001092 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001093 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 }
1095 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1096 }
1097
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001098 /**
1099 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1100 */
1101 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001102 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001103 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001104 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001105 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001106 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001107 }
1108
1109 /**
1110 * Return true if a tap at (x, y) should trigger a flip to the next page.
1111 */
1112 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001113 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001114 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001115 }
1116 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001117 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001118 }
Winson Chung52aee602013-01-30 12:01:02 -08001119
Adam Cohen7d30a372013-07-01 17:03:59 -07001120 /** Returns whether x and y originated within the buffered viewport */
1121 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001122 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001123 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001124 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001125 }
1126
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 @Override
1128 public boolean onInterceptTouchEvent(MotionEvent ev) {
1129 /*
1130 * This method JUST determines whether we want to intercept the motion.
1131 * If we return true, onTouchEvent will be called and we do the actual
1132 * scrolling there.
1133 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001134 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001135
Winson Chung45e1d6e2010-11-09 17:19:49 -08001136 // Skip touch handling if there are no pages to swipe
1137 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1138
Winson Chung321e9ee2010-08-09 13:37:56 -07001139 /*
1140 * Shortcut the most recurring case: the user is in the dragging
1141 * state and he is moving his finger. We want to intercept this
1142 * motion.
1143 */
1144 final int action = ev.getAction();
1145 if ((action == MotionEvent.ACTION_MOVE) &&
1146 (mTouchState == TOUCH_STATE_SCROLLING)) {
1147 return true;
1148 }
1149
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 switch (action & MotionEvent.ACTION_MASK) {
1151 case MotionEvent.ACTION_MOVE: {
1152 /*
1153 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1154 * whether the user has moved far enough from his original down touch.
1155 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001156 if (mActivePointerId != INVALID_POINTER) {
1157 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001158 }
1159 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1160 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1161 // i.e. fall through to the next case (don't break)
1162 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1163 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001164 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 }
1166
1167 case MotionEvent.ACTION_DOWN: {
1168 final float x = ev.getX();
1169 final float y = ev.getY();
1170 // Remember location of down touch
1171 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001172 mDownMotionY = y;
1173 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 mLastMotionX = x;
1175 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001176 float[] p = mapPointFromViewToParent(this, x, y);
1177 mParentDownMotionX = p[0];
1178 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001179 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001180 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001182
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 /*
1184 * If being flinged and user touches the screen, initiate drag;
1185 * otherwise don't. mScroller.isFinished should be false when
1186 * being flinged.
1187 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001188 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001189 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001190
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001191 if (finishedScrolling) {
1192 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001193 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001194 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001195 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001196 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001197 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001198 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1199 mTouchState = TOUCH_STATE_SCROLLING;
1200 } else {
1201 mTouchState = TOUCH_STATE_REST;
1202 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001203 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001204
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 break;
1206 }
1207
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001209 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001210 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 break;
1212
1213 case MotionEvent.ACTION_POINTER_UP:
1214 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001215 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 break;
1217 }
1218
1219 /*
1220 * The only time we want to intercept motion events is if we are in the
1221 * drag mode.
1222 */
1223 return mTouchState != TOUCH_STATE_REST;
1224 }
1225
Adam Cohenf8d28232011-02-01 21:47:00 -08001226 protected void determineScrollingStart(MotionEvent ev) {
1227 determineScrollingStart(ev, 1.0f);
1228 }
1229
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 /*
1231 * Determines if we should change the touch state to start scrolling after the
1232 * user moves their touch point too far.
1233 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001234 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001235 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001237 if (pointerIndex == -1) return;
1238
1239 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 final float x = ev.getX(pointerIndex);
1241 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001242 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1243
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245
Adam Cohenf8d28232011-02-01 21:47:00 -08001246 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001248
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001249 if (xMoved) {
1250 // Scroll if the user moved far enough along the X axis
1251 mTouchState = TOUCH_STATE_SCROLLING;
1252 mTotalMotionX += Math.abs(mLastMotionX - x);
1253 mLastMotionX = x;
1254 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001255 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001256 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001257 // Stop listening for things like pinches.
1258 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001259 }
1260 }
1261
1262 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001263 // Try canceling the long press. It could also have been scheduled
1264 // by a distant descendant, so use the mAllowLongPress flag to block
1265 // everything
1266 final View currentPage = getPageAt(mCurrentPage);
1267 if (currentPage != null) {
1268 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 }
1270 }
1271
Adam Cohenb5ba0972011-09-07 18:02:31 -07001272 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001273 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001274
Adam Cohenedb40762013-07-18 16:45:45 -07001275 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001276 int count = getChildCount();
1277
1278 final int totalDistance;
1279
1280 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001281 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001282 adjacentPage = page - 1;
1283 }
1284
1285 if (adjacentPage < 0 || adjacentPage > count - 1) {
1286 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1287 } else {
1288 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1289 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001290
1291 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001292 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1293 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001294 return scrollProgress;
1295 }
1296
Adam Cohenedb40762013-07-18 16:45:45 -07001297 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001298 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001299 return 0;
1300 } else {
1301 return mPageScrolls[index];
1302 }
1303 }
1304
Adam Cohen8d769d62017-06-23 17:27:38 -07001305 // This curve determines how the effect of scrolling over the limits of the page dimishes
1306 // as the user pulls further and further from the bounds
1307 private float overScrollInfluenceCurve(float f) {
1308 f -= 1.0f;
1309 return f * f * f + 1.0f;
1310 }
1311
1312 protected float acceleratedOverFactor(float amount) {
1313 int screenSize = getViewportWidth();
1314
1315 // We want to reach the max over scroll effect when the user has
1316 // over scrolled half the size of the screen
1317 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1318
1319 if (Float.compare(f, 0f) == 0) return 0;
1320
1321 // Clamp this factor, f, to -1 < f < 1
1322 if (Math.abs(f) >= 1) {
1323 f /= Math.abs(f);
1324 }
1325 return f;
1326 }
1327
Adam Cohen564a2e72013-10-09 14:47:32 -07001328 // While layout transitions are occurring, a child's position may stray from its baseline
1329 // position. This method returns the magnitude of this stray at any given time.
1330 public int getLayoutTransitionOffsetForPage(int index) {
1331 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1332 return 0;
1333 } else {
1334 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001335
1336 int scrollOffset = 0;
1337 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1338 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001339 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001340 }
1341
Adam Cohen564a2e72013-10-09 14:47:32 -07001342 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1343 return (int) (child.getX() - baselineX);
1344 }
1345 }
1346
Adam Cohenb5ba0972011-09-07 18:02:31 -07001347 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001348 int screenSize = getViewportWidth();
Adam Cohen8d769d62017-06-23 17:27:38 -07001349
Adam Cohene0f66b52010-11-23 15:06:07 -08001350 float f = (amount / screenSize);
Adam Cohen8d769d62017-06-23 17:27:38 -07001351
1352 if (Float.compare(f, 0f) == 0) return;
1353
1354 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1355
1356 // Clamp this factor, f, to -1 < f < 1
1357 if (Math.abs(f) >= 1) {
1358 f /= Math.abs(f);
1359 }
1360
1361 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
1362 if (amount < 0) {
1363 mOverScrollX = overScrollAmount;
1364 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001365 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001366 mOverScrollX = mMaxScrollX + overScrollAmount;
1367 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001368 }
1369 invalidate();
1370 }
1371
Adam Cohenb5ba0972011-09-07 18:02:31 -07001372 protected void overScroll(float amount) {
1373 dampedOverScroll(amount);
1374 }
1375
Adam Cohen8d769d62017-06-23 17:27:38 -07001376 protected float maxOverScroll() {
1377 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
1378 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
1379 float f = 1.0f;
1380 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1381 return OVERSCROLL_DAMP_FACTOR * f;
1382 }
1383
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001384 /**
1385 * return true if freescroll has been enabled, false otherwise
1386 */
1387 public boolean enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001388 setEnableFreeScroll(true);
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001389 return true;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 }
1391
Winson Chungdc61c4d2015-04-20 18:26:57 -07001392 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001393 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001394 }
1395
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001396 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001397 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001398 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001399 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1400 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1401 } else {
1402 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1403 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1404 }
1405 }
1406
Adam Cohenf9618852013-11-08 06:45:03 -08001407 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001408 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001409 mFreeScroll = freeScroll;
1410
Adam Cohenf9618852013-11-08 06:45:03 -08001411 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001412 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001413 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1415 setCurrentPage(mTempVisiblePagesRange[0]);
1416 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1417 setCurrentPage(mTempVisiblePagesRange[1]);
1418 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001419 } else if (wasFreeScroll) {
1420 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421 }
1422
1423 setEnableOverscroll(!freeScroll);
1424 }
1425
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001426 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001427 mAllowOverScroll = enable;
1428 }
1429
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001430 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001431 if (mDragView != null) {
1432 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1433 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001434 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001435 int minDistance = Integer.MAX_VALUE;
1436 int minIndex = indexOfChild(mDragView);
1437 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1438 View page = getPageAt(i);
1439 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1440 int d = Math.abs(dragX - pageX);
1441 if (d < minDistance) {
1442 minIndex = i;
1443 minDistance = d;
1444 }
1445 }
1446 return minIndex;
1447 }
1448 return -1;
1449 }
1450
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 @Override
1452 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001453 super.onTouchEvent(ev);
1454
Winson Chung45e1d6e2010-11-09 17:19:49 -08001455 // Skip touch handling if there are no pages to swipe
1456 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1457
Michael Jurkab8f06722010-10-10 15:58:46 -07001458 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001459
1460 final int action = ev.getAction();
1461
1462 switch (action & MotionEvent.ACTION_MASK) {
1463 case MotionEvent.ACTION_DOWN:
1464 /*
1465 * If being flinged and user touches, stop the fling. isFinished
1466 * will be false if being flinged.
1467 */
1468 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001469 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 }
1471
1472 // Remember where the motion event started
1473 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001474 mDownMotionY = mLastMotionY = ev.getY();
1475 mDownScrollX = getScrollX();
1476 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1477 mParentDownMotionX = p[0];
1478 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001479 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001480 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001482
Michael Jurka0142d492010-08-25 17:46:15 -07001483 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001484 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001485 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001486 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 break;
1488
1489 case MotionEvent.ACTION_MOVE:
1490 if (mTouchState == TOUCH_STATE_SCROLLING) {
1491 // Scroll to follow the motion event
1492 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001493
1494 if (pointerIndex == -1) return true;
1495
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001497 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001498
Adam Cohenaefd4e12011-02-14 16:39:38 -08001499 mTotalMotionX += Math.abs(deltaX);
1500
Winson Chungc0844aa2011-02-02 15:25:58 -08001501 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1502 // keep the remainder because we are actually testing if we've moved from the last
1503 // scrolled position (which is discrete).
1504 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001505 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001506 mLastMotionX = x;
1507 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 } else {
1509 awakenScrollBars();
1510 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001511 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1512 // Update the last motion position
1513 mLastMotionX = ev.getX();
1514 mLastMotionY = ev.getY();
1515
1516 // Update the parent down so that our zoom animations take this new movement into
1517 // account
1518 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1519 mParentDownMotionX = pt[0];
1520 mParentDownMotionY = pt[1];
1521 updateDragViewTranslationDuringDrag();
1522
1523 // Find the closest page to the touch point
1524 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001525
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1527 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1528 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1529 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1530
Adam Cohenf358a4b2013-07-23 16:47:31 -07001531 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001532 // Do not allow any page to be moved to 0th position.
1533 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001534 mTempVisiblePagesRange[0] = 0;
1535 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001536 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001537 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1538 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1539 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1540 mSidePageHoverIndex = pageUnderPointIndex;
1541 mSidePageHoverRunnable = new Runnable() {
1542 @Override
1543 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001544 // Setup the scroll to the correct page before we swap the views
1545 snapToPage(pageUnderPointIndex);
1546
1547 // For each of the pages between the paged view and the drag view,
1548 // animate them from the previous position to the new position in
1549 // the layout (as a result of the drag view moving in the layout)
1550 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1551 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1552 dragViewIndex + 1 : pageUnderPointIndex;
1553 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1554 dragViewIndex - 1 : pageUnderPointIndex;
1555 for (int i = lowerIndex; i <= upperIndex; ++i) {
1556 View v = getChildAt(i);
1557 // dragViewIndex < pageUnderPointIndex, so after we remove the
1558 // drag view all subsequent views to pageUnderPointIndex will
1559 // shift down.
1560 int oldX = getViewportOffsetX() + getChildOffset(i);
1561 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1562
1563 // Animate the view translation from its old position to its new
1564 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001565 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001566 if (anim != null) {
1567 anim.cancel();
1568 }
1569
1570 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001571 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001572 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001573 anim.start();
1574 v.setTag(anim);
1575 }
1576
1577 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001578 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001579 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001580 if (mPageIndicator != null) {
1581 mPageIndicator.setActiveMarker(getNextPage());
1582 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001583 }
1584 };
1585 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1586 }
1587 } else {
1588 removeCallbacks(mSidePageHoverRunnable);
1589 mSidePageHoverIndex = -1;
1590 }
Adam Cohen564976a2010-10-13 18:52:07 -07001591 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001592 determineScrollingStart(ev);
1593 }
1594 break;
1595
1596 case MotionEvent.ACTION_UP:
1597 if (mTouchState == TOUCH_STATE_SCROLLING) {
1598 final int activePointerId = mActivePointerId;
1599 final int pointerIndex = ev.findPointerIndex(activePointerId);
1600 final float x = ev.getX(pointerIndex);
1601 final VelocityTracker velocityTracker = mVelocityTracker;
1602 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1603 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001604 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001605 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001606 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1607 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001608
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001609 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1610
Adam Cohen00481b32011-11-18 12:03:48 -08001611 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001612 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001613
Adam Cohenf358a4b2013-07-23 16:47:31 -07001614 if (!mFreeScroll) {
1615 // In the case that the page is moved far to one direction and then is flung
1616 // in the opposite direction, we use a threshold to determine whether we should
1617 // just return to the starting page, or if we should skip one further.
1618 boolean returnToOriginalPage = false;
1619 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1620 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1621 returnToOriginalPage = true;
1622 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001623
Adam Cohenf358a4b2013-07-23 16:47:31 -07001624 int finalPage;
1625 // We give flings precedence over large moves, which is why we short-circuit our
1626 // test for a large move if a fling has been registered. That is, a large
1627 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001628 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1629 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001630 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1631 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1632 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1633 snapToPageWithVelocity(finalPage, velocityX);
1634 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1635 (isFling && isVelocityXLeft)) &&
1636 mCurrentPage < getChildCount() - 1) {
1637 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1638 snapToPageWithVelocity(finalPage, velocityX);
1639 } else {
1640 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001641 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001642 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001643 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001644 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001645 }
1646
1647 float scaleX = getScaleX();
1648 int vX = (int) (-velocityX * scaleX);
1649 int initialScrollX = (int) (getScrollX() * scaleX);
1650
Adam Cohenf9618852013-11-08 06:45:03 -08001651 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001652 mScroller.fling(initialScrollX,
1653 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001654 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001655 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001656 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001657 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001658 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1659 // at this point we have not moved beyond the touch slop
1660 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1661 // we can just page
1662 int nextPage = Math.max(0, mCurrentPage - 1);
1663 if (nextPage != mCurrentPage) {
1664 snapToPage(nextPage);
1665 } else {
1666 snapToDestination();
1667 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001668 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001669 // at this point we have not moved beyond the touch slop
1670 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1671 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001672 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1673 if (nextPage != mCurrentPage) {
1674 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001675 } else {
1676 snapToDestination();
1677 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001678 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1679 // Update the last motion position
1680 mLastMotionX = ev.getX();
1681 mLastMotionY = ev.getY();
1682
1683 // Update the parent down so that our zoom animations take this new movement into
1684 // account
1685 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1686 mParentDownMotionX = pt[0];
1687 mParentDownMotionY = pt[1];
1688 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001689 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001690 if (!mCancelTap) {
1691 onUnhandledTap(ev);
1692 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001693 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001694
1695 // Remove the callback to wait for the side page hover timeout
1696 removeCallbacks(mSidePageHoverRunnable);
1697 // End any intermediate reordering states
1698 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001699 break;
1700
1701 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001702 if (mTouchState == TOUCH_STATE_SCROLLING) {
1703 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001704 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001705 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001706 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001707 break;
1708
1709 case MotionEvent.ACTION_POINTER_UP:
1710 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001711 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001712 break;
1713 }
1714
1715 return true;
1716 }
1717
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001718 protected boolean shouldFlingForVelocity(int velocityX) {
1719 return Math.abs(velocityX) > mFlingThresholdVelocity;
1720 }
1721
Adam Cohen7d30a372013-07-01 17:03:59 -07001722 private void resetTouchState() {
1723 releaseVelocityTracker();
1724 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001725 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001726 mTouchState = TOUCH_STATE_REST;
1727 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001728 }
1729
Adam Cohenc2d6e892014-10-16 09:49:24 -07001730 /**
1731 * Triggered by scrolling via touch
1732 */
1733 protected void onScrollInteractionBegin() {
1734 }
1735
1736 protected void onScrollInteractionEnd() {
1737 }
1738
Adam Cohen1697b792013-09-17 19:08:21 -07001739 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001740 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001741 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001742
Winson Chung185d7162011-02-28 13:47:29 -08001743 @Override
1744 public boolean onGenericMotionEvent(MotionEvent event) {
1745 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1746 switch (event.getAction()) {
1747 case MotionEvent.ACTION_SCROLL: {
1748 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1749 final float vscroll;
1750 final float hscroll;
1751 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1752 vscroll = 0;
1753 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1754 } else {
1755 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1756 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1757 }
1758 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001759 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001760 : (hscroll > 0 || vscroll > 0);
1761 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001762 scrollRight();
1763 } else {
1764 scrollLeft();
1765 }
1766 return true;
1767 }
1768 }
1769 }
1770 }
1771 return super.onGenericMotionEvent(event);
1772 }
1773
Michael Jurkab8f06722010-10-10 15:58:46 -07001774 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1775 if (mVelocityTracker == null) {
1776 mVelocityTracker = VelocityTracker.obtain();
1777 }
1778 mVelocityTracker.addMovement(ev);
1779 }
1780
1781 private void releaseVelocityTracker() {
1782 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001783 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001784 mVelocityTracker.recycle();
1785 mVelocityTracker = null;
1786 }
1787 }
1788
Winson Chung321e9ee2010-08-09 13:37:56 -07001789 private void onSecondaryPointerUp(MotionEvent ev) {
1790 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1791 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1792 final int pointerId = ev.getPointerId(pointerIndex);
1793 if (pointerId == mActivePointerId) {
1794 // This was our active pointer going up. Choose a new
1795 // active pointer and adjust accordingly.
1796 // TODO: Make this decision more intelligent.
1797 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1798 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1799 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001800 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001801 mActivePointerId = ev.getPointerId(newPointerIndex);
1802 if (mVelocityTracker != null) {
1803 mVelocityTracker.clear();
1804 }
1805 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001806 }
1807
Winson Chung321e9ee2010-08-09 13:37:56 -07001808 @Override
1809 public void requestChildFocus(View child, View focused) {
1810 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001811 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001812 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001813 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001814 }
1815 }
1816
Adam Cohend19d3ca2010-09-15 14:43:42 -07001817 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001818 return getPageNearestToCenterOfScreen(getScrollX());
1819 }
1820
1821 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1822 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001823 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001824 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001825 final int childCount = getChildCount();
1826 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001827 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001828 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001829 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001830 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001831 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1832 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1833 minDistanceFromScreenCenter = distanceFromScreenCenter;
1834 minDistanceFromScreenCenterIndex = i;
1835 }
1836 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001837 return minDistanceFromScreenCenterIndex;
1838 }
1839
1840 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001841 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1842 }
1843
1844 protected boolean isInOverScroll() {
1845 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1846 }
1847
1848 protected int getPageSnapDuration() {
1849 if (isInOverScroll()) {
1850 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1851 }
1852 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001853 }
1854
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001855 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001856 public ScrollInterpolator() {
1857 }
1858
1859 public float getInterpolation(float t) {
1860 t -= 1.0f;
1861 return t*t*t*t*t + 1;
1862 }
1863 }
1864
1865 // We want the duration of the page snap animation to be influenced by the distance that
1866 // the screen has to travel, however, we don't want this duration to be effected in a
1867 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1868 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001869 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001870 f -= 0.5f; // center the values about 0.
1871 f *= 0.3f * Math.PI / 2.0f;
1872 return (float) Math.sin(f);
1873 }
1874
Michael Jurka0142d492010-08-25 17:46:15 -07001875 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001876 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001877 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001878
Adam Cohenedb40762013-07-18 16:45:45 -07001879 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001880 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001881 int duration = 0;
1882
Sunny Goyal4d113a52015-05-27 10:05:28 -07001883 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001884 // If the velocity is low enough, then treat this more as an automatic page advance
1885 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001886 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001887 return;
1888 }
1889
1890 // Here we compute a "distance" that will be used in the computation of the overall
1891 // snap duration. This is a function of the actual distance that needs to be traveled;
1892 // we keep this value close to half screen size in order to reduce the variance in snap
1893 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001894 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001895 float distance = halfScreenSize + halfScreenSize *
1896 distanceInfluenceForSnapDuration(distanceRatio);
1897
1898 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001899 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001900
1901 // we want the page's snap velocity to approximately match the velocity at which the
1902 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001903 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1904 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001905
1906 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001907 }
1908
Sunny Goyal1740d902015-05-27 11:14:01 -07001909 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001910 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001911 }
1912
Adam Cohenf9c184a2016-01-15 16:47:43 -08001913 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001914 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001915 }
1916
Michael Jurka0142d492010-08-25 17:46:15 -07001917 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001918 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001919 }
1920
Adam Cohenf9618852013-11-08 06:45:03 -08001921 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1922 snapToPage(whichPage, duration, false, interpolator);
1923 }
1924
1925 protected void snapToPage(int whichPage, int duration, boolean immediate,
1926 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001927 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001928
Adam Cohenedb40762013-07-18 16:45:45 -07001929 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001930 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001931 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001932 }
1933
1934 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001935 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001936 }
Michael Jurka0142d492010-08-25 17:46:15 -07001937
Adam Cohenf9618852013-11-08 06:45:03 -08001938 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1939 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001940 whichPage = validateNewPage(whichPage);
1941
Adam Cohen7d30a372013-07-01 17:03:59 -07001942 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001943
Winson Chung321e9ee2010-08-09 13:37:56 -07001944 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001945 if (immediate) {
1946 duration = 0;
1947 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 duration = Math.abs(delta);
1949 }
1950
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001951 if (duration != 0) {
1952 pageBeginTransition();
1953 }
1954
Adam Cohenf358a4b2013-07-23 16:47:31 -07001955 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001956 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001957 }
Adam Cohenf9618852013-11-08 06:45:03 -08001958
1959 if (interpolator != null) {
1960 mScroller.setInterpolator(interpolator);
1961 } else {
1962 mScroller.setInterpolator(mDefaultInterpolator);
1963 }
1964
Sunny Goyalc86df472016-02-25 09:19:38 -08001965 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001966
Adam Cohen674531f2013-12-13 15:07:14 -08001967 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001968
1969 // Trigger a compute() to finish switching pages if necessary
1970 if (immediate) {
1971 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001972 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001973 }
1974
Winson Chung321e9ee2010-08-09 13:37:56 -07001975 invalidate();
1976 }
1977
Winson Chung321e9ee2010-08-09 13:37:56 -07001978 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001979 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001980 }
1981
1982 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001983 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 }
1985
Adam Cohendbdff6b2013-09-18 19:09:15 -07001986 @Override
1987 public boolean performLongClick() {
1988 mCancelTap = true;
1989 return super.performLongClick();
1990 }
1991
Winson Chung321e9ee2010-08-09 13:37:56 -07001992 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001993 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001994
1995 SavedState(Parcelable superState) {
1996 super(superState);
1997 }
1998
Adam Cohen091440a2015-03-18 14:16:05 -07001999 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002000 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002001 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002002 }
2003
2004 @Override
2005 public void writeToParcel(Parcel out, int flags) {
2006 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002007 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002008 }
2009
2010 public static final Parcelable.Creator<SavedState> CREATOR =
2011 new Parcelable.Creator<SavedState>() {
2012 public SavedState createFromParcel(Parcel in) {
2013 return new SavedState(in);
2014 }
2015
2016 public SavedState[] newArray(int size) {
2017 return new SavedState[size];
2018 }
2019 };
2020 }
2021
Adam Cohen7d30a372013-07-01 17:03:59 -07002022 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002023 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002024 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08002025 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
2026 new PropertyListBuilder()
2027 .scale(1)
2028 .translationX(0)
2029 .translationY(0)
2030 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002031 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002032 anim.addListener(new AnimatorListenerAdapter() {
2033 @Override
2034 public void onAnimationEnd(Animator animation) {
2035 onPostReorderingAnimationCompleted();
2036 }
2037 });
2038 anim.start();
2039 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002040 }
2041
Sunny Goyal1d08f702015-05-04 15:50:25 -07002042 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002043 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2044 mTouchState = TOUCH_STATE_REORDERING;
2045 mIsReordering = true;
2046
Adam Cohen7d30a372013-07-01 17:03:59 -07002047 // We must invalidate to trigger a redraw to update the layers such that the drag view
2048 // is always drawn on top
2049 invalidate();
2050 }
2051
Adam Cohen091440a2015-03-18 14:16:05 -07002052 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002053 // Trigger the callback when reordering has settled
2054 --mPostReorderingPreZoomInRemainingAnimationCount;
2055 if (mPostReorderingPreZoomInRunnable != null &&
2056 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2057 mPostReorderingPreZoomInRunnable.run();
2058 mPostReorderingPreZoomInRunnable = null;
2059 }
2060 }
2061
Sunny Goyal1d08f702015-05-04 15:50:25 -07002062 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002063 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002064 }
2065
Adam Cohenf358a4b2013-07-23 16:47:31 -07002066 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002067 int dragViewIndex = indexOfChild(v);
2068
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002069 // Do not allow the first page to be moved around
2070 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002071
Adam Cohen7d30a372013-07-01 17:03:59 -07002072 mTempVisiblePagesRange[0] = 0;
2073 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002074 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002075 mReorderingStarted = true;
2076
2077 // Check if we are within the reordering range
2078 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002079 dragViewIndex <= mTempVisiblePagesRange[1]) {
2080 // Find the drag view under the pointer
2081 mDragView = getChildAt(dragViewIndex);
2082 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2083 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002084 snapToPage(getPageNearestToCenterOfScreen());
2085 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002086 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002087 return true;
2088 }
2089 return false;
2090 }
2091
2092 boolean isReordering(boolean testTouchState) {
2093 boolean state = mIsReordering;
2094 if (testTouchState) {
2095 state &= (mTouchState == TOUCH_STATE_REORDERING);
2096 }
2097 return state;
2098 }
2099 void endReordering() {
2100 // For simplicity, we call endReordering sometimes even if reordering was never started.
2101 // In that case, we don't want to do anything.
2102 if (!mReorderingStarted) return;
2103 mReorderingStarted = false;
2104
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002105 mPostReorderingPreZoomInRunnable = new Runnable() {
2106 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002107 // If we haven't flung-to-delete the current child,
2108 // then we just animate the drag view back into position
2109 onEndReordering();
2110
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002111 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002112 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002113 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002114
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002115 mPostReorderingPreZoomInRemainingAnimationCount =
2116 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2117 // Snap to the current page
2118 snapToPage(indexOfChild(mDragView), 0);
2119 // Animate the drag view back to the front position
2120 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002121 }
2122
Winson Chung6a0f57d2011-06-29 20:10:49 -07002123 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002124 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002125 @Override
2126 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2127 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002128 info.setScrollable(getPageCount() > 1);
2129 if (getCurrentPage() < getPageCount() - 1) {
2130 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2131 }
2132 if (getCurrentPage() > 0) {
2133 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2134 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002135 info.setClassName(getClass().getName());
2136
2137 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2138 // Besides disabling the accessibility long-click, this also prevents this view from getting
2139 // accessibility focus.
2140 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002141 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002142 }
2143
2144 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002145 public void sendAccessibilityEvent(int eventType) {
2146 // Don't let the view send real scroll events.
2147 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2148 super.sendAccessibilityEvent(eventType);
2149 }
2150 }
2151
2152 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002153 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2154 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002155 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002156 }
2157
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002158 @Override
2159 public boolean performAccessibilityAction(int action, Bundle arguments) {
2160 if (super.performAccessibilityAction(action, arguments)) {
2161 return true;
2162 }
2163 switch (action) {
2164 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2165 if (getCurrentPage() < getPageCount() - 1) {
2166 scrollRight();
2167 return true;
2168 }
2169 } break;
2170 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2171 if (getCurrentPage() > 0) {
2172 scrollLeft();
2173 return true;
2174 }
2175 } break;
2176 }
2177 return false;
2178 }
2179
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002180 protected String getPageIndicatorDescription() {
2181 return getCurrentPageDescription();
2182 }
2183
Adam Cohen0ffac432013-07-10 11:19:26 -07002184 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002185 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002186 getNextPage() + 1, getChildCount());
2187 }
2188
Winson Chungd11265e2011-08-30 13:37:23 -07002189 @Override
2190 public boolean onHoverEvent(android.view.MotionEvent event) {
2191 return true;
2192 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002193}