blob: ee7f9f805c370297536f0865bbb52923670136ef [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;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal316490e2015-06-02 09:38:28 -070031import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080050import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
52import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070053import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070055
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chung321e9ee2010-08-09 13:37:56 -070058/**
59 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070060 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070061 */
Michael Jurka8b805b12012-04-18 14:23:14 -070062public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070063 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070064 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070065 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Winson Chung86f77532010-08-24 11:08:22 -070067 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070068 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Vadim Tryshevfedca432015-08-19 17:55:02 -070070 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070071 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080074 // The page is moved more than halfway, automatically move to the next page on touch up.
75 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080076
Sunny Goyal8e2133b2015-05-06 13:39:07 -070077 private static final float MAX_SCROLL_PROGRESS = 1.0f;
78
Adam Cohen265b9a62011-12-07 14:37:18 -080079 // The following constants need to be scaled based on density. The scaled versions will be
80 // assigned to the corresponding member variables below.
81 private static final int FLING_THRESHOLD_VELOCITY = 500;
82 private static final int MIN_SNAP_VELOCITY = 1500;
83 private static final int MIN_FLING_VELOCITY = 250;
84
Adam Cohen21cd0022013-10-09 18:57:02 -070085 public static final int INVALID_RESTORE_PAGE = -1001;
86
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private boolean mFreeScroll = false;
88 private int mFreeScrollMinScrollX = -1;
89 private int mFreeScrollMaxScrollX = -1;
90
Adam Cohen265b9a62011-12-07 14:37:18 -080091 protected int mFlingThresholdVelocity;
92 protected int mMinFlingVelocity;
93 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070096 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070097
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700100 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700101
Sunny Goyal4ffec482016-02-09 11:28:52 -0800102 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800104 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800105 protected LauncherScroller mScroller;
106 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Adam Cohen7d30a372013-07-01 17:03:59 -0700110 private float mParentDownMotionX;
111 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700113 private float mDownMotionY;
114 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700115 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700116 private float mLastMotionX;
117 private float mLastMotionXRemainder;
118 private float mLastMotionY;
119 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700120
Adam Cohendbdff6b2013-09-18 19:09:15 -0700121 private boolean mCancelTap;
122
Adam Cohenedb40762013-07-18 16:45:45 -0700123 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected final static int TOUCH_STATE_REST = 0;
126 protected final static int TOUCH_STATE_SCROLLING = 1;
127 protected final static int TOUCH_STATE_PREV_PAGE = 2;
128 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 protected final static int TOUCH_STATE_REORDERING = 4;
130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800138 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700144 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Sunny Goyal061380a2016-02-29 15:15:03 -0800146 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700147
Winson Chungd2be3812013-07-16 11:11:32 -0700148 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700149 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700150 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700151 // The viewport whether the pages are to be contained (the actual view may be larger than the
152 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800153 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700154 private Rect mViewport = new Rect();
155
156 // Reordering
157 // We use the min scale to determine how much to expand the actually PagedView measured
158 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700159 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700160 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700161 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
162
Adam Cohen7d30a372013-07-01 17:03:59 -0700163 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700164 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700165 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700166 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700167 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700168 // This variable's scope is only for the duration of startReordering() and endReordering()
169 private boolean mReorderingStarted = false;
170 // This variable's scope is for the duration of startReordering() and after the zoomIn()
171 // animation after endReordering()
172 private boolean mIsReordering;
173 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700174 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 private int mPostReorderingPreZoomInRemainingAnimationCount;
176 private Runnable mPostReorderingPreZoomInRunnable;
177
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700179 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700180 private static final float[] sTmpPoint = new float[2];
181 private static final int[] sTmpIntPoint = new int[2];
182 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700183
John Spurlock77e1f472013-09-11 10:09:51 -0400184 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700185 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400186
Sunny Goyal4d113a52015-05-27 10:05:28 -0700187 // Edge effect
188 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
189 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
190
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 public PagedView(Context context) {
192 this(context, null);
193 }
194
195 public PagedView(Context context, AttributeSet attrs) {
196 this(context, attrs, 0);
197 }
198
199 public PagedView(Context context, AttributeSet attrs, int defStyle) {
200 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700201
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 TypedArray a = context.obtainStyledAttributes(attrs,
203 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700204 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 a.recycle();
206
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700208 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700209 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 }
211
212 /**
213 * Initializes various states for this workspace.
214 */
Michael Jurka0142d492010-08-25 17:46:15 -0700215 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800216 mScroller = new LauncherScroller(getContext());
217 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700218 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700219
220 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700221 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800223
Sunny Goyalcf25b522015-07-09 00:01:18 -0700224 float density = getResources().getDisplayMetrics().density;
225 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
226 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
227 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700228 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700229 setWillNotDraw(false);
230 }
231
232 protected void setEdgeGlowColor(int color) {
233 mEdgeGlowLeft.setColor(color);
234 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 }
236
Adam Cohenf9618852013-11-08 06:45:03 -0800237 protected void setDefaultInterpolator(Interpolator interpolator) {
238 mDefaultInterpolator = interpolator;
239 mScroller.setInterpolator(mDefaultInterpolator);
240 }
241
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700242 public void initParentViews(View parent) {
243 if (mPageIndicatorViewId > -1) {
244 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700245 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700246 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700247 }
248 }
249
Adam Cohen7d30a372013-07-01 17:03:59 -0700250 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700251 private float[] mapPointFromViewToParent(View v, float x, float y) {
252 sTmpPoint[0] = x;
253 sTmpPoint[1] = y;
254 v.getMatrix().mapPoints(sTmpPoint);
255 sTmpPoint[0] += v.getLeft();
256 sTmpPoint[1] += v.getTop();
257 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700258 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700259 private float[] mapPointFromParentToView(View v, float x, float y) {
260 sTmpPoint[0] = x - v.getLeft();
261 sTmpPoint[1] = y - v.getTop();
262 v.getMatrix().invert(sTmpInvMatrix);
263 sTmpInvMatrix.mapPoints(sTmpPoint);
264 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700265 }
266
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700267 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268 if (mDragView != null) {
269 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
270 (mDragViewBaselineLeft - mDragView.getLeft());
271 float y = mLastMotionY - mDownMotionY;
272 mDragView.setTranslationX(x);
273 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700274
Adam Cohenf358a4b2013-07-23 16:47:31 -0700275 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
276 + x + ", " + y);
277 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700278 }
279
280 public void setMinScale(float f) {
281 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700282 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700283 requestLayout();
284 }
285
286 @Override
287 public void setScaleX(float scaleX) {
288 super.setScaleX(scaleX);
289 if (isReordering(true)) {
290 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
291 mLastMotionX = p[0];
292 mLastMotionY = p[1];
293 updateDragViewTranslationDuringDrag();
294 }
295 }
296
297 // Convenience methods to get the actual width/height of the PagedView (since it is measured
298 // to be larger to account for the minimum possible scale)
299 int getViewportWidth() {
300 return mViewport.width();
301 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800302 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700303 return mViewport.height();
304 }
305
306 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
307 // PagedView both horizontally and vertically
308 int getViewportOffsetX() {
309 return (getMeasuredWidth() - getViewportWidth()) / 2;
310 }
311
312 int getViewportOffsetY() {
313 return (getMeasuredHeight() - getViewportHeight()) / 2;
314 }
315
Hyunyoung Song77441692016-06-27 22:00:48 -0700316 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700317 return mPageIndicator;
318 }
319
Adam Cohen674531f2013-12-13 15:07:14 -0800320 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700321 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
322 * that the user was on before entering free scroll mode (e.g. the home screen page they
323 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
324 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700326 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700327 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700329
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700330 /**
331 * Returns the index of page to be shown immediately afterwards.
332 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700333 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700334 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
335 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700336
Adam Cohenf9c184a2016-01-15 16:47:43 -0800337 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 return getChildCount();
339 }
340
Sunny Goyal83a8f042015-05-19 12:52:12 -0700341 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 return getChildAt(index);
343 }
344
Adam Cohenae4f1552011-10-20 00:15:42 -0700345 protected int indexToPage(int index) {
346 return index;
347 }
348
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800350 * Updates the scroll of the current page immediately to its final scroll position. We use this
351 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
352 * the previous tab page.
353 */
354 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700355 // If the current page is invalid, just reset the scroll position to zero
356 int newX = 0;
357 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700358 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700359 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800360 scrollTo(newX, 0);
361 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700362 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800363 }
364
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700365 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700366 mScroller.abortAnimation();
367 // We need to clean up the next page here to avoid computeScrollHelper from
368 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700369 if (resetNextPage) {
370 mNextPage = INVALID_PAGE;
371 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700372 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700373
Tony Wickham8f7ead32016-04-07 18:46:44 -0700374 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700375 mScroller.forceFinished(true);
376 // We need to clean up the next page here to avoid computeScrollHelper from
377 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700378 if (resetNextPage) {
379 mNextPage = INVALID_PAGE;
380 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700381 }
382
Adam Cohen6f127a62014-06-12 14:54:41 -0700383 private int validateNewPage(int newPage) {
384 int validatedPage = newPage;
385 // When in free scroll mode, we need to clamp to the free scroll page range.
386 if (mFreeScroll) {
387 getFreeScrollPageRange(mTempVisiblePagesRange);
388 validatedPage = Math.max(mTempVisiblePagesRange[0],
389 Math.min(newPage, mTempVisiblePagesRange[1]));
390 }
391 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700392 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700393 return validatedPage;
394 }
395
Chet Haasebc2f0822012-10-26 17:59:53 -0700396 /**
Winson Chung86f77532010-08-24 11:08:22 -0700397 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700398 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700399 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800400 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700401 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800402 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800403 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
404 // the default
405 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800406 return;
407 }
Adam Cohen6f127a62014-06-12 14:54:41 -0700408 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700409 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700410 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800411 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 }
413
Winson Chung8c87cd82013-07-23 16:20:10 -0700414 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800415 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
416 * has settled.
417 */
Michael Jurka0142d492010-08-25 17:46:15 -0700418 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800419 updatePageIndicator();
420 }
421
422 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700423 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700424 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700425 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700426 if (!isReordering(false)) {
427 mPageIndicator.setActiveMarker(getNextPage());
428 }
Winson Chungd2be3812013-07-16 11:11:32 -0700429 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700431 protected void pageBeginTransition() {
432 if (!mIsPageInTransition) {
433 mIsPageInTransition = true;
434 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700435 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700436 }
437
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700438 protected void pageEndTransition() {
439 if (mIsPageInTransition) {
440 mIsPageInTransition = false;
441 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700442 }
Michael Jurka0142d492010-08-25 17:46:15 -0700443 }
444
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700445 protected boolean isPageInTransition() {
446 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700447 }
448
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700449 /**
450 * Called when the page starts moving as part of the scroll. Subclasses can override this
451 * to provide custom behavior during animation.
452 */
453 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700454 }
455
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700456 /**
457 * Called when the page ends moving as part of the scroll. Subclasses can override this
458 * to provide custom behavior during animation.
459 */
460 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700461 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700462 }
463
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 /**
Winson Chung86f77532010-08-24 11:08:22 -0700465 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 *
467 * @param l The listener used to respond to long clicks.
468 */
469 @Override
470 public void setOnLongClickListener(OnLongClickListener l) {
471 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700472 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700473 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700474 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
Adam Cohen1697b792013-09-17 19:08:21 -0700476 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 }
478
Sunny Goyalc86df472016-02-25 09:19:38 -0800479 protected int getUnboundedScrollX() {
480 return getScrollX();
481 }
482
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800484 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800485 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800486 }
487
488 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700489 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700490 // In free scroll mode, we clamp the scrollX
491 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700492 // If the scroller is trying to move to a location beyond the maximum allowed
493 // in the free scroll mode, we make sure to end the scroll operation.
494 if (!mScroller.isFinished() &&
495 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700496 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700497 }
498
Adam Cohenf358a4b2013-07-23 16:47:31 -0700499 x = Math.min(x, mFreeScrollMaxScrollX);
500 x = Math.max(x, mFreeScrollMinScrollX);
501 }
502
Sunny Goyal70660032015-05-14 00:07:08 -0700503 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
504 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700505 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700506 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800507 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700508 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700509 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700510 overScroll(x - mMaxScrollX);
511 } else {
512 overScroll(x);
513 }
Adam Cohen68d73932010-11-15 10:50:58 -0800514 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700515 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700516 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800517 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700518 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700519 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700520 overScroll(x);
521 } else {
522 overScroll(x - mMaxScrollX);
523 }
Adam Cohen68d73932010-11-15 10:50:58 -0800524 }
525 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700526 if (mWasInOverscroll) {
527 overScroll(0);
528 mWasInOverscroll = false;
529 }
Adam Cohen68d73932010-11-15 10:50:58 -0800530 super.scrollTo(x, y);
531 }
532
Adam Cohen7d30a372013-07-01 17:03:59 -0700533 // Update the last motion events when scrolling
534 if (isReordering(true)) {
535 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
536 mLastMotionX = p[0];
537 mLastMotionY = p[1];
538 updateDragViewTranslationDuringDrag();
539 }
Michael Jurka0142d492010-08-25 17:46:15 -0700540 }
541
Adam Cohen53805212013-10-01 10:39:23 -0700542 private void sendScrollAccessibilityEvent() {
543 AccessibilityManager am =
544 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
545 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700546 if (mCurrentPage != getNextPage()) {
547 AccessibilityEvent ev =
548 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700549 ev.setScrollable(true);
550 ev.setScrollX(getScrollX());
551 ev.setScrollY(getScrollY());
552 ev.setMaxScrollX(mMaxScrollX);
553 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700554
Vadim Tryshevf4715972015-05-08 17:21:03 -0700555 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700556 }
Adam Cohen53805212013-10-01 10:39:23 -0700557 }
558 }
559
Michael Jurka0142d492010-08-25 17:46:15 -0700560 // we moved this functionality to a helper function so SmoothPagedView can reuse it
561 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800562 return computeScrollHelper(true);
563 }
564
565 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700567 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800569 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700570 float scaleX = mFreeScroll ? getScaleX() : 1f;
571 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
572 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700573 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800574 if (shouldInvalidate) {
575 invalidate();
576 }
Michael Jurka0142d492010-08-25 17:46:15 -0700577 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800578 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700579 sendScrollAccessibilityEvent();
580
Adam Cohen6f127a62014-06-12 14:54:41 -0700581 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700582 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700583 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700584
Adam Cohen73aa9752010-11-24 16:26:58 -0800585 // We don't want to trigger a page end moving unless the page has settled
586 // and the user has stopped scrolling
587 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700588 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800589 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700590
Adam Cohen7d30a372013-07-01 17:03:59 -0700591 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700592 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700593 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700594 if (am.isEnabled()) {
595 // Notify the user when the page changes
596 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700597 }
Michael Jurka0142d492010-08-25 17:46:15 -0700598 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
Michael Jurka0142d492010-08-25 17:46:15 -0700600 return false;
601 }
602
603 @Override
604 public void computeScroll() {
605 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700606 }
607
Adam Cohen96d30a12013-07-16 18:13:21 -0700608 public static class LayoutParams extends ViewGroup.LayoutParams {
609 public boolean isFullScreenPage = false;
610
Sunny Goyal7c786f72016-06-01 14:08:21 -0700611 // If true, the start edge of the page snaps to the start edge of the viewport.
612 public boolean matchStartEdge = false;
613
Adam Cohen96d30a12013-07-16 18:13:21 -0700614 /**
615 * {@inheritDoc}
616 */
617 public LayoutParams(int width, int height) {
618 super(width, height);
619 }
620
Sunny Goyal41b22c02015-05-14 15:20:48 -0700621 public LayoutParams(Context context, AttributeSet attrs) {
622 super(context, attrs);
623 }
624
Adam Cohen96d30a12013-07-16 18:13:21 -0700625 public LayoutParams(ViewGroup.LayoutParams source) {
626 super(source);
627 }
628 }
629
Sunny Goyal41b22c02015-05-14 15:20:48 -0700630 @Override
631 public LayoutParams generateLayoutParams(AttributeSet attrs) {
632 return new LayoutParams(getContext(), attrs);
633 }
634
635 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700636 protected LayoutParams generateDefaultLayoutParams() {
637 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
638 }
639
Sunny Goyal41b22c02015-05-14 15:20:48 -0700640 @Override
641 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
642 return new LayoutParams(p);
643 }
644
645 @Override
646 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
647 return p instanceof LayoutParams;
648 }
649
Adam Cohenedb40762013-07-18 16:45:45 -0700650 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700651 LayoutParams lp = generateDefaultLayoutParams();
652 lp.isFullScreenPage = true;
653 super.addView(page, 0, lp);
654 }
655
Adam Cohen410f3cd2013-09-22 12:09:32 -0700656 public int getNormalChildHeight() {
657 return mNormalChildHeight;
658 }
659
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 @Override
661 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700662 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700663 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
664 return;
665 }
666
Adam Cohen7d30a372013-07-01 17:03:59 -0700667 // We measure the dimensions of the PagedView to be larger than the pages so that when we
668 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700669 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
670 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
671 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700672 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800673 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700674 // viewport, we can be at most one and a half screens offset once we scale down
675 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700676 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
677 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700678
Winson Chungc82d2622013-11-06 13:23:29 -0800679 int parentWidthSize = (int) (2f * maxSize);
680 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700681 int scaledWidthSize, scaledHeightSize;
682 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700683 scaledWidthSize = (int) (parentWidthSize / mMinScale);
684 scaledHeightSize = (int) (parentHeightSize / mMinScale);
685 } else {
686 scaledWidthSize = widthSize;
687 scaledHeightSize = heightSize;
688 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700689 mViewport.set(0, 0, widthSize, heightSize);
690
691 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
692 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
693 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 }
695
Winson Chung8aad6102012-05-11 16:27:49 -0700696 // Return early if we aren't given a proper dimension
697 if (widthSize <= 0 || heightSize <= 0) {
698 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
699 return;
700 }
701
Adam Lesinski6b879f02010-11-04 16:15:23 -0700702 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
703 * of the All apps view on XLarge displays to not take up more space then it needs. Width
704 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
705 * each page to have the same width.
706 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 final int verticalPadding = getPaddingTop() + getPaddingBottom();
708 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700709
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700710 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700712 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700713 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700714 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
715 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
716 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
717 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 final int childCount = getChildCount();
719 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700720 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700721 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100722 if (child.getVisibility() != GONE) {
723 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700724
Vladimir Marko2824b072013-10-04 16:42:17 +0100725 int childWidthMode;
726 int childHeightMode;
727 int childWidth;
728 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700729
Vladimir Marko2824b072013-10-04 16:42:17 +0100730 if (!lp.isFullScreenPage) {
731 if (lp.width == LayoutParams.WRAP_CONTENT) {
732 childWidthMode = MeasureSpec.AT_MOST;
733 } else {
734 childWidthMode = MeasureSpec.EXACTLY;
735 }
736
737 if (lp.height == LayoutParams.WRAP_CONTENT) {
738 childHeightMode = MeasureSpec.AT_MOST;
739 } else {
740 childHeightMode = MeasureSpec.EXACTLY;
741 }
742
Adam Cohen3b185e22013-10-29 14:45:58 -0700743 childWidth = getViewportWidth() - horizontalPadding
744 - mInsets.left - mInsets.right;
745 childHeight = getViewportHeight() - verticalPadding
746 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100747 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700748 } else {
749 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700750 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100751
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800752 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700753 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700754 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700755 if (referenceChildWidth == 0) {
756 referenceChildWidth = childWidth;
757 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700758
Vladimir Marko2824b072013-10-04 16:42:17 +0100759 final int childWidthMeasureSpec =
760 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
761 final int childHeightMeasureSpec =
762 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
763 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700764 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700765 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700766 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800767 }
768
Sunny Goyalcf25b522015-07-09 00:01:18 -0700769 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700771 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700772 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700773 return;
774 }
775
Winson Chung785d2eb2011-04-14 16:08:02 -0700776 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700778
Adam Cohen7d30a372013-07-01 17:03:59 -0700779 int offsetX = getViewportOffsetX();
780 int offsetY = getViewportOffsetY();
781
782 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700783 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700784
Sunny Goyal70660032015-05-14 00:07:08 -0700785 final int startIndex = mIsRtl ? childCount - 1 : 0;
786 final int endIndex = mIsRtl ? -1 : childCount;
787 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700788
Adam Cohen7d30a372013-07-01 17:03:59 -0700789 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700790
Adam Cohen3b185e22013-10-29 14:45:58 -0700791 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000792 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700793
Sunny Goyal6178f132016-07-11 17:30:03 -0700794 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700795 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
796 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700797 }
798
Adam Cohen0ffac432013-07-10 11:19:26 -0700799 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700800 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700802 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100803 int childTop;
804 if (lp.isFullScreenPage) {
805 childTop = offsetY;
806 } else {
807 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700808 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100809 }
810
Adam Cohen96d30a12013-07-16 18:13:21 -0700811 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700812 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800813
Winson Chung785d2eb2011-04-14 16:08:02 -0700814 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700815 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800816 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700817
Sunny Goyal6178f132016-07-11 17:30:03 -0700818 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700819 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000820
821 int pageGap = mPageSpacing;
822 int next = i + delta;
823 if (next != endIndex) {
824 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
825 } else {
826 nextLp = null;
827 }
828
829 // Prevent full screen pages from showing in the viewport
830 // when they are not the current page.
831 if (lp.isFullScreenPage) {
832 pageGap = getPaddingLeft();
833 } else if (nextLp != null && nextLp.isFullScreenPage) {
834 pageGap = getPaddingRight();
835 }
836
Sunny Goyala1fbd842015-05-20 13:40:27 -0700837 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700840
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700841 final LayoutTransition transition = getLayoutTransition();
842 // If the transition is running defer updating max scroll, as some empty pages could
843 // still be present, and a max scroll change could cause sudden jumps in scroll.
844 if (transition != null && transition.isRunning()) {
845 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
846
847 @Override
848 public void startTransition(LayoutTransition transition, ViewGroup container,
849 View view, int transitionType) { }
850
851 @Override
852 public void endTransition(LayoutTransition transition, ViewGroup container,
853 View view, int transitionType) {
854 // Wait until all transitions are complete.
855 if (!transition.isRunning()) {
856 transition.removeTransitionListener(this);
857 updateMaxScrollX();
858 }
859 }
860 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700861 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700862 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700863 }
864
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700865 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
866 updateCurrentPageScroll();
867 mFirstLayout = false;
868 }
869
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700870 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700871 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700872 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700873 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700874
875 if (isReordering(true)) {
876 updateDragViewTranslationDuringDrag();
877 }
Winson Chunge3193b92010-09-10 11:44:42 -0700878 }
879
Sunny Goyala1fbd842015-05-20 13:40:27 -0700880 protected int getChildGap() {
881 return 0;
882 }
883
Sunny Goyal316490e2015-06-02 09:38:28 -0700884 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700885 mMaxScrollX = computeMaxScrollX();
886 }
887
888 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700889 int childCount = getChildCount();
890 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700891 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700892 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700893 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700894 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700895 }
896 }
897
Adam Cohen3b185e22013-10-29 14:45:58 -0700898 public void setPageSpacing(int pageSpacing) {
899 mPageSpacing = pageSpacing;
900 requestLayout();
901 }
902
Winson Chunge3193b92010-09-10 11:44:42 -0700903 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700904 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700905 // Update the page indicator, we don't update the page indicator as we
906 // add/remove pages
907 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700908 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700909 }
910
Adam Cohen2591f6a2011-10-25 14:36:40 -0700911 // This ensures that when children are added, they get the correct transforms / alphas
912 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700913 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700914 invalidate();
915 }
916
Michael Jurka8b805b12012-04-18 14:23:14 -0700917 @Override
918 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700919 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700920 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700921 }
922
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700923 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700924 // Update the page indicator, we don't update the page indicator as we
925 // add/remove pages
926 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700927 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700928 }
929 }
930
931 @Override
932 public void removeView(View v) {
933 // XXX: We should find a better way to hook into this before the view
934 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700935 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700936 super.removeView(v);
937 }
938 @Override
939 public void removeViewInLayout(View v) {
940 // XXX: We should find a better way to hook into this before the view
941 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700942 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700943 super.removeViewInLayout(v);
944 }
945 @Override
946 public void removeViewAt(int index) {
947 // XXX: We should find a better way to hook into this before the view
948 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700949 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700950 super.removeViewAt(index);
951 }
952 @Override
953 public void removeAllViewsInLayout() {
954 // Update the page indicator, we don't update the page indicator as we
955 // add/remove pages
956 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700957 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700958 }
959
960 super.removeAllViewsInLayout();
961 }
962
Adam Cohen73894962011-10-31 13:17:17 -0700963 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700964 if (index < 0 || index > getChildCount() - 1) return 0;
965
Adam Cohenf698c6e2013-07-17 18:44:25 -0700966 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700967
Adam Cohenf698c6e2013-07-17 18:44:25 -0700968 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700969 }
970
Adam Cohen6f127a62014-06-12 14:54:41 -0700971 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700972 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700973 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700974 }
975
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -0700977 public void draw(Canvas canvas) {
978 super.draw(canvas);
979 if (getPageCount() > 0) {
980 if (!mEdgeGlowLeft.isFinished()) {
981 final int restoreCount = canvas.save();
982 Rect display = mViewport;
983 canvas.translate(display.left, display.top);
984 canvas.rotate(270);
985
Hyunyoung Song0de01172016-10-05 16:27:48 -0700986 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700987 canvas.translate(display.top - sTmpIntPoint[1], 0);
988 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
989 if (mEdgeGlowLeft.draw(canvas)) {
990 postInvalidateOnAnimation();
991 }
992 canvas.restoreToCount(restoreCount);
993 }
994 if (!mEdgeGlowRight.isFinished()) {
995 final int restoreCount = canvas.save();
996 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700997 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700998 canvas.rotate(90);
999
Hyunyoung Song0de01172016-10-05 16:27:48 -07001000 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001001
Sunny Goyal93264612015-11-23 11:47:50 -08001002 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1003 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001004 if (mEdgeGlowRight.draw(canvas)) {
1005 postInvalidateOnAnimation();
1006 }
1007 canvas.restoreToCount(restoreCount);
1008 }
1009 }
1010 }
1011
1012 /**
1013 * Returns the top and bottom position for the edge effect.
1014 */
Hyunyoung Song0de01172016-10-05 16:27:48 -07001015 protected abstract void getEdgeVerticalPosition(int[] pos);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001016
1017 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001019 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001020 if (page != mCurrentPage || !mScroller.isFinished()) {
1021 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 return true;
1023 }
1024 return false;
1025 }
1026
1027 @Override
1028 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001029 int focusablePage;
1030 if (mNextPage != INVALID_PAGE) {
1031 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001033 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 }
Winson Chung86f77532010-08-24 11:08:22 -07001035 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001037 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 }
1039 return false;
1040 }
1041
1042 @Override
1043 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001044 if (super.dispatchUnhandledMove(focused, direction)) {
1045 return true;
1046 }
1047
1048 if (mIsRtl) {
1049 if (direction == View.FOCUS_LEFT) {
1050 direction = View.FOCUS_RIGHT;
1051 } else if (direction == View.FOCUS_RIGHT) {
1052 direction = View.FOCUS_LEFT;
1053 }
1054 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001056 if (getCurrentPage() > 0) {
1057 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 return true;
1059 }
1060 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001061 if (getCurrentPage() < getPageCount() - 1) {
1062 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 return true;
1064 }
1065 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001066 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068
1069 @Override
1070 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001071 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1072 return;
1073 }
1074
Adam Cohen0ffac432013-07-10 11:19:26 -07001075 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001076 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001077 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 }
1079 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001080 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001081 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 }
1083 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001084 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001085 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001086 }
1087 }
1088 }
1089
1090 /**
1091 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001092 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 *
Winson Chung86f77532010-08-24 11:08:22 -07001094 * This happens when live folders requery, and if they're off page, they
1095 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 */
1097 @Override
1098 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001099 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001100 View v = focused;
1101 while (true) {
1102 if (v == current) {
1103 super.focusableViewAvailable(focused);
1104 return;
1105 }
1106 if (v == this) {
1107 return;
1108 }
1109 ViewParent parent = v.getParent();
1110 if (parent instanceof View) {
1111 v = (View)v.getParent();
1112 } else {
1113 return;
1114 }
1115 }
1116 }
1117
1118 /**
1119 * {@inheritDoc}
1120 */
1121 @Override
1122 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1123 if (disallowIntercept) {
1124 // We need to make sure to cancel our long press if
1125 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001126 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001127 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001128 }
1129 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1130 }
1131
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001132 /**
1133 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1134 */
1135 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001136 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001137 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001138 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001139 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001140 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001141 }
1142
1143 /**
1144 * Return true if a tap at (x, y) should trigger a flip to the next page.
1145 */
1146 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001147 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001148 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001149 }
1150 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001151 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001152 }
Winson Chung52aee602013-01-30 12:01:02 -08001153
Adam Cohen7d30a372013-07-01 17:03:59 -07001154 /** Returns whether x and y originated within the buffered viewport */
1155 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001156 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001157 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001158 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001159 }
1160
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 @Override
1162 public boolean onInterceptTouchEvent(MotionEvent ev) {
1163 /*
1164 * This method JUST determines whether we want to intercept the motion.
1165 * If we return true, onTouchEvent will be called and we do the actual
1166 * scrolling there.
1167 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001168 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001169
Winson Chung45e1d6e2010-11-09 17:19:49 -08001170 // Skip touch handling if there are no pages to swipe
1171 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1172
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 /*
1174 * Shortcut the most recurring case: the user is in the dragging
1175 * state and he is moving his finger. We want to intercept this
1176 * motion.
1177 */
1178 final int action = ev.getAction();
1179 if ((action == MotionEvent.ACTION_MOVE) &&
1180 (mTouchState == TOUCH_STATE_SCROLLING)) {
1181 return true;
1182 }
1183
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 switch (action & MotionEvent.ACTION_MASK) {
1185 case MotionEvent.ACTION_MOVE: {
1186 /*
1187 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1188 * whether the user has moved far enough from his original down touch.
1189 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001190 if (mActivePointerId != INVALID_POINTER) {
1191 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001192 }
1193 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1194 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1195 // i.e. fall through to the next case (don't break)
1196 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1197 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001198 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 }
1200
1201 case MotionEvent.ACTION_DOWN: {
1202 final float x = ev.getX();
1203 final float y = ev.getY();
1204 // Remember location of down touch
1205 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001206 mDownMotionY = y;
1207 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 mLastMotionX = x;
1209 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001210 float[] p = mapPointFromViewToParent(this, x, y);
1211 mParentDownMotionX = p[0];
1212 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001213 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001214 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001216
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 /*
1218 * If being flinged and user touches the screen, initiate drag;
1219 * otherwise don't. mScroller.isFinished should be false when
1220 * being flinged.
1221 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001222 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001223 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001224
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001225 if (finishedScrolling) {
1226 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001227 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001228 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001229 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001230 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001231 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001232 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1233 mTouchState = TOUCH_STATE_SCROLLING;
1234 } else {
1235 mTouchState = TOUCH_STATE_REST;
1236 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001237 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001238
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 break;
1240 }
1241
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001243 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001244 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 break;
1246
1247 case MotionEvent.ACTION_POINTER_UP:
1248 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001249 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 break;
1251 }
1252
1253 /*
1254 * The only time we want to intercept motion events is if we are in the
1255 * drag mode.
1256 */
1257 return mTouchState != TOUCH_STATE_REST;
1258 }
1259
Adam Cohenf8d28232011-02-01 21:47:00 -08001260 protected void determineScrollingStart(MotionEvent ev) {
1261 determineScrollingStart(ev, 1.0f);
1262 }
1263
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 /*
1265 * Determines if we should change the touch state to start scrolling after the
1266 * user moves their touch point too far.
1267 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001268 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001269 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001271 if (pointerIndex == -1) return;
1272
1273 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 final float x = ev.getX(pointerIndex);
1275 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001276 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1277
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279
Adam Cohenf8d28232011-02-01 21:47:00 -08001280 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001282
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001283 if (xMoved) {
1284 // Scroll if the user moved far enough along the X axis
1285 mTouchState = TOUCH_STATE_SCROLLING;
1286 mTotalMotionX += Math.abs(mLastMotionX - x);
1287 mLastMotionX = x;
1288 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001289 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001290 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001291 // Stop listening for things like pinches.
1292 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001293 }
1294 }
1295
1296 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001297 // Try canceling the long press. It could also have been scheduled
1298 // by a distant descendant, so use the mAllowLongPress flag to block
1299 // everything
1300 final View currentPage = getPageAt(mCurrentPage);
1301 if (currentPage != null) {
1302 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 }
1304 }
1305
Adam Cohenb5ba0972011-09-07 18:02:31 -07001306 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001307 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001308
Adam Cohenedb40762013-07-18 16:45:45 -07001309 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001310 int count = getChildCount();
1311
1312 final int totalDistance;
1313
1314 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001315 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001316 adjacentPage = page - 1;
1317 }
1318
1319 if (adjacentPage < 0 || adjacentPage > count - 1) {
1320 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1321 } else {
1322 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1323 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001324
1325 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001326 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1327 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001328 return scrollProgress;
1329 }
1330
Adam Cohenedb40762013-07-18 16:45:45 -07001331 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001332 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001333 return 0;
1334 } else {
1335 return mPageScrolls[index];
1336 }
1337 }
1338
Adam Cohen564a2e72013-10-09 14:47:32 -07001339 // While layout transitions are occurring, a child's position may stray from its baseline
1340 // position. This method returns the magnitude of this stray at any given time.
1341 public int getLayoutTransitionOffsetForPage(int index) {
1342 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1343 return 0;
1344 } else {
1345 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001346
1347 int scrollOffset = 0;
1348 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1349 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001350 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001351 }
1352
Adam Cohen564a2e72013-10-09 14:47:32 -07001353 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1354 return (int) (child.getX() - baselineX);
1355 }
1356 }
1357
Adam Cohenb5ba0972011-09-07 18:02:31 -07001358 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001359 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001360 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001361 if (f < 0) {
1362 mEdgeGlowLeft.onPull(-f);
1363 } else if (f > 0) {
1364 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001365 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001366 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001367 }
1368 invalidate();
1369 }
1370
Adam Cohenb5ba0972011-09-07 18:02:31 -07001371 protected void overScroll(float amount) {
1372 dampedOverScroll(amount);
1373 }
1374
Winson Chungdc61c4d2015-04-20 18:26:57 -07001375 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001376 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001377 }
1378
Winson Chungdc61c4d2015-04-20 18:26:57 -07001379 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001380 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001381 }
1382
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001383 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001384 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001385 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001386 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1387 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1388 } else {
1389 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1390 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1391 }
1392 }
1393
Adam Cohenf9618852013-11-08 06:45:03 -08001394 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001395 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396 mFreeScroll = freeScroll;
1397
Adam Cohenf9618852013-11-08 06:45:03 -08001398 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001399 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001400 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1402 setCurrentPage(mTempVisiblePagesRange[0]);
1403 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1404 setCurrentPage(mTempVisiblePagesRange[1]);
1405 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001406 } else if (wasFreeScroll) {
1407 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001408 }
1409
1410 setEnableOverscroll(!freeScroll);
1411 }
1412
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001413 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 mAllowOverScroll = enable;
1415 }
1416
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001417 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 if (mDragView != null) {
1419 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1420 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001421 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001422 int minDistance = Integer.MAX_VALUE;
1423 int minIndex = indexOfChild(mDragView);
1424 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1425 View page = getPageAt(i);
1426 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1427 int d = Math.abs(dragX - pageX);
1428 if (d < minDistance) {
1429 minIndex = i;
1430 minDistance = d;
1431 }
1432 }
1433 return minIndex;
1434 }
1435 return -1;
1436 }
1437
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 @Override
1439 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001440 super.onTouchEvent(ev);
1441
Winson Chung45e1d6e2010-11-09 17:19:49 -08001442 // Skip touch handling if there are no pages to swipe
1443 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1444
Michael Jurkab8f06722010-10-10 15:58:46 -07001445 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001446
1447 final int action = ev.getAction();
1448
1449 switch (action & MotionEvent.ACTION_MASK) {
1450 case MotionEvent.ACTION_DOWN:
1451 /*
1452 * If being flinged and user touches, stop the fling. isFinished
1453 * will be false if being flinged.
1454 */
1455 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001456 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 }
1458
1459 // Remember where the motion event started
1460 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001461 mDownMotionY = mLastMotionY = ev.getY();
1462 mDownScrollX = getScrollX();
1463 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1464 mParentDownMotionX = p[0];
1465 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001466 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001467 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001469
Michael Jurka0142d492010-08-25 17:46:15 -07001470 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001471 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001472 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001473 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 break;
1475
1476 case MotionEvent.ACTION_MOVE:
1477 if (mTouchState == TOUCH_STATE_SCROLLING) {
1478 // Scroll to follow the motion event
1479 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001480
1481 if (pointerIndex == -1) return true;
1482
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001484 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001485
Adam Cohenaefd4e12011-02-14 16:39:38 -08001486 mTotalMotionX += Math.abs(deltaX);
1487
Winson Chungc0844aa2011-02-02 15:25:58 -08001488 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1489 // keep the remainder because we are actually testing if we've moved from the last
1490 // scrolled position (which is discrete).
1491 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001492 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001493 mLastMotionX = x;
1494 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 } else {
1496 awakenScrollBars();
1497 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001498 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1499 // Update the last motion position
1500 mLastMotionX = ev.getX();
1501 mLastMotionY = ev.getY();
1502
1503 // Update the parent down so that our zoom animations take this new movement into
1504 // account
1505 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1506 mParentDownMotionX = pt[0];
1507 mParentDownMotionY = pt[1];
1508 updateDragViewTranslationDuringDrag();
1509
1510 // Find the closest page to the touch point
1511 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001512
Adam Cohen7d30a372013-07-01 17:03:59 -07001513 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1514 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1515 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1516 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1517
Adam Cohenf358a4b2013-07-23 16:47:31 -07001518 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001519 // Do not allow any page to be moved to 0th position.
1520 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001521 mTempVisiblePagesRange[0] = 0;
1522 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001523 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001524 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1525 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1526 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1527 mSidePageHoverIndex = pageUnderPointIndex;
1528 mSidePageHoverRunnable = new Runnable() {
1529 @Override
1530 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 // Setup the scroll to the correct page before we swap the views
1532 snapToPage(pageUnderPointIndex);
1533
1534 // For each of the pages between the paged view and the drag view,
1535 // animate them from the previous position to the new position in
1536 // the layout (as a result of the drag view moving in the layout)
1537 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1538 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1539 dragViewIndex + 1 : pageUnderPointIndex;
1540 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1541 dragViewIndex - 1 : pageUnderPointIndex;
1542 for (int i = lowerIndex; i <= upperIndex; ++i) {
1543 View v = getChildAt(i);
1544 // dragViewIndex < pageUnderPointIndex, so after we remove the
1545 // drag view all subsequent views to pageUnderPointIndex will
1546 // shift down.
1547 int oldX = getViewportOffsetX() + getChildOffset(i);
1548 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1549
1550 // Animate the view translation from its old position to its new
1551 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001552 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001553 if (anim != null) {
1554 anim.cancel();
1555 }
1556
1557 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001558 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001559 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001560 anim.start();
1561 v.setTag(anim);
1562 }
1563
1564 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001565 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001566 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001567 if (mPageIndicator != null) {
1568 mPageIndicator.setActiveMarker(getNextPage());
1569 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001570 }
1571 };
1572 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1573 }
1574 } else {
1575 removeCallbacks(mSidePageHoverRunnable);
1576 mSidePageHoverIndex = -1;
1577 }
Adam Cohen564976a2010-10-13 18:52:07 -07001578 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001579 determineScrollingStart(ev);
1580 }
1581 break;
1582
1583 case MotionEvent.ACTION_UP:
1584 if (mTouchState == TOUCH_STATE_SCROLLING) {
1585 final int activePointerId = mActivePointerId;
1586 final int pointerIndex = ev.findPointerIndex(activePointerId);
1587 final float x = ev.getX(pointerIndex);
1588 final VelocityTracker velocityTracker = mVelocityTracker;
1589 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1590 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001591 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001592 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001593 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1594 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001595
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001596 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1597
Adam Cohen00481b32011-11-18 12:03:48 -08001598 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001599 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001600
Adam Cohenf358a4b2013-07-23 16:47:31 -07001601 if (!mFreeScroll) {
1602 // In the case that the page is moved far to one direction and then is flung
1603 // in the opposite direction, we use a threshold to determine whether we should
1604 // just return to the starting page, or if we should skip one further.
1605 boolean returnToOriginalPage = false;
1606 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1607 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1608 returnToOriginalPage = true;
1609 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001610
Adam Cohenf358a4b2013-07-23 16:47:31 -07001611 int finalPage;
1612 // We give flings precedence over large moves, which is why we short-circuit our
1613 // test for a large move if a fling has been registered. That is, a large
1614 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001615 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1616 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001617 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1618 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1619 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1620 snapToPageWithVelocity(finalPage, velocityX);
1621 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1622 (isFling && isVelocityXLeft)) &&
1623 mCurrentPage < getChildCount() - 1) {
1624 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1625 snapToPageWithVelocity(finalPage, velocityX);
1626 } else {
1627 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001628 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001629 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001630 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001631 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001632 }
1633
1634 float scaleX = getScaleX();
1635 int vX = (int) (-velocityX * scaleX);
1636 int initialScrollX = (int) (getScrollX() * scaleX);
1637
Adam Cohenf9618852013-11-08 06:45:03 -08001638 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001639 mScroller.fling(initialScrollX,
1640 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001641 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001642 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001643 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001644 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001645 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1646 // at this point we have not moved beyond the touch slop
1647 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1648 // we can just page
1649 int nextPage = Math.max(0, mCurrentPage - 1);
1650 if (nextPage != mCurrentPage) {
1651 snapToPage(nextPage);
1652 } else {
1653 snapToDestination();
1654 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001655 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001656 // at this point we have not moved beyond the touch slop
1657 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1658 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001659 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1660 if (nextPage != mCurrentPage) {
1661 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001662 } else {
1663 snapToDestination();
1664 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001665 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1666 // Update the last motion position
1667 mLastMotionX = ev.getX();
1668 mLastMotionY = ev.getY();
1669
1670 // Update the parent down so that our zoom animations take this new movement into
1671 // account
1672 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1673 mParentDownMotionX = pt[0];
1674 mParentDownMotionY = pt[1];
1675 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001676 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001677 if (!mCancelTap) {
1678 onUnhandledTap(ev);
1679 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001680 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001681
1682 // Remove the callback to wait for the side page hover timeout
1683 removeCallbacks(mSidePageHoverRunnable);
1684 // End any intermediate reordering states
1685 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001686 break;
1687
1688 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001689 if (mTouchState == TOUCH_STATE_SCROLLING) {
1690 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001691 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001692 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001693 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001694 break;
1695
1696 case MotionEvent.ACTION_POINTER_UP:
1697 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001698 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001699 break;
1700 }
1701
1702 return true;
1703 }
1704
Adam Cohen7d30a372013-07-01 17:03:59 -07001705 private void resetTouchState() {
1706 releaseVelocityTracker();
1707 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001708 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001709 mTouchState = TOUCH_STATE_REST;
1710 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001711 mEdgeGlowLeft.onRelease();
1712 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001713 }
1714
Adam Cohenc2d6e892014-10-16 09:49:24 -07001715 /**
1716 * Triggered by scrolling via touch
1717 */
1718 protected void onScrollInteractionBegin() {
1719 }
1720
1721 protected void onScrollInteractionEnd() {
1722 }
1723
Adam Cohen1697b792013-09-17 19:08:21 -07001724 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001725 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001726 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001727
Winson Chung185d7162011-02-28 13:47:29 -08001728 @Override
1729 public boolean onGenericMotionEvent(MotionEvent event) {
1730 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1731 switch (event.getAction()) {
1732 case MotionEvent.ACTION_SCROLL: {
1733 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1734 final float vscroll;
1735 final float hscroll;
1736 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1737 vscroll = 0;
1738 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1739 } else {
1740 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1741 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1742 }
1743 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001744 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001745 : (hscroll > 0 || vscroll > 0);
1746 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001747 scrollRight();
1748 } else {
1749 scrollLeft();
1750 }
1751 return true;
1752 }
1753 }
1754 }
1755 }
1756 return super.onGenericMotionEvent(event);
1757 }
1758
Michael Jurkab8f06722010-10-10 15:58:46 -07001759 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1760 if (mVelocityTracker == null) {
1761 mVelocityTracker = VelocityTracker.obtain();
1762 }
1763 mVelocityTracker.addMovement(ev);
1764 }
1765
1766 private void releaseVelocityTracker() {
1767 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001768 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001769 mVelocityTracker.recycle();
1770 mVelocityTracker = null;
1771 }
1772 }
1773
Winson Chung321e9ee2010-08-09 13:37:56 -07001774 private void onSecondaryPointerUp(MotionEvent ev) {
1775 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1776 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1777 final int pointerId = ev.getPointerId(pointerIndex);
1778 if (pointerId == mActivePointerId) {
1779 // This was our active pointer going up. Choose a new
1780 // active pointer and adjust accordingly.
1781 // TODO: Make this decision more intelligent.
1782 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1783 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1784 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001785 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001786 mActivePointerId = ev.getPointerId(newPointerIndex);
1787 if (mVelocityTracker != null) {
1788 mVelocityTracker.clear();
1789 }
1790 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001791 }
1792
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 @Override
1794 public void requestChildFocus(View child, View focused) {
1795 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001796 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001797 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001798 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001799 }
1800 }
1801
Adam Cohend19d3ca2010-09-15 14:43:42 -07001802 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001803 return getPageNearestToCenterOfScreen(getScrollX());
1804 }
1805
1806 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1807 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001808 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001809 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001810 final int childCount = getChildCount();
1811 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001812 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001813 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001814 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001815 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001816 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1817 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1818 minDistanceFromScreenCenter = distanceFromScreenCenter;
1819 minDistanceFromScreenCenterIndex = i;
1820 }
1821 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001822 return minDistanceFromScreenCenterIndex;
1823 }
1824
1825 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001826 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001827 }
1828
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001829 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001830 public ScrollInterpolator() {
1831 }
1832
1833 public float getInterpolation(float t) {
1834 t -= 1.0f;
1835 return t*t*t*t*t + 1;
1836 }
1837 }
1838
1839 // We want the duration of the page snap animation to be influenced by the distance that
1840 // the screen has to travel, however, we don't want this duration to be effected in a
1841 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1842 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001843 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001844 f -= 0.5f; // center the values about 0.
1845 f *= 0.3f * Math.PI / 2.0f;
1846 return (float) Math.sin(f);
1847 }
1848
Michael Jurka0142d492010-08-25 17:46:15 -07001849 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001850 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001851 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001852
Adam Cohenedb40762013-07-18 16:45:45 -07001853 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001854 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001855 int duration = 0;
1856
Sunny Goyal4d113a52015-05-27 10:05:28 -07001857 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001858 // If the velocity is low enough, then treat this more as an automatic page advance
1859 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001860 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001861 return;
1862 }
1863
1864 // Here we compute a "distance" that will be used in the computation of the overall
1865 // snap duration. This is a function of the actual distance that needs to be traveled;
1866 // we keep this value close to half screen size in order to reduce the variance in snap
1867 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001868 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001869 float distance = halfScreenSize + halfScreenSize *
1870 distanceInfluenceForSnapDuration(distanceRatio);
1871
1872 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001873 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001874
1875 // we want the page's snap velocity to approximately match the velocity at which the
1876 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001877 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1878 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001879
1880 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001881 }
1882
Sunny Goyal1740d902015-05-27 11:14:01 -07001883 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001884 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001885 }
1886
Adam Cohenf9c184a2016-01-15 16:47:43 -08001887 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001888 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001889 }
1890
Michael Jurka0142d492010-08-25 17:46:15 -07001891 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001892 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001893 }
1894
Adam Cohenf9618852013-11-08 06:45:03 -08001895 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1896 snapToPage(whichPage, duration, false, interpolator);
1897 }
1898
1899 protected void snapToPage(int whichPage, int duration, boolean immediate,
1900 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001901 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001902
Adam Cohenedb40762013-07-18 16:45:45 -07001903 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001904 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001905 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001906 }
1907
1908 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001909 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001910 }
Michael Jurka0142d492010-08-25 17:46:15 -07001911
Adam Cohenf9618852013-11-08 06:45:03 -08001912 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1913 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001914 whichPage = validateNewPage(whichPage);
1915
Adam Cohen7d30a372013-07-01 17:03:59 -07001916 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001917
Winson Chung321e9ee2010-08-09 13:37:56 -07001918 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001919 if (immediate) {
1920 duration = 0;
1921 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001922 duration = Math.abs(delta);
1923 }
1924
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001925 if (duration != 0) {
1926 pageBeginTransition();
1927 }
1928
Adam Cohenf358a4b2013-07-23 16:47:31 -07001929 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001930 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 }
Adam Cohenf9618852013-11-08 06:45:03 -08001932
1933 if (interpolator != null) {
1934 mScroller.setInterpolator(interpolator);
1935 } else {
1936 mScroller.setInterpolator(mDefaultInterpolator);
1937 }
1938
Sunny Goyalc86df472016-02-25 09:19:38 -08001939 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001940
Adam Cohen674531f2013-12-13 15:07:14 -08001941 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001942
1943 // Trigger a compute() to finish switching pages if necessary
1944 if (immediate) {
1945 computeScroll();
1946 }
1947
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 invalidate();
1949 }
1950
Winson Chung321e9ee2010-08-09 13:37:56 -07001951 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001952 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 }
1954
1955 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001956 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001957 }
1958
Adam Cohendbdff6b2013-09-18 19:09:15 -07001959 @Override
1960 public boolean performLongClick() {
1961 mCancelTap = true;
1962 return super.performLongClick();
1963 }
1964
Winson Chung321e9ee2010-08-09 13:37:56 -07001965 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001966 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001967
1968 SavedState(Parcelable superState) {
1969 super(superState);
1970 }
1971
Adam Cohen091440a2015-03-18 14:16:05 -07001972 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001973 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001974 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001975 }
1976
1977 @Override
1978 public void writeToParcel(Parcel out, int flags) {
1979 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001980 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001981 }
1982
1983 public static final Parcelable.Creator<SavedState> CREATOR =
1984 new Parcelable.Creator<SavedState>() {
1985 public SavedState createFromParcel(Parcel in) {
1986 return new SavedState(in);
1987 }
1988
1989 public SavedState[] newArray(int size) {
1990 return new SavedState[size];
1991 }
1992 };
1993 }
1994
Adam Cohen7d30a372013-07-01 17:03:59 -07001995 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001996 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001997 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001998 Animator anim = new LauncherViewPropertyAnimator(mDragView)
1999 .translationX(0)
2000 .translationY(0)
2001 .scaleX(1)
2002 .scaleY(1)
2003 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002004 anim.addListener(new AnimatorListenerAdapter() {
2005 @Override
2006 public void onAnimationEnd(Animator animation) {
2007 onPostReorderingAnimationCompleted();
2008 }
2009 });
2010 anim.start();
2011 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002012 }
2013
Sunny Goyal1d08f702015-05-04 15:50:25 -07002014 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2016 mTouchState = TOUCH_STATE_REORDERING;
2017 mIsReordering = true;
2018
Adam Cohen7d30a372013-07-01 17:03:59 -07002019 // We must invalidate to trigger a redraw to update the layers such that the drag view
2020 // is always drawn on top
2021 invalidate();
2022 }
2023
Adam Cohen091440a2015-03-18 14:16:05 -07002024 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 // Trigger the callback when reordering has settled
2026 --mPostReorderingPreZoomInRemainingAnimationCount;
2027 if (mPostReorderingPreZoomInRunnable != null &&
2028 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2029 mPostReorderingPreZoomInRunnable.run();
2030 mPostReorderingPreZoomInRunnable = null;
2031 }
2032 }
2033
Sunny Goyal1d08f702015-05-04 15:50:25 -07002034 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002035 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002036 }
2037
Adam Cohenf358a4b2013-07-23 16:47:31 -07002038 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002039 int dragViewIndex = indexOfChild(v);
2040
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002041 // Do not allow the first page to be moved around
2042 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002043
Adam Cohen7d30a372013-07-01 17:03:59 -07002044 mTempVisiblePagesRange[0] = 0;
2045 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002046 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002047 mReorderingStarted = true;
2048
2049 // Check if we are within the reordering range
2050 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002051 dragViewIndex <= mTempVisiblePagesRange[1]) {
2052 // Find the drag view under the pointer
2053 mDragView = getChildAt(dragViewIndex);
2054 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2055 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002056 snapToPage(getPageNearestToCenterOfScreen());
2057 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002058 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002059 return true;
2060 }
2061 return false;
2062 }
2063
2064 boolean isReordering(boolean testTouchState) {
2065 boolean state = mIsReordering;
2066 if (testTouchState) {
2067 state &= (mTouchState == TOUCH_STATE_REORDERING);
2068 }
2069 return state;
2070 }
2071 void endReordering() {
2072 // For simplicity, we call endReordering sometimes even if reordering was never started.
2073 // In that case, we don't want to do anything.
2074 if (!mReorderingStarted) return;
2075 mReorderingStarted = false;
2076
2077 // If we haven't flung-to-delete the current child, then we just animate the drag view
2078 // back into position
2079 final Runnable onCompleteRunnable = new Runnable() {
2080 @Override
2081 public void run() {
2082 onEndReordering();
2083 }
2084 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002085
2086 mPostReorderingPreZoomInRunnable = new Runnable() {
2087 public void run() {
2088 onCompleteRunnable.run();
2089 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002090 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002091 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002092
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002093 mPostReorderingPreZoomInRemainingAnimationCount =
2094 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2095 // Snap to the current page
2096 snapToPage(indexOfChild(mDragView), 0);
2097 // Animate the drag view back to the front position
2098 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002099 }
2100
Winson Chung6a0f57d2011-06-29 20:10:49 -07002101 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002102 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002103 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002104 @Override
2105 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2106 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002107 info.setScrollable(getPageCount() > 1);
2108 if (getCurrentPage() < getPageCount() - 1) {
2109 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2110 }
2111 if (getCurrentPage() > 0) {
2112 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2113 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002114 info.setClassName(getClass().getName());
2115
2116 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2117 // Besides disabling the accessibility long-click, this also prevents this view from getting
2118 // accessibility focus.
2119 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002120 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002121 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2122 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002123 }
2124
2125 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002126 public void sendAccessibilityEvent(int eventType) {
2127 // Don't let the view send real scroll events.
2128 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2129 super.sendAccessibilityEvent(eventType);
2130 }
2131 }
2132
2133 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002134 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2135 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002136 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002137 }
2138
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002139 @Override
2140 public boolean performAccessibilityAction(int action, Bundle arguments) {
2141 if (super.performAccessibilityAction(action, arguments)) {
2142 return true;
2143 }
2144 switch (action) {
2145 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2146 if (getCurrentPage() < getPageCount() - 1) {
2147 scrollRight();
2148 return true;
2149 }
2150 } break;
2151 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2152 if (getCurrentPage() > 0) {
2153 scrollLeft();
2154 return true;
2155 }
2156 } break;
2157 }
2158 return false;
2159 }
2160
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002161 protected String getPageIndicatorDescription() {
2162 return getCurrentPageDescription();
2163 }
2164
Adam Cohen0ffac432013-07-10 11:19:26 -07002165 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002166 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002167 getNextPage() + 1, getChildCount());
2168 }
2169
Winson Chungd11265e2011-08-30 13:37:23 -07002170 @Override
2171 public boolean onHoverEvent(android.view.MotionEvent event) {
2172 return true;
2173 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002174}