blob: e380e265adacb7032955537e59f2eb894e8bfd77 [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 Goyal8bf6f312016-01-23 14:40:35 -080031import android.graphics.RectF;
Sunny Goyal316490e2015-06-02 09:38:28 -070032import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070049import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
53import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070054import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070055import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070056
Winson Chung6a0f57d2011-06-29 20:10:49 -070057import java.util.ArrayList;
58
Winson Chung321e9ee2010-08-09 13:37:56 -070059/**
60 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070061 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070062 */
Michael Jurka8b805b12012-04-18 14:23:14 -070063public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070064 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070065 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070066 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Winson Chung86f77532010-08-24 11:08:22 -070068 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070069 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Vadim Tryshevfedca432015-08-19 17:55:02 -070071 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070072 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070073
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080075 // The page is moved more than halfway, automatically move to the next page on touch up.
76 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080077
Sunny Goyal8e2133b2015-05-06 13:39:07 -070078 private static final float MAX_SCROLL_PROGRESS = 1.0f;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 // The following constants need to be scaled based on density. The scaled versions will be
81 // assigned to the corresponding member variables below.
82 private static final int FLING_THRESHOLD_VELOCITY = 500;
83 private static final int MIN_SNAP_VELOCITY = 1500;
84 private static final int MIN_FLING_VELOCITY = 250;
85
Adam Cohen21cd0022013-10-09 18:57:02 -070086 public static final int INVALID_RESTORE_PAGE = -1001;
87
Adam Cohenf358a4b2013-07-23 16:47:31 -070088 private boolean mFreeScroll = false;
89 private int mFreeScrollMinScrollX = -1;
90 private int mFreeScrollMaxScrollX = -1;
91
Adam Cohen265b9a62011-12-07 14:37:18 -080092 protected int mFlingThresholdVelocity;
93 protected int mMinFlingVelocity;
94 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070097 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070098
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700101 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700102 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700103
Sunny Goyal4ffec482016-02-09 11:28:52 -0800104 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800106 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800107 protected LauncherScroller mScroller;
108 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mParentDownMotionX;
113 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700115 private float mDownMotionY;
116 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700117 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700118 private float mLastMotionX;
119 private float mLastMotionXRemainder;
120 private float mLastMotionY;
121 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700122 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700123
Adam Cohendbdff6b2013-09-18 19:09:15 -0700124 private boolean mCancelTap;
125
Adam Cohenedb40762013-07-18 16:45:45 -0700126 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka0142d492010-08-25 17:46:15 -0700128 protected final static int TOUCH_STATE_REST = 0;
129 protected final static int TOUCH_STATE_SCROLLING = 1;
130 protected final static int TOUCH_STATE_PREV_PAGE = 2;
131 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 protected final static int TOUCH_STATE_REORDERING = 4;
133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700135 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800136
Michael Jurka0142d492010-08-25 17:46:15 -0700137 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka7426c422010-11-11 15:23:47 -0800139 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800141 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800142 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka5f1c5092010-09-03 14:15:02 -0700144 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148 protected boolean mIsPageMoving = false;
149
Sunny Goyal061380a2016-02-29 15:15:03 -0800150 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700151
Winson Chungd2be3812013-07-16 11:11:32 -0700152 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700154 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700155 // The viewport whether the pages are to be contained (the actual view may be larger than the
156 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800157 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700158 private Rect mViewport = new Rect();
159
160 // Reordering
161 // We use the min scale to determine how much to expand the actually PagedView measured
162 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700163 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700164 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700165 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
166
Adam Cohen7d30a372013-07-01 17:03:59 -0700167 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700168 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700169 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 // This variable's scope is only for the duration of startReordering() and endReordering()
173 private boolean mReorderingStarted = false;
174 // This variable's scope is for the duration of startReordering() and after the zoomIn()
175 // animation after endReordering()
176 private boolean mIsReordering;
177 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700178 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700179 private int mPostReorderingPreZoomInRemainingAnimationCount;
180 private Runnable mPostReorderingPreZoomInRunnable;
181
Adam Cohen7d30a372013-07-01 17:03:59 -0700182 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700183 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700184 private static final float[] sTmpPoint = new float[2];
185 private static final int[] sTmpIntPoint = new int[2];
186 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800187 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700188
John Spurlock77e1f472013-09-11 10:09:51 -0400189 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700190 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400191
Sunny Goyal4d113a52015-05-27 10:05:28 -0700192 // Edge effect
193 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
194 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
195
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 public PagedView(Context context) {
197 this(context, null);
198 }
199
200 public PagedView(Context context, AttributeSet attrs) {
201 this(context, attrs, 0);
202 }
203
204 public PagedView(Context context, AttributeSet attrs, int defStyle) {
205 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700206
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 TypedArray a = context.obtainStyledAttributes(attrs,
208 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700209 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 a.recycle();
211
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700213 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700214 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 }
216
217 /**
218 * Initializes various states for this workspace.
219 */
Michael Jurka0142d492010-08-25 17:46:15 -0700220 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800221 mScroller = new LauncherScroller(getContext());
222 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700223 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700224
225 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700226 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800228
Sunny Goyalcf25b522015-07-09 00:01:18 -0700229 float density = getResources().getDisplayMetrics().density;
230 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
231 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
232 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700233 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700234 setWillNotDraw(false);
235 }
236
237 protected void setEdgeGlowColor(int color) {
238 mEdgeGlowLeft.setColor(color);
239 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 }
241
Adam Cohenf9618852013-11-08 06:45:03 -0800242 protected void setDefaultInterpolator(Interpolator interpolator) {
243 mDefaultInterpolator = interpolator;
244 mScroller.setInterpolator(mDefaultInterpolator);
245 }
246
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700247 public void initParentViews(View parent) {
248 if (mPageIndicatorViewId > -1) {
249 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700250 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700251 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700252 }
253 }
254
Adam Cohen7d30a372013-07-01 17:03:59 -0700255 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700256 private float[] mapPointFromViewToParent(View v, float x, float y) {
257 sTmpPoint[0] = x;
258 sTmpPoint[1] = y;
259 v.getMatrix().mapPoints(sTmpPoint);
260 sTmpPoint[0] += v.getLeft();
261 sTmpPoint[1] += v.getTop();
262 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700263 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700264 private float[] mapPointFromParentToView(View v, float x, float y) {
265 sTmpPoint[0] = x - v.getLeft();
266 sTmpPoint[1] = y - v.getTop();
267 v.getMatrix().invert(sTmpInvMatrix);
268 sTmpInvMatrix.mapPoints(sTmpPoint);
269 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700270 }
271
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700272 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273 if (mDragView != null) {
274 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
275 (mDragViewBaselineLeft - mDragView.getLeft());
276 float y = mLastMotionY - mDownMotionY;
277 mDragView.setTranslationX(x);
278 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700279
Adam Cohenf358a4b2013-07-23 16:47:31 -0700280 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
281 + x + ", " + y);
282 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700283 }
284
285 public void setMinScale(float f) {
286 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700287 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700288 requestLayout();
289 }
290
291 @Override
292 public void setScaleX(float scaleX) {
293 super.setScaleX(scaleX);
294 if (isReordering(true)) {
295 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
296 mLastMotionX = p[0];
297 mLastMotionY = p[1];
298 updateDragViewTranslationDuringDrag();
299 }
300 }
301
302 // Convenience methods to get the actual width/height of the PagedView (since it is measured
303 // to be larger to account for the minimum possible scale)
304 int getViewportWidth() {
305 return mViewport.width();
306 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800307 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700308 return mViewport.height();
309 }
310
311 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
312 // PagedView both horizontally and vertically
313 int getViewportOffsetX() {
314 return (getMeasuredWidth() - getViewportWidth()) / 2;
315 }
316
317 int getViewportOffsetY() {
318 return (getMeasuredHeight() - getViewportHeight()) / 2;
319 }
320
Hyunyoung Song77441692016-06-27 22:00:48 -0700321 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700322 return mPageIndicator;
323 }
324
Adam Cohen674531f2013-12-13 15:07:14 -0800325 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700326 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
327 * that the user was on before entering free scroll mode (e.g. the home screen page they
328 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
329 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700331 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700332 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700334
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700335 /**
336 * Returns the index of page to be shown immediately afterwards.
337 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700338 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700339 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
340 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700341
Adam Cohenf9c184a2016-01-15 16:47:43 -0800342 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 return getChildCount();
344 }
345
Sunny Goyal83a8f042015-05-19 12:52:12 -0700346 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 return getChildAt(index);
348 }
349
Adam Cohenae4f1552011-10-20 00:15:42 -0700350 protected int indexToPage(int index) {
351 return index;
352 }
353
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800355 * Updates the scroll of the current page immediately to its final scroll position. We use this
356 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
357 * the previous tab page.
358 */
359 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700360 // If the current page is invalid, just reset the scroll position to zero
361 int newX = 0;
362 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700363 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700364 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800365 scrollTo(newX, 0);
366 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700367 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800368 }
369
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700370 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700371 mScroller.abortAnimation();
372 // We need to clean up the next page here to avoid computeScrollHelper from
373 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700374 if (resetNextPage) {
375 mNextPage = INVALID_PAGE;
376 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700377 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700378
Tony Wickham8f7ead32016-04-07 18:46:44 -0700379 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700380 mScroller.forceFinished(true);
381 // We need to clean up the next page here to avoid computeScrollHelper from
382 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700383 if (resetNextPage) {
384 mNextPage = INVALID_PAGE;
385 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700386 }
387
Adam Cohen6f127a62014-06-12 14:54:41 -0700388 private int validateNewPage(int newPage) {
389 int validatedPage = newPage;
390 // When in free scroll mode, we need to clamp to the free scroll page range.
391 if (mFreeScroll) {
392 getFreeScrollPageRange(mTempVisiblePagesRange);
393 validatedPage = Math.max(mTempVisiblePagesRange[0],
394 Math.min(newPage, mTempVisiblePagesRange[1]));
395 }
396 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700397 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700398 return validatedPage;
399 }
400
Chet Haasebc2f0822012-10-26 17:59:53 -0700401 /**
Winson Chung86f77532010-08-24 11:08:22 -0700402 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700404 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800405 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700406 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800407 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800408 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
409 // the default
410 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800411 return;
412 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700413 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700414 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700415 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700416 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800417 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 }
419
Winson Chung8c87cd82013-07-23 16:20:10 -0700420 /**
421 * The restore page will be set in place of the current page at the next (likely first)
422 * layout.
423 */
424 void setRestorePage(int restorePage) {
425 mRestorePage = restorePage;
426 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800427 int getRestorePage() {
428 return mRestorePage;
429 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700430
Adam Cohen674531f2013-12-13 15:07:14 -0800431 /**
432 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
433 * has settled.
434 */
Michael Jurka0142d492010-08-25 17:46:15 -0700435 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800436 updatePageIndicator();
437 }
438
439 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700440 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700441 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700442 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700443 if (!isReordering(false)) {
444 mPageIndicator.setActiveMarker(getNextPage());
445 }
Winson Chungd2be3812013-07-16 11:11:32 -0700446 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800448 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700449 if (!mIsPageMoving) {
450 mIsPageMoving = true;
451 onPageBeginMoving();
452 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700453 }
454
Michael Jurkace7e05f2011-02-01 22:02:35 -0800455 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700456 if (mIsPageMoving) {
457 mIsPageMoving = false;
458 onPageEndMoving();
459 }
Michael Jurka0142d492010-08-25 17:46:15 -0700460 }
461
Adam Cohen26976d92011-03-22 15:33:33 -0700462 protected boolean isPageMoving() {
463 return mIsPageMoving;
464 }
465
Michael Jurka0142d492010-08-25 17:46:15 -0700466 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700467 protected void onPageBeginMoving() {
468 }
469
470 // a method that subclasses can override to add behavior
471 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700472 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700473 }
474
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 /**
Winson Chung86f77532010-08-24 11:08:22 -0700476 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 *
478 * @param l The listener used to respond to long clicks.
479 */
480 @Override
481 public void setOnLongClickListener(OnLongClickListener l) {
482 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700483 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700485 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
Adam Cohen1697b792013-09-17 19:08:21 -0700487 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 }
489
Sunny Goyalc86df472016-02-25 09:19:38 -0800490 protected int getUnboundedScrollX() {
491 return getScrollX();
492 }
493
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800495 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800496 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800497 }
498
499 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700500 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700501 // In free scroll mode, we clamp the scrollX
502 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700503 // If the scroller is trying to move to a location beyond the maximum allowed
504 // in the free scroll mode, we make sure to end the scroll operation.
505 if (!mScroller.isFinished() &&
506 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700507 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700508 }
509
Adam Cohenf358a4b2013-07-23 16:47:31 -0700510 x = Math.min(x, mFreeScrollMaxScrollX);
511 x = Math.max(x, mFreeScrollMinScrollX);
512 }
513
Sunny Goyal70660032015-05-14 00:07:08 -0700514 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
515 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700516 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700517 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800518 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700519 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700520 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700521 overScroll(x - mMaxScrollX);
522 } else {
523 overScroll(x);
524 }
Adam Cohen68d73932010-11-15 10:50:58 -0800525 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700526 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700527 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800528 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700529 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700530 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700531 overScroll(x);
532 } else {
533 overScroll(x - mMaxScrollX);
534 }
Adam Cohen68d73932010-11-15 10:50:58 -0800535 }
536 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700537 if (mWasInOverscroll) {
538 overScroll(0);
539 mWasInOverscroll = false;
540 }
Adam Cohen68d73932010-11-15 10:50:58 -0800541 super.scrollTo(x, y);
542 }
543
Adam Cohen7d30a372013-07-01 17:03:59 -0700544 // Update the last motion events when scrolling
545 if (isReordering(true)) {
546 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
547 mLastMotionX = p[0];
548 mLastMotionY = p[1];
549 updateDragViewTranslationDuringDrag();
550 }
Michael Jurka0142d492010-08-25 17:46:15 -0700551 }
552
Adam Cohen53805212013-10-01 10:39:23 -0700553 private void sendScrollAccessibilityEvent() {
554 AccessibilityManager am =
555 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
556 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700557 if (mCurrentPage != getNextPage()) {
558 AccessibilityEvent ev =
559 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700560 ev.setScrollable(true);
561 ev.setScrollX(getScrollX());
562 ev.setScrollY(getScrollY());
563 ev.setMaxScrollX(mMaxScrollX);
564 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700565
Vadim Tryshevf4715972015-05-08 17:21:03 -0700566 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700567 }
Adam Cohen53805212013-10-01 10:39:23 -0700568 }
569 }
570
Michael Jurka0142d492010-08-25 17:46:15 -0700571 // we moved this functionality to a helper function so SmoothPagedView can reuse it
572 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800573 return computeScrollHelper(true);
574 }
575
576 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700578 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800580 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700581 float scaleX = mFreeScroll ? getScaleX() : 1f;
582 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
583 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700584 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800585 if (shouldInvalidate) {
586 invalidate();
587 }
Michael Jurka0142d492010-08-25 17:46:15 -0700588 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800589 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700590 sendScrollAccessibilityEvent();
591
Adam Cohen6f127a62014-06-12 14:54:41 -0700592 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700593 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700594 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700595
Adam Cohen73aa9752010-11-24 16:26:58 -0800596 // We don't want to trigger a page end moving unless the page has settled
597 // and the user has stopped scrolling
598 if (mTouchState == TOUCH_STATE_REST) {
599 pageEndMoving();
600 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700601
Adam Cohen7d30a372013-07-01 17:03:59 -0700602 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700603 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700604 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700605 if (am.isEnabled()) {
606 // Notify the user when the page changes
607 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700608 }
Michael Jurka0142d492010-08-25 17:46:15 -0700609 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
Michael Jurka0142d492010-08-25 17:46:15 -0700611 return false;
612 }
613
614 @Override
615 public void computeScroll() {
616 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 }
618
Adam Cohen96d30a12013-07-16 18:13:21 -0700619 public static class LayoutParams extends ViewGroup.LayoutParams {
620 public boolean isFullScreenPage = false;
621
Sunny Goyal7c786f72016-06-01 14:08:21 -0700622 // If true, the start edge of the page snaps to the start edge of the viewport.
623 public boolean matchStartEdge = false;
624
Adam Cohen96d30a12013-07-16 18:13:21 -0700625 /**
626 * {@inheritDoc}
627 */
628 public LayoutParams(int width, int height) {
629 super(width, height);
630 }
631
Sunny Goyal41b22c02015-05-14 15:20:48 -0700632 public LayoutParams(Context context, AttributeSet attrs) {
633 super(context, attrs);
634 }
635
Adam Cohen96d30a12013-07-16 18:13:21 -0700636 public LayoutParams(ViewGroup.LayoutParams source) {
637 super(source);
638 }
639 }
640
Sunny Goyal41b22c02015-05-14 15:20:48 -0700641 @Override
642 public LayoutParams generateLayoutParams(AttributeSet attrs) {
643 return new LayoutParams(getContext(), attrs);
644 }
645
646 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700647 protected LayoutParams generateDefaultLayoutParams() {
648 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
649 }
650
Sunny Goyal41b22c02015-05-14 15:20:48 -0700651 @Override
652 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
653 return new LayoutParams(p);
654 }
655
656 @Override
657 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
658 return p instanceof LayoutParams;
659 }
660
Adam Cohenedb40762013-07-18 16:45:45 -0700661 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700662 LayoutParams lp = generateDefaultLayoutParams();
663 lp.isFullScreenPage = true;
664 super.addView(page, 0, lp);
665 }
666
Adam Cohen410f3cd2013-09-22 12:09:32 -0700667 public int getNormalChildHeight() {
668 return mNormalChildHeight;
669 }
670
Winson Chung321e9ee2010-08-09 13:37:56 -0700671 @Override
672 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700673 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700674 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
675 return;
676 }
677
Adam Cohen7d30a372013-07-01 17:03:59 -0700678 // We measure the dimensions of the PagedView to be larger than the pages so that when we
679 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700680 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
681 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
682 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700683 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800684 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700685 // viewport, we can be at most one and a half screens offset once we scale down
686 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700687 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
688 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700689
Winson Chungc82d2622013-11-06 13:23:29 -0800690 int parentWidthSize = (int) (2f * maxSize);
691 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700692 int scaledWidthSize, scaledHeightSize;
693 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700694 scaledWidthSize = (int) (parentWidthSize / mMinScale);
695 scaledHeightSize = (int) (parentHeightSize / mMinScale);
696 } else {
697 scaledWidthSize = widthSize;
698 scaledHeightSize = heightSize;
699 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700700 mViewport.set(0, 0, widthSize, heightSize);
701
702 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
703 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
704 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 }
706
Winson Chung8aad6102012-05-11 16:27:49 -0700707 // Return early if we aren't given a proper dimension
708 if (widthSize <= 0 || heightSize <= 0) {
709 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
710 return;
711 }
712
Adam Lesinski6b879f02010-11-04 16:15:23 -0700713 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
714 * of the All apps view on XLarge displays to not take up more space then it needs. Width
715 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
716 * each page to have the same width.
717 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 final int verticalPadding = getPaddingTop() + getPaddingBottom();
719 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700720
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700721 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700723 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700725 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
726 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
727 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
728 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 final int childCount = getChildCount();
730 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700731 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700732 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100733 if (child.getVisibility() != GONE) {
734 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700735
Vladimir Marko2824b072013-10-04 16:42:17 +0100736 int childWidthMode;
737 int childHeightMode;
738 int childWidth;
739 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700740
Vladimir Marko2824b072013-10-04 16:42:17 +0100741 if (!lp.isFullScreenPage) {
742 if (lp.width == LayoutParams.WRAP_CONTENT) {
743 childWidthMode = MeasureSpec.AT_MOST;
744 } else {
745 childWidthMode = MeasureSpec.EXACTLY;
746 }
747
748 if (lp.height == LayoutParams.WRAP_CONTENT) {
749 childHeightMode = MeasureSpec.AT_MOST;
750 } else {
751 childHeightMode = MeasureSpec.EXACTLY;
752 }
753
Adam Cohen3b185e22013-10-29 14:45:58 -0700754 childWidth = getViewportWidth() - horizontalPadding
755 - mInsets.left - mInsets.right;
756 childHeight = getViewportHeight() - verticalPadding
757 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100758 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700759 } else {
760 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700761 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100762
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800763 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700764 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700765 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700766 if (referenceChildWidth == 0) {
767 referenceChildWidth = childWidth;
768 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700769
Vladimir Marko2824b072013-10-04 16:42:17 +0100770 final int childWidthMeasureSpec =
771 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
772 final int childHeightMeasureSpec =
773 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
774 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700775 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700776 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700777 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800778 }
779
Sunny Goyalcf25b522015-07-09 00:01:18 -0700780 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700782 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700783 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700784 return;
785 }
786
Winson Chung785d2eb2011-04-14 16:08:02 -0700787 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700789
Adam Cohen7d30a372013-07-01 17:03:59 -0700790 int offsetX = getViewportOffsetX();
791 int offsetY = getViewportOffsetY();
792
793 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700794 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700795
Sunny Goyal70660032015-05-14 00:07:08 -0700796 final int startIndex = mIsRtl ? childCount - 1 : 0;
797 final int endIndex = mIsRtl ? -1 : childCount;
798 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700799
Adam Cohen7d30a372013-07-01 17:03:59 -0700800 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700801
Adam Cohen3b185e22013-10-29 14:45:58 -0700802 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000803 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700804
Sunny Goyal6178f132016-07-11 17:30:03 -0700805 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700806 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
807 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700808 }
809
Adam Cohen0ffac432013-07-10 11:19:26 -0700810 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700811 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700813 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100814 int childTop;
815 if (lp.isFullScreenPage) {
816 childTop = offsetY;
817 } else {
818 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700819 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100820 }
821
Adam Cohen96d30a12013-07-16 18:13:21 -0700822 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700823 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800824
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700826 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800827 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700828
Sunny Goyal6178f132016-07-11 17:30:03 -0700829 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700830 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000831
832 int pageGap = mPageSpacing;
833 int next = i + delta;
834 if (next != endIndex) {
835 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
836 } else {
837 nextLp = null;
838 }
839
840 // Prevent full screen pages from showing in the viewport
841 // when they are not the current page.
842 if (lp.isFullScreenPage) {
843 pageGap = getPaddingLeft();
844 } else if (nextLp != null && nextLp.isFullScreenPage) {
845 pageGap = getPaddingRight();
846 }
847
Sunny Goyala1fbd842015-05-20 13:40:27 -0700848 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 }
850 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700851
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700852 final LayoutTransition transition = getLayoutTransition();
853 // If the transition is running defer updating max scroll, as some empty pages could
854 // still be present, and a max scroll change could cause sudden jumps in scroll.
855 if (transition != null && transition.isRunning()) {
856 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
857
858 @Override
859 public void startTransition(LayoutTransition transition, ViewGroup container,
860 View view, int transitionType) { }
861
862 @Override
863 public void endTransition(LayoutTransition transition, ViewGroup container,
864 View view, int transitionType) {
865 // Wait until all transitions are complete.
866 if (!transition.isRunning()) {
867 transition.removeTransitionListener(this);
868 updateMaxScrollX();
869 }
870 }
871 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700872 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700873 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700874 }
875
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700876 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
877 updateCurrentPageScroll();
878 mFirstLayout = false;
879 }
880
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700881 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700882 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700883 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700884 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700885 } else {
886 setCurrentPage(getNextPage());
887 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700888 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700889 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700890
891 if (isReordering(true)) {
892 updateDragViewTranslationDuringDrag();
893 }
Winson Chunge3193b92010-09-10 11:44:42 -0700894 }
895
Sunny Goyala1fbd842015-05-20 13:40:27 -0700896 protected int getChildGap() {
897 return 0;
898 }
899
Sunny Goyal316490e2015-06-02 09:38:28 -0700900 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700901 mMaxScrollX = computeMaxScrollX();
902 }
903
904 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700905 int childCount = getChildCount();
906 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700907 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700908 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700909 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700910 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700911 }
912 }
913
Adam Cohen3b185e22013-10-29 14:45:58 -0700914 public void setPageSpacing(int pageSpacing) {
915 mPageSpacing = pageSpacing;
916 requestLayout();
917 }
918
Sunny Goyal4d113a52015-05-27 10:05:28 -0700919 /**
920 * Called when the center screen changes during scrolling.
921 */
922 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700923
Winson Chunge3193b92010-09-10 11:44:42 -0700924 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700925 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700926 // Update the page indicator, we don't update the page indicator as we
927 // add/remove pages
928 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700929 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700930 }
931
Adam Cohen2591f6a2011-10-25 14:36:40 -0700932 // This ensures that when children are added, they get the correct transforms / alphas
933 // in accordance with any scroll effects.
934 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700935 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700936 invalidate();
937 }
938
Michael Jurka8b805b12012-04-18 14:23:14 -0700939 @Override
940 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700941 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700942 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700943 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700944 }
945
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700946 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700947 // Update the page indicator, we don't update the page indicator as we
948 // add/remove pages
949 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700950 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700951 }
952 }
953
954 @Override
955 public void removeView(View v) {
956 // XXX: We should find a better way to hook into this before the view
957 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700958 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700959 super.removeView(v);
960 }
961 @Override
962 public void removeViewInLayout(View v) {
963 // XXX: We should find a better way to hook into this before the view
964 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700965 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700966 super.removeViewInLayout(v);
967 }
968 @Override
969 public void removeViewAt(int index) {
970 // XXX: We should find a better way to hook into this before the view
971 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700972 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700973 super.removeViewAt(index);
974 }
975 @Override
976 public void removeAllViewsInLayout() {
977 // Update the page indicator, we don't update the page indicator as we
978 // add/remove pages
979 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700980 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700981 }
982
983 super.removeAllViewsInLayout();
984 }
985
Adam Cohen73894962011-10-31 13:17:17 -0700986 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700987 if (index < 0 || index > getChildCount() - 1) return 0;
988
Adam Cohenf698c6e2013-07-17 18:44:25 -0700989 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700990
Adam Cohenf698c6e2013-07-17 18:44:25 -0700991 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700992 }
993
Adam Cohen6f127a62014-06-12 14:54:41 -0700994 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700995 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700996 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700997 }
998
Michael Jurkadde558b2011-11-09 22:09:06 -0800999 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001000 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001001 range[0] = -1;
1002 range[1] = -1;
1003
Sunny Goyalee688162016-02-12 14:24:21 -08001004 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001005 final int visibleLeft = -getLeft();
1006 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001007 final Matrix pageShiftMatrix = getPageShiftMatrix();
1008 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001009
Sunny Goyalee688162016-02-12 14:24:21 -08001010 for (int i = 0; i < count; i++) {
1011 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001012
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001013 // Verify if the page bounds are within the visible range.
1014 sTmpRectF.left = 0;
1015 sTmpRectF.right = currPage.getMeasuredWidth();
1016 currPage.getMatrix().mapRect(sTmpRectF);
1017 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001018 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001019
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001020 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001021 if (range[0] == -1) {
1022 continue;
1023 } else {
1024 break;
1025 }
1026 }
Sunny Goyalee688162016-02-12 14:24:21 -08001027 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001028 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001029 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001030 }
1031 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001032
Sunny Goyalee688162016-02-12 14:24:21 -08001033 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001034 } else {
1035 range[0] = -1;
1036 range[1] = -1;
1037 }
1038 }
1039
Sunny Goyalee688162016-02-12 14:24:21 -08001040 protected Matrix getPageShiftMatrix() {
1041 return getMatrix();
1042 }
1043
Michael Jurka920d7f42012-05-14 16:29:55 -07001044 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001045 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001046 }
1047
Michael Jurkadde558b2011-11-09 22:09:06 -08001048 @Override
1049 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001050 // Find out which screens are visible; as an optimization we only call draw on them
1051 final int pageCount = getChildCount();
1052 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001053 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001054 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001055
1056 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1057 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1058 // set it for the next frame
1059 mForceScreenScrolled = false;
1060 screenScrolled(screenCenter);
1061 mLastScreenCenter = screenCenter;
1062 }
1063
Michael Jurkadde558b2011-11-09 22:09:06 -08001064 getVisiblePages(mTempVisiblePagesRange);
1065 final int leftScreen = mTempVisiblePagesRange[0];
1066 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001067 if (leftScreen != -1 && rightScreen != -1) {
1068 final long drawingTime = getDrawingTime();
1069 // Clip to the bounds
1070 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001071 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1072 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001073
Adam Cohen7d30a372013-07-01 17:03:59 -07001074 // Draw all the children, leaving the drag view for last
1075 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001076 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001077 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001078 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001079 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001080 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001081 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001082 // Draw the drag view on top (if there is one)
1083 if (mDragView != null) {
1084 drawChild(canvas, mDragView, drawingTime);
1085 }
1086
Winson Chungc6f10b92011-11-14 11:39:07 -08001087 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001088 }
Michael Jurka0142d492010-08-25 17:46:15 -07001089 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001090 }
1091
1092 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001093 public void draw(Canvas canvas) {
1094 super.draw(canvas);
1095 if (getPageCount() > 0) {
1096 if (!mEdgeGlowLeft.isFinished()) {
1097 final int restoreCount = canvas.save();
1098 Rect display = mViewport;
1099 canvas.translate(display.left, display.top);
1100 canvas.rotate(270);
1101
1102 getEdgeVerticalPostion(sTmpIntPoint);
1103 canvas.translate(display.top - sTmpIntPoint[1], 0);
1104 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1105 if (mEdgeGlowLeft.draw(canvas)) {
1106 postInvalidateOnAnimation();
1107 }
1108 canvas.restoreToCount(restoreCount);
1109 }
1110 if (!mEdgeGlowRight.isFinished()) {
1111 final int restoreCount = canvas.save();
1112 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001113 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001114 canvas.rotate(90);
1115
1116 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001117
Sunny Goyal93264612015-11-23 11:47:50 -08001118 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1119 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001120 if (mEdgeGlowRight.draw(canvas)) {
1121 postInvalidateOnAnimation();
1122 }
1123 canvas.restoreToCount(restoreCount);
1124 }
1125 }
1126 }
1127
1128 /**
1129 * Returns the top and bottom position for the edge effect.
1130 */
1131 protected abstract void getEdgeVerticalPostion(int[] pos);
1132
1133 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001134 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001135 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001136 if (page != mCurrentPage || !mScroller.isFinished()) {
1137 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001138 return true;
1139 }
1140 return false;
1141 }
1142
1143 @Override
1144 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001145 int focusablePage;
1146 if (mNextPage != INVALID_PAGE) {
1147 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001149 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 }
Winson Chung86f77532010-08-24 11:08:22 -07001151 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001153 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001154 }
1155 return false;
1156 }
1157
1158 @Override
1159 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001160 if (super.dispatchUnhandledMove(focused, direction)) {
1161 return true;
1162 }
1163
1164 if (mIsRtl) {
1165 if (direction == View.FOCUS_LEFT) {
1166 direction = View.FOCUS_RIGHT;
1167 } else if (direction == View.FOCUS_RIGHT) {
1168 direction = View.FOCUS_LEFT;
1169 }
1170 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001172 if (getCurrentPage() > 0) {
1173 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 return true;
1175 }
1176 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001177 if (getCurrentPage() < getPageCount() - 1) {
1178 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 return true;
1180 }
1181 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001182 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 }
1184
1185 @Override
1186 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001187 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001188 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001189 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 }
1191 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001192 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001193 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 }
1195 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001196 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001197 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001198 }
1199 }
1200 }
1201
1202 /**
1203 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001204 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 *
Winson Chung86f77532010-08-24 11:08:22 -07001206 * This happens when live folders requery, and if they're off page, they
1207 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 */
1209 @Override
1210 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001211 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 View v = focused;
1213 while (true) {
1214 if (v == current) {
1215 super.focusableViewAvailable(focused);
1216 return;
1217 }
1218 if (v == this) {
1219 return;
1220 }
1221 ViewParent parent = v.getParent();
1222 if (parent instanceof View) {
1223 v = (View)v.getParent();
1224 } else {
1225 return;
1226 }
1227 }
1228 }
1229
1230 /**
1231 * {@inheritDoc}
1232 */
1233 @Override
1234 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1235 if (disallowIntercept) {
1236 // We need to make sure to cancel our long press if
1237 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001238 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001239 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 }
1241 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1242 }
1243
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001244 /**
1245 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1246 */
1247 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001248 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001249 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001250 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001251 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001252 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001253 }
1254
1255 /**
1256 * Return true if a tap at (x, y) should trigger a flip to the next page.
1257 */
1258 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001259 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001260 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001261 }
1262 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001263 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001264 }
Winson Chung52aee602013-01-30 12:01:02 -08001265
Adam Cohen7d30a372013-07-01 17:03:59 -07001266 /** Returns whether x and y originated within the buffered viewport */
1267 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001268 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001269 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001270 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001271 }
1272
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 @Override
1274 public boolean onInterceptTouchEvent(MotionEvent ev) {
1275 /*
1276 * This method JUST determines whether we want to intercept the motion.
1277 * If we return true, onTouchEvent will be called and we do the actual
1278 * scrolling there.
1279 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001280 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281
Winson Chung45e1d6e2010-11-09 17:19:49 -08001282 // Skip touch handling if there are no pages to swipe
1283 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1284
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 /*
1286 * Shortcut the most recurring case: the user is in the dragging
1287 * state and he is moving his finger. We want to intercept this
1288 * motion.
1289 */
1290 final int action = ev.getAction();
1291 if ((action == MotionEvent.ACTION_MOVE) &&
1292 (mTouchState == TOUCH_STATE_SCROLLING)) {
1293 return true;
1294 }
1295
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 switch (action & MotionEvent.ACTION_MASK) {
1297 case MotionEvent.ACTION_MOVE: {
1298 /*
1299 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1300 * whether the user has moved far enough from his original down touch.
1301 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001302 if (mActivePointerId != INVALID_POINTER) {
1303 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001304 }
1305 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1306 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1307 // i.e. fall through to the next case (don't break)
1308 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1309 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001310 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 }
1312
1313 case MotionEvent.ACTION_DOWN: {
1314 final float x = ev.getX();
1315 final float y = ev.getY();
1316 // Remember location of down touch
1317 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001318 mDownMotionY = y;
1319 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 mLastMotionX = x;
1321 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 float[] p = mapPointFromViewToParent(this, x, y);
1323 mParentDownMotionX = p[0];
1324 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001325 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001326 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001328
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 /*
1330 * If being flinged and user touches the screen, initiate drag;
1331 * otherwise don't. mScroller.isFinished should be false when
1332 * being flinged.
1333 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001334 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001335 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001336
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001337 if (finishedScrolling) {
1338 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001339 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001340 setCurrentPage(getNextPage());
1341 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001342 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001343 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1345 mTouchState = TOUCH_STATE_SCROLLING;
1346 } else {
1347 mTouchState = TOUCH_STATE_REST;
1348 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001349 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001350
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 break;
1352 }
1353
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001355 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001356 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 break;
1358
1359 case MotionEvent.ACTION_POINTER_UP:
1360 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001361 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 break;
1363 }
1364
1365 /*
1366 * The only time we want to intercept motion events is if we are in the
1367 * drag mode.
1368 */
1369 return mTouchState != TOUCH_STATE_REST;
1370 }
1371
Adam Cohenf8d28232011-02-01 21:47:00 -08001372 protected void determineScrollingStart(MotionEvent ev) {
1373 determineScrollingStart(ev, 1.0f);
1374 }
1375
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 /*
1377 * Determines if we should change the touch state to start scrolling after the
1378 * user moves their touch point too far.
1379 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001380 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001381 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001383 if (pointerIndex == -1) return;
1384
1385 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 final float x = ev.getX(pointerIndex);
1387 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001388 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1389
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001391
Adam Cohenf8d28232011-02-01 21:47:00 -08001392 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001394
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001395 if (xMoved) {
1396 // Scroll if the user moved far enough along the X axis
1397 mTouchState = TOUCH_STATE_SCROLLING;
1398 mTotalMotionX += Math.abs(mLastMotionX - x);
1399 mLastMotionX = x;
1400 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001401 onScrollInteractionBegin();
1402 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001403 // Stop listening for things like pinches.
1404 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001405 }
1406 }
1407
1408 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001409 // Try canceling the long press. It could also have been scheduled
1410 // by a distant descendant, so use the mAllowLongPress flag to block
1411 // everything
1412 final View currentPage = getPageAt(mCurrentPage);
1413 if (currentPage != null) {
1414 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 }
1416 }
1417
Adam Cohenb5ba0972011-09-07 18:02:31 -07001418 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001419 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001420
Adam Cohenedb40762013-07-18 16:45:45 -07001421 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001422 int count = getChildCount();
1423
1424 final int totalDistance;
1425
1426 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001427 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001428 adjacentPage = page - 1;
1429 }
1430
1431 if (adjacentPage < 0 || adjacentPage > count - 1) {
1432 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1433 } else {
1434 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1435 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001436
1437 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001438 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1439 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001440 return scrollProgress;
1441 }
1442
Adam Cohenedb40762013-07-18 16:45:45 -07001443 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001444 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001445 return 0;
1446 } else {
1447 return mPageScrolls[index];
1448 }
1449 }
1450
Adam Cohen564a2e72013-10-09 14:47:32 -07001451 // While layout transitions are occurring, a child's position may stray from its baseline
1452 // position. This method returns the magnitude of this stray at any given time.
1453 public int getLayoutTransitionOffsetForPage(int index) {
1454 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1455 return 0;
1456 } else {
1457 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001458
1459 int scrollOffset = 0;
1460 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1461 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001462 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001463 }
1464
Adam Cohen564a2e72013-10-09 14:47:32 -07001465 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1466 return (int) (child.getX() - baselineX);
1467 }
1468 }
1469
Adam Cohenb5ba0972011-09-07 18:02:31 -07001470 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001471 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001472 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001473 if (f < 0) {
1474 mEdgeGlowLeft.onPull(-f);
1475 } else if (f > 0) {
1476 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001477 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001478 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001479 }
1480 invalidate();
1481 }
1482
Adam Cohenb5ba0972011-09-07 18:02:31 -07001483 protected void overScroll(float amount) {
1484 dampedOverScroll(amount);
1485 }
1486
Winson Chungdc61c4d2015-04-20 18:26:57 -07001487 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001488 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001489 }
1490
Winson Chungdc61c4d2015-04-20 18:26:57 -07001491 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001492 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001493 }
1494
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001495 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001496 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001497 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001498 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1499 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1500 } else {
1501 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1502 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1503 }
1504 }
1505
Adam Cohenf9618852013-11-08 06:45:03 -08001506 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001507 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001508 mFreeScroll = freeScroll;
1509
Adam Cohenf9618852013-11-08 06:45:03 -08001510 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001511 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001512 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001513 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1514 setCurrentPage(mTempVisiblePagesRange[0]);
1515 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1516 setCurrentPage(mTempVisiblePagesRange[1]);
1517 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001518 } else if (wasFreeScroll) {
1519 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001520 }
1521
1522 setEnableOverscroll(!freeScroll);
1523 }
1524
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001525 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001526 mAllowOverScroll = enable;
1527 }
1528
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001529 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001530 if (mDragView != null) {
1531 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1532 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001533 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001534 int minDistance = Integer.MAX_VALUE;
1535 int minIndex = indexOfChild(mDragView);
1536 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1537 View page = getPageAt(i);
1538 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1539 int d = Math.abs(dragX - pageX);
1540 if (d < minDistance) {
1541 minIndex = i;
1542 minDistance = d;
1543 }
1544 }
1545 return minIndex;
1546 }
1547 return -1;
1548 }
1549
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 @Override
1551 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001552 super.onTouchEvent(ev);
1553
Winson Chung45e1d6e2010-11-09 17:19:49 -08001554 // Skip touch handling if there are no pages to swipe
1555 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1556
Michael Jurkab8f06722010-10-10 15:58:46 -07001557 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001558
1559 final int action = ev.getAction();
1560
1561 switch (action & MotionEvent.ACTION_MASK) {
1562 case MotionEvent.ACTION_DOWN:
1563 /*
1564 * If being flinged and user touches, stop the fling. isFinished
1565 * will be false if being flinged.
1566 */
1567 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001568 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 }
1570
1571 // Remember where the motion event started
1572 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001573 mDownMotionY = mLastMotionY = ev.getY();
1574 mDownScrollX = getScrollX();
1575 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1576 mParentDownMotionX = p[0];
1577 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001578 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001579 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001580 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001581
Michael Jurka0142d492010-08-25 17:46:15 -07001582 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001583 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001584 pageBeginMoving();
1585 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001586 break;
1587
1588 case MotionEvent.ACTION_MOVE:
1589 if (mTouchState == TOUCH_STATE_SCROLLING) {
1590 // Scroll to follow the motion event
1591 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001592
1593 if (pointerIndex == -1) return true;
1594
Winson Chung321e9ee2010-08-09 13:37:56 -07001595 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001596 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001597
Adam Cohenaefd4e12011-02-14 16:39:38 -08001598 mTotalMotionX += Math.abs(deltaX);
1599
Winson Chungc0844aa2011-02-02 15:25:58 -08001600 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1601 // keep the remainder because we are actually testing if we've moved from the last
1602 // scrolled position (which is discrete).
1603 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001604 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001605 mLastMotionX = x;
1606 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 } else {
1608 awakenScrollBars();
1609 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001610 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1611 // Update the last motion position
1612 mLastMotionX = ev.getX();
1613 mLastMotionY = ev.getY();
1614
1615 // Update the parent down so that our zoom animations take this new movement into
1616 // account
1617 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1618 mParentDownMotionX = pt[0];
1619 mParentDownMotionY = pt[1];
1620 updateDragViewTranslationDuringDrag();
1621
1622 // Find the closest page to the touch point
1623 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001624
Adam Cohen7d30a372013-07-01 17:03:59 -07001625 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1626 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1627 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1628 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1629
Adam Cohenf358a4b2013-07-23 16:47:31 -07001630 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001631 // Do not allow any page to be moved to 0th position.
1632 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001633 mTempVisiblePagesRange[0] = 0;
1634 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001635 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001636 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1637 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1638 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1639 mSidePageHoverIndex = pageUnderPointIndex;
1640 mSidePageHoverRunnable = new Runnable() {
1641 @Override
1642 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001643 // Setup the scroll to the correct page before we swap the views
1644 snapToPage(pageUnderPointIndex);
1645
1646 // For each of the pages between the paged view and the drag view,
1647 // animate them from the previous position to the new position in
1648 // the layout (as a result of the drag view moving in the layout)
1649 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1650 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1651 dragViewIndex + 1 : pageUnderPointIndex;
1652 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1653 dragViewIndex - 1 : pageUnderPointIndex;
1654 for (int i = lowerIndex; i <= upperIndex; ++i) {
1655 View v = getChildAt(i);
1656 // dragViewIndex < pageUnderPointIndex, so after we remove the
1657 // drag view all subsequent views to pageUnderPointIndex will
1658 // shift down.
1659 int oldX = getViewportOffsetX() + getChildOffset(i);
1660 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1661
1662 // Animate the view translation from its old position to its new
1663 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001664 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001665 if (anim != null) {
1666 anim.cancel();
1667 }
1668
1669 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001670 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001671 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001672 anim.start();
1673 v.setTag(anim);
1674 }
1675
1676 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001678 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001679 if (mPageIndicator != null) {
1680 mPageIndicator.setActiveMarker(getNextPage());
1681 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001682 }
1683 };
1684 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1685 }
1686 } else {
1687 removeCallbacks(mSidePageHoverRunnable);
1688 mSidePageHoverIndex = -1;
1689 }
Adam Cohen564976a2010-10-13 18:52:07 -07001690 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001691 determineScrollingStart(ev);
1692 }
1693 break;
1694
1695 case MotionEvent.ACTION_UP:
1696 if (mTouchState == TOUCH_STATE_SCROLLING) {
1697 final int activePointerId = mActivePointerId;
1698 final int pointerIndex = ev.findPointerIndex(activePointerId);
1699 final float x = ev.getX(pointerIndex);
1700 final VelocityTracker velocityTracker = mVelocityTracker;
1701 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1702 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001703 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001704 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001705 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1706 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001707
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001708 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1709
Adam Cohen00481b32011-11-18 12:03:48 -08001710 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001711 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001712
Adam Cohenf358a4b2013-07-23 16:47:31 -07001713 if (!mFreeScroll) {
1714 // In the case that the page is moved far to one direction and then is flung
1715 // in the opposite direction, we use a threshold to determine whether we should
1716 // just return to the starting page, or if we should skip one further.
1717 boolean returnToOriginalPage = false;
1718 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1719 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1720 returnToOriginalPage = true;
1721 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001722
Adam Cohenf358a4b2013-07-23 16:47:31 -07001723 int finalPage;
1724 // We give flings precedence over large moves, which is why we short-circuit our
1725 // test for a large move if a fling has been registered. That is, a large
1726 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001727 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1728 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001729 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1730 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1731 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1732 snapToPageWithVelocity(finalPage, velocityX);
1733 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1734 (isFling && isVelocityXLeft)) &&
1735 mCurrentPage < getChildCount() - 1) {
1736 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1737 snapToPageWithVelocity(finalPage, velocityX);
1738 } else {
1739 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001740 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001741 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001742 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001743 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001744 }
1745
1746 float scaleX = getScaleX();
1747 int vX = (int) (-velocityX * scaleX);
1748 int initialScrollX = (int) (getScrollX() * scaleX);
1749
Adam Cohenf9618852013-11-08 06:45:03 -08001750 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001751 mScroller.fling(initialScrollX,
1752 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001753 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001754 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001755 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001756 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001757 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1758 // at this point we have not moved beyond the touch slop
1759 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1760 // we can just page
1761 int nextPage = Math.max(0, mCurrentPage - 1);
1762 if (nextPage != mCurrentPage) {
1763 snapToPage(nextPage);
1764 } else {
1765 snapToDestination();
1766 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001767 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001768 // at this point we have not moved beyond the touch slop
1769 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1770 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001771 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1772 if (nextPage != mCurrentPage) {
1773 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001774 } else {
1775 snapToDestination();
1776 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001777 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1778 // Update the last motion position
1779 mLastMotionX = ev.getX();
1780 mLastMotionY = ev.getY();
1781
1782 // Update the parent down so that our zoom animations take this new movement into
1783 // account
1784 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1785 mParentDownMotionX = pt[0];
1786 mParentDownMotionY = pt[1];
1787 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001788 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001789 if (!mCancelTap) {
1790 onUnhandledTap(ev);
1791 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001792 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001793
1794 // Remove the callback to wait for the side page hover timeout
1795 removeCallbacks(mSidePageHoverRunnable);
1796 // End any intermediate reordering states
1797 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001798 break;
1799
1800 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001801 if (mTouchState == TOUCH_STATE_SCROLLING) {
1802 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001803 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001804 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001805 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001806 break;
1807
1808 case MotionEvent.ACTION_POINTER_UP:
1809 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001810 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001811 break;
1812 }
1813
1814 return true;
1815 }
1816
Adam Cohen7d30a372013-07-01 17:03:59 -07001817 private void resetTouchState() {
1818 releaseVelocityTracker();
1819 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001820 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001821 mTouchState = TOUCH_STATE_REST;
1822 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001823 mEdgeGlowLeft.onRelease();
1824 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001825 }
1826
Adam Cohenc2d6e892014-10-16 09:49:24 -07001827 /**
1828 * Triggered by scrolling via touch
1829 */
1830 protected void onScrollInteractionBegin() {
1831 }
1832
1833 protected void onScrollInteractionEnd() {
1834 }
1835
Adam Cohen1697b792013-09-17 19:08:21 -07001836 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001837 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001838 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001839
Winson Chung185d7162011-02-28 13:47:29 -08001840 @Override
1841 public boolean onGenericMotionEvent(MotionEvent event) {
1842 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1843 switch (event.getAction()) {
1844 case MotionEvent.ACTION_SCROLL: {
1845 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1846 final float vscroll;
1847 final float hscroll;
1848 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1849 vscroll = 0;
1850 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1851 } else {
1852 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1853 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1854 }
1855 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001856 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001857 : (hscroll > 0 || vscroll > 0);
1858 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001859 scrollRight();
1860 } else {
1861 scrollLeft();
1862 }
1863 return true;
1864 }
1865 }
1866 }
1867 }
1868 return super.onGenericMotionEvent(event);
1869 }
1870
Michael Jurkab8f06722010-10-10 15:58:46 -07001871 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1872 if (mVelocityTracker == null) {
1873 mVelocityTracker = VelocityTracker.obtain();
1874 }
1875 mVelocityTracker.addMovement(ev);
1876 }
1877
1878 private void releaseVelocityTracker() {
1879 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001880 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001881 mVelocityTracker.recycle();
1882 mVelocityTracker = null;
1883 }
1884 }
1885
Winson Chung321e9ee2010-08-09 13:37:56 -07001886 private void onSecondaryPointerUp(MotionEvent ev) {
1887 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1888 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1889 final int pointerId = ev.getPointerId(pointerIndex);
1890 if (pointerId == mActivePointerId) {
1891 // This was our active pointer going up. Choose a new
1892 // active pointer and adjust accordingly.
1893 // TODO: Make this decision more intelligent.
1894 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1895 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1896 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001897 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001898 mActivePointerId = ev.getPointerId(newPointerIndex);
1899 if (mVelocityTracker != null) {
1900 mVelocityTracker.clear();
1901 }
1902 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001903 }
1904
Winson Chung321e9ee2010-08-09 13:37:56 -07001905 @Override
1906 public void requestChildFocus(View child, View focused) {
1907 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001908 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001909 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001910 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001911 }
1912 }
1913
Adam Cohend19d3ca2010-09-15 14:43:42 -07001914 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001915 return getPageNearestToCenterOfScreen(getScrollX());
1916 }
1917
1918 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1919 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001920 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001921 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001922 final int childCount = getChildCount();
1923 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001924 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001925 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001926 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001927 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1929 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1930 minDistanceFromScreenCenter = distanceFromScreenCenter;
1931 minDistanceFromScreenCenterIndex = i;
1932 }
1933 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001934 return minDistanceFromScreenCenterIndex;
1935 }
1936
1937 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001938 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001939 }
1940
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001941 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001942 public ScrollInterpolator() {
1943 }
1944
1945 public float getInterpolation(float t) {
1946 t -= 1.0f;
1947 return t*t*t*t*t + 1;
1948 }
1949 }
1950
1951 // We want the duration of the page snap animation to be influenced by the distance that
1952 // the screen has to travel, however, we don't want this duration to be effected in a
1953 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1954 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001955 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001956 f -= 0.5f; // center the values about 0.
1957 f *= 0.3f * Math.PI / 2.0f;
1958 return (float) Math.sin(f);
1959 }
1960
Michael Jurka0142d492010-08-25 17:46:15 -07001961 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001962 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001963 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001964
Adam Cohenedb40762013-07-18 16:45:45 -07001965 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001966 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001967 int duration = 0;
1968
Sunny Goyal4d113a52015-05-27 10:05:28 -07001969 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001970 // If the velocity is low enough, then treat this more as an automatic page advance
1971 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001972 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001973 return;
1974 }
1975
1976 // Here we compute a "distance" that will be used in the computation of the overall
1977 // snap duration. This is a function of the actual distance that needs to be traveled;
1978 // we keep this value close to half screen size in order to reduce the variance in snap
1979 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001980 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001981 float distance = halfScreenSize + halfScreenSize *
1982 distanceInfluenceForSnapDuration(distanceRatio);
1983
1984 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001985 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001986
1987 // we want the page's snap velocity to approximately match the velocity at which the
1988 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001989 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1990 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001991
1992 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001993 }
1994
Sunny Goyal1740d902015-05-27 11:14:01 -07001995 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001996 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001997 }
1998
Adam Cohenf9c184a2016-01-15 16:47:43 -08001999 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002000 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002001 }
2002
Michael Jurka0142d492010-08-25 17:46:15 -07002003 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002004 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002005 }
2006
Adam Cohenf9618852013-11-08 06:45:03 -08002007 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2008 snapToPage(whichPage, duration, false, interpolator);
2009 }
2010
2011 protected void snapToPage(int whichPage, int duration, boolean immediate,
2012 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002013 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002014
Adam Cohenedb40762013-07-18 16:45:45 -07002015 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002016 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002017 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002018 }
2019
2020 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002021 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002022 }
Michael Jurka0142d492010-08-25 17:46:15 -07002023
Adam Cohenf9618852013-11-08 06:45:03 -08002024 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2025 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002026 whichPage = validateNewPage(whichPage);
2027
Adam Cohen7d30a372013-07-01 17:03:59 -07002028 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002029
2030 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002031 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002032 if (immediate) {
2033 duration = 0;
2034 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002035 duration = Math.abs(delta);
2036 }
2037
Adam Cohenf358a4b2013-07-23 16:47:31 -07002038 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002039 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002040 }
Adam Cohenf9618852013-11-08 06:45:03 -08002041
2042 if (interpolator != null) {
2043 mScroller.setInterpolator(interpolator);
2044 } else {
2045 mScroller.setInterpolator(mDefaultInterpolator);
2046 }
2047
Sunny Goyalc86df472016-02-25 09:19:38 -08002048 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002049
Adam Cohen674531f2013-12-13 15:07:14 -08002050 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002051
2052 // Trigger a compute() to finish switching pages if necessary
2053 if (immediate) {
2054 computeScroll();
2055 }
2056
2057 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002058 invalidate();
2059 }
2060
Winson Chung321e9ee2010-08-09 13:37:56 -07002061 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002062 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002063 }
2064
2065 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002066 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002067 }
2068
Adam Cohendbdff6b2013-09-18 19:09:15 -07002069 @Override
2070 public boolean performLongClick() {
2071 mCancelTap = true;
2072 return super.performLongClick();
2073 }
2074
Winson Chung321e9ee2010-08-09 13:37:56 -07002075 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002076 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002077
2078 SavedState(Parcelable superState) {
2079 super(superState);
2080 }
2081
Adam Cohen091440a2015-03-18 14:16:05 -07002082 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002083 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002084 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002085 }
2086
2087 @Override
2088 public void writeToParcel(Parcel out, int flags) {
2089 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002090 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 }
2092
2093 public static final Parcelable.Creator<SavedState> CREATOR =
2094 new Parcelable.Creator<SavedState>() {
2095 public SavedState createFromParcel(Parcel in) {
2096 return new SavedState(in);
2097 }
2098
2099 public SavedState[] newArray(int size) {
2100 return new SavedState[size];
2101 }
2102 };
2103 }
2104
Adam Cohen7d30a372013-07-01 17:03:59 -07002105 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002106 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002107 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002108 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2109 .translationX(0)
2110 .translationY(0)
2111 .scaleX(1)
2112 .scaleY(1)
2113 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002114 anim.addListener(new AnimatorListenerAdapter() {
2115 @Override
2116 public void onAnimationEnd(Animator animation) {
2117 onPostReorderingAnimationCompleted();
2118 }
2119 });
2120 anim.start();
2121 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002122 }
2123
Sunny Goyal1d08f702015-05-04 15:50:25 -07002124 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002125 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2126 mTouchState = TOUCH_STATE_REORDERING;
2127 mIsReordering = true;
2128
Adam Cohen7d30a372013-07-01 17:03:59 -07002129 // We must invalidate to trigger a redraw to update the layers such that the drag view
2130 // is always drawn on top
2131 invalidate();
2132 }
2133
Adam Cohen091440a2015-03-18 14:16:05 -07002134 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002135 // Trigger the callback when reordering has settled
2136 --mPostReorderingPreZoomInRemainingAnimationCount;
2137 if (mPostReorderingPreZoomInRunnable != null &&
2138 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2139 mPostReorderingPreZoomInRunnable.run();
2140 mPostReorderingPreZoomInRunnable = null;
2141 }
2142 }
2143
Sunny Goyal1d08f702015-05-04 15:50:25 -07002144 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002145 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002146 }
2147
Adam Cohenf358a4b2013-07-23 16:47:31 -07002148 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002149 int dragViewIndex = indexOfChild(v);
2150
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002151 // Do not allow the first page to be moved around
2152 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002153
Adam Cohen7d30a372013-07-01 17:03:59 -07002154 mTempVisiblePagesRange[0] = 0;
2155 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002156 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002157 mReorderingStarted = true;
2158
2159 // Check if we are within the reordering range
2160 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002161 dragViewIndex <= mTempVisiblePagesRange[1]) {
2162 // Find the drag view under the pointer
2163 mDragView = getChildAt(dragViewIndex);
2164 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2165 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002166 snapToPage(getPageNearestToCenterOfScreen());
2167 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002168 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002169 return true;
2170 }
2171 return false;
2172 }
2173
2174 boolean isReordering(boolean testTouchState) {
2175 boolean state = mIsReordering;
2176 if (testTouchState) {
2177 state &= (mTouchState == TOUCH_STATE_REORDERING);
2178 }
2179 return state;
2180 }
2181 void endReordering() {
2182 // For simplicity, we call endReordering sometimes even if reordering was never started.
2183 // In that case, we don't want to do anything.
2184 if (!mReorderingStarted) return;
2185 mReorderingStarted = false;
2186
2187 // If we haven't flung-to-delete the current child, then we just animate the drag view
2188 // back into position
2189 final Runnable onCompleteRunnable = new Runnable() {
2190 @Override
2191 public void run() {
2192 onEndReordering();
2193 }
2194 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002195
2196 mPostReorderingPreZoomInRunnable = new Runnable() {
2197 public void run() {
2198 onCompleteRunnable.run();
2199 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002200 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002201 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002202
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002203 mPostReorderingPreZoomInRemainingAnimationCount =
2204 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2205 // Snap to the current page
2206 snapToPage(indexOfChild(mDragView), 0);
2207 // Animate the drag view back to the front position
2208 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002209 }
2210
Winson Chung6a0f57d2011-06-29 20:10:49 -07002211 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002212 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002213 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002214 @Override
2215 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2216 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002217 info.setScrollable(getPageCount() > 1);
2218 if (getCurrentPage() < getPageCount() - 1) {
2219 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2220 }
2221 if (getCurrentPage() > 0) {
2222 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2223 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002224 info.setClassName(getClass().getName());
2225
2226 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2227 // Besides disabling the accessibility long-click, this also prevents this view from getting
2228 // accessibility focus.
2229 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002230 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002231 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2232 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002233 }
2234
2235 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002236 public void sendAccessibilityEvent(int eventType) {
2237 // Don't let the view send real scroll events.
2238 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2239 super.sendAccessibilityEvent(eventType);
2240 }
2241 }
2242
2243 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002244 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2245 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002246 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002247 }
2248
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002249 @Override
2250 public boolean performAccessibilityAction(int action, Bundle arguments) {
2251 if (super.performAccessibilityAction(action, arguments)) {
2252 return true;
2253 }
2254 switch (action) {
2255 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2256 if (getCurrentPage() < getPageCount() - 1) {
2257 scrollRight();
2258 return true;
2259 }
2260 } break;
2261 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2262 if (getCurrentPage() > 0) {
2263 scrollLeft();
2264 return true;
2265 }
2266 } break;
2267 }
2268 return false;
2269 }
2270
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002271 protected String getPageIndicatorDescription() {
2272 return getCurrentPageDescription();
2273 }
2274
Adam Cohen0ffac432013-07-10 11:19:26 -07002275 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002276 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002277 getNextPage() + 1, getChildCount());
2278 }
2279
Winson Chungd11265e2011-08-30 13:37:23 -07002280 @Override
2281 public boolean onHoverEvent(android.view.MotionEvent event) {
2282 return true;
2283 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002284}