blob: 0abac3670e4cfa9db18cb293e1f6c27873fbff42 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal316490e2015-06-02 09:38:28 -070031import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080050import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
52import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070053import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070055
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chung321e9ee2010-08-09 13:37:56 -070058/**
59 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070060 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070061 */
Michael Jurka8b805b12012-04-18 14:23:14 -070062public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070063 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070064 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070065 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Winson Chung86f77532010-08-24 11:08:22 -070067 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070068 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Vadim Tryshevfedca432015-08-19 17:55:02 -070070 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070071 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080074 // The page is moved more than halfway, automatically move to the next page on touch up.
75 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080076
Sunny Goyal8e2133b2015-05-06 13:39:07 -070077 private static final float MAX_SCROLL_PROGRESS = 1.0f;
78
Adam Cohen265b9a62011-12-07 14:37:18 -080079 // The following constants need to be scaled based on density. The scaled versions will be
80 // assigned to the corresponding member variables below.
81 private static final int FLING_THRESHOLD_VELOCITY = 500;
82 private static final int MIN_SNAP_VELOCITY = 1500;
83 private static final int MIN_FLING_VELOCITY = 250;
84
Adam Cohen21cd0022013-10-09 18:57:02 -070085 public static final int INVALID_RESTORE_PAGE = -1001;
86
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private boolean mFreeScroll = false;
88 private int mFreeScrollMinScrollX = -1;
89 private int mFreeScrollMaxScrollX = -1;
90
Adam Cohen265b9a62011-12-07 14:37:18 -080091 protected int mFlingThresholdVelocity;
92 protected int mMinFlingVelocity;
93 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070096 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070097
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700100 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700101
Sunny Goyal4ffec482016-02-09 11:28:52 -0800102 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800104 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800105 protected LauncherScroller mScroller;
106 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Adam Cohen7d30a372013-07-01 17:03:59 -0700110 private float mParentDownMotionX;
111 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700113 private float mDownMotionY;
114 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700115 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700116 private float mLastMotionX;
117 private float mLastMotionXRemainder;
118 private float mLastMotionY;
119 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700120
Adam Cohendbdff6b2013-09-18 19:09:15 -0700121 private boolean mCancelTap;
122
Adam Cohenedb40762013-07-18 16:45:45 -0700123 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected final static int TOUCH_STATE_REST = 0;
126 protected final static int TOUCH_STATE_SCROLLING = 1;
127 protected final static int TOUCH_STATE_PREV_PAGE = 2;
128 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 protected final static int TOUCH_STATE_REORDERING = 4;
130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800138 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700144 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Sunny Goyal061380a2016-02-29 15:15:03 -0800146 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700147
Winson Chungd2be3812013-07-16 11:11:32 -0700148 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700149 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700150 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700151 // The viewport whether the pages are to be contained (the actual view may be larger than the
152 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800153 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700154 private Rect mViewport = new Rect();
155
156 // Reordering
157 // We use the min scale to determine how much to expand the actually PagedView measured
158 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700159 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700160 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700161 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
162
Adam Cohen7d30a372013-07-01 17:03:59 -0700163 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700164 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700165 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700166 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700167 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700168 // This variable's scope is only for the duration of startReordering() and endReordering()
169 private boolean mReorderingStarted = false;
170 // This variable's scope is for the duration of startReordering() and after the zoomIn()
171 // animation after endReordering()
172 private boolean mIsReordering;
173 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700174 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 private int mPostReorderingPreZoomInRemainingAnimationCount;
176 private Runnable mPostReorderingPreZoomInRunnable;
177
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700179 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700180 private static final float[] sTmpPoint = new float[2];
181 private static final int[] sTmpIntPoint = new int[2];
182 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700183
John Spurlock77e1f472013-09-11 10:09:51 -0400184 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700185 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400186
Sunny Goyal4d113a52015-05-27 10:05:28 -0700187 // Edge effect
188 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
189 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
190
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 public PagedView(Context context) {
192 this(context, null);
193 }
194
195 public PagedView(Context context, AttributeSet attrs) {
196 this(context, attrs, 0);
197 }
198
199 public PagedView(Context context, AttributeSet attrs, int defStyle) {
200 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700201
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 TypedArray a = context.obtainStyledAttributes(attrs,
203 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700204 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 a.recycle();
206
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700208 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700209 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 }
211
212 /**
213 * Initializes various states for this workspace.
214 */
Michael Jurka0142d492010-08-25 17:46:15 -0700215 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800216 mScroller = new LauncherScroller(getContext());
217 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700218 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700219
220 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700221 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800223
Sunny Goyalcf25b522015-07-09 00:01:18 -0700224 float density = getResources().getDisplayMetrics().density;
225 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
226 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
227 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700228 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700229 setWillNotDraw(false);
230 }
231
232 protected void setEdgeGlowColor(int color) {
233 mEdgeGlowLeft.setColor(color);
234 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 }
236
Adam Cohenf9618852013-11-08 06:45:03 -0800237 protected void setDefaultInterpolator(Interpolator interpolator) {
238 mDefaultInterpolator = interpolator;
239 mScroller.setInterpolator(mDefaultInterpolator);
240 }
241
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700242 public void initParentViews(View parent) {
243 if (mPageIndicatorViewId > -1) {
244 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700245 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700246 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700247 }
248 }
249
Adam Cohen7d30a372013-07-01 17:03:59 -0700250 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700251 private float[] mapPointFromViewToParent(View v, float x, float y) {
252 sTmpPoint[0] = x;
253 sTmpPoint[1] = y;
254 v.getMatrix().mapPoints(sTmpPoint);
255 sTmpPoint[0] += v.getLeft();
256 sTmpPoint[1] += v.getTop();
257 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700258 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700259 private float[] mapPointFromParentToView(View v, float x, float y) {
260 sTmpPoint[0] = x - v.getLeft();
261 sTmpPoint[1] = y - v.getTop();
262 v.getMatrix().invert(sTmpInvMatrix);
263 sTmpInvMatrix.mapPoints(sTmpPoint);
264 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700265 }
266
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700267 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268 if (mDragView != null) {
269 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
270 (mDragViewBaselineLeft - mDragView.getLeft());
271 float y = mLastMotionY - mDownMotionY;
272 mDragView.setTranslationX(x);
273 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700274
Adam Cohenf358a4b2013-07-23 16:47:31 -0700275 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
276 + x + ", " + y);
277 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700278 }
279
280 public void setMinScale(float f) {
281 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700282 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700283 requestLayout();
284 }
285
286 @Override
287 public void setScaleX(float scaleX) {
288 super.setScaleX(scaleX);
289 if (isReordering(true)) {
290 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
291 mLastMotionX = p[0];
292 mLastMotionY = p[1];
293 updateDragViewTranslationDuringDrag();
294 }
295 }
296
297 // Convenience methods to get the actual width/height of the PagedView (since it is measured
298 // to be larger to account for the minimum possible scale)
299 int getViewportWidth() {
300 return mViewport.width();
301 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800302 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700303 return mViewport.height();
304 }
305
306 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
307 // PagedView both horizontally and vertically
308 int getViewportOffsetX() {
309 return (getMeasuredWidth() - getViewportWidth()) / 2;
310 }
311
312 int getViewportOffsetY() {
313 return (getMeasuredHeight() - getViewportHeight()) / 2;
314 }
315
Hyunyoung Song77441692016-06-27 22:00:48 -0700316 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700317 return mPageIndicator;
318 }
319
Adam Cohen674531f2013-12-13 15:07:14 -0800320 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700321 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
322 * that the user was on before entering free scroll mode (e.g. the home screen page they
323 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
324 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700326 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700327 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700329
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700330 /**
331 * Returns the index of page to be shown immediately afterwards.
332 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700333 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700334 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
335 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700336
Adam Cohenf9c184a2016-01-15 16:47:43 -0800337 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 return getChildCount();
339 }
340
Sunny Goyal83a8f042015-05-19 12:52:12 -0700341 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 return getChildAt(index);
343 }
344
Adam Cohenae4f1552011-10-20 00:15:42 -0700345 protected int indexToPage(int index) {
346 return index;
347 }
348
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800350 * Updates the scroll of the current page immediately to its final scroll position. We use this
351 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
352 * the previous tab page.
353 */
354 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700355 // If the current page is invalid, just reset the scroll position to zero
356 int newX = 0;
357 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700358 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700359 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800360 scrollTo(newX, 0);
361 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700362 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800363 }
364
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700365 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700366 mScroller.abortAnimation();
367 // We need to clean up the next page here to avoid computeScrollHelper from
368 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700369 if (resetNextPage) {
370 mNextPage = INVALID_PAGE;
371 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700372 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700373
Tony Wickham8f7ead32016-04-07 18:46:44 -0700374 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700375 mScroller.forceFinished(true);
376 // We need to clean up the next page here to avoid computeScrollHelper from
377 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700378 if (resetNextPage) {
379 mNextPage = INVALID_PAGE;
380 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700381 }
382
Adam Cohen6f127a62014-06-12 14:54:41 -0700383 private int validateNewPage(int newPage) {
384 int validatedPage = newPage;
385 // When in free scroll mode, we need to clamp to the free scroll page range.
386 if (mFreeScroll) {
387 getFreeScrollPageRange(mTempVisiblePagesRange);
388 validatedPage = Math.max(mTempVisiblePagesRange[0],
389 Math.min(newPage, mTempVisiblePagesRange[1]));
390 }
391 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700392 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700393 return validatedPage;
394 }
395
Chet Haasebc2f0822012-10-26 17:59:53 -0700396 /**
Winson Chung86f77532010-08-24 11:08:22 -0700397 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700398 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700399 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800400 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700401 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800402 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800403 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
404 // the default
405 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800406 return;
407 }
Adam Cohen6f127a62014-06-12 14:54:41 -0700408 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700409 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700410 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800411 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 }
413
Winson Chung8c87cd82013-07-23 16:20:10 -0700414 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800415 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
416 * has settled.
417 */
Michael Jurka0142d492010-08-25 17:46:15 -0700418 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800419 updatePageIndicator();
420 }
421
422 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700423 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700424 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700425 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700426 if (!isReordering(false)) {
427 mPageIndicator.setActiveMarker(getNextPage());
428 }
Winson Chungd2be3812013-07-16 11:11:32 -0700429 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700431 protected void pageBeginTransition() {
432 if (!mIsPageInTransition) {
433 mIsPageInTransition = true;
434 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700435 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700436 }
437
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700438 protected void pageEndTransition() {
439 if (mIsPageInTransition) {
440 mIsPageInTransition = false;
441 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700442 }
Michael Jurka0142d492010-08-25 17:46:15 -0700443 }
444
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700445 protected boolean isPageInTransition() {
446 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700447 }
448
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700449 /**
450 * Called when the page starts moving as part of the scroll. Subclasses can override this
451 * to provide custom behavior during animation.
452 */
453 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700454 }
455
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700456 /**
457 * Called when the page ends moving as part of the scroll. Subclasses can override this
458 * to provide custom behavior during animation.
459 */
460 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700461 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700462 }
463
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 /**
Winson Chung86f77532010-08-24 11:08:22 -0700465 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 *
467 * @param l The listener used to respond to long clicks.
468 */
469 @Override
470 public void setOnLongClickListener(OnLongClickListener l) {
471 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700472 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700473 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700474 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
Adam Cohen1697b792013-09-17 19:08:21 -0700476 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 }
478
Sunny Goyalc86df472016-02-25 09:19:38 -0800479 protected int getUnboundedScrollX() {
480 return getScrollX();
481 }
482
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800484 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800485 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800486 }
487
488 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700489 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700490 // In free scroll mode, we clamp the scrollX
491 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700492 // If the scroller is trying to move to a location beyond the maximum allowed
493 // in the free scroll mode, we make sure to end the scroll operation.
494 if (!mScroller.isFinished() &&
495 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700496 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700497 }
498
Adam Cohenf358a4b2013-07-23 16:47:31 -0700499 x = Math.min(x, mFreeScrollMaxScrollX);
500 x = Math.max(x, mFreeScrollMinScrollX);
501 }
502
Sunny Goyal70660032015-05-14 00:07:08 -0700503 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
504 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700505 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700506 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800507 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700508 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700509 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700510 overScroll(x - mMaxScrollX);
511 } else {
512 overScroll(x);
513 }
Adam Cohen68d73932010-11-15 10:50:58 -0800514 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700515 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700516 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800517 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700518 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700519 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700520 overScroll(x);
521 } else {
522 overScroll(x - mMaxScrollX);
523 }
Adam Cohen68d73932010-11-15 10:50:58 -0800524 }
525 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700526 if (mWasInOverscroll) {
527 overScroll(0);
528 mWasInOverscroll = false;
529 }
Adam Cohen68d73932010-11-15 10:50:58 -0800530 super.scrollTo(x, y);
531 }
532
Adam Cohen7d30a372013-07-01 17:03:59 -0700533 // Update the last motion events when scrolling
534 if (isReordering(true)) {
535 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
536 mLastMotionX = p[0];
537 mLastMotionY = p[1];
538 updateDragViewTranslationDuringDrag();
539 }
Michael Jurka0142d492010-08-25 17:46:15 -0700540 }
541
Adam Cohen53805212013-10-01 10:39:23 -0700542 private void sendScrollAccessibilityEvent() {
543 AccessibilityManager am =
544 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
545 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700546 if (mCurrentPage != getNextPage()) {
547 AccessibilityEvent ev =
548 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700549 ev.setScrollable(true);
550 ev.setScrollX(getScrollX());
551 ev.setScrollY(getScrollY());
552 ev.setMaxScrollX(mMaxScrollX);
553 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700554
Vadim Tryshevf4715972015-05-08 17:21:03 -0700555 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700556 }
Adam Cohen53805212013-10-01 10:39:23 -0700557 }
558 }
559
Michael Jurka0142d492010-08-25 17:46:15 -0700560 // we moved this functionality to a helper function so SmoothPagedView can reuse it
561 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800562 return computeScrollHelper(true);
563 }
564
565 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700567 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800569 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700570 float scaleX = mFreeScroll ? getScaleX() : 1f;
571 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
572 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700573 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800574 if (shouldInvalidate) {
575 invalidate();
576 }
Michael Jurka0142d492010-08-25 17:46:15 -0700577 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800578 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700579 sendScrollAccessibilityEvent();
580
Adam Cohen6f127a62014-06-12 14:54:41 -0700581 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700582 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700583 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700584
Adam Cohen73aa9752010-11-24 16:26:58 -0800585 // We don't want to trigger a page end moving unless the page has settled
586 // and the user has stopped scrolling
587 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700588 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800589 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700590
Adam Cohen7d30a372013-07-01 17:03:59 -0700591 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700592 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700593 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700594 if (am.isEnabled()) {
595 // Notify the user when the page changes
596 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700597 }
Michael Jurka0142d492010-08-25 17:46:15 -0700598 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
Michael Jurka0142d492010-08-25 17:46:15 -0700600 return false;
601 }
602
603 @Override
604 public void computeScroll() {
605 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700606 }
607
Adam Cohen96d30a12013-07-16 18:13:21 -0700608 public static class LayoutParams extends ViewGroup.LayoutParams {
609 public boolean isFullScreenPage = false;
610
Sunny Goyal7c786f72016-06-01 14:08:21 -0700611 // If true, the start edge of the page snaps to the start edge of the viewport.
612 public boolean matchStartEdge = false;
613
Adam Cohen96d30a12013-07-16 18:13:21 -0700614 /**
615 * {@inheritDoc}
616 */
617 public LayoutParams(int width, int height) {
618 super(width, height);
619 }
620
Sunny Goyal41b22c02015-05-14 15:20:48 -0700621 public LayoutParams(Context context, AttributeSet attrs) {
622 super(context, attrs);
623 }
624
Adam Cohen96d30a12013-07-16 18:13:21 -0700625 public LayoutParams(ViewGroup.LayoutParams source) {
626 super(source);
627 }
628 }
629
Sunny Goyal41b22c02015-05-14 15:20:48 -0700630 @Override
631 public LayoutParams generateLayoutParams(AttributeSet attrs) {
632 return new LayoutParams(getContext(), attrs);
633 }
634
635 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700636 protected LayoutParams generateDefaultLayoutParams() {
637 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
638 }
639
Sunny Goyal41b22c02015-05-14 15:20:48 -0700640 @Override
641 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
642 return new LayoutParams(p);
643 }
644
645 @Override
646 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
647 return p instanceof LayoutParams;
648 }
649
Adam Cohenedb40762013-07-18 16:45:45 -0700650 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700651 LayoutParams lp = generateDefaultLayoutParams();
652 lp.isFullScreenPage = true;
653 super.addView(page, 0, lp);
654 }
655
Adam Cohen410f3cd2013-09-22 12:09:32 -0700656 public int getNormalChildHeight() {
657 return mNormalChildHeight;
658 }
659
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 @Override
661 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700662 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700663 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
664 return;
665 }
666
Adam Cohen7d30a372013-07-01 17:03:59 -0700667 // We measure the dimensions of the PagedView to be larger than the pages so that when we
668 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700669 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
670 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
671 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700672 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800673 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700674 // viewport, we can be at most one and a half screens offset once we scale down
675 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700676 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
677 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700678
Winson Chungc82d2622013-11-06 13:23:29 -0800679 int parentWidthSize = (int) (2f * maxSize);
680 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700681 int scaledWidthSize, scaledHeightSize;
682 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700683 scaledWidthSize = (int) (parentWidthSize / mMinScale);
684 scaledHeightSize = (int) (parentHeightSize / mMinScale);
685 } else {
686 scaledWidthSize = widthSize;
687 scaledHeightSize = heightSize;
688 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700689 mViewport.set(0, 0, widthSize, heightSize);
690
691 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
692 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
693 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 }
695
Winson Chung8aad6102012-05-11 16:27:49 -0700696 // Return early if we aren't given a proper dimension
697 if (widthSize <= 0 || heightSize <= 0) {
698 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
699 return;
700 }
701
Adam Lesinski6b879f02010-11-04 16:15:23 -0700702 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
703 * of the All apps view on XLarge displays to not take up more space then it needs. Width
704 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
705 * each page to have the same width.
706 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 final int verticalPadding = getPaddingTop() + getPaddingBottom();
708 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700709
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700710 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700712 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700713 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700714 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
715 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
716 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
717 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 final int childCount = getChildCount();
719 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700720 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700721 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100722 if (child.getVisibility() != GONE) {
723 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700724
Vladimir Marko2824b072013-10-04 16:42:17 +0100725 int childWidthMode;
726 int childHeightMode;
727 int childWidth;
728 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700729
Vladimir Marko2824b072013-10-04 16:42:17 +0100730 if (!lp.isFullScreenPage) {
731 if (lp.width == LayoutParams.WRAP_CONTENT) {
732 childWidthMode = MeasureSpec.AT_MOST;
733 } else {
734 childWidthMode = MeasureSpec.EXACTLY;
735 }
736
737 if (lp.height == LayoutParams.WRAP_CONTENT) {
738 childHeightMode = MeasureSpec.AT_MOST;
739 } else {
740 childHeightMode = MeasureSpec.EXACTLY;
741 }
742
Adam Cohen3b185e22013-10-29 14:45:58 -0700743 childWidth = getViewportWidth() - horizontalPadding
744 - mInsets.left - mInsets.right;
745 childHeight = getViewportHeight() - verticalPadding
746 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100747 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700748 } else {
749 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700750 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100751
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800752 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700753 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700754 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700755 if (referenceChildWidth == 0) {
756 referenceChildWidth = childWidth;
757 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700758
Vladimir Marko2824b072013-10-04 16:42:17 +0100759 final int childWidthMeasureSpec =
760 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
761 final int childHeightMeasureSpec =
762 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
763 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700764 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700765 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700766 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800767 }
768
Sunny Goyalcf25b522015-07-09 00:01:18 -0700769 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700771 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700772 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700773 return;
774 }
775
Winson Chung785d2eb2011-04-14 16:08:02 -0700776 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700778
Adam Cohen7d30a372013-07-01 17:03:59 -0700779 int offsetX = getViewportOffsetX();
780 int offsetY = getViewportOffsetY();
781
782 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700783 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700784
Sunny Goyal70660032015-05-14 00:07:08 -0700785 final int startIndex = mIsRtl ? childCount - 1 : 0;
786 final int endIndex = mIsRtl ? -1 : childCount;
787 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700788
Adam Cohen7d30a372013-07-01 17:03:59 -0700789 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700790
Adam Cohen3b185e22013-10-29 14:45:58 -0700791 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000792 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700793
Sunny Goyal6178f132016-07-11 17:30:03 -0700794 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700795 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
796 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700797 }
798
Adam Cohen0ffac432013-07-10 11:19:26 -0700799 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700800 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700802 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100803 int childTop;
804 if (lp.isFullScreenPage) {
805 childTop = offsetY;
806 } else {
807 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700808 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100809 }
810
Adam Cohen96d30a12013-07-16 18:13:21 -0700811 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700812 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800813
Winson Chung785d2eb2011-04-14 16:08:02 -0700814 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700815 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800816 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700817
Sunny Goyal6178f132016-07-11 17:30:03 -0700818 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700819 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000820
821 int pageGap = mPageSpacing;
822 int next = i + delta;
823 if (next != endIndex) {
824 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
825 } else {
826 nextLp = null;
827 }
828
829 // Prevent full screen pages from showing in the viewport
830 // when they are not the current page.
831 if (lp.isFullScreenPage) {
832 pageGap = getPaddingLeft();
833 } else if (nextLp != null && nextLp.isFullScreenPage) {
834 pageGap = getPaddingRight();
835 }
836
Sunny Goyala1fbd842015-05-20 13:40:27 -0700837 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700840
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700841 final LayoutTransition transition = getLayoutTransition();
842 // If the transition is running defer updating max scroll, as some empty pages could
843 // still be present, and a max scroll change could cause sudden jumps in scroll.
844 if (transition != null && transition.isRunning()) {
845 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
846
847 @Override
848 public void startTransition(LayoutTransition transition, ViewGroup container,
849 View view, int transitionType) { }
850
851 @Override
852 public void endTransition(LayoutTransition transition, ViewGroup container,
853 View view, int transitionType) {
854 // Wait until all transitions are complete.
855 if (!transition.isRunning()) {
856 transition.removeTransitionListener(this);
857 updateMaxScrollX();
858 }
859 }
860 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700861 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700862 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700863 }
864
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700865 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
866 updateCurrentPageScroll();
867 mFirstLayout = false;
868 }
869
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700870 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700871 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700872 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700873 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700874
875 if (isReordering(true)) {
876 updateDragViewTranslationDuringDrag();
877 }
Winson Chunge3193b92010-09-10 11:44:42 -0700878 }
879
Sunny Goyala1fbd842015-05-20 13:40:27 -0700880 protected int getChildGap() {
881 return 0;
882 }
883
Sunny Goyal316490e2015-06-02 09:38:28 -0700884 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700885 mMaxScrollX = computeMaxScrollX();
886 }
887
888 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700889 int childCount = getChildCount();
890 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700891 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700892 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700893 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700894 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700895 }
896 }
897
Adam Cohen3b185e22013-10-29 14:45:58 -0700898 public void setPageSpacing(int pageSpacing) {
899 mPageSpacing = pageSpacing;
900 requestLayout();
901 }
902
Winson Chunge3193b92010-09-10 11:44:42 -0700903 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700904 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700905 // Update the page indicator, we don't update the page indicator as we
906 // add/remove pages
907 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700908 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700909 }
910
Adam Cohen2591f6a2011-10-25 14:36:40 -0700911 // This ensures that when children are added, they get the correct transforms / alphas
912 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700913 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700914 invalidate();
915 }
916
Michael Jurka8b805b12012-04-18 14:23:14 -0700917 @Override
918 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700919 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700920 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700921 }
922
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700923 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700924 // Update the page indicator, we don't update the page indicator as we
925 // add/remove pages
926 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700927 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700928 }
929 }
930
931 @Override
932 public void removeView(View v) {
933 // XXX: We should find a better way to hook into this before the view
934 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700935 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700936 super.removeView(v);
937 }
938 @Override
939 public void removeViewInLayout(View v) {
940 // XXX: We should find a better way to hook into this before the view
941 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700942 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700943 super.removeViewInLayout(v);
944 }
945 @Override
946 public void removeViewAt(int index) {
947 // XXX: We should find a better way to hook into this before the view
948 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700949 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700950 super.removeViewAt(index);
951 }
952 @Override
953 public void removeAllViewsInLayout() {
954 // Update the page indicator, we don't update the page indicator as we
955 // add/remove pages
956 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700957 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700958 }
959
960 super.removeAllViewsInLayout();
961 }
962
Adam Cohen73894962011-10-31 13:17:17 -0700963 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700964 if (index < 0 || index > getChildCount() - 1) return 0;
965
Adam Cohenf698c6e2013-07-17 18:44:25 -0700966 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700967
Adam Cohenf698c6e2013-07-17 18:44:25 -0700968 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700969 }
970
Adam Cohen6f127a62014-06-12 14:54:41 -0700971 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700972 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700973 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700974 }
975
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -0700977 public void draw(Canvas canvas) {
978 super.draw(canvas);
979 if (getPageCount() > 0) {
980 if (!mEdgeGlowLeft.isFinished()) {
981 final int restoreCount = canvas.save();
982 Rect display = mViewport;
983 canvas.translate(display.left, display.top);
984 canvas.rotate(270);
985
Hyunyoung Song0de01172016-10-05 16:27:48 -0700986 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700987 canvas.translate(display.top - sTmpIntPoint[1], 0);
988 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
989 if (mEdgeGlowLeft.draw(canvas)) {
990 postInvalidateOnAnimation();
991 }
992 canvas.restoreToCount(restoreCount);
993 }
994 if (!mEdgeGlowRight.isFinished()) {
995 final int restoreCount = canvas.save();
996 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700997 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700998 canvas.rotate(90);
999
Hyunyoung Song0de01172016-10-05 16:27:48 -07001000 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001001
Sunny Goyal93264612015-11-23 11:47:50 -08001002 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1003 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001004 if (mEdgeGlowRight.draw(canvas)) {
1005 postInvalidateOnAnimation();
1006 }
1007 canvas.restoreToCount(restoreCount);
1008 }
1009 }
1010 }
1011
1012 /**
1013 * Returns the top and bottom position for the edge effect.
1014 */
Hyunyoung Song0de01172016-10-05 16:27:48 -07001015 protected abstract void getEdgeVerticalPosition(int[] pos);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001016
1017 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001019 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001020 if (page != mCurrentPage || !mScroller.isFinished()) {
1021 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 return true;
1023 }
1024 return false;
1025 }
1026
1027 @Override
1028 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001029 int focusablePage;
1030 if (mNextPage != INVALID_PAGE) {
1031 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001033 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 }
Winson Chung86f77532010-08-24 11:08:22 -07001035 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001037 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 }
1039 return false;
1040 }
1041
1042 @Override
1043 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001044 if (super.dispatchUnhandledMove(focused, direction)) {
1045 return true;
1046 }
1047
1048 if (mIsRtl) {
1049 if (direction == View.FOCUS_LEFT) {
1050 direction = View.FOCUS_RIGHT;
1051 } else if (direction == View.FOCUS_RIGHT) {
1052 direction = View.FOCUS_LEFT;
1053 }
1054 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001056 if (getCurrentPage() > 0) {
1057 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 return true;
1059 }
1060 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001061 if (getCurrentPage() < getPageCount() - 1) {
1062 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 return true;
1064 }
1065 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001066 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068
1069 @Override
1070 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001071 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001072 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001073 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 }
1075 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001076 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001077 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 }
1079 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001080 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001081 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 }
1083 }
1084 }
1085
1086 /**
1087 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001088 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 *
Winson Chung86f77532010-08-24 11:08:22 -07001090 * This happens when live folders requery, and if they're off page, they
1091 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 */
1093 @Override
1094 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001095 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 View v = focused;
1097 while (true) {
1098 if (v == current) {
1099 super.focusableViewAvailable(focused);
1100 return;
1101 }
1102 if (v == this) {
1103 return;
1104 }
1105 ViewParent parent = v.getParent();
1106 if (parent instanceof View) {
1107 v = (View)v.getParent();
1108 } else {
1109 return;
1110 }
1111 }
1112 }
1113
1114 /**
1115 * {@inheritDoc}
1116 */
1117 @Override
1118 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1119 if (disallowIntercept) {
1120 // We need to make sure to cancel our long press if
1121 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001122 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001123 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 }
1125 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1126 }
1127
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001128 /**
1129 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1130 */
1131 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001132 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001133 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001134 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001135 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001136 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001137 }
1138
1139 /**
1140 * Return true if a tap at (x, y) should trigger a flip to the next page.
1141 */
1142 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001143 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001144 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001145 }
1146 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001147 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001148 }
Winson Chung52aee602013-01-30 12:01:02 -08001149
Adam Cohen7d30a372013-07-01 17:03:59 -07001150 /** Returns whether x and y originated within the buffered viewport */
1151 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001152 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001153 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001154 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001155 }
1156
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 @Override
1158 public boolean onInterceptTouchEvent(MotionEvent ev) {
1159 /*
1160 * This method JUST determines whether we want to intercept the motion.
1161 * If we return true, onTouchEvent will be called and we do the actual
1162 * scrolling there.
1163 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001164 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001165
Winson Chung45e1d6e2010-11-09 17:19:49 -08001166 // Skip touch handling if there are no pages to swipe
1167 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1168
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 /*
1170 * Shortcut the most recurring case: the user is in the dragging
1171 * state and he is moving his finger. We want to intercept this
1172 * motion.
1173 */
1174 final int action = ev.getAction();
1175 if ((action == MotionEvent.ACTION_MOVE) &&
1176 (mTouchState == TOUCH_STATE_SCROLLING)) {
1177 return true;
1178 }
1179
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 switch (action & MotionEvent.ACTION_MASK) {
1181 case MotionEvent.ACTION_MOVE: {
1182 /*
1183 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1184 * whether the user has moved far enough from his original down touch.
1185 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001186 if (mActivePointerId != INVALID_POINTER) {
1187 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001188 }
1189 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1190 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1191 // i.e. fall through to the next case (don't break)
1192 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1193 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001194 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 }
1196
1197 case MotionEvent.ACTION_DOWN: {
1198 final float x = ev.getX();
1199 final float y = ev.getY();
1200 // Remember location of down touch
1201 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001202 mDownMotionY = y;
1203 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 mLastMotionX = x;
1205 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001206 float[] p = mapPointFromViewToParent(this, x, y);
1207 mParentDownMotionX = p[0];
1208 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001209 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001210 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001212
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 /*
1214 * If being flinged and user touches the screen, initiate drag;
1215 * otherwise don't. mScroller.isFinished should be false when
1216 * being flinged.
1217 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001218 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001219 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001220
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001221 if (finishedScrolling) {
1222 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001223 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001224 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001225 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001226 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001227 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001228 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1229 mTouchState = TOUCH_STATE_SCROLLING;
1230 } else {
1231 mTouchState = TOUCH_STATE_REST;
1232 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001233 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001234
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 break;
1236 }
1237
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001239 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001240 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 break;
1242
1243 case MotionEvent.ACTION_POINTER_UP:
1244 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001245 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 break;
1247 }
1248
1249 /*
1250 * The only time we want to intercept motion events is if we are in the
1251 * drag mode.
1252 */
1253 return mTouchState != TOUCH_STATE_REST;
1254 }
1255
Adam Cohenf8d28232011-02-01 21:47:00 -08001256 protected void determineScrollingStart(MotionEvent ev) {
1257 determineScrollingStart(ev, 1.0f);
1258 }
1259
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 /*
1261 * Determines if we should change the touch state to start scrolling after the
1262 * user moves their touch point too far.
1263 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001264 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001265 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001267 if (pointerIndex == -1) return;
1268
1269 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 final float x = ev.getX(pointerIndex);
1271 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001272 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1273
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001275
Adam Cohenf8d28232011-02-01 21:47:00 -08001276 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001278
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001279 if (xMoved) {
1280 // Scroll if the user moved far enough along the X axis
1281 mTouchState = TOUCH_STATE_SCROLLING;
1282 mTotalMotionX += Math.abs(mLastMotionX - x);
1283 mLastMotionX = x;
1284 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001285 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001286 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001287 // Stop listening for things like pinches.
1288 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001289 }
1290 }
1291
1292 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001293 // Try canceling the long press. It could also have been scheduled
1294 // by a distant descendant, so use the mAllowLongPress flag to block
1295 // everything
1296 final View currentPage = getPageAt(mCurrentPage);
1297 if (currentPage != null) {
1298 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 }
1300 }
1301
Adam Cohenb5ba0972011-09-07 18:02:31 -07001302 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001303 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001304
Adam Cohenedb40762013-07-18 16:45:45 -07001305 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001306 int count = getChildCount();
1307
1308 final int totalDistance;
1309
1310 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001311 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001312 adjacentPage = page - 1;
1313 }
1314
1315 if (adjacentPage < 0 || adjacentPage > count - 1) {
1316 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1317 } else {
1318 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1319 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001320
1321 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001322 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1323 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001324 return scrollProgress;
1325 }
1326
Adam Cohenedb40762013-07-18 16:45:45 -07001327 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001328 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001329 return 0;
1330 } else {
1331 return mPageScrolls[index];
1332 }
1333 }
1334
Adam Cohen564a2e72013-10-09 14:47:32 -07001335 // While layout transitions are occurring, a child's position may stray from its baseline
1336 // position. This method returns the magnitude of this stray at any given time.
1337 public int getLayoutTransitionOffsetForPage(int index) {
1338 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1339 return 0;
1340 } else {
1341 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001342
1343 int scrollOffset = 0;
1344 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1345 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001346 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001347 }
1348
Adam Cohen564a2e72013-10-09 14:47:32 -07001349 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1350 return (int) (child.getX() - baselineX);
1351 }
1352 }
1353
Adam Cohenb5ba0972011-09-07 18:02:31 -07001354 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001355 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001356 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001357 if (f < 0) {
1358 mEdgeGlowLeft.onPull(-f);
1359 } else if (f > 0) {
1360 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001361 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001362 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001363 }
1364 invalidate();
1365 }
1366
Adam Cohenb5ba0972011-09-07 18:02:31 -07001367 protected void overScroll(float amount) {
1368 dampedOverScroll(amount);
1369 }
1370
Winson Chungdc61c4d2015-04-20 18:26:57 -07001371 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001372 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001373 }
1374
Winson Chungdc61c4d2015-04-20 18:26:57 -07001375 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001376 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001377 }
1378
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001379 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001380 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001381 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001382 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1383 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1384 } else {
1385 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1386 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1387 }
1388 }
1389
Adam Cohenf9618852013-11-08 06:45:03 -08001390 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001391 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 mFreeScroll = freeScroll;
1393
Adam Cohenf9618852013-11-08 06:45:03 -08001394 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001395 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001396 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1398 setCurrentPage(mTempVisiblePagesRange[0]);
1399 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1400 setCurrentPage(mTempVisiblePagesRange[1]);
1401 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001402 } else if (wasFreeScroll) {
1403 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001404 }
1405
1406 setEnableOverscroll(!freeScroll);
1407 }
1408
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001409 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410 mAllowOverScroll = enable;
1411 }
1412
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001413 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 if (mDragView != null) {
1415 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1416 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001417 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 int minDistance = Integer.MAX_VALUE;
1419 int minIndex = indexOfChild(mDragView);
1420 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1421 View page = getPageAt(i);
1422 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1423 int d = Math.abs(dragX - pageX);
1424 if (d < minDistance) {
1425 minIndex = i;
1426 minDistance = d;
1427 }
1428 }
1429 return minIndex;
1430 }
1431 return -1;
1432 }
1433
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 @Override
1435 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001436 super.onTouchEvent(ev);
1437
Winson Chung45e1d6e2010-11-09 17:19:49 -08001438 // Skip touch handling if there are no pages to swipe
1439 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1440
Michael Jurkab8f06722010-10-10 15:58:46 -07001441 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001442
1443 final int action = ev.getAction();
1444
1445 switch (action & MotionEvent.ACTION_MASK) {
1446 case MotionEvent.ACTION_DOWN:
1447 /*
1448 * If being flinged and user touches, stop the fling. isFinished
1449 * will be false if being flinged.
1450 */
1451 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001452 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 }
1454
1455 // Remember where the motion event started
1456 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001457 mDownMotionY = mLastMotionY = ev.getY();
1458 mDownScrollX = getScrollX();
1459 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1460 mParentDownMotionX = p[0];
1461 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001462 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001463 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001465
Michael Jurka0142d492010-08-25 17:46:15 -07001466 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001467 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001468 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001469 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 break;
1471
1472 case MotionEvent.ACTION_MOVE:
1473 if (mTouchState == TOUCH_STATE_SCROLLING) {
1474 // Scroll to follow the motion event
1475 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001476
1477 if (pointerIndex == -1) return true;
1478
Winson Chung321e9ee2010-08-09 13:37:56 -07001479 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001480 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001481
Adam Cohenaefd4e12011-02-14 16:39:38 -08001482 mTotalMotionX += Math.abs(deltaX);
1483
Winson Chungc0844aa2011-02-02 15:25:58 -08001484 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1485 // keep the remainder because we are actually testing if we've moved from the last
1486 // scrolled position (which is discrete).
1487 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001488 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001489 mLastMotionX = x;
1490 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 } else {
1492 awakenScrollBars();
1493 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1495 // Update the last motion position
1496 mLastMotionX = ev.getX();
1497 mLastMotionY = ev.getY();
1498
1499 // Update the parent down so that our zoom animations take this new movement into
1500 // account
1501 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1502 mParentDownMotionX = pt[0];
1503 mParentDownMotionY = pt[1];
1504 updateDragViewTranslationDuringDrag();
1505
1506 // Find the closest page to the touch point
1507 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001508
Adam Cohen7d30a372013-07-01 17:03:59 -07001509 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1510 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1511 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1512 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1513
Adam Cohenf358a4b2013-07-23 16:47:31 -07001514 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001515 // Do not allow any page to be moved to 0th position.
1516 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001517 mTempVisiblePagesRange[0] = 0;
1518 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001519 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001520 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1521 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1522 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1523 mSidePageHoverIndex = pageUnderPointIndex;
1524 mSidePageHoverRunnable = new Runnable() {
1525 @Override
1526 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001527 // Setup the scroll to the correct page before we swap the views
1528 snapToPage(pageUnderPointIndex);
1529
1530 // For each of the pages between the paged view and the drag view,
1531 // animate them from the previous position to the new position in
1532 // the layout (as a result of the drag view moving in the layout)
1533 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1534 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1535 dragViewIndex + 1 : pageUnderPointIndex;
1536 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1537 dragViewIndex - 1 : pageUnderPointIndex;
1538 for (int i = lowerIndex; i <= upperIndex; ++i) {
1539 View v = getChildAt(i);
1540 // dragViewIndex < pageUnderPointIndex, so after we remove the
1541 // drag view all subsequent views to pageUnderPointIndex will
1542 // shift down.
1543 int oldX = getViewportOffsetX() + getChildOffset(i);
1544 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1545
1546 // Animate the view translation from its old position to its new
1547 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001548 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001549 if (anim != null) {
1550 anim.cancel();
1551 }
1552
1553 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001554 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001555 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001556 anim.start();
1557 v.setTag(anim);
1558 }
1559
1560 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001561 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001562 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001563 if (mPageIndicator != null) {
1564 mPageIndicator.setActiveMarker(getNextPage());
1565 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001566 }
1567 };
1568 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1569 }
1570 } else {
1571 removeCallbacks(mSidePageHoverRunnable);
1572 mSidePageHoverIndex = -1;
1573 }
Adam Cohen564976a2010-10-13 18:52:07 -07001574 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 determineScrollingStart(ev);
1576 }
1577 break;
1578
1579 case MotionEvent.ACTION_UP:
1580 if (mTouchState == TOUCH_STATE_SCROLLING) {
1581 final int activePointerId = mActivePointerId;
1582 final int pointerIndex = ev.findPointerIndex(activePointerId);
1583 final float x = ev.getX(pointerIndex);
1584 final VelocityTracker velocityTracker = mVelocityTracker;
1585 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1586 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001587 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001588 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001589 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1590 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001591
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001592 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1593
Adam Cohen00481b32011-11-18 12:03:48 -08001594 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001595 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001596
Adam Cohenf358a4b2013-07-23 16:47:31 -07001597 if (!mFreeScroll) {
1598 // In the case that the page is moved far to one direction and then is flung
1599 // in the opposite direction, we use a threshold to determine whether we should
1600 // just return to the starting page, or if we should skip one further.
1601 boolean returnToOriginalPage = false;
1602 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1603 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1604 returnToOriginalPage = true;
1605 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001606
Adam Cohenf358a4b2013-07-23 16:47:31 -07001607 int finalPage;
1608 // We give flings precedence over large moves, which is why we short-circuit our
1609 // test for a large move if a fling has been registered. That is, a large
1610 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001611 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1612 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001613 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1614 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1615 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1616 snapToPageWithVelocity(finalPage, velocityX);
1617 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1618 (isFling && isVelocityXLeft)) &&
1619 mCurrentPage < getChildCount() - 1) {
1620 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1621 snapToPageWithVelocity(finalPage, velocityX);
1622 } else {
1623 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001624 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001625 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001626 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001627 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001628 }
1629
1630 float scaleX = getScaleX();
1631 int vX = (int) (-velocityX * scaleX);
1632 int initialScrollX = (int) (getScrollX() * scaleX);
1633
Adam Cohenf9618852013-11-08 06:45:03 -08001634 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001635 mScroller.fling(initialScrollX,
1636 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001637 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001638 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001639 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001640 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001641 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1642 // at this point we have not moved beyond the touch slop
1643 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1644 // we can just page
1645 int nextPage = Math.max(0, mCurrentPage - 1);
1646 if (nextPage != mCurrentPage) {
1647 snapToPage(nextPage);
1648 } else {
1649 snapToDestination();
1650 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001651 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001652 // at this point we have not moved beyond the touch slop
1653 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1654 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001655 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1656 if (nextPage != mCurrentPage) {
1657 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001658 } else {
1659 snapToDestination();
1660 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001661 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1662 // Update the last motion position
1663 mLastMotionX = ev.getX();
1664 mLastMotionY = ev.getY();
1665
1666 // Update the parent down so that our zoom animations take this new movement into
1667 // account
1668 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1669 mParentDownMotionX = pt[0];
1670 mParentDownMotionY = pt[1];
1671 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001672 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001673 if (!mCancelTap) {
1674 onUnhandledTap(ev);
1675 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001676 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001677
1678 // Remove the callback to wait for the side page hover timeout
1679 removeCallbacks(mSidePageHoverRunnable);
1680 // End any intermediate reordering states
1681 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001682 break;
1683
1684 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001685 if (mTouchState == TOUCH_STATE_SCROLLING) {
1686 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001687 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001688 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001689 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001690 break;
1691
1692 case MotionEvent.ACTION_POINTER_UP:
1693 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001694 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001695 break;
1696 }
1697
1698 return true;
1699 }
1700
Adam Cohen7d30a372013-07-01 17:03:59 -07001701 private void resetTouchState() {
1702 releaseVelocityTracker();
1703 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001704 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001705 mTouchState = TOUCH_STATE_REST;
1706 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001707 mEdgeGlowLeft.onRelease();
1708 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001709 }
1710
Adam Cohenc2d6e892014-10-16 09:49:24 -07001711 /**
1712 * Triggered by scrolling via touch
1713 */
1714 protected void onScrollInteractionBegin() {
1715 }
1716
1717 protected void onScrollInteractionEnd() {
1718 }
1719
Adam Cohen1697b792013-09-17 19:08:21 -07001720 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001721 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001722 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001723
Winson Chung185d7162011-02-28 13:47:29 -08001724 @Override
1725 public boolean onGenericMotionEvent(MotionEvent event) {
1726 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1727 switch (event.getAction()) {
1728 case MotionEvent.ACTION_SCROLL: {
1729 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1730 final float vscroll;
1731 final float hscroll;
1732 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1733 vscroll = 0;
1734 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1735 } else {
1736 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1737 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1738 }
1739 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001740 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001741 : (hscroll > 0 || vscroll > 0);
1742 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001743 scrollRight();
1744 } else {
1745 scrollLeft();
1746 }
1747 return true;
1748 }
1749 }
1750 }
1751 }
1752 return super.onGenericMotionEvent(event);
1753 }
1754
Michael Jurkab8f06722010-10-10 15:58:46 -07001755 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1756 if (mVelocityTracker == null) {
1757 mVelocityTracker = VelocityTracker.obtain();
1758 }
1759 mVelocityTracker.addMovement(ev);
1760 }
1761
1762 private void releaseVelocityTracker() {
1763 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001764 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001765 mVelocityTracker.recycle();
1766 mVelocityTracker = null;
1767 }
1768 }
1769
Winson Chung321e9ee2010-08-09 13:37:56 -07001770 private void onSecondaryPointerUp(MotionEvent ev) {
1771 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1772 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1773 final int pointerId = ev.getPointerId(pointerIndex);
1774 if (pointerId == mActivePointerId) {
1775 // This was our active pointer going up. Choose a new
1776 // active pointer and adjust accordingly.
1777 // TODO: Make this decision more intelligent.
1778 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1779 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1780 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001781 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001782 mActivePointerId = ev.getPointerId(newPointerIndex);
1783 if (mVelocityTracker != null) {
1784 mVelocityTracker.clear();
1785 }
1786 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001787 }
1788
Winson Chung321e9ee2010-08-09 13:37:56 -07001789 @Override
1790 public void requestChildFocus(View child, View focused) {
1791 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001792 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001793 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001794 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001795 }
1796 }
1797
Adam Cohend19d3ca2010-09-15 14:43:42 -07001798 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001799 return getPageNearestToCenterOfScreen(getScrollX());
1800 }
1801
1802 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1803 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001804 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001805 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001806 final int childCount = getChildCount();
1807 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001808 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001809 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001810 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001811 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001812 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1813 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1814 minDistanceFromScreenCenter = distanceFromScreenCenter;
1815 minDistanceFromScreenCenterIndex = i;
1816 }
1817 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001818 return minDistanceFromScreenCenterIndex;
1819 }
1820
1821 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001822 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001823 }
1824
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001825 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001826 public ScrollInterpolator() {
1827 }
1828
1829 public float getInterpolation(float t) {
1830 t -= 1.0f;
1831 return t*t*t*t*t + 1;
1832 }
1833 }
1834
1835 // We want the duration of the page snap animation to be influenced by the distance that
1836 // the screen has to travel, however, we don't want this duration to be effected in a
1837 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1838 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001839 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001840 f -= 0.5f; // center the values about 0.
1841 f *= 0.3f * Math.PI / 2.0f;
1842 return (float) Math.sin(f);
1843 }
1844
Michael Jurka0142d492010-08-25 17:46:15 -07001845 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001846 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001847 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001848
Adam Cohenedb40762013-07-18 16:45:45 -07001849 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001850 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001851 int duration = 0;
1852
Sunny Goyal4d113a52015-05-27 10:05:28 -07001853 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001854 // If the velocity is low enough, then treat this more as an automatic page advance
1855 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001856 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001857 return;
1858 }
1859
1860 // Here we compute a "distance" that will be used in the computation of the overall
1861 // snap duration. This is a function of the actual distance that needs to be traveled;
1862 // we keep this value close to half screen size in order to reduce the variance in snap
1863 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001864 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001865 float distance = halfScreenSize + halfScreenSize *
1866 distanceInfluenceForSnapDuration(distanceRatio);
1867
1868 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001869 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001870
1871 // we want the page's snap velocity to approximately match the velocity at which the
1872 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001873 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1874 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001875
1876 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001877 }
1878
Sunny Goyal1740d902015-05-27 11:14:01 -07001879 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001880 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001881 }
1882
Adam Cohenf9c184a2016-01-15 16:47:43 -08001883 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001884 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001885 }
1886
Michael Jurka0142d492010-08-25 17:46:15 -07001887 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001888 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001889 }
1890
Adam Cohenf9618852013-11-08 06:45:03 -08001891 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1892 snapToPage(whichPage, duration, false, interpolator);
1893 }
1894
1895 protected void snapToPage(int whichPage, int duration, boolean immediate,
1896 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001897 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001898
Adam Cohenedb40762013-07-18 16:45:45 -07001899 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001900 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001901 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001902 }
1903
1904 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001905 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001906 }
Michael Jurka0142d492010-08-25 17:46:15 -07001907
Adam Cohenf9618852013-11-08 06:45:03 -08001908 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1909 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001910 whichPage = validateNewPage(whichPage);
1911
Adam Cohen7d30a372013-07-01 17:03:59 -07001912 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001913
Winson Chung321e9ee2010-08-09 13:37:56 -07001914 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001915 if (immediate) {
1916 duration = 0;
1917 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001918 duration = Math.abs(delta);
1919 }
1920
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001921 if (duration != 0) {
1922 pageBeginTransition();
1923 }
1924
Adam Cohenf358a4b2013-07-23 16:47:31 -07001925 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001926 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001927 }
Adam Cohenf9618852013-11-08 06:45:03 -08001928
1929 if (interpolator != null) {
1930 mScroller.setInterpolator(interpolator);
1931 } else {
1932 mScroller.setInterpolator(mDefaultInterpolator);
1933 }
1934
Sunny Goyalc86df472016-02-25 09:19:38 -08001935 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001936
Adam Cohen674531f2013-12-13 15:07:14 -08001937 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001938
1939 // Trigger a compute() to finish switching pages if necessary
1940 if (immediate) {
1941 computeScroll();
1942 }
1943
Winson Chung321e9ee2010-08-09 13:37:56 -07001944 invalidate();
1945 }
1946
Winson Chung321e9ee2010-08-09 13:37:56 -07001947 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001948 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001949 }
1950
1951 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001952 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 }
1954
Adam Cohendbdff6b2013-09-18 19:09:15 -07001955 @Override
1956 public boolean performLongClick() {
1957 mCancelTap = true;
1958 return super.performLongClick();
1959 }
1960
Winson Chung321e9ee2010-08-09 13:37:56 -07001961 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001962 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001963
1964 SavedState(Parcelable superState) {
1965 super(superState);
1966 }
1967
Adam Cohen091440a2015-03-18 14:16:05 -07001968 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001969 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001970 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001971 }
1972
1973 @Override
1974 public void writeToParcel(Parcel out, int flags) {
1975 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001976 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001977 }
1978
1979 public static final Parcelable.Creator<SavedState> CREATOR =
1980 new Parcelable.Creator<SavedState>() {
1981 public SavedState createFromParcel(Parcel in) {
1982 return new SavedState(in);
1983 }
1984
1985 public SavedState[] newArray(int size) {
1986 return new SavedState[size];
1987 }
1988 };
1989 }
1990
Adam Cohen7d30a372013-07-01 17:03:59 -07001991 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001992 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001993 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001994 Animator anim = new LauncherViewPropertyAnimator(mDragView)
1995 .translationX(0)
1996 .translationY(0)
1997 .scaleX(1)
1998 .scaleY(1)
1999 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002000 anim.addListener(new AnimatorListenerAdapter() {
2001 @Override
2002 public void onAnimationEnd(Animator animation) {
2003 onPostReorderingAnimationCompleted();
2004 }
2005 });
2006 anim.start();
2007 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002008 }
2009
Sunny Goyal1d08f702015-05-04 15:50:25 -07002010 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002011 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2012 mTouchState = TOUCH_STATE_REORDERING;
2013 mIsReordering = true;
2014
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 // We must invalidate to trigger a redraw to update the layers such that the drag view
2016 // is always drawn on top
2017 invalidate();
2018 }
2019
Adam Cohen091440a2015-03-18 14:16:05 -07002020 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002021 // Trigger the callback when reordering has settled
2022 --mPostReorderingPreZoomInRemainingAnimationCount;
2023 if (mPostReorderingPreZoomInRunnable != null &&
2024 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2025 mPostReorderingPreZoomInRunnable.run();
2026 mPostReorderingPreZoomInRunnable = null;
2027 }
2028 }
2029
Sunny Goyal1d08f702015-05-04 15:50:25 -07002030 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002031 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002032 }
2033
Adam Cohenf358a4b2013-07-23 16:47:31 -07002034 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002035 int dragViewIndex = indexOfChild(v);
2036
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002037 // Do not allow the first page to be moved around
2038 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002039
Adam Cohen7d30a372013-07-01 17:03:59 -07002040 mTempVisiblePagesRange[0] = 0;
2041 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002042 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002043 mReorderingStarted = true;
2044
2045 // Check if we are within the reordering range
2046 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002047 dragViewIndex <= mTempVisiblePagesRange[1]) {
2048 // Find the drag view under the pointer
2049 mDragView = getChildAt(dragViewIndex);
2050 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2051 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002052 snapToPage(getPageNearestToCenterOfScreen());
2053 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002054 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002055 return true;
2056 }
2057 return false;
2058 }
2059
2060 boolean isReordering(boolean testTouchState) {
2061 boolean state = mIsReordering;
2062 if (testTouchState) {
2063 state &= (mTouchState == TOUCH_STATE_REORDERING);
2064 }
2065 return state;
2066 }
2067 void endReordering() {
2068 // For simplicity, we call endReordering sometimes even if reordering was never started.
2069 // In that case, we don't want to do anything.
2070 if (!mReorderingStarted) return;
2071 mReorderingStarted = false;
2072
2073 // If we haven't flung-to-delete the current child, then we just animate the drag view
2074 // back into position
2075 final Runnable onCompleteRunnable = new Runnable() {
2076 @Override
2077 public void run() {
2078 onEndReordering();
2079 }
2080 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002081
2082 mPostReorderingPreZoomInRunnable = new Runnable() {
2083 public void run() {
2084 onCompleteRunnable.run();
2085 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002086 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002087 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002088
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002089 mPostReorderingPreZoomInRemainingAnimationCount =
2090 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2091 // Snap to the current page
2092 snapToPage(indexOfChild(mDragView), 0);
2093 // Animate the drag view back to the front position
2094 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002095 }
2096
Winson Chung6a0f57d2011-06-29 20:10:49 -07002097 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002098 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002099 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002100 @Override
2101 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2102 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002103 info.setScrollable(getPageCount() > 1);
2104 if (getCurrentPage() < getPageCount() - 1) {
2105 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2106 }
2107 if (getCurrentPage() > 0) {
2108 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2109 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002110 info.setClassName(getClass().getName());
2111
2112 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2113 // Besides disabling the accessibility long-click, this also prevents this view from getting
2114 // accessibility focus.
2115 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002116 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002117 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2118 }
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}