blob: 87f3ddaf47c2df818c437f358a8c20851e29790a [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070027import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070029import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.os.Parcel;
31import android.os.Parcelable;
32import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070033import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080035import android.view.InputDevice;
36import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070037import android.view.MotionEvent;
38import android.view.VelocityTracker;
39import android.view.View;
40import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080041import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.view.ViewGroup;
43import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070044import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070045import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080047import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070048
Sunny Goyal9e76f682017-02-13 12:13:43 -080049import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070050import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070051import com.android.launcher3.touch.OverScroll;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080052import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070054
Winson Chung6a0f57d2011-06-29 20:10:49 -070055import java.util.ArrayList;
56
Winson Chung321e9ee2010-08-09 13:37:56 -070057/**
58 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070059 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070060 */
Michael Jurka8b805b12012-04-18 14:23:14 -070061public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070062 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070063 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Winson Chung86f77532010-08-24 11:08:22 -070066 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070067 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Vadim Tryshevfedca432015-08-19 17:55:02 -070069 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070070 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070071
Sunny Goyalb72d8b22017-07-14 00:02:27 -070072 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070073 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070074
Adam Cohenb64cb5a2011-02-15 13:53:42 -080075 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080076 // The page is moved more than halfway, automatically move to the next page on touch up.
77 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080078
Sunny Goyal8e2133b2015-05-06 13:39:07 -070079 private static final float MAX_SCROLL_PROGRESS = 1.0f;
80
Adam Cohen265b9a62011-12-07 14:37:18 -080081 // The following constants need to be scaled based on density. The scaled versions will be
82 // assigned to the corresponding member variables below.
83 private static final int FLING_THRESHOLD_VELOCITY = 500;
84 private static final int MIN_SNAP_VELOCITY = 1500;
85 private static final int MIN_FLING_VELOCITY = 250;
86
Adam Cohen21cd0022013-10-09 18:57:02 -070087 public static final int INVALID_RESTORE_PAGE = -1001;
88
Adam Cohenf358a4b2013-07-23 16:47:31 -070089 private boolean mFreeScroll = false;
90 private int mFreeScrollMinScrollX = -1;
91 private int mFreeScrollMaxScrollX = -1;
92
Adam Cohen265b9a62011-12-07 14:37:18 -080093 protected int mFlingThresholdVelocity;
94 protected int mMinFlingVelocity;
95 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070096
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070098 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070099
Sunny Goyal4ffec482016-02-09 11:28:52 -0800100 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected int mCurrentPage;
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 Cohenedb40762013-07-18 16:45:45 -0700122
Adam Cohendbdff6b2013-09-18 19:09:15 -0700123 private boolean mCancelTap;
124
Adam Cohenedb40762013-07-18 16:45:45 -0700125 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Michael Jurka0142d492010-08-25 17:46:15 -0700127 protected final static int TOUCH_STATE_REST = 0;
128 protected final static int TOUCH_STATE_SCROLLING = 1;
129 protected final static int TOUCH_STATE_PREV_PAGE = 2;
130 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 protected final static int TOUCH_STATE_REORDERING = 4;
132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800134
Michael Jurka0142d492010-08-25 17:46:15 -0700135 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Michael Jurka7426c422010-11-11 15:23:47 -0800137 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800139 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800140 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka5f1c5092010-09-03 14:15:02 -0700144 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700146 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147
Sunny Goyal061380a2016-02-29 15:15:03 -0800148 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700149
Adam Cohen8d769d62017-06-23 17:27:38 -0700150 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
151 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
152 // the screens from continuing to translate beyond the normal bounds.
153 protected int mOverScrollX;
154
155 protected int mUnboundedScrollX;
156
Winson Chungd2be3812013-07-16 11:11:32 -0700157 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700159 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700160 // The viewport whether the pages are to be contained (the actual view may be larger than the
161 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800162 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700163 private Rect mViewport = new Rect();
164
165 // Reordering
166 // We use the min scale to determine how much to expand the actually PagedView measured
167 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700168 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700169 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700170 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
171
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700173 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700174 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700176 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700177 // This variable's scope is only for the duration of startReordering() and endReordering()
178 private boolean mReorderingStarted = false;
179 // This variable's scope is for the duration of startReordering() and after the zoomIn()
180 // animation after endReordering()
181 private boolean mIsReordering;
182 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700183 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 private int mPostReorderingPreZoomInRemainingAnimationCount;
185 private Runnable mPostReorderingPreZoomInRunnable;
186
Adam Cohen7d30a372013-07-01 17:03:59 -0700187 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700188 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700189 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700190 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700191
John Spurlock77e1f472013-09-11 10:09:51 -0400192 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700193 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400194
Winson Chung321e9ee2010-08-09 13:37:56 -0700195 public PagedView(Context context) {
196 this(context, null);
197 }
198
199 public PagedView(Context context, AttributeSet attrs) {
200 this(context, attrs, 0);
201 }
202
203 public PagedView(Context context, AttributeSet attrs, int defStyle) {
204 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700205
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 TypedArray a = context.obtainStyledAttributes(attrs,
207 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700208 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 a.recycle();
210
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700212 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700213 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 }
215
216 /**
217 * Initializes various states for this workspace.
218 */
Michael Jurka0142d492010-08-25 17:46:15 -0700219 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800220 mScroller = new LauncherScroller(getContext());
221 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700222 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223
224 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700225 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800227
Sunny Goyalcf25b522015-07-09 00:01:18 -0700228 float density = getResources().getDisplayMetrics().density;
229 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
230 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
231 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700232 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700233 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 }
235
Adam Cohenf9618852013-11-08 06:45:03 -0800236 protected void setDefaultInterpolator(Interpolator interpolator) {
237 mDefaultInterpolator = interpolator;
238 mScroller.setInterpolator(mDefaultInterpolator);
239 }
240
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700241 public void initParentViews(View parent) {
242 if (mPageIndicatorViewId > -1) {
243 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700244 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700245 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700246 }
247 }
248
Adam Cohen7d30a372013-07-01 17:03:59 -0700249 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700250 private float[] mapPointFromViewToParent(View v, float x, float y) {
251 sTmpPoint[0] = x;
252 sTmpPoint[1] = y;
253 v.getMatrix().mapPoints(sTmpPoint);
254 sTmpPoint[0] += v.getLeft();
255 sTmpPoint[1] += v.getTop();
256 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700257 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700258 private float[] mapPointFromParentToView(View v, float x, float y) {
259 sTmpPoint[0] = x - v.getLeft();
260 sTmpPoint[1] = y - v.getTop();
261 v.getMatrix().invert(sTmpInvMatrix);
262 sTmpInvMatrix.mapPoints(sTmpPoint);
263 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700264 }
265
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700266 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700267 if (mDragView != null) {
268 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
269 (mDragViewBaselineLeft - mDragView.getLeft());
270 float y = mLastMotionY - mDownMotionY;
271 mDragView.setTranslationX(x);
272 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700273
Adam Cohenf358a4b2013-07-23 16:47:31 -0700274 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
275 + x + ", " + y);
276 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700277 }
278
279 public void setMinScale(float f) {
280 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700281 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700282 requestLayout();
283 }
284
285 @Override
286 public void setScaleX(float scaleX) {
287 super.setScaleX(scaleX);
288 if (isReordering(true)) {
289 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
290 mLastMotionX = p[0];
291 mLastMotionY = p[1];
292 updateDragViewTranslationDuringDrag();
293 }
294 }
295
296 // Convenience methods to get the actual width/height of the PagedView (since it is measured
297 // to be larger to account for the minimum possible scale)
298 int getViewportWidth() {
299 return mViewport.width();
300 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800301 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700302 return mViewport.height();
303 }
304
305 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
306 // PagedView both horizontally and vertically
307 int getViewportOffsetX() {
308 return (getMeasuredWidth() - getViewportWidth()) / 2;
309 }
310
311 int getViewportOffsetY() {
312 return (getMeasuredHeight() - getViewportHeight()) / 2;
313 }
314
Hyunyoung Song77441692016-06-27 22:00:48 -0700315 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700316 return mPageIndicator;
317 }
318
Adam Cohen674531f2013-12-13 15:07:14 -0800319 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700320 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
321 * that the user was on before entering free scroll mode (e.g. the home screen page they
322 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
323 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700325 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700326 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700328
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700329 /**
330 * Returns the index of page to be shown immediately afterwards.
331 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700332 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700333 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
334 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700335
Adam Cohenf9c184a2016-01-15 16:47:43 -0800336 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 return getChildCount();
338 }
339
Sunny Goyal83a8f042015-05-19 12:52:12 -0700340 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 return getChildAt(index);
342 }
343
Adam Cohenae4f1552011-10-20 00:15:42 -0700344 protected int indexToPage(int index) {
345 return index;
346 }
347
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800349 * Updates the scroll of the current page immediately to its final scroll position. We use this
350 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
351 * the previous tab page.
352 */
353 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700354 // If the current page is invalid, just reset the scroll position to zero
355 int newX = 0;
356 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700357 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700358 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800359 scrollTo(newX, 0);
360 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700361 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800362 }
363
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700364 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700365 mScroller.abortAnimation();
366 // We need to clean up the next page here to avoid computeScrollHelper from
367 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700368 if (resetNextPage) {
369 mNextPage = INVALID_PAGE;
370 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700371 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700372
Tony Wickham8f7ead32016-04-07 18:46:44 -0700373 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700374 mScroller.forceFinished(true);
375 // We need to clean up the next page here to avoid computeScrollHelper from
376 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700377 if (resetNextPage) {
378 mNextPage = INVALID_PAGE;
379 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700380 }
381
Adam Cohen6f127a62014-06-12 14:54:41 -0700382 private int validateNewPage(int newPage) {
383 int validatedPage = newPage;
384 // When in free scroll mode, we need to clamp to the free scroll page range.
385 if (mFreeScroll) {
386 getFreeScrollPageRange(mTempVisiblePagesRange);
387 validatedPage = Math.max(mTempVisiblePagesRange[0],
388 Math.min(newPage, mTempVisiblePagesRange[1]));
389 }
390 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700391 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700392 return validatedPage;
393 }
394
Chet Haasebc2f0822012-10-26 17:59:53 -0700395 /**
Winson Chung86f77532010-08-24 11:08:22 -0700396 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700397 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700398 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800399 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700400 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800401 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800402 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
403 // the default
404 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800405 return;
406 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700407 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700408 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700409 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700410 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800411 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 }
413
Winson Chung8c87cd82013-07-23 16:20:10 -0700414 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800415 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
416 * has settled.
417 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700418 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800419 updatePageIndicator();
420 }
421
422 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700423 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700424 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700425 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700426 if (!isReordering(false)) {
427 mPageIndicator.setActiveMarker(getNextPage());
428 }
Winson Chungd2be3812013-07-16 11:11:32 -0700429 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700431 protected void pageBeginTransition() {
432 if (!mIsPageInTransition) {
433 mIsPageInTransition = true;
434 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700435 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700436 }
437
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700438 protected void pageEndTransition() {
439 if (mIsPageInTransition) {
440 mIsPageInTransition = false;
441 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700442 }
Michael Jurka0142d492010-08-25 17:46:15 -0700443 }
444
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700445 protected boolean isPageInTransition() {
446 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700447 }
448
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700449 /**
450 * Called when the page starts moving as part of the scroll. Subclasses can override this
451 * to provide custom behavior during animation.
452 */
453 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700454 }
455
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700456 /**
457 * Called when the page ends moving as part of the scroll. Subclasses can override this
458 * to provide custom behavior during animation.
459 */
460 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700461 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700462 }
463
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 /**
Winson Chung86f77532010-08-24 11:08:22 -0700465 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 *
467 * @param l The listener used to respond to long clicks.
468 */
469 @Override
470 public void setOnLongClickListener(OnLongClickListener l) {
471 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700472 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700473 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700474 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
Adam Cohen1697b792013-09-17 19:08:21 -0700476 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 }
478
Sunny Goyalc86df472016-02-25 09:19:38 -0800479 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700480 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800481 }
482
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800484 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800485 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800486 }
487
488 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700489 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700490 // In free scroll mode, we clamp the scrollX
491 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700492 // If the scroller is trying to move to a location beyond the maximum allowed
493 // in the free scroll mode, we make sure to end the scroll operation.
494 if (!mScroller.isFinished() &&
495 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700496 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700497 }
498
Adam Cohenf358a4b2013-07-23 16:47:31 -0700499 x = Math.min(x, mFreeScrollMaxScrollX);
500 x = Math.max(x, mFreeScrollMinScrollX);
501 }
502
Adam Cohen8d769d62017-06-23 17:27:38 -0700503 mUnboundedScrollX = x;
504
Sunny Goyal70660032015-05-14 00:07:08 -0700505 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
506 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700507 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700508 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800509 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700510 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700511 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700512 overScroll(x - mMaxScrollX);
513 } else {
514 overScroll(x);
515 }
Adam Cohen68d73932010-11-15 10:50:58 -0800516 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700517 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700518 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800519 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700520 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700521 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700522 overScroll(x);
523 } else {
524 overScroll(x - mMaxScrollX);
525 }
Adam Cohen68d73932010-11-15 10:50:58 -0800526 }
527 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700528 if (mWasInOverscroll) {
529 overScroll(0);
530 mWasInOverscroll = false;
531 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700532 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800533 super.scrollTo(x, y);
534 }
535
Adam Cohen7d30a372013-07-01 17:03:59 -0700536 // Update the last motion events when scrolling
537 if (isReordering(true)) {
538 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
539 mLastMotionX = p[0];
540 mLastMotionY = p[1];
541 updateDragViewTranslationDuringDrag();
542 }
Michael Jurka0142d492010-08-25 17:46:15 -0700543 }
544
Adam Cohen53805212013-10-01 10:39:23 -0700545 private void sendScrollAccessibilityEvent() {
546 AccessibilityManager am =
547 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
548 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700549 if (mCurrentPage != getNextPage()) {
550 AccessibilityEvent ev =
551 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700552 ev.setScrollable(true);
553 ev.setScrollX(getScrollX());
554 ev.setScrollY(getScrollY());
555 ev.setMaxScrollX(mMaxScrollX);
556 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700557
Vadim Tryshevf4715972015-05-08 17:21:03 -0700558 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700559 }
Adam Cohen53805212013-10-01 10:39:23 -0700560 }
561 }
562
Michael Jurka0142d492010-08-25 17:46:15 -0700563 // we moved this functionality to a helper function so SmoothPagedView can reuse it
564 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800565 return computeScrollHelper(true);
566 }
567
568 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700570 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800571 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700572 || getScrollY() != mScroller.getCurrY()
573 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700574 float scaleX = mFreeScroll ? getScaleX() : 1f;
575 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
576 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700577 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800578 if (shouldInvalidate) {
579 invalidate();
580 }
Michael Jurka0142d492010-08-25 17:46:15 -0700581 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800582 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700583 sendScrollAccessibilityEvent();
584
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700585 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700586 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700587 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700588 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700589
Adam Cohen73aa9752010-11-24 16:26:58 -0800590 // We don't want to trigger a page end moving unless the page has settled
591 // and the user has stopped scrolling
592 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700593 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800594 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700595
Adam Cohen7d30a372013-07-01 17:03:59 -0700596 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700597 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700598 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700599 if (am.isEnabled()) {
600 // Notify the user when the page changes
601 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700602 }
Michael Jurka0142d492010-08-25 17:46:15 -0700603 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 }
Michael Jurka0142d492010-08-25 17:46:15 -0700605 return false;
606 }
607
608 @Override
609 public void computeScroll() {
610 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 }
612
Adam Cohen96d30a12013-07-16 18:13:21 -0700613 public static class LayoutParams extends ViewGroup.LayoutParams {
614 public boolean isFullScreenPage = false;
615
Sunny Goyal7c786f72016-06-01 14:08:21 -0700616 // If true, the start edge of the page snaps to the start edge of the viewport.
617 public boolean matchStartEdge = false;
618
Adam Cohen96d30a12013-07-16 18:13:21 -0700619 /**
620 * {@inheritDoc}
621 */
622 public LayoutParams(int width, int height) {
623 super(width, height);
624 }
625
Sunny Goyal41b22c02015-05-14 15:20:48 -0700626 public LayoutParams(Context context, AttributeSet attrs) {
627 super(context, attrs);
628 }
629
Adam Cohen96d30a12013-07-16 18:13:21 -0700630 public LayoutParams(ViewGroup.LayoutParams source) {
631 super(source);
632 }
633 }
634
Sunny Goyal41b22c02015-05-14 15:20:48 -0700635 @Override
636 public LayoutParams generateLayoutParams(AttributeSet attrs) {
637 return new LayoutParams(getContext(), attrs);
638 }
639
640 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700641 protected LayoutParams generateDefaultLayoutParams() {
642 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
643 }
644
Sunny Goyal41b22c02015-05-14 15:20:48 -0700645 @Override
646 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
647 return new LayoutParams(p);
648 }
649
650 @Override
651 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
652 return p instanceof LayoutParams;
653 }
654
Adam Cohenedb40762013-07-18 16:45:45 -0700655 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700656 LayoutParams lp = generateDefaultLayoutParams();
657 lp.isFullScreenPage = true;
658 super.addView(page, 0, lp);
659 }
660
Adam Cohen410f3cd2013-09-22 12:09:32 -0700661 public int getNormalChildHeight() {
662 return mNormalChildHeight;
663 }
664
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 @Override
666 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700667 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700668 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
669 return;
670 }
671
Adam Cohen7d30a372013-07-01 17:03:59 -0700672 // We measure the dimensions of the PagedView to be larger than the pages so that when we
673 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700674 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
675 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
676 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700677 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800678 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700679 // viewport, we can be at most one and a half screens offset once we scale down
680 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700681 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
682 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700683
Winson Chungc82d2622013-11-06 13:23:29 -0800684 int parentWidthSize = (int) (2f * maxSize);
685 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700686 int scaledWidthSize, scaledHeightSize;
687 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700688 scaledWidthSize = (int) (parentWidthSize / mMinScale);
689 scaledHeightSize = (int) (parentHeightSize / mMinScale);
690 } else {
691 scaledWidthSize = widthSize;
692 scaledHeightSize = heightSize;
693 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700694 mViewport.set(0, 0, widthSize, heightSize);
695
696 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
697 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
698 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 }
700
Winson Chung8aad6102012-05-11 16:27:49 -0700701 // Return early if we aren't given a proper dimension
702 if (widthSize <= 0 || heightSize <= 0) {
703 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
704 return;
705 }
706
Adam Lesinski6b879f02010-11-04 16:15:23 -0700707 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
708 * of the All apps view on XLarge displays to not take up more space then it needs. Width
709 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
710 * each page to have the same width.
711 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 final int verticalPadding = getPaddingTop() + getPaddingBottom();
713 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700714
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700715 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700717 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700718 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700719 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
720 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
721 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
722 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 final int childCount = getChildCount();
724 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700725 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700726 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100727 if (child.getVisibility() != GONE) {
728 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700729
Vladimir Marko2824b072013-10-04 16:42:17 +0100730 int childWidthMode;
731 int childHeightMode;
732 int childWidth;
733 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700734
Vladimir Marko2824b072013-10-04 16:42:17 +0100735 if (!lp.isFullScreenPage) {
736 if (lp.width == LayoutParams.WRAP_CONTENT) {
737 childWidthMode = MeasureSpec.AT_MOST;
738 } else {
739 childWidthMode = MeasureSpec.EXACTLY;
740 }
741
742 if (lp.height == LayoutParams.WRAP_CONTENT) {
743 childHeightMode = MeasureSpec.AT_MOST;
744 } else {
745 childHeightMode = MeasureSpec.EXACTLY;
746 }
747
Adam Cohen3b185e22013-10-29 14:45:58 -0700748 childWidth = getViewportWidth() - horizontalPadding
749 - mInsets.left - mInsets.right;
750 childHeight = getViewportHeight() - verticalPadding
751 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100752 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700753 } else {
754 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700755 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100756
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800757 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700758 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700759 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700760 if (referenceChildWidth == 0) {
761 referenceChildWidth = childWidth;
762 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700763
Vladimir Marko2824b072013-10-04 16:42:17 +0100764 final int childWidthMeasureSpec =
765 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
766 final int childHeightMeasureSpec =
767 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
768 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700769 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700770 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700771 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800772 }
773
Sunny Goyalcf25b522015-07-09 00:01:18 -0700774 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700776 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700777 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700778 return;
779 }
780
Winson Chung785d2eb2011-04-14 16:08:02 -0700781 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700783
Adam Cohen7d30a372013-07-01 17:03:59 -0700784 int offsetX = getViewportOffsetX();
785 int offsetY = getViewportOffsetY();
786
787 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700788 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700789
Sunny Goyal70660032015-05-14 00:07:08 -0700790 final int startIndex = mIsRtl ? childCount - 1 : 0;
791 final int endIndex = mIsRtl ? -1 : childCount;
792 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700793
Adam Cohen7d30a372013-07-01 17:03:59 -0700794 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700795
Adam Cohen3b185e22013-10-29 14:45:58 -0700796 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000797 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700798
Sunny Goyal6178f132016-07-11 17:30:03 -0700799 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700800 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
801 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700802 }
803
Adam Cohen0ffac432013-07-10 11:19:26 -0700804 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700805 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700807 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100808 int childTop;
809 if (lp.isFullScreenPage) {
810 childTop = offsetY;
811 } else {
812 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700813 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100814 }
815
Adam Cohen96d30a12013-07-16 18:13:21 -0700816 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700817 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800818
Winson Chung785d2eb2011-04-14 16:08:02 -0700819 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700820 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800821 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700822
Sunny Goyal6178f132016-07-11 17:30:03 -0700823 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700824 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000825
826 int pageGap = mPageSpacing;
827 int next = i + delta;
828 if (next != endIndex) {
829 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
830 } else {
831 nextLp = null;
832 }
833
834 // Prevent full screen pages from showing in the viewport
835 // when they are not the current page.
836 if (lp.isFullScreenPage) {
837 pageGap = getPaddingLeft();
838 } else if (nextLp != null && nextLp.isFullScreenPage) {
839 pageGap = getPaddingRight();
840 }
841
Sunny Goyala1fbd842015-05-20 13:40:27 -0700842 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700845
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700846 final LayoutTransition transition = getLayoutTransition();
847 // If the transition is running defer updating max scroll, as some empty pages could
848 // still be present, and a max scroll change could cause sudden jumps in scroll.
849 if (transition != null && transition.isRunning()) {
850 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
851
852 @Override
853 public void startTransition(LayoutTransition transition, ViewGroup container,
854 View view, int transitionType) { }
855
856 @Override
857 public void endTransition(LayoutTransition transition, ViewGroup container,
858 View view, int transitionType) {
859 // Wait until all transitions are complete.
860 if (!transition.isRunning()) {
861 transition.removeTransitionListener(this);
862 updateMaxScrollX();
863 }
864 }
865 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700866 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700867 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700868 }
869
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700870 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
871 updateCurrentPageScroll();
872 mFirstLayout = false;
873 }
874
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700875 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700876 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700877 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700878 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700879
880 if (isReordering(true)) {
881 updateDragViewTranslationDuringDrag();
882 }
Winson Chunge3193b92010-09-10 11:44:42 -0700883 }
884
Sunny Goyala1fbd842015-05-20 13:40:27 -0700885 protected int getChildGap() {
886 return 0;
887 }
888
Sunny Goyal316490e2015-06-02 09:38:28 -0700889 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700890 mMaxScrollX = computeMaxScrollX();
891 }
892
893 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700894 int childCount = getChildCount();
895 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700896 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700897 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700898 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700899 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700900 }
901 }
902
Adam Cohen3b185e22013-10-29 14:45:58 -0700903 public void setPageSpacing(int pageSpacing) {
904 mPageSpacing = pageSpacing;
905 requestLayout();
906 }
907
Winson Chunge3193b92010-09-10 11:44:42 -0700908 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700909 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700910 // Update the page indicator, we don't update the page indicator as we
911 // add/remove pages
912 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700913 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700914 }
915
Adam Cohen2591f6a2011-10-25 14:36:40 -0700916 // This ensures that when children are added, they get the correct transforms / alphas
917 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700918 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700919 invalidate();
920 }
921
Michael Jurka8b805b12012-04-18 14:23:14 -0700922 @Override
923 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700924 updateFreescrollBounds();
Tonya361c722017-07-04 09:43:06 -0700925 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700926 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700927 }
928
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700929 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700930 // Update the page indicator, we don't update the page indicator as we
931 // add/remove pages
932 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700933 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700934 }
935 }
936
937 @Override
938 public void removeView(View v) {
939 // XXX: We should find a better way to hook into this before the view
940 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700941 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700942 super.removeView(v);
943 }
944 @Override
945 public void removeViewInLayout(View v) {
946 // XXX: We should find a better way to hook into this before the view
947 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700948 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700949 super.removeViewInLayout(v);
950 }
951 @Override
952 public void removeViewAt(int index) {
953 // XXX: We should find a better way to hook into this before the view
954 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700955 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700956 super.removeViewAt(index);
957 }
958 @Override
959 public void removeAllViewsInLayout() {
960 // Update the page indicator, we don't update the page indicator as we
961 // add/remove pages
962 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700963 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700964 }
965
966 super.removeAllViewsInLayout();
967 }
968
Adam Cohen73894962011-10-31 13:17:17 -0700969 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700970 if (index < 0 || index > getChildCount() - 1) return 0;
971
Adam Cohenf698c6e2013-07-17 18:44:25 -0700972 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700973
Adam Cohenf698c6e2013-07-17 18:44:25 -0700974 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700975 }
976
Adam Cohen6f127a62014-06-12 14:54:41 -0700977 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700978 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700979 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 }
981
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 @Override
983 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700984 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700985 if (page != mCurrentPage || !mScroller.isFinished()) {
986 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 return true;
988 }
989 return false;
990 }
991
992 @Override
993 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700994 int focusablePage;
995 if (mNextPage != INVALID_PAGE) {
996 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700998 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 }
Winson Chung86f77532010-08-24 11:08:22 -07001000 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001002 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 }
1004 return false;
1005 }
1006
1007 @Override
1008 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001009 if (super.dispatchUnhandledMove(focused, direction)) {
1010 return true;
1011 }
1012
1013 if (mIsRtl) {
1014 if (direction == View.FOCUS_LEFT) {
1015 direction = View.FOCUS_RIGHT;
1016 } else if (direction == View.FOCUS_RIGHT) {
1017 direction = View.FOCUS_LEFT;
1018 }
1019 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001021 if (getCurrentPage() > 0) {
1022 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 return true;
1024 }
1025 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001026 if (getCurrentPage() < getPageCount() - 1) {
1027 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001028 return true;
1029 }
1030 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001031 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 }
1033
1034 @Override
1035 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001036 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1037 return;
1038 }
1039
Adam Cohen0ffac432013-07-10 11:19:26 -07001040 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001041 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001042 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 }
1044 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001045 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001046 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001047 }
1048 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001049 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001050 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 }
1052 }
1053 }
1054
1055 /**
1056 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001057 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 *
Winson Chung86f77532010-08-24 11:08:22 -07001059 * This happens when live folders requery, and if they're off page, they
1060 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 */
1062 @Override
1063 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001064 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 View v = focused;
1066 while (true) {
1067 if (v == current) {
1068 super.focusableViewAvailable(focused);
1069 return;
1070 }
1071 if (v == this) {
1072 return;
1073 }
1074 ViewParent parent = v.getParent();
1075 if (parent instanceof View) {
1076 v = (View)v.getParent();
1077 } else {
1078 return;
1079 }
1080 }
1081 }
1082
1083 /**
1084 * {@inheritDoc}
1085 */
1086 @Override
1087 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1088 if (disallowIntercept) {
1089 // We need to make sure to cancel our long press if
1090 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001091 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001092 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 }
1094 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1095 }
1096
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001097 /**
1098 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1099 */
1100 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001101 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001102 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001103 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001104 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001105 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001106 }
1107
1108 /**
1109 * Return true if a tap at (x, y) should trigger a flip to the next page.
1110 */
1111 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001112 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001113 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001114 }
1115 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001116 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001117 }
Winson Chung52aee602013-01-30 12:01:02 -08001118
Adam Cohen7d30a372013-07-01 17:03:59 -07001119 /** Returns whether x and y originated within the buffered viewport */
1120 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001121 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001122 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001123 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001124 }
1125
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 @Override
1127 public boolean onInterceptTouchEvent(MotionEvent ev) {
1128 /*
1129 * This method JUST determines whether we want to intercept the motion.
1130 * If we return true, onTouchEvent will be called and we do the actual
1131 * scrolling there.
1132 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001133 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001134
Winson Chung45e1d6e2010-11-09 17:19:49 -08001135 // Skip touch handling if there are no pages to swipe
1136 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1137
Winson Chung321e9ee2010-08-09 13:37:56 -07001138 /*
1139 * Shortcut the most recurring case: the user is in the dragging
1140 * state and he is moving his finger. We want to intercept this
1141 * motion.
1142 */
1143 final int action = ev.getAction();
1144 if ((action == MotionEvent.ACTION_MOVE) &&
1145 (mTouchState == TOUCH_STATE_SCROLLING)) {
1146 return true;
1147 }
1148
Winson Chung321e9ee2010-08-09 13:37:56 -07001149 switch (action & MotionEvent.ACTION_MASK) {
1150 case MotionEvent.ACTION_MOVE: {
1151 /*
1152 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1153 * whether the user has moved far enough from his original down touch.
1154 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001155 if (mActivePointerId != INVALID_POINTER) {
1156 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001157 }
1158 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1159 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1160 // i.e. fall through to the next case (don't break)
1161 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1162 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001163 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001164 }
1165
1166 case MotionEvent.ACTION_DOWN: {
1167 final float x = ev.getX();
1168 final float y = ev.getY();
1169 // Remember location of down touch
1170 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001171 mDownMotionY = y;
1172 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 mLastMotionX = x;
1174 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001175 float[] p = mapPointFromViewToParent(this, x, y);
1176 mParentDownMotionX = p[0];
1177 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001178 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001179 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001181
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 /*
1183 * If being flinged and user touches the screen, initiate drag;
1184 * otherwise don't. mScroller.isFinished should be false when
1185 * being flinged.
1186 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001187 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001188 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001189
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001190 if (finishedScrolling) {
1191 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001192 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001193 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001194 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001195 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001196 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001197 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1198 mTouchState = TOUCH_STATE_SCROLLING;
1199 } else {
1200 mTouchState = TOUCH_STATE_REST;
1201 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001202 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001203
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 break;
1205 }
1206
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001208 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001209 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 break;
1211
1212 case MotionEvent.ACTION_POINTER_UP:
1213 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001214 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 break;
1216 }
1217
1218 /*
1219 * The only time we want to intercept motion events is if we are in the
1220 * drag mode.
1221 */
1222 return mTouchState != TOUCH_STATE_REST;
1223 }
1224
Adam Cohenf8d28232011-02-01 21:47:00 -08001225 protected void determineScrollingStart(MotionEvent ev) {
1226 determineScrollingStart(ev, 1.0f);
1227 }
1228
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 /*
1230 * Determines if we should change the touch state to start scrolling after the
1231 * user moves their touch point too far.
1232 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001233 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001234 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001236 if (pointerIndex == -1) return;
1237
1238 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 final float x = ev.getX(pointerIndex);
1240 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001241 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1242
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001244
Adam Cohenf8d28232011-02-01 21:47:00 -08001245 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001247
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001248 if (xMoved) {
1249 // Scroll if the user moved far enough along the X axis
1250 mTouchState = TOUCH_STATE_SCROLLING;
1251 mTotalMotionX += Math.abs(mLastMotionX - x);
1252 mLastMotionX = x;
1253 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001254 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001255 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001256 // Stop listening for things like pinches.
1257 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001258 }
1259 }
1260
1261 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001262 // Try canceling the long press. It could also have been scheduled
1263 // by a distant descendant, so use the mAllowLongPress flag to block
1264 // everything
1265 final View currentPage = getPageAt(mCurrentPage);
1266 if (currentPage != null) {
1267 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 }
1269 }
1270
Adam Cohenb5ba0972011-09-07 18:02:31 -07001271 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001272 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001273
Adam Cohenedb40762013-07-18 16:45:45 -07001274 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001275 int count = getChildCount();
1276
1277 final int totalDistance;
1278
1279 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001280 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001281 adjacentPage = page - 1;
1282 }
1283
1284 if (adjacentPage < 0 || adjacentPage > count - 1) {
1285 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1286 } else {
1287 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1288 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001289
1290 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001291 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1292 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001293 return scrollProgress;
1294 }
1295
Adam Cohenedb40762013-07-18 16:45:45 -07001296 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001297 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001298 return 0;
1299 } else {
1300 return mPageScrolls[index];
1301 }
1302 }
1303
Adam Cohen564a2e72013-10-09 14:47:32 -07001304 // While layout transitions are occurring, a child's position may stray from its baseline
1305 // position. This method returns the magnitude of this stray at any given time.
1306 public int getLayoutTransitionOffsetForPage(int index) {
1307 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1308 return 0;
1309 } else {
1310 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001311
1312 int scrollOffset = 0;
1313 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1314 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001315 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001316 }
1317
Adam Cohen564a2e72013-10-09 14:47:32 -07001318 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1319 return (int) (child.getX() - baselineX);
1320 }
1321 }
1322
Adam Cohenb5ba0972011-09-07 18:02:31 -07001323 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001324 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001325
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001326 int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001327 if (amount < 0) {
1328 mOverScrollX = overScrollAmount;
1329 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001330 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001331 mOverScrollX = mMaxScrollX + overScrollAmount;
1332 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001333 }
1334 invalidate();
1335 }
1336
Adam Cohenb5ba0972011-09-07 18:02:31 -07001337 protected void overScroll(float amount) {
1338 dampedOverScroll(amount);
1339 }
1340
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001341 /**
1342 * return true if freescroll has been enabled, false otherwise
1343 */
1344 public boolean enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001345 setEnableFreeScroll(true);
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001346 return true;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001347 }
1348
Winson Chungdc61c4d2015-04-20 18:26:57 -07001349 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001350 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001351 }
1352
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001353 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001354 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001355 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001356 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1357 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1358 } else {
1359 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1360 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1361 }
1362 }
1363
Adam Cohenf9618852013-11-08 06:45:03 -08001364 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001365 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366 mFreeScroll = freeScroll;
1367
Adam Cohenf9618852013-11-08 06:45:03 -08001368 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001369 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001370 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001371 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1372 setCurrentPage(mTempVisiblePagesRange[0]);
1373 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1374 setCurrentPage(mTempVisiblePagesRange[1]);
1375 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001376 } else if (wasFreeScroll) {
1377 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001378 }
1379
1380 setEnableOverscroll(!freeScroll);
1381 }
1382
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001383 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001384 mAllowOverScroll = enable;
1385 }
1386
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001387 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388 if (mDragView != null) {
1389 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1390 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001391 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 int minDistance = Integer.MAX_VALUE;
1393 int minIndex = indexOfChild(mDragView);
1394 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1395 View page = getPageAt(i);
1396 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1397 int d = Math.abs(dragX - pageX);
1398 if (d < minDistance) {
1399 minIndex = i;
1400 minDistance = d;
1401 }
1402 }
1403 return minIndex;
1404 }
1405 return -1;
1406 }
1407
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 @Override
1409 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001410 super.onTouchEvent(ev);
1411
Winson Chung45e1d6e2010-11-09 17:19:49 -08001412 // Skip touch handling if there are no pages to swipe
1413 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1414
Michael Jurkab8f06722010-10-10 15:58:46 -07001415 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001416
1417 final int action = ev.getAction();
1418
1419 switch (action & MotionEvent.ACTION_MASK) {
1420 case MotionEvent.ACTION_DOWN:
1421 /*
1422 * If being flinged and user touches, stop the fling. isFinished
1423 * will be false if being flinged.
1424 */
1425 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001426 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 }
1428
1429 // Remember where the motion event started
1430 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001431 mDownMotionY = mLastMotionY = ev.getY();
1432 mDownScrollX = getScrollX();
1433 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1434 mParentDownMotionX = p[0];
1435 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001436 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001437 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001439
Michael Jurka0142d492010-08-25 17:46:15 -07001440 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001441 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001442 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001443 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 break;
1445
1446 case MotionEvent.ACTION_MOVE:
1447 if (mTouchState == TOUCH_STATE_SCROLLING) {
1448 // Scroll to follow the motion event
1449 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001450
1451 if (pointerIndex == -1) return true;
1452
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001454 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001455
Adam Cohenaefd4e12011-02-14 16:39:38 -08001456 mTotalMotionX += Math.abs(deltaX);
1457
Winson Chungc0844aa2011-02-02 15:25:58 -08001458 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1459 // keep the remainder because we are actually testing if we've moved from the last
1460 // scrolled position (which is discrete).
1461 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001462 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001463 mLastMotionX = x;
1464 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 } else {
1466 awakenScrollBars();
1467 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001468 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1469 // Update the last motion position
1470 mLastMotionX = ev.getX();
1471 mLastMotionY = ev.getY();
1472
1473 // Update the parent down so that our zoom animations take this new movement into
1474 // account
1475 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1476 mParentDownMotionX = pt[0];
1477 mParentDownMotionY = pt[1];
1478 updateDragViewTranslationDuringDrag();
1479
1480 // Find the closest page to the touch point
1481 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001482
Adam Cohen7d30a372013-07-01 17:03:59 -07001483 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1484 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1485 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1486 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1487
Adam Cohenf358a4b2013-07-23 16:47:31 -07001488 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001489 // Do not allow any page to be moved to 0th position.
1490 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001491 mTempVisiblePagesRange[0] = 0;
1492 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001493 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1495 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1496 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1497 mSidePageHoverIndex = pageUnderPointIndex;
1498 mSidePageHoverRunnable = new Runnable() {
1499 @Override
1500 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001501 // Setup the scroll to the correct page before we swap the views
1502 snapToPage(pageUnderPointIndex);
1503
1504 // For each of the pages between the paged view and the drag view,
1505 // animate them from the previous position to the new position in
1506 // the layout (as a result of the drag view moving in the layout)
1507 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1508 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1509 dragViewIndex + 1 : pageUnderPointIndex;
1510 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1511 dragViewIndex - 1 : pageUnderPointIndex;
1512 for (int i = lowerIndex; i <= upperIndex; ++i) {
1513 View v = getChildAt(i);
1514 // dragViewIndex < pageUnderPointIndex, so after we remove the
1515 // drag view all subsequent views to pageUnderPointIndex will
1516 // shift down.
1517 int oldX = getViewportOffsetX() + getChildOffset(i);
1518 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1519
1520 // Animate the view translation from its old position to its new
1521 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001522 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001523 if (anim != null) {
1524 anim.cancel();
1525 }
1526
1527 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001528 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001529 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001530 anim.start();
1531 v.setTag(anim);
1532 }
1533
1534 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001535 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001536 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001537 if (mPageIndicator != null) {
1538 mPageIndicator.setActiveMarker(getNextPage());
1539 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001540 }
1541 };
1542 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1543 }
1544 } else {
1545 removeCallbacks(mSidePageHoverRunnable);
1546 mSidePageHoverIndex = -1;
1547 }
Adam Cohen564976a2010-10-13 18:52:07 -07001548 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 determineScrollingStart(ev);
1550 }
1551 break;
1552
1553 case MotionEvent.ACTION_UP:
1554 if (mTouchState == TOUCH_STATE_SCROLLING) {
1555 final int activePointerId = mActivePointerId;
1556 final int pointerIndex = ev.findPointerIndex(activePointerId);
1557 final float x = ev.getX(pointerIndex);
1558 final VelocityTracker velocityTracker = mVelocityTracker;
1559 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1560 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001561 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001562 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001563 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1564 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001565
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001566 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1567
Adam Cohen00481b32011-11-18 12:03:48 -08001568 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001569 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001570
Adam Cohenf358a4b2013-07-23 16:47:31 -07001571 if (!mFreeScroll) {
1572 // In the case that the page is moved far to one direction and then is flung
1573 // in the opposite direction, we use a threshold to determine whether we should
1574 // just return to the starting page, or if we should skip one further.
1575 boolean returnToOriginalPage = false;
1576 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1577 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1578 returnToOriginalPage = true;
1579 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001580
Adam Cohenf358a4b2013-07-23 16:47:31 -07001581 int finalPage;
1582 // We give flings precedence over large moves, which is why we short-circuit our
1583 // test for a large move if a fling has been registered. That is, a large
1584 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001585 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1586 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001587 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1588 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1589 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1590 snapToPageWithVelocity(finalPage, velocityX);
1591 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1592 (isFling && isVelocityXLeft)) &&
1593 mCurrentPage < getChildCount() - 1) {
1594 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1595 snapToPageWithVelocity(finalPage, velocityX);
1596 } else {
1597 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001598 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001599 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001600 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001601 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001602 }
1603
1604 float scaleX = getScaleX();
1605 int vX = (int) (-velocityX * scaleX);
1606 int initialScrollX = (int) (getScrollX() * scaleX);
1607
Adam Cohenf9618852013-11-08 06:45:03 -08001608 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001609 mScroller.fling(initialScrollX,
1610 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001611 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001612 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001613 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001614 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001615 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1616 // at this point we have not moved beyond the touch slop
1617 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1618 // we can just page
1619 int nextPage = Math.max(0, mCurrentPage - 1);
1620 if (nextPage != mCurrentPage) {
1621 snapToPage(nextPage);
1622 } else {
1623 snapToDestination();
1624 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001625 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001626 // at this point we have not moved beyond the touch slop
1627 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1628 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001629 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1630 if (nextPage != mCurrentPage) {
1631 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001632 } else {
1633 snapToDestination();
1634 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001635 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1636 // Update the last motion position
1637 mLastMotionX = ev.getX();
1638 mLastMotionY = ev.getY();
1639
1640 // Update the parent down so that our zoom animations take this new movement into
1641 // account
1642 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1643 mParentDownMotionX = pt[0];
1644 mParentDownMotionY = pt[1];
1645 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001646 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001647 if (!mCancelTap) {
1648 onUnhandledTap(ev);
1649 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001650 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001651
1652 // Remove the callback to wait for the side page hover timeout
1653 removeCallbacks(mSidePageHoverRunnable);
1654 // End any intermediate reordering states
1655 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001656 break;
1657
1658 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001659 if (mTouchState == TOUCH_STATE_SCROLLING) {
1660 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001661 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001662 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001663 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001664 break;
1665
1666 case MotionEvent.ACTION_POINTER_UP:
1667 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001668 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001669 break;
1670 }
1671
1672 return true;
1673 }
1674
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001675 protected boolean shouldFlingForVelocity(int velocityX) {
1676 return Math.abs(velocityX) > mFlingThresholdVelocity;
1677 }
1678
Adam Cohen7d30a372013-07-01 17:03:59 -07001679 private void resetTouchState() {
1680 releaseVelocityTracker();
1681 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001682 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001683 mTouchState = TOUCH_STATE_REST;
1684 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001685 }
1686
Adam Cohenc2d6e892014-10-16 09:49:24 -07001687 /**
1688 * Triggered by scrolling via touch
1689 */
1690 protected void onScrollInteractionBegin() {
1691 }
1692
1693 protected void onScrollInteractionEnd() {
1694 }
1695
Adam Cohen1697b792013-09-17 19:08:21 -07001696 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001697 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001698 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001699
Winson Chung185d7162011-02-28 13:47:29 -08001700 @Override
1701 public boolean onGenericMotionEvent(MotionEvent event) {
1702 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1703 switch (event.getAction()) {
1704 case MotionEvent.ACTION_SCROLL: {
1705 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1706 final float vscroll;
1707 final float hscroll;
1708 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1709 vscroll = 0;
1710 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1711 } else {
1712 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1713 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1714 }
1715 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001716 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001717 : (hscroll > 0 || vscroll > 0);
1718 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001719 scrollRight();
1720 } else {
1721 scrollLeft();
1722 }
1723 return true;
1724 }
1725 }
1726 }
1727 }
1728 return super.onGenericMotionEvent(event);
1729 }
1730
Michael Jurkab8f06722010-10-10 15:58:46 -07001731 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1732 if (mVelocityTracker == null) {
1733 mVelocityTracker = VelocityTracker.obtain();
1734 }
1735 mVelocityTracker.addMovement(ev);
1736 }
1737
1738 private void releaseVelocityTracker() {
1739 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001740 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001741 mVelocityTracker.recycle();
1742 mVelocityTracker = null;
1743 }
1744 }
1745
Winson Chung321e9ee2010-08-09 13:37:56 -07001746 private void onSecondaryPointerUp(MotionEvent ev) {
1747 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1748 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1749 final int pointerId = ev.getPointerId(pointerIndex);
1750 if (pointerId == mActivePointerId) {
1751 // This was our active pointer going up. Choose a new
1752 // active pointer and adjust accordingly.
1753 // TODO: Make this decision more intelligent.
1754 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1755 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1756 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001757 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001758 mActivePointerId = ev.getPointerId(newPointerIndex);
1759 if (mVelocityTracker != null) {
1760 mVelocityTracker.clear();
1761 }
1762 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001763 }
1764
Winson Chung321e9ee2010-08-09 13:37:56 -07001765 @Override
1766 public void requestChildFocus(View child, View focused) {
1767 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001768 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001769 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001770 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001771 }
1772 }
1773
Adam Cohend19d3ca2010-09-15 14:43:42 -07001774 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001775 return getPageNearestToCenterOfScreen(getScrollX());
1776 }
1777
1778 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1779 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001780 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001782 final int childCount = getChildCount();
1783 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001784 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001785 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001786 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001787 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001788 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1789 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1790 minDistanceFromScreenCenter = distanceFromScreenCenter;
1791 minDistanceFromScreenCenterIndex = i;
1792 }
1793 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001794 return minDistanceFromScreenCenterIndex;
1795 }
1796
1797 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001798 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1799 }
1800
1801 protected boolean isInOverScroll() {
1802 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1803 }
1804
1805 protected int getPageSnapDuration() {
1806 if (isInOverScroll()) {
1807 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1808 }
1809 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001810 }
1811
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001812 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001813 public ScrollInterpolator() {
1814 }
1815
1816 public float getInterpolation(float t) {
1817 t -= 1.0f;
1818 return t*t*t*t*t + 1;
1819 }
1820 }
1821
1822 // We want the duration of the page snap animation to be influenced by the distance that
1823 // the screen has to travel, however, we don't want this duration to be effected in a
1824 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1825 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001826 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001827 f -= 0.5f; // center the values about 0.
1828 f *= 0.3f * Math.PI / 2.0f;
1829 return (float) Math.sin(f);
1830 }
1831
Michael Jurka0142d492010-08-25 17:46:15 -07001832 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001833 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001834 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001835
Adam Cohenedb40762013-07-18 16:45:45 -07001836 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001837 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001838 int duration = 0;
1839
Sunny Goyal4d113a52015-05-27 10:05:28 -07001840 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001841 // If the velocity is low enough, then treat this more as an automatic page advance
1842 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001843 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001844 return;
1845 }
1846
1847 // Here we compute a "distance" that will be used in the computation of the overall
1848 // snap duration. This is a function of the actual distance that needs to be traveled;
1849 // we keep this value close to half screen size in order to reduce the variance in snap
1850 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001851 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001852 float distance = halfScreenSize + halfScreenSize *
1853 distanceInfluenceForSnapDuration(distanceRatio);
1854
1855 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001856 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001857
1858 // we want the page's snap velocity to approximately match the velocity at which the
1859 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001860 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1861 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001862
1863 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001864 }
1865
Sunny Goyal1740d902015-05-27 11:14:01 -07001866 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001867 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001868 }
1869
Adam Cohenf9c184a2016-01-15 16:47:43 -08001870 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001871 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001872 }
1873
Michael Jurka0142d492010-08-25 17:46:15 -07001874 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001875 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001876 }
1877
Adam Cohenf9618852013-11-08 06:45:03 -08001878 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1879 snapToPage(whichPage, duration, false, interpolator);
1880 }
1881
1882 protected void snapToPage(int whichPage, int duration, boolean immediate,
1883 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001884 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001885
Adam Cohenedb40762013-07-18 16:45:45 -07001886 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001887 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001888 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001889 }
1890
1891 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001892 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001893 }
Michael Jurka0142d492010-08-25 17:46:15 -07001894
Adam Cohenf9618852013-11-08 06:45:03 -08001895 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1896 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001897 whichPage = validateNewPage(whichPage);
1898
Adam Cohen7d30a372013-07-01 17:03:59 -07001899 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001900
Winson Chung321e9ee2010-08-09 13:37:56 -07001901 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001902 if (immediate) {
1903 duration = 0;
1904 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001905 duration = Math.abs(delta);
1906 }
1907
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001908 if (duration != 0) {
1909 pageBeginTransition();
1910 }
1911
Adam Cohenf358a4b2013-07-23 16:47:31 -07001912 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001913 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001914 }
Adam Cohenf9618852013-11-08 06:45:03 -08001915
1916 if (interpolator != null) {
1917 mScroller.setInterpolator(interpolator);
1918 } else {
1919 mScroller.setInterpolator(mDefaultInterpolator);
1920 }
1921
Sunny Goyalc86df472016-02-25 09:19:38 -08001922 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001923
Adam Cohen674531f2013-12-13 15:07:14 -08001924 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001925
1926 // Trigger a compute() to finish switching pages if necessary
1927 if (immediate) {
1928 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001929 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001930 }
1931
Winson Chung321e9ee2010-08-09 13:37:56 -07001932 invalidate();
1933 }
1934
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001936 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001937 }
1938
1939 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001940 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 }
1942
Adam Cohendbdff6b2013-09-18 19:09:15 -07001943 @Override
1944 public boolean performLongClick() {
1945 mCancelTap = true;
1946 return super.performLongClick();
1947 }
1948
Winson Chung321e9ee2010-08-09 13:37:56 -07001949 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001950 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001951
1952 SavedState(Parcelable superState) {
1953 super(superState);
1954 }
1955
Adam Cohen091440a2015-03-18 14:16:05 -07001956 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001957 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001958 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 }
1960
1961 @Override
1962 public void writeToParcel(Parcel out, int flags) {
1963 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001964 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001965 }
1966
1967 public static final Parcelable.Creator<SavedState> CREATOR =
1968 new Parcelable.Creator<SavedState>() {
1969 public SavedState createFromParcel(Parcel in) {
1970 return new SavedState(in);
1971 }
1972
1973 public SavedState[] newArray(int size) {
1974 return new SavedState[size];
1975 }
1976 };
1977 }
1978
Adam Cohen7d30a372013-07-01 17:03:59 -07001979 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001980 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001981 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001982 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1983 new PropertyListBuilder()
1984 .scale(1)
1985 .translationX(0)
1986 .translationY(0)
1987 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001988 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001989 anim.addListener(new AnimatorListenerAdapter() {
1990 @Override
1991 public void onAnimationEnd(Animator animation) {
1992 onPostReorderingAnimationCompleted();
1993 }
1994 });
1995 anim.start();
1996 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001997 }
1998
Sunny Goyal1d08f702015-05-04 15:50:25 -07001999 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002000 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2001 mTouchState = TOUCH_STATE_REORDERING;
2002 mIsReordering = true;
2003
Adam Cohen7d30a372013-07-01 17:03:59 -07002004 // We must invalidate to trigger a redraw to update the layers such that the drag view
2005 // is always drawn on top
2006 invalidate();
2007 }
2008
Adam Cohen091440a2015-03-18 14:16:05 -07002009 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002010 // Trigger the callback when reordering has settled
2011 --mPostReorderingPreZoomInRemainingAnimationCount;
2012 if (mPostReorderingPreZoomInRunnable != null &&
2013 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2014 mPostReorderingPreZoomInRunnable.run();
2015 mPostReorderingPreZoomInRunnable = null;
2016 }
2017 }
2018
Sunny Goyal1d08f702015-05-04 15:50:25 -07002019 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002020 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002021 }
2022
Adam Cohenf358a4b2013-07-23 16:47:31 -07002023 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002024 int dragViewIndex = indexOfChild(v);
2025
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002026 // Do not allow the first page to be moved around
2027 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002028
Adam Cohen7d30a372013-07-01 17:03:59 -07002029 mTempVisiblePagesRange[0] = 0;
2030 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002031 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002032 mReorderingStarted = true;
2033
2034 // Check if we are within the reordering range
2035 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002036 dragViewIndex <= mTempVisiblePagesRange[1]) {
2037 // Find the drag view under the pointer
2038 mDragView = getChildAt(dragViewIndex);
2039 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2040 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002041 snapToPage(getPageNearestToCenterOfScreen());
2042 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002043 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002044 return true;
2045 }
2046 return false;
2047 }
2048
2049 boolean isReordering(boolean testTouchState) {
2050 boolean state = mIsReordering;
2051 if (testTouchState) {
2052 state &= (mTouchState == TOUCH_STATE_REORDERING);
2053 }
2054 return state;
2055 }
2056 void endReordering() {
2057 // For simplicity, we call endReordering sometimes even if reordering was never started.
2058 // In that case, we don't want to do anything.
2059 if (!mReorderingStarted) return;
2060 mReorderingStarted = false;
2061
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002062 mPostReorderingPreZoomInRunnable = new Runnable() {
2063 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002064 // If we haven't flung-to-delete the current child,
2065 // then we just animate the drag view back into position
2066 onEndReordering();
2067
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002068 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002069 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002070 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002071
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002072 mPostReorderingPreZoomInRemainingAnimationCount =
2073 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2074 // Snap to the current page
2075 snapToPage(indexOfChild(mDragView), 0);
2076 // Animate the drag view back to the front position
2077 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002078 }
2079
Winson Chung6a0f57d2011-06-29 20:10:49 -07002080 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002081 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002082 @Override
2083 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2084 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002085 info.setScrollable(getPageCount() > 1);
2086 if (getCurrentPage() < getPageCount() - 1) {
2087 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2088 }
2089 if (getCurrentPage() > 0) {
2090 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2091 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002092 info.setClassName(getClass().getName());
2093
2094 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2095 // Besides disabling the accessibility long-click, this also prevents this view from getting
2096 // accessibility focus.
2097 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002098 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002099 }
2100
2101 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002102 public void sendAccessibilityEvent(int eventType) {
2103 // Don't let the view send real scroll events.
2104 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2105 super.sendAccessibilityEvent(eventType);
2106 }
2107 }
2108
2109 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002110 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2111 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002112 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002113 }
2114
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002115 @Override
2116 public boolean performAccessibilityAction(int action, Bundle arguments) {
2117 if (super.performAccessibilityAction(action, arguments)) {
2118 return true;
2119 }
2120 switch (action) {
2121 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2122 if (getCurrentPage() < getPageCount() - 1) {
2123 scrollRight();
2124 return true;
2125 }
2126 } break;
2127 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2128 if (getCurrentPage() > 0) {
2129 scrollLeft();
2130 return true;
2131 }
2132 } break;
2133 }
2134 return false;
2135 }
2136
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002137 protected String getPageIndicatorDescription() {
2138 return getCurrentPageDescription();
2139 }
2140
Adam Cohen0ffac432013-07-10 11:19:26 -07002141 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002142 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002143 getNextPage() + 1, getChildCount());
2144 }
2145
Winson Chungd11265e2011-08-30 13:37:23 -07002146 @Override
2147 public boolean onHoverEvent(android.view.MotionEvent event) {
2148 return true;
2149 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002150}