blob: fb6a611e7585b770e37cb588fbca368c94761bab [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070030import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.os.Parcel;
32import android.os.Parcelable;
33import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070034import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080036import android.view.InputDevice;
37import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.MotionEvent;
39import android.view.VelocityTracker;
40import android.view.View;
41import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080042import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.view.ViewGroup;
44import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070045import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070046import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080048import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049
Sunny Goyal9e76f682017-02-13 12:13:43 -080050import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070052import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070054
Winson Chung6a0f57d2011-06-29 20:10:49 -070055import java.util.ArrayList;
56
Winson Chung321e9ee2010-08-09 13:37:56 -070057/**
58 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070059 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070060 */
Michael Jurka8b805b12012-04-18 14:23:14 -070061public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070062 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070063 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Winson Chung86f77532010-08-24 11:08:22 -070066 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070067 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Vadim Tryshevfedca432015-08-19 17:55:02 -070069 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070070 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070071
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080073 // The page is moved more than halfway, automatically move to the next page on touch up.
74 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Sunny Goyal8e2133b2015-05-06 13:39:07 -070076 private static final float MAX_SCROLL_PROGRESS = 1.0f;
77
Adam Cohen265b9a62011-12-07 14:37:18 -080078 // The following constants need to be scaled based on density. The scaled versions will be
79 // assigned to the corresponding member variables below.
80 private static final int FLING_THRESHOLD_VELOCITY = 500;
81 private static final int MIN_SNAP_VELOCITY = 1500;
82 private static final int MIN_FLING_VELOCITY = 250;
83
Adam Cohen21cd0022013-10-09 18:57:02 -070084 public static final int INVALID_RESTORE_PAGE = -1001;
85
Adam Cohenf358a4b2013-07-23 16:47:31 -070086 private boolean mFreeScroll = false;
87 private int mFreeScrollMinScrollX = -1;
88 private int mFreeScrollMaxScrollX = -1;
89
Adam Cohen265b9a62011-12-07 14:37:18 -080090 protected int mFlingThresholdVelocity;
91 protected int mMinFlingVelocity;
92 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070095 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070096
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070099 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800103 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800104 protected LauncherScroller mScroller;
105 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mParentDownMotionX;
110 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mDownMotionY;
113 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700114 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700115 private float mLastMotionX;
116 private float mLastMotionXRemainder;
117 private float mLastMotionY;
118 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700119
Adam Cohendbdff6b2013-09-18 19:09:15 -0700120 private boolean mCancelTap;
121
Adam Cohenedb40762013-07-18 16:45:45 -0700122 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Michael Jurka0142d492010-08-25 17:46:15 -0700124 protected final static int TOUCH_STATE_REST = 0;
125 protected final static int TOUCH_STATE_SCROLLING = 1;
126 protected final static int TOUCH_STATE_PREV_PAGE = 2;
127 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700128 protected final static int TOUCH_STATE_REORDERING = 4;
129
Michael Jurka0142d492010-08-25 17:46:15 -0700130 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800131
Michael Jurka0142d492010-08-25 17:46:15 -0700132 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800136 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800137 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700143 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700144
Sunny Goyal061380a2016-02-29 15:15:03 -0800145 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700146
Winson Chungd2be3812013-07-16 11:11:32 -0700147 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700148 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700149 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700150 // The viewport whether the pages are to be contained (the actual view may be larger than the
151 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800152 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700153 private Rect mViewport = new Rect();
154
155 // Reordering
156 // We use the min scale to determine how much to expand the actually PagedView measured
157 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700158 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700159 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700160 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
161
Adam Cohen7d30a372013-07-01 17:03:59 -0700162 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700163 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700164 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700165 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700166 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700167 // This variable's scope is only for the duration of startReordering() and endReordering()
168 private boolean mReorderingStarted = false;
169 // This variable's scope is for the duration of startReordering() and after the zoomIn()
170 // animation after endReordering()
171 private boolean mIsReordering;
172 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700173 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700174 private int mPostReorderingPreZoomInRemainingAnimationCount;
175 private Runnable mPostReorderingPreZoomInRunnable;
176
Adam Cohen7d30a372013-07-01 17:03:59 -0700177 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700178 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700179 private static final float[] sTmpPoint = new float[2];
180 private static final int[] sTmpIntPoint = new int[2];
181 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700182
John Spurlock77e1f472013-09-11 10:09:51 -0400183 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700184 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400185
Sunny Goyal4d113a52015-05-27 10:05:28 -0700186 // Edge effect
187 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
188 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
189
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 public PagedView(Context context) {
191 this(context, null);
192 }
193
194 public PagedView(Context context, AttributeSet attrs) {
195 this(context, attrs, 0);
196 }
197
198 public PagedView(Context context, AttributeSet attrs, int defStyle) {
199 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700200
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 TypedArray a = context.obtainStyledAttributes(attrs,
202 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700203 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 a.recycle();
205
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700207 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700208 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 }
210
211 /**
212 * Initializes various states for this workspace.
213 */
Michael Jurka0142d492010-08-25 17:46:15 -0700214 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800215 mScroller = new LauncherScroller(getContext());
216 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700217 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700218
219 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700220 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800222
Sunny Goyalcf25b522015-07-09 00:01:18 -0700223 float density = getResources().getDisplayMetrics().density;
224 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
225 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
226 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700227 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700228 setWillNotDraw(false);
229 }
230
231 protected void setEdgeGlowColor(int color) {
232 mEdgeGlowLeft.setColor(color);
233 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 }
235
Adam Cohenf9618852013-11-08 06:45:03 -0800236 protected void setDefaultInterpolator(Interpolator interpolator) {
237 mDefaultInterpolator = interpolator;
238 mScroller.setInterpolator(mDefaultInterpolator);
239 }
240
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700241 public void initParentViews(View parent) {
242 if (mPageIndicatorViewId > -1) {
243 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700244 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700245 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700246 }
247 }
248
Adam Cohen7d30a372013-07-01 17:03:59 -0700249 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700250 private float[] mapPointFromViewToParent(View v, float x, float y) {
251 sTmpPoint[0] = x;
252 sTmpPoint[1] = y;
253 v.getMatrix().mapPoints(sTmpPoint);
254 sTmpPoint[0] += v.getLeft();
255 sTmpPoint[1] += v.getTop();
256 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700257 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700258 private float[] mapPointFromParentToView(View v, float x, float y) {
259 sTmpPoint[0] = x - v.getLeft();
260 sTmpPoint[1] = y - v.getTop();
261 v.getMatrix().invert(sTmpInvMatrix);
262 sTmpInvMatrix.mapPoints(sTmpPoint);
263 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700264 }
265
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700266 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700267 if (mDragView != null) {
268 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
269 (mDragViewBaselineLeft - mDragView.getLeft());
270 float y = mLastMotionY - mDownMotionY;
271 mDragView.setTranslationX(x);
272 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700273
Adam Cohenf358a4b2013-07-23 16:47:31 -0700274 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
275 + x + ", " + y);
276 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700277 }
278
279 public void setMinScale(float f) {
280 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700281 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700282 requestLayout();
283 }
284
285 @Override
286 public void setScaleX(float scaleX) {
287 super.setScaleX(scaleX);
288 if (isReordering(true)) {
289 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
290 mLastMotionX = p[0];
291 mLastMotionY = p[1];
292 updateDragViewTranslationDuringDrag();
293 }
294 }
295
296 // Convenience methods to get the actual width/height of the PagedView (since it is measured
297 // to be larger to account for the minimum possible scale)
298 int getViewportWidth() {
299 return mViewport.width();
300 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800301 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700302 return mViewport.height();
303 }
304
305 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
306 // PagedView both horizontally and vertically
307 int getViewportOffsetX() {
308 return (getMeasuredWidth() - getViewportWidth()) / 2;
309 }
310
311 int getViewportOffsetY() {
312 return (getMeasuredHeight() - getViewportHeight()) / 2;
313 }
314
Hyunyoung Song77441692016-06-27 22:00:48 -0700315 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700316 return mPageIndicator;
317 }
318
Adam Cohen674531f2013-12-13 15:07:14 -0800319 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700320 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
321 * that the user was on before entering free scroll mode (e.g. the home screen page they
322 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
323 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700325 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700326 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700328
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700329 /**
330 * Returns the index of page to be shown immediately afterwards.
331 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700332 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700333 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
334 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700335
Adam Cohenf9c184a2016-01-15 16:47:43 -0800336 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 return getChildCount();
338 }
339
Sunny Goyal83a8f042015-05-19 12:52:12 -0700340 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 return getChildAt(index);
342 }
343
Adam Cohenae4f1552011-10-20 00:15:42 -0700344 protected int indexToPage(int index) {
345 return index;
346 }
347
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800349 * Updates the scroll of the current page immediately to its final scroll position. We use this
350 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
351 * the previous tab page.
352 */
353 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700354 // If the current page is invalid, just reset the scroll position to zero
355 int newX = 0;
356 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700357 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700358 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800359 scrollTo(newX, 0);
360 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700361 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800362 }
363
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700364 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700365 mScroller.abortAnimation();
366 // We need to clean up the next page here to avoid computeScrollHelper from
367 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700368 if (resetNextPage) {
369 mNextPage = INVALID_PAGE;
370 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700371 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700372
Tony Wickham8f7ead32016-04-07 18:46:44 -0700373 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700374 mScroller.forceFinished(true);
375 // We need to clean up the next page here to avoid computeScrollHelper from
376 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700377 if (resetNextPage) {
378 mNextPage = INVALID_PAGE;
379 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700380 }
381
Adam Cohen6f127a62014-06-12 14:54:41 -0700382 private int validateNewPage(int newPage) {
383 int validatedPage = newPage;
384 // When in free scroll mode, we need to clamp to the free scroll page range.
385 if (mFreeScroll) {
386 getFreeScrollPageRange(mTempVisiblePagesRange);
387 validatedPage = Math.max(mTempVisiblePagesRange[0],
388 Math.min(newPage, mTempVisiblePagesRange[1]));
389 }
390 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700391 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700392 return validatedPage;
393 }
394
Chet Haasebc2f0822012-10-26 17:59:53 -0700395 /**
Winson Chung86f77532010-08-24 11:08:22 -0700396 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700397 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700398 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800399 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700400 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800401 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800402 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
403 // the default
404 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800405 return;
406 }
Adam Cohen6f127a62014-06-12 14:54:41 -0700407 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700408 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700409 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800410 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700411 }
412
Winson Chung8c87cd82013-07-23 16:20:10 -0700413 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800414 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
415 * has settled.
416 */
Michael Jurka0142d492010-08-25 17:46:15 -0700417 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800418 updatePageIndicator();
419 }
420
421 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700422 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700423 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700424 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700425 if (!isReordering(false)) {
426 mPageIndicator.setActiveMarker(getNextPage());
427 }
Winson Chungd2be3812013-07-16 11:11:32 -0700428 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700429 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700430 protected void pageBeginTransition() {
431 if (!mIsPageInTransition) {
432 mIsPageInTransition = true;
433 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700434 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700435 }
436
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700437 protected void pageEndTransition() {
438 if (mIsPageInTransition) {
439 mIsPageInTransition = false;
440 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700441 }
Michael Jurka0142d492010-08-25 17:46:15 -0700442 }
443
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700444 protected boolean isPageInTransition() {
445 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700446 }
447
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700448 /**
449 * Called when the page starts moving as part of the scroll. Subclasses can override this
450 * to provide custom behavior during animation.
451 */
452 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700453 }
454
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700455 /**
456 * Called when the page ends moving as part of the scroll. Subclasses can override this
457 * to provide custom behavior during animation.
458 */
459 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700460 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700461 }
462
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 /**
Winson Chung86f77532010-08-24 11:08:22 -0700464 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 *
466 * @param l The listener used to respond to long clicks.
467 */
468 @Override
469 public void setOnLongClickListener(OnLongClickListener l) {
470 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700471 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700473 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
Adam Cohen1697b792013-09-17 19:08:21 -0700475 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 }
477
Sunny Goyalc86df472016-02-25 09:19:38 -0800478 protected int getUnboundedScrollX() {
479 return getScrollX();
480 }
481
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800483 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800484 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800485 }
486
487 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700488 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700489 // In free scroll mode, we clamp the scrollX
490 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700491 // If the scroller is trying to move to a location beyond the maximum allowed
492 // in the free scroll mode, we make sure to end the scroll operation.
493 if (!mScroller.isFinished() &&
494 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700495 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700496 }
497
Adam Cohenf358a4b2013-07-23 16:47:31 -0700498 x = Math.min(x, mFreeScrollMaxScrollX);
499 x = Math.max(x, mFreeScrollMinScrollX);
500 }
501
Sunny Goyal70660032015-05-14 00:07:08 -0700502 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
503 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700504 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700505 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800506 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700507 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700508 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700509 overScroll(x - mMaxScrollX);
510 } else {
511 overScroll(x);
512 }
Adam Cohen68d73932010-11-15 10:50:58 -0800513 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700514 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700515 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800516 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700517 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700518 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700519 overScroll(x);
520 } else {
521 overScroll(x - mMaxScrollX);
522 }
Adam Cohen68d73932010-11-15 10:50:58 -0800523 }
524 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700525 if (mWasInOverscroll) {
526 overScroll(0);
527 mWasInOverscroll = false;
528 }
Adam Cohen68d73932010-11-15 10:50:58 -0800529 super.scrollTo(x, y);
530 }
531
Adam Cohen7d30a372013-07-01 17:03:59 -0700532 // Update the last motion events when scrolling
533 if (isReordering(true)) {
534 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
535 mLastMotionX = p[0];
536 mLastMotionY = p[1];
537 updateDragViewTranslationDuringDrag();
538 }
Michael Jurka0142d492010-08-25 17:46:15 -0700539 }
540
Adam Cohen53805212013-10-01 10:39:23 -0700541 private void sendScrollAccessibilityEvent() {
542 AccessibilityManager am =
543 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
544 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700545 if (mCurrentPage != getNextPage()) {
546 AccessibilityEvent ev =
547 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700548 ev.setScrollable(true);
549 ev.setScrollX(getScrollX());
550 ev.setScrollY(getScrollY());
551 ev.setMaxScrollX(mMaxScrollX);
552 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700553
Vadim Tryshevf4715972015-05-08 17:21:03 -0700554 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700555 }
Adam Cohen53805212013-10-01 10:39:23 -0700556 }
557 }
558
Michael Jurka0142d492010-08-25 17:46:15 -0700559 // we moved this functionality to a helper function so SmoothPagedView can reuse it
560 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800561 return computeScrollHelper(true);
562 }
563
564 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700566 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800567 if (getUnboundedScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800568 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700569 float scaleX = mFreeScroll ? getScaleX() : 1f;
570 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
571 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700572 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800573 if (shouldInvalidate) {
574 invalidate();
575 }
Michael Jurka0142d492010-08-25 17:46:15 -0700576 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800577 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700578 sendScrollAccessibilityEvent();
579
Adam Cohen6f127a62014-06-12 14:54:41 -0700580 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700581 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700582 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700583
Adam Cohen73aa9752010-11-24 16:26:58 -0800584 // We don't want to trigger a page end moving unless the page has settled
585 // and the user has stopped scrolling
586 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700587 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800588 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700589
Adam Cohen7d30a372013-07-01 17:03:59 -0700590 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700591 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700592 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700593 if (am.isEnabled()) {
594 // Notify the user when the page changes
595 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700596 }
Michael Jurka0142d492010-08-25 17:46:15 -0700597 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
Michael Jurka0142d492010-08-25 17:46:15 -0700599 return false;
600 }
601
602 @Override
603 public void computeScroll() {
604 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 }
606
Adam Cohen96d30a12013-07-16 18:13:21 -0700607 public static class LayoutParams extends ViewGroup.LayoutParams {
608 public boolean isFullScreenPage = false;
609
Sunny Goyal7c786f72016-06-01 14:08:21 -0700610 // If true, the start edge of the page snaps to the start edge of the viewport.
611 public boolean matchStartEdge = false;
612
Adam Cohen96d30a12013-07-16 18:13:21 -0700613 /**
614 * {@inheritDoc}
615 */
616 public LayoutParams(int width, int height) {
617 super(width, height);
618 }
619
Sunny Goyal41b22c02015-05-14 15:20:48 -0700620 public LayoutParams(Context context, AttributeSet attrs) {
621 super(context, attrs);
622 }
623
Adam Cohen96d30a12013-07-16 18:13:21 -0700624 public LayoutParams(ViewGroup.LayoutParams source) {
625 super(source);
626 }
627 }
628
Sunny Goyal41b22c02015-05-14 15:20:48 -0700629 @Override
630 public LayoutParams generateLayoutParams(AttributeSet attrs) {
631 return new LayoutParams(getContext(), attrs);
632 }
633
634 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700635 protected LayoutParams generateDefaultLayoutParams() {
636 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
637 }
638
Sunny Goyal41b22c02015-05-14 15:20:48 -0700639 @Override
640 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
641 return new LayoutParams(p);
642 }
643
644 @Override
645 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
646 return p instanceof LayoutParams;
647 }
648
Adam Cohenedb40762013-07-18 16:45:45 -0700649 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700650 LayoutParams lp = generateDefaultLayoutParams();
651 lp.isFullScreenPage = true;
652 super.addView(page, 0, lp);
653 }
654
Adam Cohen410f3cd2013-09-22 12:09:32 -0700655 public int getNormalChildHeight() {
656 return mNormalChildHeight;
657 }
658
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 @Override
660 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700661 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700662 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
663 return;
664 }
665
Adam Cohen7d30a372013-07-01 17:03:59 -0700666 // We measure the dimensions of the PagedView to be larger than the pages so that when we
667 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700668 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
669 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
670 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700671 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800672 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700673 // viewport, we can be at most one and a half screens offset once we scale down
674 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700675 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
676 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700677
Winson Chungc82d2622013-11-06 13:23:29 -0800678 int parentWidthSize = (int) (2f * maxSize);
679 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700680 int scaledWidthSize, scaledHeightSize;
681 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700682 scaledWidthSize = (int) (parentWidthSize / mMinScale);
683 scaledHeightSize = (int) (parentHeightSize / mMinScale);
684 } else {
685 scaledWidthSize = widthSize;
686 scaledHeightSize = heightSize;
687 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700688 mViewport.set(0, 0, widthSize, heightSize);
689
690 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
691 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
692 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700693 }
694
Winson Chung8aad6102012-05-11 16:27:49 -0700695 // Return early if we aren't given a proper dimension
696 if (widthSize <= 0 || heightSize <= 0) {
697 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
698 return;
699 }
700
Adam Lesinski6b879f02010-11-04 16:15:23 -0700701 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
702 * of the All apps view on XLarge displays to not take up more space then it needs. Width
703 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
704 * each page to have the same width.
705 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int verticalPadding = getPaddingTop() + getPaddingBottom();
707 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700708
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700709 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700711 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700712 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700713 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
714 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
715 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
716 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 final int childCount = getChildCount();
718 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700719 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700720 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100721 if (child.getVisibility() != GONE) {
722 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700723
Vladimir Marko2824b072013-10-04 16:42:17 +0100724 int childWidthMode;
725 int childHeightMode;
726 int childWidth;
727 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700728
Vladimir Marko2824b072013-10-04 16:42:17 +0100729 if (!lp.isFullScreenPage) {
730 if (lp.width == LayoutParams.WRAP_CONTENT) {
731 childWidthMode = MeasureSpec.AT_MOST;
732 } else {
733 childWidthMode = MeasureSpec.EXACTLY;
734 }
735
736 if (lp.height == LayoutParams.WRAP_CONTENT) {
737 childHeightMode = MeasureSpec.AT_MOST;
738 } else {
739 childHeightMode = MeasureSpec.EXACTLY;
740 }
741
Adam Cohen3b185e22013-10-29 14:45:58 -0700742 childWidth = getViewportWidth() - horizontalPadding
743 - mInsets.left - mInsets.right;
744 childHeight = getViewportHeight() - verticalPadding
745 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100746 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700747 } else {
748 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700749 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100750
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800751 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700752 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700753 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700754 if (referenceChildWidth == 0) {
755 referenceChildWidth = childWidth;
756 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700757
Vladimir Marko2824b072013-10-04 16:42:17 +0100758 final int childWidthMeasureSpec =
759 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
760 final int childHeightMeasureSpec =
761 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
762 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700763 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700764 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700765 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800766 }
767
Sunny Goyalcf25b522015-07-09 00:01:18 -0700768 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700770 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700771 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700772 return;
773 }
774
Winson Chung785d2eb2011-04-14 16:08:02 -0700775 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700777
Adam Cohen7d30a372013-07-01 17:03:59 -0700778 int offsetX = getViewportOffsetX();
779 int offsetY = getViewportOffsetY();
780
781 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700782 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700783
Sunny Goyal70660032015-05-14 00:07:08 -0700784 final int startIndex = mIsRtl ? childCount - 1 : 0;
785 final int endIndex = mIsRtl ? -1 : childCount;
786 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700787
Adam Cohen7d30a372013-07-01 17:03:59 -0700788 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700789
Adam Cohen3b185e22013-10-29 14:45:58 -0700790 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000791 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700792
Sunny Goyal6178f132016-07-11 17:30:03 -0700793 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700794 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
795 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700796 }
797
Adam Cohen0ffac432013-07-10 11:19:26 -0700798 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700799 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700801 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100802 int childTop;
803 if (lp.isFullScreenPage) {
804 childTop = offsetY;
805 } else {
806 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700807 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100808 }
809
Adam Cohen96d30a12013-07-16 18:13:21 -0700810 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700811 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800812
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700814 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800815 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700816
Sunny Goyal6178f132016-07-11 17:30:03 -0700817 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700818 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000819
820 int pageGap = mPageSpacing;
821 int next = i + delta;
822 if (next != endIndex) {
823 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
824 } else {
825 nextLp = null;
826 }
827
828 // Prevent full screen pages from showing in the viewport
829 // when they are not the current page.
830 if (lp.isFullScreenPage) {
831 pageGap = getPaddingLeft();
832 } else if (nextLp != null && nextLp.isFullScreenPage) {
833 pageGap = getPaddingRight();
834 }
835
Sunny Goyala1fbd842015-05-20 13:40:27 -0700836 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 }
838 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700839
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700840 final LayoutTransition transition = getLayoutTransition();
841 // If the transition is running defer updating max scroll, as some empty pages could
842 // still be present, and a max scroll change could cause sudden jumps in scroll.
843 if (transition != null && transition.isRunning()) {
844 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
845
846 @Override
847 public void startTransition(LayoutTransition transition, ViewGroup container,
848 View view, int transitionType) { }
849
850 @Override
851 public void endTransition(LayoutTransition transition, ViewGroup container,
852 View view, int transitionType) {
853 // Wait until all transitions are complete.
854 if (!transition.isRunning()) {
855 transition.removeTransitionListener(this);
856 updateMaxScrollX();
857 }
858 }
859 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700860 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700861 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700862 }
863
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700864 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
865 updateCurrentPageScroll();
866 mFirstLayout = false;
867 }
868
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700869 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700870 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700871 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700872 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700873
874 if (isReordering(true)) {
875 updateDragViewTranslationDuringDrag();
876 }
Winson Chunge3193b92010-09-10 11:44:42 -0700877 }
878
Sunny Goyala1fbd842015-05-20 13:40:27 -0700879 protected int getChildGap() {
880 return 0;
881 }
882
Sunny Goyal316490e2015-06-02 09:38:28 -0700883 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700884 mMaxScrollX = computeMaxScrollX();
885 }
886
887 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700888 int childCount = getChildCount();
889 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700890 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700891 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700892 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700893 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700894 }
895 }
896
Adam Cohen3b185e22013-10-29 14:45:58 -0700897 public void setPageSpacing(int pageSpacing) {
898 mPageSpacing = pageSpacing;
899 requestLayout();
900 }
901
Winson Chunge3193b92010-09-10 11:44:42 -0700902 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700903 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700904 // Update the page indicator, we don't update the page indicator as we
905 // add/remove pages
906 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700907 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700908 }
909
Adam Cohen2591f6a2011-10-25 14:36:40 -0700910 // This ensures that when children are added, they get the correct transforms / alphas
911 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700912 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700913 invalidate();
914 }
915
Michael Jurka8b805b12012-04-18 14:23:14 -0700916 @Override
917 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700918 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700919 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700920 }
921
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700922 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700923 // Update the page indicator, we don't update the page indicator as we
924 // add/remove pages
925 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700926 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700927 }
928 }
929
930 @Override
931 public void removeView(View v) {
932 // XXX: We should find a better way to hook into this before the view
933 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700934 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700935 super.removeView(v);
936 }
937 @Override
938 public void removeViewInLayout(View v) {
939 // XXX: We should find a better way to hook into this before the view
940 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700941 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700942 super.removeViewInLayout(v);
943 }
944 @Override
945 public void removeViewAt(int index) {
946 // XXX: We should find a better way to hook into this before the view
947 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700948 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700949 super.removeViewAt(index);
950 }
951 @Override
952 public void removeAllViewsInLayout() {
953 // Update the page indicator, we don't update the page indicator as we
954 // add/remove pages
955 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700956 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700957 }
958
959 super.removeAllViewsInLayout();
960 }
961
Adam Cohen73894962011-10-31 13:17:17 -0700962 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700963 if (index < 0 || index > getChildCount() - 1) return 0;
964
Adam Cohenf698c6e2013-07-17 18:44:25 -0700965 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700966
Adam Cohenf698c6e2013-07-17 18:44:25 -0700967 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700968 }
969
Adam Cohen6f127a62014-06-12 14:54:41 -0700970 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700971 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700972 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700973 }
974
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -0700976 public void draw(Canvas canvas) {
977 super.draw(canvas);
978 if (getPageCount() > 0) {
979 if (!mEdgeGlowLeft.isFinished()) {
980 final int restoreCount = canvas.save();
981 Rect display = mViewport;
982 canvas.translate(display.left, display.top);
983 canvas.rotate(270);
984
Hyunyoung Song0de01172016-10-05 16:27:48 -0700985 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700986 canvas.translate(display.top - sTmpIntPoint[1], 0);
987 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
988 if (mEdgeGlowLeft.draw(canvas)) {
989 postInvalidateOnAnimation();
990 }
991 canvas.restoreToCount(restoreCount);
992 }
993 if (!mEdgeGlowRight.isFinished()) {
994 final int restoreCount = canvas.save();
995 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700996 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700997 canvas.rotate(90);
998
Hyunyoung Song0de01172016-10-05 16:27:48 -0700999 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001000
Sunny Goyal93264612015-11-23 11:47:50 -08001001 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1002 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001003 if (mEdgeGlowRight.draw(canvas)) {
1004 postInvalidateOnAnimation();
1005 }
1006 canvas.restoreToCount(restoreCount);
1007 }
1008 }
1009 }
1010
1011 /**
1012 * Returns the top and bottom position for the edge effect.
1013 */
Hyunyoung Song0de01172016-10-05 16:27:48 -07001014 protected abstract void getEdgeVerticalPosition(int[] pos);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001015
1016 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001018 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001019 if (page != mCurrentPage || !mScroller.isFinished()) {
1020 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 return true;
1022 }
1023 return false;
1024 }
1025
1026 @Override
1027 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001028 int focusablePage;
1029 if (mNextPage != INVALID_PAGE) {
1030 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001032 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 }
Winson Chung86f77532010-08-24 11:08:22 -07001034 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001036 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 }
1038 return false;
1039 }
1040
1041 @Override
1042 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001043 if (super.dispatchUnhandledMove(focused, direction)) {
1044 return true;
1045 }
1046
1047 if (mIsRtl) {
1048 if (direction == View.FOCUS_LEFT) {
1049 direction = View.FOCUS_RIGHT;
1050 } else if (direction == View.FOCUS_RIGHT) {
1051 direction = View.FOCUS_LEFT;
1052 }
1053 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001054 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001055 if (getCurrentPage() > 0) {
1056 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 return true;
1058 }
1059 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001060 if (getCurrentPage() < getPageCount() - 1) {
1061 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 return true;
1063 }
1064 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001065 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 }
1067
1068 @Override
1069 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001070 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1071 return;
1072 }
1073
Adam Cohen0ffac432013-07-10 11:19:26 -07001074 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001075 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001076 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001077 }
1078 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001079 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001080 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 }
1082 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001083 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001084 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001085 }
1086 }
1087 }
1088
1089 /**
1090 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001091 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 *
Winson Chung86f77532010-08-24 11:08:22 -07001093 * This happens when live folders requery, and if they're off page, they
1094 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 */
1096 @Override
1097 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001098 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001099 View v = focused;
1100 while (true) {
1101 if (v == current) {
1102 super.focusableViewAvailable(focused);
1103 return;
1104 }
1105 if (v == this) {
1106 return;
1107 }
1108 ViewParent parent = v.getParent();
1109 if (parent instanceof View) {
1110 v = (View)v.getParent();
1111 } else {
1112 return;
1113 }
1114 }
1115 }
1116
1117 /**
1118 * {@inheritDoc}
1119 */
1120 @Override
1121 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1122 if (disallowIntercept) {
1123 // We need to make sure to cancel our long press if
1124 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001125 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001126 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 }
1128 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1129 }
1130
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001131 /**
1132 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1133 */
1134 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001135 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001136 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001137 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001138 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001139 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001140 }
1141
1142 /**
1143 * Return true if a tap at (x, y) should trigger a flip to the next page.
1144 */
1145 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001146 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001147 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001148 }
1149 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001150 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001151 }
Winson Chung52aee602013-01-30 12:01:02 -08001152
Adam Cohen7d30a372013-07-01 17:03:59 -07001153 /** Returns whether x and y originated within the buffered viewport */
1154 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001155 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001156 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001157 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001158 }
1159
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 @Override
1161 public boolean onInterceptTouchEvent(MotionEvent ev) {
1162 /*
1163 * This method JUST determines whether we want to intercept the motion.
1164 * If we return true, onTouchEvent will be called and we do the actual
1165 * scrolling there.
1166 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001167 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001168
Winson Chung45e1d6e2010-11-09 17:19:49 -08001169 // Skip touch handling if there are no pages to swipe
1170 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1171
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 /*
1173 * Shortcut the most recurring case: the user is in the dragging
1174 * state and he is moving his finger. We want to intercept this
1175 * motion.
1176 */
1177 final int action = ev.getAction();
1178 if ((action == MotionEvent.ACTION_MOVE) &&
1179 (mTouchState == TOUCH_STATE_SCROLLING)) {
1180 return true;
1181 }
1182
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 switch (action & MotionEvent.ACTION_MASK) {
1184 case MotionEvent.ACTION_MOVE: {
1185 /*
1186 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1187 * whether the user has moved far enough from his original down touch.
1188 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001189 if (mActivePointerId != INVALID_POINTER) {
1190 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001191 }
1192 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1193 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1194 // i.e. fall through to the next case (don't break)
1195 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1196 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001197 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001198 }
1199
1200 case MotionEvent.ACTION_DOWN: {
1201 final float x = ev.getX();
1202 final float y = ev.getY();
1203 // Remember location of down touch
1204 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001205 mDownMotionY = y;
1206 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 mLastMotionX = x;
1208 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001209 float[] p = mapPointFromViewToParent(this, x, y);
1210 mParentDownMotionX = p[0];
1211 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001212 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001213 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001215
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 /*
1217 * If being flinged and user touches the screen, initiate drag;
1218 * otherwise don't. mScroller.isFinished should be false when
1219 * being flinged.
1220 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001221 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001222 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001223
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001224 if (finishedScrolling) {
1225 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001226 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001227 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001228 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001229 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001230 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001231 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1232 mTouchState = TOUCH_STATE_SCROLLING;
1233 } else {
1234 mTouchState = TOUCH_STATE_REST;
1235 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001236 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001237
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 break;
1239 }
1240
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001242 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001243 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 break;
1245
1246 case MotionEvent.ACTION_POINTER_UP:
1247 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001248 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 break;
1250 }
1251
1252 /*
1253 * The only time we want to intercept motion events is if we are in the
1254 * drag mode.
1255 */
1256 return mTouchState != TOUCH_STATE_REST;
1257 }
1258
Adam Cohenf8d28232011-02-01 21:47:00 -08001259 protected void determineScrollingStart(MotionEvent ev) {
1260 determineScrollingStart(ev, 1.0f);
1261 }
1262
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 /*
1264 * Determines if we should change the touch state to start scrolling after the
1265 * user moves their touch point too far.
1266 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001267 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001268 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001270 if (pointerIndex == -1) return;
1271
1272 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 final float x = ev.getX(pointerIndex);
1274 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001275 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1276
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001278
Adam Cohenf8d28232011-02-01 21:47:00 -08001279 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001281
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001282 if (xMoved) {
1283 // Scroll if the user moved far enough along the X axis
1284 mTouchState = TOUCH_STATE_SCROLLING;
1285 mTotalMotionX += Math.abs(mLastMotionX - x);
1286 mLastMotionX = x;
1287 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001288 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001289 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001290 // Stop listening for things like pinches.
1291 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001292 }
1293 }
1294
1295 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001296 // Try canceling the long press. It could also have been scheduled
1297 // by a distant descendant, so use the mAllowLongPress flag to block
1298 // everything
1299 final View currentPage = getPageAt(mCurrentPage);
1300 if (currentPage != null) {
1301 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 }
1303 }
1304
Adam Cohenb5ba0972011-09-07 18:02:31 -07001305 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001306 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001307
Adam Cohenedb40762013-07-18 16:45:45 -07001308 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001309 int count = getChildCount();
1310
1311 final int totalDistance;
1312
1313 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001314 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001315 adjacentPage = page - 1;
1316 }
1317
1318 if (adjacentPage < 0 || adjacentPage > count - 1) {
1319 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1320 } else {
1321 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1322 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001323
1324 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001325 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1326 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001327 return scrollProgress;
1328 }
1329
Adam Cohenedb40762013-07-18 16:45:45 -07001330 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001331 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001332 return 0;
1333 } else {
1334 return mPageScrolls[index];
1335 }
1336 }
1337
Adam Cohen564a2e72013-10-09 14:47:32 -07001338 // While layout transitions are occurring, a child's position may stray from its baseline
1339 // position. This method returns the magnitude of this stray at any given time.
1340 public int getLayoutTransitionOffsetForPage(int index) {
1341 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1342 return 0;
1343 } else {
1344 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001345
1346 int scrollOffset = 0;
1347 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1348 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001349 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001350 }
1351
Adam Cohen564a2e72013-10-09 14:47:32 -07001352 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1353 return (int) (child.getX() - baselineX);
1354 }
1355 }
1356
Adam Cohenb5ba0972011-09-07 18:02:31 -07001357 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001358 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001359 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001360 if (f < 0) {
1361 mEdgeGlowLeft.onPull(-f);
1362 } else if (f > 0) {
1363 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001364 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001365 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001366 }
1367 invalidate();
1368 }
1369
Adam Cohenb5ba0972011-09-07 18:02:31 -07001370 protected void overScroll(float amount) {
1371 dampedOverScroll(amount);
1372 }
1373
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001374 /**
1375 * return true if freescroll has been enabled, false otherwise
1376 */
1377 public boolean enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001378 setEnableFreeScroll(true);
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001379 return true;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001380 }
1381
Winson Chungdc61c4d2015-04-20 18:26:57 -07001382 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001383 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001384 }
1385
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001386 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001387 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001388 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001389 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1390 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1391 } else {
1392 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1393 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1394 }
1395 }
1396
Adam Cohenf9618852013-11-08 06:45:03 -08001397 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001398 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001399 mFreeScroll = freeScroll;
1400
Adam Cohenf9618852013-11-08 06:45:03 -08001401 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001402 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001403 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001404 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1405 setCurrentPage(mTempVisiblePagesRange[0]);
1406 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1407 setCurrentPage(mTempVisiblePagesRange[1]);
1408 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001409 } else if (wasFreeScroll) {
1410 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001411 }
1412
1413 setEnableOverscroll(!freeScroll);
1414 }
1415
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001416 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001417 mAllowOverScroll = enable;
1418 }
1419
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001420 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421 if (mDragView != null) {
1422 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1423 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001424 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 int minDistance = Integer.MAX_VALUE;
1426 int minIndex = indexOfChild(mDragView);
1427 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1428 View page = getPageAt(i);
1429 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1430 int d = Math.abs(dragX - pageX);
1431 if (d < minDistance) {
1432 minIndex = i;
1433 minDistance = d;
1434 }
1435 }
1436 return minIndex;
1437 }
1438 return -1;
1439 }
1440
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 @Override
1442 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001443 super.onTouchEvent(ev);
1444
Winson Chung45e1d6e2010-11-09 17:19:49 -08001445 // Skip touch handling if there are no pages to swipe
1446 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1447
Michael Jurkab8f06722010-10-10 15:58:46 -07001448 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001449
1450 final int action = ev.getAction();
1451
1452 switch (action & MotionEvent.ACTION_MASK) {
1453 case MotionEvent.ACTION_DOWN:
1454 /*
1455 * If being flinged and user touches, stop the fling. isFinished
1456 * will be false if being flinged.
1457 */
1458 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001459 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 }
1461
1462 // Remember where the motion event started
1463 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001464 mDownMotionY = mLastMotionY = ev.getY();
1465 mDownScrollX = getScrollX();
1466 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1467 mParentDownMotionX = p[0];
1468 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001469 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001470 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001471 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001472
Michael Jurka0142d492010-08-25 17:46:15 -07001473 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001474 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001475 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001476 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001477 break;
1478
1479 case MotionEvent.ACTION_MOVE:
1480 if (mTouchState == TOUCH_STATE_SCROLLING) {
1481 // Scroll to follow the motion event
1482 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001483
1484 if (pointerIndex == -1) return true;
1485
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001487 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001488
Adam Cohenaefd4e12011-02-14 16:39:38 -08001489 mTotalMotionX += Math.abs(deltaX);
1490
Winson Chungc0844aa2011-02-02 15:25:58 -08001491 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1492 // keep the remainder because we are actually testing if we've moved from the last
1493 // scrolled position (which is discrete).
1494 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001495 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001496 mLastMotionX = x;
1497 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 } else {
1499 awakenScrollBars();
1500 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001501 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1502 // Update the last motion position
1503 mLastMotionX = ev.getX();
1504 mLastMotionY = ev.getY();
1505
1506 // Update the parent down so that our zoom animations take this new movement into
1507 // account
1508 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1509 mParentDownMotionX = pt[0];
1510 mParentDownMotionY = pt[1];
1511 updateDragViewTranslationDuringDrag();
1512
1513 // Find the closest page to the touch point
1514 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001515
Adam Cohen7d30a372013-07-01 17:03:59 -07001516 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1517 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1518 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1519 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1520
Adam Cohenf358a4b2013-07-23 16:47:31 -07001521 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001522 // Do not allow any page to be moved to 0th position.
1523 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001524 mTempVisiblePagesRange[0] = 0;
1525 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001526 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001527 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1528 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1529 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1530 mSidePageHoverIndex = pageUnderPointIndex;
1531 mSidePageHoverRunnable = new Runnable() {
1532 @Override
1533 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001534 // Setup the scroll to the correct page before we swap the views
1535 snapToPage(pageUnderPointIndex);
1536
1537 // For each of the pages between the paged view and the drag view,
1538 // animate them from the previous position to the new position in
1539 // the layout (as a result of the drag view moving in the layout)
1540 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1541 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1542 dragViewIndex + 1 : pageUnderPointIndex;
1543 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1544 dragViewIndex - 1 : pageUnderPointIndex;
1545 for (int i = lowerIndex; i <= upperIndex; ++i) {
1546 View v = getChildAt(i);
1547 // dragViewIndex < pageUnderPointIndex, so after we remove the
1548 // drag view all subsequent views to pageUnderPointIndex will
1549 // shift down.
1550 int oldX = getViewportOffsetX() + getChildOffset(i);
1551 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1552
1553 // Animate the view translation from its old position to its new
1554 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001555 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001556 if (anim != null) {
1557 anim.cancel();
1558 }
1559
1560 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001561 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001562 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001563 anim.start();
1564 v.setTag(anim);
1565 }
1566
1567 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001568 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001569 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001570 if (mPageIndicator != null) {
1571 mPageIndicator.setActiveMarker(getNextPage());
1572 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001573 }
1574 };
1575 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1576 }
1577 } else {
1578 removeCallbacks(mSidePageHoverRunnable);
1579 mSidePageHoverIndex = -1;
1580 }
Adam Cohen564976a2010-10-13 18:52:07 -07001581 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001582 determineScrollingStart(ev);
1583 }
1584 break;
1585
1586 case MotionEvent.ACTION_UP:
1587 if (mTouchState == TOUCH_STATE_SCROLLING) {
1588 final int activePointerId = mActivePointerId;
1589 final int pointerIndex = ev.findPointerIndex(activePointerId);
1590 final float x = ev.getX(pointerIndex);
1591 final VelocityTracker velocityTracker = mVelocityTracker;
1592 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1593 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001594 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001595 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001596 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1597 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001598
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001599 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1600
Adam Cohen00481b32011-11-18 12:03:48 -08001601 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001602 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001603
Adam Cohenf358a4b2013-07-23 16:47:31 -07001604 if (!mFreeScroll) {
1605 // In the case that the page is moved far to one direction and then is flung
1606 // in the opposite direction, we use a threshold to determine whether we should
1607 // just return to the starting page, or if we should skip one further.
1608 boolean returnToOriginalPage = false;
1609 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1610 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1611 returnToOriginalPage = true;
1612 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001613
Adam Cohenf358a4b2013-07-23 16:47:31 -07001614 int finalPage;
1615 // We give flings precedence over large moves, which is why we short-circuit our
1616 // test for a large move if a fling has been registered. That is, a large
1617 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001618 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1619 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001620 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1621 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1622 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1623 snapToPageWithVelocity(finalPage, velocityX);
1624 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1625 (isFling && isVelocityXLeft)) &&
1626 mCurrentPage < getChildCount() - 1) {
1627 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1628 snapToPageWithVelocity(finalPage, velocityX);
1629 } else {
1630 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001631 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001632 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001633 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001634 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001635 }
1636
1637 float scaleX = getScaleX();
1638 int vX = (int) (-velocityX * scaleX);
1639 int initialScrollX = (int) (getScrollX() * scaleX);
1640
Adam Cohenf9618852013-11-08 06:45:03 -08001641 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001642 mScroller.fling(initialScrollX,
1643 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001644 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001645 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001646 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001647 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001648 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1649 // at this point we have not moved beyond the touch slop
1650 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1651 // we can just page
1652 int nextPage = Math.max(0, mCurrentPage - 1);
1653 if (nextPage != mCurrentPage) {
1654 snapToPage(nextPage);
1655 } else {
1656 snapToDestination();
1657 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001658 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 // at this point we have not moved beyond the touch slop
1660 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1661 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001662 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1663 if (nextPage != mCurrentPage) {
1664 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001665 } else {
1666 snapToDestination();
1667 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001668 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1669 // Update the last motion position
1670 mLastMotionX = ev.getX();
1671 mLastMotionY = ev.getY();
1672
1673 // Update the parent down so that our zoom animations take this new movement into
1674 // account
1675 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1676 mParentDownMotionX = pt[0];
1677 mParentDownMotionY = pt[1];
1678 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001679 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001680 if (!mCancelTap) {
1681 onUnhandledTap(ev);
1682 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001683 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001684
1685 // Remove the callback to wait for the side page hover timeout
1686 removeCallbacks(mSidePageHoverRunnable);
1687 // End any intermediate reordering states
1688 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001689 break;
1690
1691 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001692 if (mTouchState == TOUCH_STATE_SCROLLING) {
1693 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001694 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001695 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001696 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001697 break;
1698
1699 case MotionEvent.ACTION_POINTER_UP:
1700 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001701 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001702 break;
1703 }
1704
1705 return true;
1706 }
1707
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 private void resetTouchState() {
1709 releaseVelocityTracker();
1710 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001711 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001712 mTouchState = TOUCH_STATE_REST;
1713 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001714 mEdgeGlowLeft.onRelease();
1715 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001716 }
1717
Adam Cohenc2d6e892014-10-16 09:49:24 -07001718 /**
1719 * Triggered by scrolling via touch
1720 */
1721 protected void onScrollInteractionBegin() {
1722 }
1723
1724 protected void onScrollInteractionEnd() {
1725 }
1726
Adam Cohen1697b792013-09-17 19:08:21 -07001727 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001728 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001729 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001730
Winson Chung185d7162011-02-28 13:47:29 -08001731 @Override
1732 public boolean onGenericMotionEvent(MotionEvent event) {
1733 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1734 switch (event.getAction()) {
1735 case MotionEvent.ACTION_SCROLL: {
1736 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1737 final float vscroll;
1738 final float hscroll;
1739 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1740 vscroll = 0;
1741 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1742 } else {
1743 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1744 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1745 }
1746 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001747 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001748 : (hscroll > 0 || vscroll > 0);
1749 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001750 scrollRight();
1751 } else {
1752 scrollLeft();
1753 }
1754 return true;
1755 }
1756 }
1757 }
1758 }
1759 return super.onGenericMotionEvent(event);
1760 }
1761
Michael Jurkab8f06722010-10-10 15:58:46 -07001762 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1763 if (mVelocityTracker == null) {
1764 mVelocityTracker = VelocityTracker.obtain();
1765 }
1766 mVelocityTracker.addMovement(ev);
1767 }
1768
1769 private void releaseVelocityTracker() {
1770 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001771 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001772 mVelocityTracker.recycle();
1773 mVelocityTracker = null;
1774 }
1775 }
1776
Winson Chung321e9ee2010-08-09 13:37:56 -07001777 private void onSecondaryPointerUp(MotionEvent ev) {
1778 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1779 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1780 final int pointerId = ev.getPointerId(pointerIndex);
1781 if (pointerId == mActivePointerId) {
1782 // This was our active pointer going up. Choose a new
1783 // active pointer and adjust accordingly.
1784 // TODO: Make this decision more intelligent.
1785 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1786 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1787 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001788 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001789 mActivePointerId = ev.getPointerId(newPointerIndex);
1790 if (mVelocityTracker != null) {
1791 mVelocityTracker.clear();
1792 }
1793 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001794 }
1795
Winson Chung321e9ee2010-08-09 13:37:56 -07001796 @Override
1797 public void requestChildFocus(View child, View focused) {
1798 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001799 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001800 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001801 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001802 }
1803 }
1804
Adam Cohend19d3ca2010-09-15 14:43:42 -07001805 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001806 return getPageNearestToCenterOfScreen(getScrollX());
1807 }
1808
1809 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1810 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001811 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001812 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001813 final int childCount = getChildCount();
1814 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001815 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001816 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001817 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001818 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001819 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1820 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1821 minDistanceFromScreenCenter = distanceFromScreenCenter;
1822 minDistanceFromScreenCenterIndex = i;
1823 }
1824 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001825 return minDistanceFromScreenCenterIndex;
1826 }
1827
1828 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001829 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001830 }
1831
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001832 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001833 public ScrollInterpolator() {
1834 }
1835
1836 public float getInterpolation(float t) {
1837 t -= 1.0f;
1838 return t*t*t*t*t + 1;
1839 }
1840 }
1841
1842 // We want the duration of the page snap animation to be influenced by the distance that
1843 // the screen has to travel, however, we don't want this duration to be effected in a
1844 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1845 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001846 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001847 f -= 0.5f; // center the values about 0.
1848 f *= 0.3f * Math.PI / 2.0f;
1849 return (float) Math.sin(f);
1850 }
1851
Michael Jurka0142d492010-08-25 17:46:15 -07001852 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001853 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001854 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001855
Adam Cohenedb40762013-07-18 16:45:45 -07001856 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001857 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001858 int duration = 0;
1859
Sunny Goyal4d113a52015-05-27 10:05:28 -07001860 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001861 // If the velocity is low enough, then treat this more as an automatic page advance
1862 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001863 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001864 return;
1865 }
1866
1867 // Here we compute a "distance" that will be used in the computation of the overall
1868 // snap duration. This is a function of the actual distance that needs to be traveled;
1869 // we keep this value close to half screen size in order to reduce the variance in snap
1870 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001871 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001872 float distance = halfScreenSize + halfScreenSize *
1873 distanceInfluenceForSnapDuration(distanceRatio);
1874
1875 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001876 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001877
1878 // we want the page's snap velocity to approximately match the velocity at which the
1879 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001880 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1881 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001882
1883 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001884 }
1885
Sunny Goyal1740d902015-05-27 11:14:01 -07001886 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001887 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001888 }
1889
Adam Cohenf9c184a2016-01-15 16:47:43 -08001890 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001891 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001892 }
1893
Michael Jurka0142d492010-08-25 17:46:15 -07001894 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001895 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001896 }
1897
Adam Cohenf9618852013-11-08 06:45:03 -08001898 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1899 snapToPage(whichPage, duration, false, interpolator);
1900 }
1901
1902 protected void snapToPage(int whichPage, int duration, boolean immediate,
1903 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001904 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001905
Adam Cohenedb40762013-07-18 16:45:45 -07001906 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001907 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001908 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001909 }
1910
1911 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001912 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001913 }
Michael Jurka0142d492010-08-25 17:46:15 -07001914
Adam Cohenf9618852013-11-08 06:45:03 -08001915 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1916 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001917 whichPage = validateNewPage(whichPage);
1918
Adam Cohen7d30a372013-07-01 17:03:59 -07001919 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001920
Winson Chung321e9ee2010-08-09 13:37:56 -07001921 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001922 if (immediate) {
1923 duration = 0;
1924 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001925 duration = Math.abs(delta);
1926 }
1927
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001928 if (duration != 0) {
1929 pageBeginTransition();
1930 }
1931
Adam Cohenf358a4b2013-07-23 16:47:31 -07001932 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001933 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001934 }
Adam Cohenf9618852013-11-08 06:45:03 -08001935
1936 if (interpolator != null) {
1937 mScroller.setInterpolator(interpolator);
1938 } else {
1939 mScroller.setInterpolator(mDefaultInterpolator);
1940 }
1941
Sunny Goyalc86df472016-02-25 09:19:38 -08001942 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001943
Adam Cohen674531f2013-12-13 15:07:14 -08001944 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001945
1946 // Trigger a compute() to finish switching pages if necessary
1947 if (immediate) {
1948 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001949 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001950 }
1951
Winson Chung321e9ee2010-08-09 13:37:56 -07001952 invalidate();
1953 }
1954
Winson Chung321e9ee2010-08-09 13:37:56 -07001955 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001956 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001957 }
1958
1959 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001960 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001961 }
1962
Adam Cohendbdff6b2013-09-18 19:09:15 -07001963 @Override
1964 public boolean performLongClick() {
1965 mCancelTap = true;
1966 return super.performLongClick();
1967 }
1968
Winson Chung321e9ee2010-08-09 13:37:56 -07001969 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001970 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001971
1972 SavedState(Parcelable superState) {
1973 super(superState);
1974 }
1975
Adam Cohen091440a2015-03-18 14:16:05 -07001976 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001977 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001978 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001979 }
1980
1981 @Override
1982 public void writeToParcel(Parcel out, int flags) {
1983 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001984 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001985 }
1986
1987 public static final Parcelable.Creator<SavedState> CREATOR =
1988 new Parcelable.Creator<SavedState>() {
1989 public SavedState createFromParcel(Parcel in) {
1990 return new SavedState(in);
1991 }
1992
1993 public SavedState[] newArray(int size) {
1994 return new SavedState[size];
1995 }
1996 };
1997 }
1998
Adam Cohen7d30a372013-07-01 17:03:59 -07001999 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002000 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002001 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08002002 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
2003 new PropertyListBuilder()
2004 .scale(1)
2005 .translationX(0)
2006 .translationY(0)
2007 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002008 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002009 anim.addListener(new AnimatorListenerAdapter() {
2010 @Override
2011 public void onAnimationEnd(Animator animation) {
2012 onPostReorderingAnimationCompleted();
2013 }
2014 });
2015 anim.start();
2016 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002017 }
2018
Sunny Goyal1d08f702015-05-04 15:50:25 -07002019 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002020 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2021 mTouchState = TOUCH_STATE_REORDERING;
2022 mIsReordering = true;
2023
Adam Cohen7d30a372013-07-01 17:03:59 -07002024 // We must invalidate to trigger a redraw to update the layers such that the drag view
2025 // is always drawn on top
2026 invalidate();
2027 }
2028
Adam Cohen091440a2015-03-18 14:16:05 -07002029 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002030 // Trigger the callback when reordering has settled
2031 --mPostReorderingPreZoomInRemainingAnimationCount;
2032 if (mPostReorderingPreZoomInRunnable != null &&
2033 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2034 mPostReorderingPreZoomInRunnable.run();
2035 mPostReorderingPreZoomInRunnable = null;
2036 }
2037 }
2038
Sunny Goyal1d08f702015-05-04 15:50:25 -07002039 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002040 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002041 }
2042
Adam Cohenf358a4b2013-07-23 16:47:31 -07002043 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002044 int dragViewIndex = indexOfChild(v);
2045
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002046 // Do not allow the first page to be moved around
2047 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002048
Adam Cohen7d30a372013-07-01 17:03:59 -07002049 mTempVisiblePagesRange[0] = 0;
2050 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002051 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002052 mReorderingStarted = true;
2053
2054 // Check if we are within the reordering range
2055 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002056 dragViewIndex <= mTempVisiblePagesRange[1]) {
2057 // Find the drag view under the pointer
2058 mDragView = getChildAt(dragViewIndex);
2059 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2060 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002061 snapToPage(getPageNearestToCenterOfScreen());
2062 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002063 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002064 return true;
2065 }
2066 return false;
2067 }
2068
2069 boolean isReordering(boolean testTouchState) {
2070 boolean state = mIsReordering;
2071 if (testTouchState) {
2072 state &= (mTouchState == TOUCH_STATE_REORDERING);
2073 }
2074 return state;
2075 }
2076 void endReordering() {
2077 // For simplicity, we call endReordering sometimes even if reordering was never started.
2078 // In that case, we don't want to do anything.
2079 if (!mReorderingStarted) return;
2080 mReorderingStarted = false;
2081
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002082 mPostReorderingPreZoomInRunnable = new Runnable() {
2083 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002084 // If we haven't flung-to-delete the current child,
2085 // then we just animate the drag view back into position
2086 onEndReordering();
2087
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002088 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002089 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002090 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002091
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002092 mPostReorderingPreZoomInRemainingAnimationCount =
2093 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2094 // Snap to the current page
2095 snapToPage(indexOfChild(mDragView), 0);
2096 // Animate the drag view back to the front position
2097 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002098 }
2099
Winson Chung6a0f57d2011-06-29 20:10:49 -07002100 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002101 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002102 @Override
2103 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2104 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002105 info.setScrollable(getPageCount() > 1);
2106 if (getCurrentPage() < getPageCount() - 1) {
2107 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2108 }
2109 if (getCurrentPage() > 0) {
2110 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2111 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002112 info.setClassName(getClass().getName());
2113
2114 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2115 // Besides disabling the accessibility long-click, this also prevents this view from getting
2116 // accessibility focus.
2117 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002118 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002119 }
2120
2121 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002122 public void sendAccessibilityEvent(int eventType) {
2123 // Don't let the view send real scroll events.
2124 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2125 super.sendAccessibilityEvent(eventType);
2126 }
2127 }
2128
2129 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002130 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2131 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002132 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002133 }
2134
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002135 @Override
2136 public boolean performAccessibilityAction(int action, Bundle arguments) {
2137 if (super.performAccessibilityAction(action, arguments)) {
2138 return true;
2139 }
2140 switch (action) {
2141 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2142 if (getCurrentPage() < getPageCount() - 1) {
2143 scrollRight();
2144 return true;
2145 }
2146 } break;
2147 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2148 if (getCurrentPage() > 0) {
2149 scrollLeft();
2150 return true;
2151 }
2152 } break;
2153 }
2154 return false;
2155 }
2156
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002157 protected String getPageIndicatorDescription() {
2158 return getCurrentPageDescription();
2159 }
2160
Adam Cohen0ffac432013-07-10 11:19:26 -07002161 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002162 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002163 getNextPage() + 1, getChildCount());
2164 }
2165
Winson Chungd11265e2011-08-30 13:37:23 -07002166 @Override
2167 public boolean onHoverEvent(android.view.MotionEvent event) {
2168 return true;
2169 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002170}