blob: 185c8879d94bba01e8f000982bfd696aea970849 [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 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700411 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700412 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700413 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700414 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800415 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 }
417
Winson Chung8c87cd82013-07-23 16:20:10 -0700418 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800419 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
420 * has settled.
421 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700422 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800423 updatePageIndicator();
424 }
425
426 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700427 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700428 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700429 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700430 if (!isReordering(false)) {
431 mPageIndicator.setActiveMarker(getNextPage());
432 }
Winson Chungd2be3812013-07-16 11:11:32 -0700433 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700435 protected void pageBeginTransition() {
436 if (!mIsPageInTransition) {
437 mIsPageInTransition = true;
438 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700439 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700440 }
441
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700442 protected void pageEndTransition() {
443 if (mIsPageInTransition) {
444 mIsPageInTransition = false;
445 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700446 }
Michael Jurka0142d492010-08-25 17:46:15 -0700447 }
448
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700449 protected boolean isPageInTransition() {
450 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700451 }
452
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700453 /**
454 * Called when the page starts moving as part of the scroll. Subclasses can override this
455 * to provide custom behavior during animation.
456 */
457 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700458 }
459
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700460 /**
461 * Called when the page ends moving as part of the scroll. Subclasses can override this
462 * to provide custom behavior during animation.
463 */
464 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700465 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700466 }
467
Winson Chung321e9ee2010-08-09 13:37:56 -0700468 /**
Winson Chung86f77532010-08-24 11:08:22 -0700469 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 *
471 * @param l The listener used to respond to long clicks.
472 */
473 @Override
474 public void setOnLongClickListener(OnLongClickListener l) {
475 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700476 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700478 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 }
Adam Cohen1697b792013-09-17 19:08:21 -0700480 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 }
482
Sunny Goyalc86df472016-02-25 09:19:38 -0800483 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700484 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800485 }
486
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800488 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800489 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800490 }
491
492 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700493 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700494 // In free scroll mode, we clamp the scrollX
495 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700496 // If the scroller is trying to move to a location beyond the maximum allowed
497 // in the free scroll mode, we make sure to end the scroll operation.
498 if (!mScroller.isFinished() &&
499 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700500 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700501 }
502
Adam Cohenf358a4b2013-07-23 16:47:31 -0700503 x = Math.min(x, mFreeScrollMaxScrollX);
504 x = Math.max(x, mFreeScrollMinScrollX);
505 }
506
Adam Cohen8d769d62017-06-23 17:27:38 -0700507 mUnboundedScrollX = x;
508
Sunny Goyal70660032015-05-14 00:07:08 -0700509 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
510 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700511 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700512 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800513 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700514 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700515 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700516 overScroll(x - mMaxScrollX);
517 } else {
518 overScroll(x);
519 }
Adam Cohen68d73932010-11-15 10:50:58 -0800520 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700521 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700522 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800523 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700524 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700525 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700526 overScroll(x);
527 } else {
528 overScroll(x - mMaxScrollX);
529 }
Adam Cohen68d73932010-11-15 10:50:58 -0800530 }
531 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700532 if (mWasInOverscroll) {
533 overScroll(0);
534 mWasInOverscroll = false;
535 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700536 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800537 super.scrollTo(x, y);
538 }
539
Adam Cohen7d30a372013-07-01 17:03:59 -0700540 // Update the last motion events when scrolling
541 if (isReordering(true)) {
542 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
543 mLastMotionX = p[0];
544 mLastMotionY = p[1];
545 updateDragViewTranslationDuringDrag();
546 }
Michael Jurka0142d492010-08-25 17:46:15 -0700547 }
548
Adam Cohen53805212013-10-01 10:39:23 -0700549 private void sendScrollAccessibilityEvent() {
550 AccessibilityManager am =
551 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
552 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700553 if (mCurrentPage != getNextPage()) {
554 AccessibilityEvent ev =
555 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700556 ev.setScrollable(true);
557 ev.setScrollX(getScrollX());
558 ev.setScrollY(getScrollY());
559 ev.setMaxScrollX(mMaxScrollX);
560 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700561
Vadim Tryshevf4715972015-05-08 17:21:03 -0700562 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700563 }
Adam Cohen53805212013-10-01 10:39:23 -0700564 }
565 }
566
Michael Jurka0142d492010-08-25 17:46:15 -0700567 // we moved this functionality to a helper function so SmoothPagedView can reuse it
568 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800569 return computeScrollHelper(true);
570 }
571
572 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700574 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800575 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700576 || getScrollY() != mScroller.getCurrY()
577 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700578 float scaleX = mFreeScroll ? getScaleX() : 1f;
579 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
580 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700581 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800582 if (shouldInvalidate) {
583 invalidate();
584 }
Michael Jurka0142d492010-08-25 17:46:15 -0700585 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800586 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700587 sendScrollAccessibilityEvent();
588
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700589 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700590 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700591 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700592 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700593
Adam Cohen73aa9752010-11-24 16:26:58 -0800594 // We don't want to trigger a page end moving unless the page has settled
595 // and the user has stopped scrolling
596 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700597 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800598 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700599
Adam Cohen7d30a372013-07-01 17:03:59 -0700600 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700601 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700602 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700603 if (am.isEnabled()) {
604 // Notify the user when the page changes
605 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700606 }
Michael Jurka0142d492010-08-25 17:46:15 -0700607 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 }
Michael Jurka0142d492010-08-25 17:46:15 -0700609 return false;
610 }
611
612 @Override
613 public void computeScroll() {
614 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 }
616
Adam Cohen96d30a12013-07-16 18:13:21 -0700617 public static class LayoutParams extends ViewGroup.LayoutParams {
618 public boolean isFullScreenPage = false;
619
Sunny Goyal7c786f72016-06-01 14:08:21 -0700620 // If true, the start edge of the page snaps to the start edge of the viewport.
621 public boolean matchStartEdge = false;
622
Adam Cohen96d30a12013-07-16 18:13:21 -0700623 /**
624 * {@inheritDoc}
625 */
626 public LayoutParams(int width, int height) {
627 super(width, height);
628 }
629
Sunny Goyal41b22c02015-05-14 15:20:48 -0700630 public LayoutParams(Context context, AttributeSet attrs) {
631 super(context, attrs);
632 }
633
Adam Cohen96d30a12013-07-16 18:13:21 -0700634 public LayoutParams(ViewGroup.LayoutParams source) {
635 super(source);
636 }
637 }
638
Sunny Goyal41b22c02015-05-14 15:20:48 -0700639 @Override
640 public LayoutParams generateLayoutParams(AttributeSet attrs) {
641 return new LayoutParams(getContext(), attrs);
642 }
643
644 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700645 protected LayoutParams generateDefaultLayoutParams() {
646 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
647 }
648
Sunny Goyal41b22c02015-05-14 15:20:48 -0700649 @Override
650 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
651 return new LayoutParams(p);
652 }
653
654 @Override
655 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
656 return p instanceof LayoutParams;
657 }
658
Adam Cohenedb40762013-07-18 16:45:45 -0700659 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700660 LayoutParams lp = generateDefaultLayoutParams();
661 lp.isFullScreenPage = true;
662 super.addView(page, 0, lp);
663 }
664
Adam Cohen410f3cd2013-09-22 12:09:32 -0700665 public int getNormalChildHeight() {
666 return mNormalChildHeight;
667 }
668
Winson Chung321e9ee2010-08-09 13:37:56 -0700669 @Override
670 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700671 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700672 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
673 return;
674 }
675
Adam Cohen7d30a372013-07-01 17:03:59 -0700676 // We measure the dimensions of the PagedView to be larger than the pages so that when we
677 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700678 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
679 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
680 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700681 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800682 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700683 // viewport, we can be at most one and a half screens offset once we scale down
684 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700685 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
686 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700687
Winson Chungc82d2622013-11-06 13:23:29 -0800688 int parentWidthSize = (int) (2f * maxSize);
689 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700690 int scaledWidthSize, scaledHeightSize;
691 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700692 scaledWidthSize = (int) (parentWidthSize / mMinScale);
693 scaledHeightSize = (int) (parentHeightSize / mMinScale);
694 } else {
695 scaledWidthSize = widthSize;
696 scaledHeightSize = heightSize;
697 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700698 mViewport.set(0, 0, widthSize, heightSize);
699
700 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
701 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
702 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 }
704
Winson Chung8aad6102012-05-11 16:27:49 -0700705 // Return early if we aren't given a proper dimension
706 if (widthSize <= 0 || heightSize <= 0) {
707 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
708 return;
709 }
710
Adam Lesinski6b879f02010-11-04 16:15:23 -0700711 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
712 * of the All apps view on XLarge displays to not take up more space then it needs. Width
713 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
714 * each page to have the same width.
715 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 final int verticalPadding = getPaddingTop() + getPaddingBottom();
717 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700718
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700719 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700721 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700722 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700723 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
724 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
725 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
726 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 final int childCount = getChildCount();
728 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700729 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700730 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100731 if (child.getVisibility() != GONE) {
732 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700733
Vladimir Marko2824b072013-10-04 16:42:17 +0100734 int childWidthMode;
735 int childHeightMode;
736 int childWidth;
737 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700738
Vladimir Marko2824b072013-10-04 16:42:17 +0100739 if (!lp.isFullScreenPage) {
740 if (lp.width == LayoutParams.WRAP_CONTENT) {
741 childWidthMode = MeasureSpec.AT_MOST;
742 } else {
743 childWidthMode = MeasureSpec.EXACTLY;
744 }
745
746 if (lp.height == LayoutParams.WRAP_CONTENT) {
747 childHeightMode = MeasureSpec.AT_MOST;
748 } else {
749 childHeightMode = MeasureSpec.EXACTLY;
750 }
751
Adam Cohen3b185e22013-10-29 14:45:58 -0700752 childWidth = getViewportWidth() - horizontalPadding
753 - mInsets.left - mInsets.right;
754 childHeight = getViewportHeight() - verticalPadding
755 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100756 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700757 } else {
758 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700759 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100760
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800761 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700762 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700763 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700764 if (referenceChildWidth == 0) {
765 referenceChildWidth = childWidth;
766 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700767
Vladimir Marko2824b072013-10-04 16:42:17 +0100768 final int childWidthMeasureSpec =
769 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
770 final int childHeightMeasureSpec =
771 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
772 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700773 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700774 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700775 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800776 }
777
Sunny Goyalcf25b522015-07-09 00:01:18 -0700778 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700780 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700781 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700782 return;
783 }
784
Winson Chung785d2eb2011-04-14 16:08:02 -0700785 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700787
Adam Cohen7d30a372013-07-01 17:03:59 -0700788 int offsetX = getViewportOffsetX();
789 int offsetY = getViewportOffsetY();
790
791 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700792 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700793
Sunny Goyal70660032015-05-14 00:07:08 -0700794 final int startIndex = mIsRtl ? childCount - 1 : 0;
795 final int endIndex = mIsRtl ? -1 : childCount;
796 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700797
Adam Cohen7d30a372013-07-01 17:03:59 -0700798 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700799
Adam Cohen3b185e22013-10-29 14:45:58 -0700800 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000801 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700802
Sunny Goyal6178f132016-07-11 17:30:03 -0700803 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700804 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
805 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700806 }
807
Adam Cohen0ffac432013-07-10 11:19:26 -0700808 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700809 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700811 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100812 int childTop;
813 if (lp.isFullScreenPage) {
814 childTop = offsetY;
815 } else {
816 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700817 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100818 }
819
Adam Cohen96d30a12013-07-16 18:13:21 -0700820 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700821 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800822
Winson Chung785d2eb2011-04-14 16:08:02 -0700823 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700824 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800825 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700826
Sunny Goyal6178f132016-07-11 17:30:03 -0700827 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700828 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000829
830 int pageGap = mPageSpacing;
831 int next = i + delta;
832 if (next != endIndex) {
833 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
834 } else {
835 nextLp = null;
836 }
837
838 // Prevent full screen pages from showing in the viewport
839 // when they are not the current page.
840 if (lp.isFullScreenPage) {
841 pageGap = getPaddingLeft();
842 } else if (nextLp != null && nextLp.isFullScreenPage) {
843 pageGap = getPaddingRight();
844 }
845
Sunny Goyala1fbd842015-05-20 13:40:27 -0700846 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700849
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700850 final LayoutTransition transition = getLayoutTransition();
851 // If the transition is running defer updating max scroll, as some empty pages could
852 // still be present, and a max scroll change could cause sudden jumps in scroll.
853 if (transition != null && transition.isRunning()) {
854 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
855
856 @Override
857 public void startTransition(LayoutTransition transition, ViewGroup container,
858 View view, int transitionType) { }
859
860 @Override
861 public void endTransition(LayoutTransition transition, ViewGroup container,
862 View view, int transitionType) {
863 // Wait until all transitions are complete.
864 if (!transition.isRunning()) {
865 transition.removeTransitionListener(this);
866 updateMaxScrollX();
867 }
868 }
869 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700870 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700871 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700872 }
873
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700874 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
875 updateCurrentPageScroll();
876 mFirstLayout = false;
877 }
878
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700879 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700880 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700881 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700882 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700883
884 if (isReordering(true)) {
885 updateDragViewTranslationDuringDrag();
886 }
Winson Chunge3193b92010-09-10 11:44:42 -0700887 }
888
Sunny Goyala1fbd842015-05-20 13:40:27 -0700889 protected int getChildGap() {
890 return 0;
891 }
892
Sunny Goyal316490e2015-06-02 09:38:28 -0700893 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700894 mMaxScrollX = computeMaxScrollX();
895 }
896
897 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700898 int childCount = getChildCount();
899 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700900 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700901 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700902 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700903 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700904 }
905 }
906
Adam Cohen3b185e22013-10-29 14:45:58 -0700907 public void setPageSpacing(int pageSpacing) {
908 mPageSpacing = pageSpacing;
909 requestLayout();
910 }
911
Winson Chunge3193b92010-09-10 11:44:42 -0700912 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700913 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700914 // Update the page indicator, we don't update the page indicator as we
915 // add/remove pages
916 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700917 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700918 }
919
Adam Cohen2591f6a2011-10-25 14:36:40 -0700920 // This ensures that when children are added, they get the correct transforms / alphas
921 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700922 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700923 invalidate();
924 }
925
Michael Jurka8b805b12012-04-18 14:23:14 -0700926 @Override
927 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700928 updateFreescrollBounds();
Tonya361c722017-07-04 09:43:06 -0700929 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700930 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700931 }
932
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700933 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700934 // Update the page indicator, we don't update the page indicator as we
935 // add/remove pages
936 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700937 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700938 }
939 }
940
941 @Override
942 public void removeView(View v) {
943 // XXX: We should find a better way to hook into this before the view
944 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700945 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700946 super.removeView(v);
947 }
948 @Override
949 public void removeViewInLayout(View v) {
950 // XXX: We should find a better way to hook into this before the view
951 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700952 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700953 super.removeViewInLayout(v);
954 }
955 @Override
956 public void removeViewAt(int index) {
957 // XXX: We should find a better way to hook into this before the view
958 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700959 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700960 super.removeViewAt(index);
961 }
962 @Override
963 public void removeAllViewsInLayout() {
964 // Update the page indicator, we don't update the page indicator as we
965 // add/remove pages
966 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700967 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700968 }
969
970 super.removeAllViewsInLayout();
971 }
972
Adam Cohen73894962011-10-31 13:17:17 -0700973 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700974 if (index < 0 || index > getChildCount() - 1) return 0;
975
Adam Cohenf698c6e2013-07-17 18:44:25 -0700976 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700977
Adam Cohenf698c6e2013-07-17 18:44:25 -0700978 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700979 }
980
Adam Cohen6f127a62014-06-12 14:54:41 -0700981 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700982 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700983 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700984 }
985
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 @Override
987 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700988 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700989 if (page != mCurrentPage || !mScroller.isFinished()) {
990 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 return true;
992 }
993 return false;
994 }
995
996 @Override
997 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700998 int focusablePage;
999 if (mNextPage != INVALID_PAGE) {
1000 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001002 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 }
Winson Chung86f77532010-08-24 11:08:22 -07001004 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001006 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 }
1008 return false;
1009 }
1010
1011 @Override
1012 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001013 if (super.dispatchUnhandledMove(focused, direction)) {
1014 return true;
1015 }
1016
1017 if (mIsRtl) {
1018 if (direction == View.FOCUS_LEFT) {
1019 direction = View.FOCUS_RIGHT;
1020 } else if (direction == View.FOCUS_RIGHT) {
1021 direction = View.FOCUS_LEFT;
1022 }
1023 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001025 if (getCurrentPage() > 0) {
1026 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 return true;
1028 }
1029 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001030 if (getCurrentPage() < getPageCount() - 1) {
1031 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 return true;
1033 }
1034 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001035 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 }
1037
1038 @Override
1039 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001040 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1041 return;
1042 }
1043
Adam Cohen0ffac432013-07-10 11:19:26 -07001044 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001045 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001046 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001047 }
1048 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001049 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001050 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 }
1052 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001053 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001054 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 }
1056 }
1057 }
1058
1059 /**
1060 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001061 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 *
Winson Chung86f77532010-08-24 11:08:22 -07001063 * This happens when live folders requery, and if they're off page, they
1064 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 */
1066 @Override
1067 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001068 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 View v = focused;
1070 while (true) {
1071 if (v == current) {
1072 super.focusableViewAvailable(focused);
1073 return;
1074 }
1075 if (v == this) {
1076 return;
1077 }
1078 ViewParent parent = v.getParent();
1079 if (parent instanceof View) {
1080 v = (View)v.getParent();
1081 } else {
1082 return;
1083 }
1084 }
1085 }
1086
1087 /**
1088 * {@inheritDoc}
1089 */
1090 @Override
1091 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1092 if (disallowIntercept) {
1093 // We need to make sure to cancel our long press if
1094 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001095 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001096 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001097 }
1098 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1099 }
1100
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001101 /**
1102 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1103 */
1104 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001105 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001106 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001107 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001108 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001109 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001110 }
1111
1112 /**
1113 * Return true if a tap at (x, y) should trigger a flip to the next page.
1114 */
1115 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001116 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001117 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001118 }
1119 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001120 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001121 }
Winson Chung52aee602013-01-30 12:01:02 -08001122
Adam Cohen7d30a372013-07-01 17:03:59 -07001123 /** Returns whether x and y originated within the buffered viewport */
1124 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001125 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001126 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001127 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001128 }
1129
Winson Chung321e9ee2010-08-09 13:37:56 -07001130 @Override
1131 public boolean onInterceptTouchEvent(MotionEvent ev) {
1132 /*
1133 * This method JUST determines whether we want to intercept the motion.
1134 * If we return true, onTouchEvent will be called and we do the actual
1135 * scrolling there.
1136 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001137 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001138
Winson Chung45e1d6e2010-11-09 17:19:49 -08001139 // Skip touch handling if there are no pages to swipe
1140 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1141
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 /*
1143 * Shortcut the most recurring case: the user is in the dragging
1144 * state and he is moving his finger. We want to intercept this
1145 * motion.
1146 */
1147 final int action = ev.getAction();
1148 if ((action == MotionEvent.ACTION_MOVE) &&
1149 (mTouchState == TOUCH_STATE_SCROLLING)) {
1150 return true;
1151 }
1152
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 switch (action & MotionEvent.ACTION_MASK) {
1154 case MotionEvent.ACTION_MOVE: {
1155 /*
1156 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1157 * whether the user has moved far enough from his original down touch.
1158 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001159 if (mActivePointerId != INVALID_POINTER) {
1160 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001161 }
1162 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1163 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1164 // i.e. fall through to the next case (don't break)
1165 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1166 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001167 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 }
1169
1170 case MotionEvent.ACTION_DOWN: {
1171 final float x = ev.getX();
1172 final float y = ev.getY();
1173 // Remember location of down touch
1174 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001175 mDownMotionY = y;
1176 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 mLastMotionX = x;
1178 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001179 float[] p = mapPointFromViewToParent(this, x, y);
1180 mParentDownMotionX = p[0];
1181 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001182 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001183 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001185
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 /*
1187 * If being flinged and user touches the screen, initiate drag;
1188 * otherwise don't. mScroller.isFinished should be false when
1189 * being flinged.
1190 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001191 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001192 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001193
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001194 if (finishedScrolling) {
1195 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001196 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001197 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001198 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001199 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001200 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001201 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1202 mTouchState = TOUCH_STATE_SCROLLING;
1203 } else {
1204 mTouchState = TOUCH_STATE_REST;
1205 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001206 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001207
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 break;
1209 }
1210
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001212 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001213 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 break;
1215
1216 case MotionEvent.ACTION_POINTER_UP:
1217 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001218 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 break;
1220 }
1221
1222 /*
1223 * The only time we want to intercept motion events is if we are in the
1224 * drag mode.
1225 */
1226 return mTouchState != TOUCH_STATE_REST;
1227 }
1228
Adam Cohenf8d28232011-02-01 21:47:00 -08001229 protected void determineScrollingStart(MotionEvent ev) {
1230 determineScrollingStart(ev, 1.0f);
1231 }
1232
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 /*
1234 * Determines if we should change the touch state to start scrolling after the
1235 * user moves their touch point too far.
1236 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001237 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001238 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001240 if (pointerIndex == -1) return;
1241
1242 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 final float x = ev.getX(pointerIndex);
1244 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001245 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1246
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248
Adam Cohenf8d28232011-02-01 21:47:00 -08001249 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001251
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001252 if (xMoved) {
1253 // Scroll if the user moved far enough along the X axis
1254 mTouchState = TOUCH_STATE_SCROLLING;
1255 mTotalMotionX += Math.abs(mLastMotionX - x);
1256 mLastMotionX = x;
1257 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001258 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001259 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001260 // Stop listening for things like pinches.
1261 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001262 }
1263 }
1264
1265 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001266 // Try canceling the long press. It could also have been scheduled
1267 // by a distant descendant, so use the mAllowLongPress flag to block
1268 // everything
1269 final View currentPage = getPageAt(mCurrentPage);
1270 if (currentPage != null) {
1271 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273 }
1274
Adam Cohenb5ba0972011-09-07 18:02:31 -07001275 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001276 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001277
Adam Cohenedb40762013-07-18 16:45:45 -07001278 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001279 int count = getChildCount();
1280
1281 final int totalDistance;
1282
1283 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001284 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001285 adjacentPage = page - 1;
1286 }
1287
1288 if (adjacentPage < 0 || adjacentPage > count - 1) {
1289 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1290 } else {
1291 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1292 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001293
1294 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001295 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1296 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001297 return scrollProgress;
1298 }
1299
Adam Cohenedb40762013-07-18 16:45:45 -07001300 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001301 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001302 return 0;
1303 } else {
1304 return mPageScrolls[index];
1305 }
1306 }
1307
Adam Cohen8d769d62017-06-23 17:27:38 -07001308 // This curve determines how the effect of scrolling over the limits of the page dimishes
1309 // as the user pulls further and further from the bounds
1310 private float overScrollInfluenceCurve(float f) {
1311 f -= 1.0f;
1312 return f * f * f + 1.0f;
1313 }
1314
1315 protected float acceleratedOverFactor(float amount) {
1316 int screenSize = getViewportWidth();
1317
1318 // We want to reach the max over scroll effect when the user has
1319 // over scrolled half the size of the screen
1320 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1321
1322 if (Float.compare(f, 0f) == 0) return 0;
1323
1324 // Clamp this factor, f, to -1 < f < 1
1325 if (Math.abs(f) >= 1) {
1326 f /= Math.abs(f);
1327 }
1328 return f;
1329 }
1330
Adam Cohen564a2e72013-10-09 14:47:32 -07001331 // While layout transitions are occurring, a child's position may stray from its baseline
1332 // position. This method returns the magnitude of this stray at any given time.
1333 public int getLayoutTransitionOffsetForPage(int index) {
1334 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1335 return 0;
1336 } else {
1337 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001338
1339 int scrollOffset = 0;
1340 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1341 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001342 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001343 }
1344
Adam Cohen564a2e72013-10-09 14:47:32 -07001345 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1346 return (int) (child.getX() - baselineX);
1347 }
1348 }
1349
Adam Cohenb5ba0972011-09-07 18:02:31 -07001350 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001351 int screenSize = getViewportWidth();
Adam Cohen8d769d62017-06-23 17:27:38 -07001352
Adam Cohene0f66b52010-11-23 15:06:07 -08001353 float f = (amount / screenSize);
Adam Cohen8d769d62017-06-23 17:27:38 -07001354
1355 if (Float.compare(f, 0f) == 0) return;
1356
1357 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1358
1359 // Clamp this factor, f, to -1 < f < 1
1360 if (Math.abs(f) >= 1) {
1361 f /= Math.abs(f);
1362 }
1363
1364 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
1365 if (amount < 0) {
1366 mOverScrollX = overScrollAmount;
1367 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001368 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001369 mOverScrollX = mMaxScrollX + overScrollAmount;
1370 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001371 }
1372 invalidate();
1373 }
1374
Adam Cohenb5ba0972011-09-07 18:02:31 -07001375 protected void overScroll(float amount) {
1376 dampedOverScroll(amount);
1377 }
1378
Adam Cohen8d769d62017-06-23 17:27:38 -07001379 protected float maxOverScroll() {
1380 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
1381 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
1382 float f = 1.0f;
1383 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1384 return OVERSCROLL_DAMP_FACTOR * f;
1385 }
1386
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001387 /**
1388 * return true if freescroll has been enabled, false otherwise
1389 */
1390 public boolean enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001391 setEnableFreeScroll(true);
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001392 return true;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 }
1394
Winson Chungdc61c4d2015-04-20 18:26:57 -07001395 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001396 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 }
1398
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001399 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001400 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001401 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001402 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1403 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1404 } else {
1405 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1406 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1407 }
1408 }
1409
Adam Cohenf9618852013-11-08 06:45:03 -08001410 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001411 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 mFreeScroll = freeScroll;
1413
Adam Cohenf9618852013-11-08 06:45:03 -08001414 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001415 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001416 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001417 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1418 setCurrentPage(mTempVisiblePagesRange[0]);
1419 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1420 setCurrentPage(mTempVisiblePagesRange[1]);
1421 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001422 } else if (wasFreeScroll) {
1423 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 }
1425
1426 setEnableOverscroll(!freeScroll);
1427 }
1428
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001429 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430 mAllowOverScroll = enable;
1431 }
1432
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001433 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001434 if (mDragView != null) {
1435 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1436 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001437 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001438 int minDistance = Integer.MAX_VALUE;
1439 int minIndex = indexOfChild(mDragView);
1440 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1441 View page = getPageAt(i);
1442 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1443 int d = Math.abs(dragX - pageX);
1444 if (d < minDistance) {
1445 minIndex = i;
1446 minDistance = d;
1447 }
1448 }
1449 return minIndex;
1450 }
1451 return -1;
1452 }
1453
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 @Override
1455 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001456 super.onTouchEvent(ev);
1457
Winson Chung45e1d6e2010-11-09 17:19:49 -08001458 // Skip touch handling if there are no pages to swipe
1459 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1460
Michael Jurkab8f06722010-10-10 15:58:46 -07001461 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001462
1463 final int action = ev.getAction();
1464
1465 switch (action & MotionEvent.ACTION_MASK) {
1466 case MotionEvent.ACTION_DOWN:
1467 /*
1468 * If being flinged and user touches, stop the fling. isFinished
1469 * will be false if being flinged.
1470 */
1471 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001472 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 }
1474
1475 // Remember where the motion event started
1476 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001477 mDownMotionY = mLastMotionY = ev.getY();
1478 mDownScrollX = getScrollX();
1479 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1480 mParentDownMotionX = p[0];
1481 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001482 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001483 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001485
Michael Jurka0142d492010-08-25 17:46:15 -07001486 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001487 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001488 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001489 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 break;
1491
1492 case MotionEvent.ACTION_MOVE:
1493 if (mTouchState == TOUCH_STATE_SCROLLING) {
1494 // Scroll to follow the motion event
1495 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001496
1497 if (pointerIndex == -1) return true;
1498
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001500 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001501
Adam Cohenaefd4e12011-02-14 16:39:38 -08001502 mTotalMotionX += Math.abs(deltaX);
1503
Winson Chungc0844aa2011-02-02 15:25:58 -08001504 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1505 // keep the remainder because we are actually testing if we've moved from the last
1506 // scrolled position (which is discrete).
1507 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001508 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001509 mLastMotionX = x;
1510 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 } else {
1512 awakenScrollBars();
1513 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1515 // Update the last motion position
1516 mLastMotionX = ev.getX();
1517 mLastMotionY = ev.getY();
1518
1519 // Update the parent down so that our zoom animations take this new movement into
1520 // account
1521 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1522 mParentDownMotionX = pt[0];
1523 mParentDownMotionY = pt[1];
1524 updateDragViewTranslationDuringDrag();
1525
1526 // Find the closest page to the touch point
1527 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001528
Adam Cohen7d30a372013-07-01 17:03:59 -07001529 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1530 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1531 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1532 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1533
Adam Cohenf358a4b2013-07-23 16:47:31 -07001534 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001535 // Do not allow any page to be moved to 0th position.
1536 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001537 mTempVisiblePagesRange[0] = 0;
1538 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001539 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001540 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1541 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1542 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1543 mSidePageHoverIndex = pageUnderPointIndex;
1544 mSidePageHoverRunnable = new Runnable() {
1545 @Override
1546 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001547 // Setup the scroll to the correct page before we swap the views
1548 snapToPage(pageUnderPointIndex);
1549
1550 // For each of the pages between the paged view and the drag view,
1551 // animate them from the previous position to the new position in
1552 // the layout (as a result of the drag view moving in the layout)
1553 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1554 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1555 dragViewIndex + 1 : pageUnderPointIndex;
1556 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1557 dragViewIndex - 1 : pageUnderPointIndex;
1558 for (int i = lowerIndex; i <= upperIndex; ++i) {
1559 View v = getChildAt(i);
1560 // dragViewIndex < pageUnderPointIndex, so after we remove the
1561 // drag view all subsequent views to pageUnderPointIndex will
1562 // shift down.
1563 int oldX = getViewportOffsetX() + getChildOffset(i);
1564 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1565
1566 // Animate the view translation from its old position to its new
1567 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001568 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001569 if (anim != null) {
1570 anim.cancel();
1571 }
1572
1573 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001574 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001575 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001576 anim.start();
1577 v.setTag(anim);
1578 }
1579
1580 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001581 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001582 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001583 if (mPageIndicator != null) {
1584 mPageIndicator.setActiveMarker(getNextPage());
1585 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001586 }
1587 };
1588 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1589 }
1590 } else {
1591 removeCallbacks(mSidePageHoverRunnable);
1592 mSidePageHoverIndex = -1;
1593 }
Adam Cohen564976a2010-10-13 18:52:07 -07001594 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001595 determineScrollingStart(ev);
1596 }
1597 break;
1598
1599 case MotionEvent.ACTION_UP:
1600 if (mTouchState == TOUCH_STATE_SCROLLING) {
1601 final int activePointerId = mActivePointerId;
1602 final int pointerIndex = ev.findPointerIndex(activePointerId);
1603 final float x = ev.getX(pointerIndex);
1604 final VelocityTracker velocityTracker = mVelocityTracker;
1605 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1606 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001607 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001608 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001609 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1610 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001611
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001612 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1613
Adam Cohen00481b32011-11-18 12:03:48 -08001614 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001615 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001616
Adam Cohenf358a4b2013-07-23 16:47:31 -07001617 if (!mFreeScroll) {
1618 // In the case that the page is moved far to one direction and then is flung
1619 // in the opposite direction, we use a threshold to determine whether we should
1620 // just return to the starting page, or if we should skip one further.
1621 boolean returnToOriginalPage = false;
1622 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1623 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1624 returnToOriginalPage = true;
1625 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001626
Adam Cohenf358a4b2013-07-23 16:47:31 -07001627 int finalPage;
1628 // We give flings precedence over large moves, which is why we short-circuit our
1629 // test for a large move if a fling has been registered. That is, a large
1630 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001631 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1632 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001633 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1634 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1635 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1636 snapToPageWithVelocity(finalPage, velocityX);
1637 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1638 (isFling && isVelocityXLeft)) &&
1639 mCurrentPage < getChildCount() - 1) {
1640 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1641 snapToPageWithVelocity(finalPage, velocityX);
1642 } else {
1643 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001644 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001645 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001646 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001647 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001648 }
1649
1650 float scaleX = getScaleX();
1651 int vX = (int) (-velocityX * scaleX);
1652 int initialScrollX = (int) (getScrollX() * scaleX);
1653
Adam Cohenf9618852013-11-08 06:45:03 -08001654 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001655 mScroller.fling(initialScrollX,
1656 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001657 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001658 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001660 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001661 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1662 // at this point we have not moved beyond the touch slop
1663 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1664 // we can just page
1665 int nextPage = Math.max(0, mCurrentPage - 1);
1666 if (nextPage != mCurrentPage) {
1667 snapToPage(nextPage);
1668 } else {
1669 snapToDestination();
1670 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001671 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001672 // at this point we have not moved beyond the touch slop
1673 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1674 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001675 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1676 if (nextPage != mCurrentPage) {
1677 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001678 } else {
1679 snapToDestination();
1680 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001681 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1682 // Update the last motion position
1683 mLastMotionX = ev.getX();
1684 mLastMotionY = ev.getY();
1685
1686 // Update the parent down so that our zoom animations take this new movement into
1687 // account
1688 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1689 mParentDownMotionX = pt[0];
1690 mParentDownMotionY = pt[1];
1691 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001692 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001693 if (!mCancelTap) {
1694 onUnhandledTap(ev);
1695 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001696 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001697
1698 // Remove the callback to wait for the side page hover timeout
1699 removeCallbacks(mSidePageHoverRunnable);
1700 // End any intermediate reordering states
1701 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001702 break;
1703
1704 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001705 if (mTouchState == TOUCH_STATE_SCROLLING) {
1706 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001707 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001708 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001709 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001710 break;
1711
1712 case MotionEvent.ACTION_POINTER_UP:
1713 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001714 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001715 break;
1716 }
1717
1718 return true;
1719 }
1720
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001721 protected boolean shouldFlingForVelocity(int velocityX) {
1722 return Math.abs(velocityX) > mFlingThresholdVelocity;
1723 }
1724
Adam Cohen7d30a372013-07-01 17:03:59 -07001725 private void resetTouchState() {
1726 releaseVelocityTracker();
1727 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001728 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001729 mTouchState = TOUCH_STATE_REST;
1730 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001731 }
1732
Adam Cohenc2d6e892014-10-16 09:49:24 -07001733 /**
1734 * Triggered by scrolling via touch
1735 */
1736 protected void onScrollInteractionBegin() {
1737 }
1738
1739 protected void onScrollInteractionEnd() {
1740 }
1741
Adam Cohen1697b792013-09-17 19:08:21 -07001742 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001743 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001744 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001745
Winson Chung185d7162011-02-28 13:47:29 -08001746 @Override
1747 public boolean onGenericMotionEvent(MotionEvent event) {
1748 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1749 switch (event.getAction()) {
1750 case MotionEvent.ACTION_SCROLL: {
1751 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1752 final float vscroll;
1753 final float hscroll;
1754 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1755 vscroll = 0;
1756 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1757 } else {
1758 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1759 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1760 }
1761 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001762 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001763 : (hscroll > 0 || vscroll > 0);
1764 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001765 scrollRight();
1766 } else {
1767 scrollLeft();
1768 }
1769 return true;
1770 }
1771 }
1772 }
1773 }
1774 return super.onGenericMotionEvent(event);
1775 }
1776
Michael Jurkab8f06722010-10-10 15:58:46 -07001777 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1778 if (mVelocityTracker == null) {
1779 mVelocityTracker = VelocityTracker.obtain();
1780 }
1781 mVelocityTracker.addMovement(ev);
1782 }
1783
1784 private void releaseVelocityTracker() {
1785 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001786 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001787 mVelocityTracker.recycle();
1788 mVelocityTracker = null;
1789 }
1790 }
1791
Winson Chung321e9ee2010-08-09 13:37:56 -07001792 private void onSecondaryPointerUp(MotionEvent ev) {
1793 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1794 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1795 final int pointerId = ev.getPointerId(pointerIndex);
1796 if (pointerId == mActivePointerId) {
1797 // This was our active pointer going up. Choose a new
1798 // active pointer and adjust accordingly.
1799 // TODO: Make this decision more intelligent.
1800 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1801 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1802 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001803 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001804 mActivePointerId = ev.getPointerId(newPointerIndex);
1805 if (mVelocityTracker != null) {
1806 mVelocityTracker.clear();
1807 }
1808 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001809 }
1810
Winson Chung321e9ee2010-08-09 13:37:56 -07001811 @Override
1812 public void requestChildFocus(View child, View focused) {
1813 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001814 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001815 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001816 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001817 }
1818 }
1819
Adam Cohend19d3ca2010-09-15 14:43:42 -07001820 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001821 return getPageNearestToCenterOfScreen(getScrollX());
1822 }
1823
1824 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1825 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001826 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001827 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001828 final int childCount = getChildCount();
1829 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001830 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001831 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001832 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001833 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001834 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1835 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1836 minDistanceFromScreenCenter = distanceFromScreenCenter;
1837 minDistanceFromScreenCenterIndex = i;
1838 }
1839 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001840 return minDistanceFromScreenCenterIndex;
1841 }
1842
1843 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001844 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1845 }
1846
1847 protected boolean isInOverScroll() {
1848 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1849 }
1850
1851 protected int getPageSnapDuration() {
1852 if (isInOverScroll()) {
1853 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1854 }
1855 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001856 }
1857
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001858 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001859 public ScrollInterpolator() {
1860 }
1861
1862 public float getInterpolation(float t) {
1863 t -= 1.0f;
1864 return t*t*t*t*t + 1;
1865 }
1866 }
1867
1868 // We want the duration of the page snap animation to be influenced by the distance that
1869 // the screen has to travel, however, we don't want this duration to be effected in a
1870 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1871 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001872 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001873 f -= 0.5f; // center the values about 0.
1874 f *= 0.3f * Math.PI / 2.0f;
1875 return (float) Math.sin(f);
1876 }
1877
Michael Jurka0142d492010-08-25 17:46:15 -07001878 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001879 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001880 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001881
Adam Cohenedb40762013-07-18 16:45:45 -07001882 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001883 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001884 int duration = 0;
1885
Sunny Goyal4d113a52015-05-27 10:05:28 -07001886 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001887 // If the velocity is low enough, then treat this more as an automatic page advance
1888 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001889 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001890 return;
1891 }
1892
1893 // Here we compute a "distance" that will be used in the computation of the overall
1894 // snap duration. This is a function of the actual distance that needs to be traveled;
1895 // we keep this value close to half screen size in order to reduce the variance in snap
1896 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001897 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001898 float distance = halfScreenSize + halfScreenSize *
1899 distanceInfluenceForSnapDuration(distanceRatio);
1900
1901 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001902 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001903
1904 // we want the page's snap velocity to approximately match the velocity at which the
1905 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001906 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1907 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001908
1909 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001910 }
1911
Sunny Goyal1740d902015-05-27 11:14:01 -07001912 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001913 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001914 }
1915
Adam Cohenf9c184a2016-01-15 16:47:43 -08001916 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001917 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001918 }
1919
Michael Jurka0142d492010-08-25 17:46:15 -07001920 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001921 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001922 }
1923
Adam Cohenf9618852013-11-08 06:45:03 -08001924 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1925 snapToPage(whichPage, duration, false, interpolator);
1926 }
1927
1928 protected void snapToPage(int whichPage, int duration, boolean immediate,
1929 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001930 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001931
Adam Cohenedb40762013-07-18 16:45:45 -07001932 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001933 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001934 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001935 }
1936
1937 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001938 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001939 }
Michael Jurka0142d492010-08-25 17:46:15 -07001940
Adam Cohenf9618852013-11-08 06:45:03 -08001941 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1942 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001943 whichPage = validateNewPage(whichPage);
1944
Adam Cohen7d30a372013-07-01 17:03:59 -07001945 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001946
Winson Chung321e9ee2010-08-09 13:37:56 -07001947 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001948 if (immediate) {
1949 duration = 0;
1950 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001951 duration = Math.abs(delta);
1952 }
1953
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001954 if (duration != 0) {
1955 pageBeginTransition();
1956 }
1957
Adam Cohenf358a4b2013-07-23 16:47:31 -07001958 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001959 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001960 }
Adam Cohenf9618852013-11-08 06:45:03 -08001961
1962 if (interpolator != null) {
1963 mScroller.setInterpolator(interpolator);
1964 } else {
1965 mScroller.setInterpolator(mDefaultInterpolator);
1966 }
1967
Sunny Goyalc86df472016-02-25 09:19:38 -08001968 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001969
Adam Cohen674531f2013-12-13 15:07:14 -08001970 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001971
1972 // Trigger a compute() to finish switching pages if necessary
1973 if (immediate) {
1974 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001975 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001976 }
1977
Winson Chung321e9ee2010-08-09 13:37:56 -07001978 invalidate();
1979 }
1980
Winson Chung321e9ee2010-08-09 13:37:56 -07001981 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001982 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001983 }
1984
1985 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001986 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001987 }
1988
Adam Cohendbdff6b2013-09-18 19:09:15 -07001989 @Override
1990 public boolean performLongClick() {
1991 mCancelTap = true;
1992 return super.performLongClick();
1993 }
1994
Winson Chung321e9ee2010-08-09 13:37:56 -07001995 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001996 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001997
1998 SavedState(Parcelable superState) {
1999 super(superState);
2000 }
2001
Adam Cohen091440a2015-03-18 14:16:05 -07002002 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002004 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002005 }
2006
2007 @Override
2008 public void writeToParcel(Parcel out, int flags) {
2009 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002010 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002011 }
2012
2013 public static final Parcelable.Creator<SavedState> CREATOR =
2014 new Parcelable.Creator<SavedState>() {
2015 public SavedState createFromParcel(Parcel in) {
2016 return new SavedState(in);
2017 }
2018
2019 public SavedState[] newArray(int size) {
2020 return new SavedState[size];
2021 }
2022 };
2023 }
2024
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002026 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002027 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08002028 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
2029 new PropertyListBuilder()
2030 .scale(1)
2031 .translationX(0)
2032 .translationY(0)
2033 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002034 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002035 anim.addListener(new AnimatorListenerAdapter() {
2036 @Override
2037 public void onAnimationEnd(Animator animation) {
2038 onPostReorderingAnimationCompleted();
2039 }
2040 });
2041 anim.start();
2042 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002043 }
2044
Sunny Goyal1d08f702015-05-04 15:50:25 -07002045 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002046 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2047 mTouchState = TOUCH_STATE_REORDERING;
2048 mIsReordering = true;
2049
Adam Cohen7d30a372013-07-01 17:03:59 -07002050 // We must invalidate to trigger a redraw to update the layers such that the drag view
2051 // is always drawn on top
2052 invalidate();
2053 }
2054
Adam Cohen091440a2015-03-18 14:16:05 -07002055 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002056 // Trigger the callback when reordering has settled
2057 --mPostReorderingPreZoomInRemainingAnimationCount;
2058 if (mPostReorderingPreZoomInRunnable != null &&
2059 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2060 mPostReorderingPreZoomInRunnable.run();
2061 mPostReorderingPreZoomInRunnable = null;
2062 }
2063 }
2064
Sunny Goyal1d08f702015-05-04 15:50:25 -07002065 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002066 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002067 }
2068
Adam Cohenf358a4b2013-07-23 16:47:31 -07002069 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002070 int dragViewIndex = indexOfChild(v);
2071
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002072 // Do not allow the first page to be moved around
2073 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002074
Adam Cohen7d30a372013-07-01 17:03:59 -07002075 mTempVisiblePagesRange[0] = 0;
2076 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002077 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002078 mReorderingStarted = true;
2079
2080 // Check if we are within the reordering range
2081 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002082 dragViewIndex <= mTempVisiblePagesRange[1]) {
2083 // Find the drag view under the pointer
2084 mDragView = getChildAt(dragViewIndex);
2085 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2086 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002087 snapToPage(getPageNearestToCenterOfScreen());
2088 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002089 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002090 return true;
2091 }
2092 return false;
2093 }
2094
2095 boolean isReordering(boolean testTouchState) {
2096 boolean state = mIsReordering;
2097 if (testTouchState) {
2098 state &= (mTouchState == TOUCH_STATE_REORDERING);
2099 }
2100 return state;
2101 }
2102 void endReordering() {
2103 // For simplicity, we call endReordering sometimes even if reordering was never started.
2104 // In that case, we don't want to do anything.
2105 if (!mReorderingStarted) return;
2106 mReorderingStarted = false;
2107
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002108 mPostReorderingPreZoomInRunnable = new Runnable() {
2109 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002110 // If we haven't flung-to-delete the current child,
2111 // then we just animate the drag view back into position
2112 onEndReordering();
2113
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002114 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002115 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002116 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002117
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002118 mPostReorderingPreZoomInRemainingAnimationCount =
2119 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2120 // Snap to the current page
2121 snapToPage(indexOfChild(mDragView), 0);
2122 // Animate the drag view back to the front position
2123 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002124 }
2125
Winson Chung6a0f57d2011-06-29 20:10:49 -07002126 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002127 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002128 @Override
2129 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2130 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002131 info.setScrollable(getPageCount() > 1);
2132 if (getCurrentPage() < getPageCount() - 1) {
2133 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2134 }
2135 if (getCurrentPage() > 0) {
2136 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2137 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002138 info.setClassName(getClass().getName());
2139
2140 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2141 // Besides disabling the accessibility long-click, this also prevents this view from getting
2142 // accessibility focus.
2143 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002144 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002145 }
2146
2147 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002148 public void sendAccessibilityEvent(int eventType) {
2149 // Don't let the view send real scroll events.
2150 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2151 super.sendAccessibilityEvent(eventType);
2152 }
2153 }
2154
2155 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002156 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2157 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002158 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002159 }
2160
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002161 @Override
2162 public boolean performAccessibilityAction(int action, Bundle arguments) {
2163 if (super.performAccessibilityAction(action, arguments)) {
2164 return true;
2165 }
2166 switch (action) {
2167 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2168 if (getCurrentPage() < getPageCount() - 1) {
2169 scrollRight();
2170 return true;
2171 }
2172 } break;
2173 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2174 if (getCurrentPage() > 0) {
2175 scrollLeft();
2176 return true;
2177 }
2178 } break;
2179 }
2180 return false;
2181 }
2182
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002183 protected String getPageIndicatorDescription() {
2184 return getCurrentPageDescription();
2185 }
2186
Adam Cohen0ffac432013-07-10 11:19:26 -07002187 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002188 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002189 getNextPage() + 1, getChildCount());
2190 }
2191
Winson Chungd11265e2011-08-30 13:37:23 -07002192 @Override
2193 public boolean onHoverEvent(android.view.MotionEvent event) {
2194 return true;
2195 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002196}