blob: 76e2073f2e3062cd82602a6f5fa7abac686897f3 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070030import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.os.Parcel;
32import android.os.Parcelable;
33import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070034import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080036import android.view.InputDevice;
37import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.MotionEvent;
39import android.view.VelocityTracker;
40import android.view.View;
41import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080042import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.view.ViewGroup;
44import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070045import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070046import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080048import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049
50import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070051import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Vadim Tryshevfedca432015-08-19 17:55:02 -070068 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Adam Cohenb64cb5a2011-02-15 13:53:42 -080071 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080072 // The page is moved more than halfway, automatically move to the next page on touch up.
73 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080074
Sunny Goyal8e2133b2015-05-06 13:39:07 -070075 private static final float MAX_SCROLL_PROGRESS = 1.0f;
76
Adam Cohen265b9a62011-12-07 14:37:18 -080077 // The following constants need to be scaled based on density. The scaled versions will be
78 // assigned to the corresponding member variables below.
79 private static final int FLING_THRESHOLD_VELOCITY = 500;
80 private static final int MIN_SNAP_VELOCITY = 1500;
81 private static final int MIN_FLING_VELOCITY = 250;
82
Adam Cohen21cd0022013-10-09 18:57:02 -070083 public static final int INVALID_RESTORE_PAGE = -1001;
84
Adam Cohenf358a4b2013-07-23 16:47:31 -070085 private boolean mFreeScroll = false;
86 private int mFreeScrollMinScrollX = -1;
87 private int mFreeScrollMaxScrollX = -1;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070094 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -070098 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070099
Sunny Goyal4ffec482016-02-09 11:28:52 -0800100 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800102 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800103 protected LauncherScroller mScroller;
104 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Adam Cohen7d30a372013-07-01 17:03:59 -0700108 private float mParentDownMotionX;
109 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700111 private float mDownMotionY;
112 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700113 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700114 private float mLastMotionX;
115 private float mLastMotionXRemainder;
116 private float mLastMotionY;
117 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700118
Adam Cohendbdff6b2013-09-18 19:09:15 -0700119 private boolean mCancelTap;
120
Adam Cohenedb40762013-07-18 16:45:45 -0700121 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Michael Jurka0142d492010-08-25 17:46:15 -0700123 protected final static int TOUCH_STATE_REST = 0;
124 protected final static int TOUCH_STATE_SCROLLING = 1;
125 protected final static int TOUCH_STATE_PREV_PAGE = 2;
126 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700127 protected final static int TOUCH_STATE_REORDERING = 4;
128
Michael Jurka0142d492010-08-25 17:46:15 -0700129 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800135 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800136 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka5f1c5092010-09-03 14:15:02 -0700138 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700142 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700143
Sunny Goyal061380a2016-02-29 15:15:03 -0800144 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700145
Winson Chungd2be3812013-07-16 11:11:32 -0700146 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700148 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700149 // The viewport whether the pages are to be contained (the actual view may be larger than the
150 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800151 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700152 private Rect mViewport = new Rect();
153
154 // Reordering
155 // We use the min scale to determine how much to expand the actually PagedView measured
156 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700157 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700158 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700159 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
160
Adam Cohen7d30a372013-07-01 17:03:59 -0700161 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700162 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700163 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700164 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700165 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700166 // This variable's scope is only for the duration of startReordering() and endReordering()
167 private boolean mReorderingStarted = false;
168 // This variable's scope is for the duration of startReordering() and after the zoomIn()
169 // animation after endReordering()
170 private boolean mIsReordering;
171 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700172 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 private int mPostReorderingPreZoomInRemainingAnimationCount;
174 private Runnable mPostReorderingPreZoomInRunnable;
175
Adam Cohen7d30a372013-07-01 17:03:59 -0700176 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700177 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700178 private static final float[] sTmpPoint = new float[2];
179 private static final int[] sTmpIntPoint = new int[2];
180 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700181
John Spurlock77e1f472013-09-11 10:09:51 -0400182 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700183 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400184
Sunny Goyal4d113a52015-05-27 10:05:28 -0700185 // Edge effect
186 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
187 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 public PagedView(Context context) {
190 this(context, null);
191 }
192
193 public PagedView(Context context, AttributeSet attrs) {
194 this(context, attrs, 0);
195 }
196
197 public PagedView(Context context, AttributeSet attrs, int defStyle) {
198 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700199
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700202 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 a.recycle();
204
Winson Chung321e9ee2010-08-09 13:37:56 -0700205 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700206 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700207 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 }
209
210 /**
211 * Initializes various states for this workspace.
212 */
Michael Jurka0142d492010-08-25 17:46:15 -0700213 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800214 mScroller = new LauncherScroller(getContext());
215 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700216 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700217
218 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700219 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800221
Sunny Goyalcf25b522015-07-09 00:01:18 -0700222 float density = getResources().getDisplayMetrics().density;
223 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
224 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
225 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700226 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700227 setWillNotDraw(false);
228 }
229
230 protected void setEdgeGlowColor(int color) {
231 mEdgeGlowLeft.setColor(color);
232 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 }
234
Adam Cohenf9618852013-11-08 06:45:03 -0800235 protected void setDefaultInterpolator(Interpolator interpolator) {
236 mDefaultInterpolator = interpolator;
237 mScroller.setInterpolator(mDefaultInterpolator);
238 }
239
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700240 public void initParentViews(View parent) {
241 if (mPageIndicatorViewId > -1) {
242 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700243 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700244 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700245 }
246 }
247
Adam Cohen7d30a372013-07-01 17:03:59 -0700248 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700249 private float[] mapPointFromViewToParent(View v, float x, float y) {
250 sTmpPoint[0] = x;
251 sTmpPoint[1] = y;
252 v.getMatrix().mapPoints(sTmpPoint);
253 sTmpPoint[0] += v.getLeft();
254 sTmpPoint[1] += v.getTop();
255 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700256 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700257 private float[] mapPointFromParentToView(View v, float x, float y) {
258 sTmpPoint[0] = x - v.getLeft();
259 sTmpPoint[1] = y - v.getTop();
260 v.getMatrix().invert(sTmpInvMatrix);
261 sTmpInvMatrix.mapPoints(sTmpPoint);
262 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700263 }
264
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700265 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700266 if (mDragView != null) {
267 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
268 (mDragViewBaselineLeft - mDragView.getLeft());
269 float y = mLastMotionY - mDownMotionY;
270 mDragView.setTranslationX(x);
271 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700272
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
274 + x + ", " + y);
275 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700276 }
277
278 public void setMinScale(float f) {
279 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700280 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700281 requestLayout();
282 }
283
284 @Override
285 public void setScaleX(float scaleX) {
286 super.setScaleX(scaleX);
287 if (isReordering(true)) {
288 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
289 mLastMotionX = p[0];
290 mLastMotionY = p[1];
291 updateDragViewTranslationDuringDrag();
292 }
293 }
294
295 // Convenience methods to get the actual width/height of the PagedView (since it is measured
296 // to be larger to account for the minimum possible scale)
297 int getViewportWidth() {
298 return mViewport.width();
299 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800300 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700301 return mViewport.height();
302 }
303
304 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
305 // PagedView both horizontally and vertically
306 int getViewportOffsetX() {
307 return (getMeasuredWidth() - getViewportWidth()) / 2;
308 }
309
310 int getViewportOffsetY() {
311 return (getMeasuredHeight() - getViewportHeight()) / 2;
312 }
313
Hyunyoung Song77441692016-06-27 22:00:48 -0700314 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700315 return mPageIndicator;
316 }
317
Adam Cohen674531f2013-12-13 15:07:14 -0800318 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700319 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
320 * that the user was on before entering free scroll mode (e.g. the home screen page they
321 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
322 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700324 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700325 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700327
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700328 /**
329 * Returns the index of page to be shown immediately afterwards.
330 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700331 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700332 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
333 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700334
Adam Cohenf9c184a2016-01-15 16:47:43 -0800335 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 return getChildCount();
337 }
338
Sunny Goyal83a8f042015-05-19 12:52:12 -0700339 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700340 return getChildAt(index);
341 }
342
Adam Cohenae4f1552011-10-20 00:15:42 -0700343 protected int indexToPage(int index) {
344 return index;
345 }
346
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800348 * Updates the scroll of the current page immediately to its final scroll position. We use this
349 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
350 * the previous tab page.
351 */
352 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700353 // If the current page is invalid, just reset the scroll position to zero
354 int newX = 0;
355 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700356 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700357 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800358 scrollTo(newX, 0);
359 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700360 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800361 }
362
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700363 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700364 mScroller.abortAnimation();
365 // We need to clean up the next page here to avoid computeScrollHelper from
366 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700367 if (resetNextPage) {
368 mNextPage = INVALID_PAGE;
369 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700370 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700371
Tony Wickham8f7ead32016-04-07 18:46:44 -0700372 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700373 mScroller.forceFinished(true);
374 // We need to clean up the next page here to avoid computeScrollHelper from
375 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700376 if (resetNextPage) {
377 mNextPage = INVALID_PAGE;
378 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700379 }
380
Adam Cohen6f127a62014-06-12 14:54:41 -0700381 private int validateNewPage(int newPage) {
382 int validatedPage = newPage;
383 // When in free scroll mode, we need to clamp to the free scroll page range.
384 if (mFreeScroll) {
385 getFreeScrollPageRange(mTempVisiblePagesRange);
386 validatedPage = Math.max(mTempVisiblePagesRange[0],
387 Math.min(newPage, mTempVisiblePagesRange[1]));
388 }
389 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700390 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700391 return validatedPage;
392 }
393
Chet Haasebc2f0822012-10-26 17:59:53 -0700394 /**
Winson Chung86f77532010-08-24 11:08:22 -0700395 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700397 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800398 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700399 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800400 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800401 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
402 // the default
403 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800404 return;
405 }
Adam Cohen6f127a62014-06-12 14:54:41 -0700406 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700407 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700408 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800409 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 }
411
Winson Chung8c87cd82013-07-23 16:20:10 -0700412 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800413 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
414 * has settled.
415 */
Michael Jurka0142d492010-08-25 17:46:15 -0700416 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800417 updatePageIndicator();
418 }
419
420 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700421 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700422 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700423 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700424 if (!isReordering(false)) {
425 mPageIndicator.setActiveMarker(getNextPage());
426 }
Winson Chungd2be3812013-07-16 11:11:32 -0700427 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700429 protected void pageBeginTransition() {
430 if (!mIsPageInTransition) {
431 mIsPageInTransition = true;
432 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700433 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700434 }
435
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700436 protected void pageEndTransition() {
437 if (mIsPageInTransition) {
438 mIsPageInTransition = false;
439 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700440 }
Michael Jurka0142d492010-08-25 17:46:15 -0700441 }
442
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700443 protected boolean isPageInTransition() {
444 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700445 }
446
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700447 /**
448 * Called when the page starts moving as part of the scroll. Subclasses can override this
449 * to provide custom behavior during animation.
450 */
451 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700452 }
453
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700454 /**
455 * Called when the page ends moving as part of the scroll. Subclasses can override this
456 * to provide custom behavior during animation.
457 */
458 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700459 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700460 }
461
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 /**
Winson Chung86f77532010-08-24 11:08:22 -0700463 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 *
465 * @param l The listener used to respond to long clicks.
466 */
467 @Override
468 public void setOnLongClickListener(OnLongClickListener l) {
469 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700470 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700471 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700472 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700473 }
Adam Cohen1697b792013-09-17 19:08:21 -0700474 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
476
Sunny Goyalc86df472016-02-25 09:19:38 -0800477 protected int getUnboundedScrollX() {
478 return getScrollX();
479 }
480
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800482 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800483 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800484 }
485
486 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700487 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700488 // In free scroll mode, we clamp the scrollX
489 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700490 // If the scroller is trying to move to a location beyond the maximum allowed
491 // in the free scroll mode, we make sure to end the scroll operation.
492 if (!mScroller.isFinished() &&
493 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700494 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700495 }
496
Adam Cohenf358a4b2013-07-23 16:47:31 -0700497 x = Math.min(x, mFreeScrollMaxScrollX);
498 x = Math.max(x, mFreeScrollMinScrollX);
499 }
500
Sunny Goyal70660032015-05-14 00:07:08 -0700501 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
502 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700503 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700504 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800505 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700506 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700507 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700508 overScroll(x - mMaxScrollX);
509 } else {
510 overScroll(x);
511 }
Adam Cohen68d73932010-11-15 10:50:58 -0800512 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700514 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800515 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700516 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700517 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700518 overScroll(x);
519 } else {
520 overScroll(x - mMaxScrollX);
521 }
Adam Cohen68d73932010-11-15 10:50:58 -0800522 }
523 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700524 if (mWasInOverscroll) {
525 overScroll(0);
526 mWasInOverscroll = false;
527 }
Adam Cohen68d73932010-11-15 10:50:58 -0800528 super.scrollTo(x, y);
529 }
530
Adam Cohen7d30a372013-07-01 17:03:59 -0700531 // Update the last motion events when scrolling
532 if (isReordering(true)) {
533 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
534 mLastMotionX = p[0];
535 mLastMotionY = p[1];
536 updateDragViewTranslationDuringDrag();
537 }
Michael Jurka0142d492010-08-25 17:46:15 -0700538 }
539
Adam Cohen53805212013-10-01 10:39:23 -0700540 private void sendScrollAccessibilityEvent() {
541 AccessibilityManager am =
542 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
543 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700544 if (mCurrentPage != getNextPage()) {
545 AccessibilityEvent ev =
546 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700547 ev.setScrollable(true);
548 ev.setScrollX(getScrollX());
549 ev.setScrollY(getScrollY());
550 ev.setMaxScrollX(mMaxScrollX);
551 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700552
Vadim Tryshevf4715972015-05-08 17:21:03 -0700553 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700554 }
Adam Cohen53805212013-10-01 10:39:23 -0700555 }
556 }
557
Michael Jurka0142d492010-08-25 17:46:15 -0700558 // we moved this functionality to a helper function so SmoothPagedView can reuse it
559 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800560 return computeScrollHelper(true);
561 }
562
563 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700565 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800566 if (getUnboundedScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800567 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700568 float scaleX = mFreeScroll ? getScaleX() : 1f;
569 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
570 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700571 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800572 if (shouldInvalidate) {
573 invalidate();
574 }
Michael Jurka0142d492010-08-25 17:46:15 -0700575 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800576 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700577 sendScrollAccessibilityEvent();
578
Adam Cohen6f127a62014-06-12 14:54:41 -0700579 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700580 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700581 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700582
Adam Cohen73aa9752010-11-24 16:26:58 -0800583 // We don't want to trigger a page end moving unless the page has settled
584 // and the user has stopped scrolling
585 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700586 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800587 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700588
Adam Cohen7d30a372013-07-01 17:03:59 -0700589 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700590 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700591 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700592 if (am.isEnabled()) {
593 // Notify the user when the page changes
594 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700595 }
Michael Jurka0142d492010-08-25 17:46:15 -0700596 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
Michael Jurka0142d492010-08-25 17:46:15 -0700598 return false;
599 }
600
601 @Override
602 public void computeScroll() {
603 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 }
605
Adam Cohen96d30a12013-07-16 18:13:21 -0700606 public static class LayoutParams extends ViewGroup.LayoutParams {
607 public boolean isFullScreenPage = false;
608
Sunny Goyal7c786f72016-06-01 14:08:21 -0700609 // If true, the start edge of the page snaps to the start edge of the viewport.
610 public boolean matchStartEdge = false;
611
Adam Cohen96d30a12013-07-16 18:13:21 -0700612 /**
613 * {@inheritDoc}
614 */
615 public LayoutParams(int width, int height) {
616 super(width, height);
617 }
618
Sunny Goyal41b22c02015-05-14 15:20:48 -0700619 public LayoutParams(Context context, AttributeSet attrs) {
620 super(context, attrs);
621 }
622
Adam Cohen96d30a12013-07-16 18:13:21 -0700623 public LayoutParams(ViewGroup.LayoutParams source) {
624 super(source);
625 }
626 }
627
Sunny Goyal41b22c02015-05-14 15:20:48 -0700628 @Override
629 public LayoutParams generateLayoutParams(AttributeSet attrs) {
630 return new LayoutParams(getContext(), attrs);
631 }
632
633 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700634 protected LayoutParams generateDefaultLayoutParams() {
635 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
636 }
637
Sunny Goyal41b22c02015-05-14 15:20:48 -0700638 @Override
639 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
640 return new LayoutParams(p);
641 }
642
643 @Override
644 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
645 return p instanceof LayoutParams;
646 }
647
Adam Cohenedb40762013-07-18 16:45:45 -0700648 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700649 LayoutParams lp = generateDefaultLayoutParams();
650 lp.isFullScreenPage = true;
651 super.addView(page, 0, lp);
652 }
653
Adam Cohen410f3cd2013-09-22 12:09:32 -0700654 public int getNormalChildHeight() {
655 return mNormalChildHeight;
656 }
657
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 @Override
659 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700660 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700661 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
662 return;
663 }
664
Adam Cohen7d30a372013-07-01 17:03:59 -0700665 // We measure the dimensions of the PagedView to be larger than the pages so that when we
666 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700667 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
668 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
669 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700670 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800671 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700672 // viewport, we can be at most one and a half screens offset once we scale down
673 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700674 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
675 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700676
Winson Chungc82d2622013-11-06 13:23:29 -0800677 int parentWidthSize = (int) (2f * maxSize);
678 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700679 int scaledWidthSize, scaledHeightSize;
680 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700681 scaledWidthSize = (int) (parentWidthSize / mMinScale);
682 scaledHeightSize = (int) (parentHeightSize / mMinScale);
683 } else {
684 scaledWidthSize = widthSize;
685 scaledHeightSize = heightSize;
686 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700687 mViewport.set(0, 0, widthSize, heightSize);
688
689 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
690 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
691 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700692 }
693
Winson Chung8aad6102012-05-11 16:27:49 -0700694 // Return early if we aren't given a proper dimension
695 if (widthSize <= 0 || heightSize <= 0) {
696 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
697 return;
698 }
699
Adam Lesinski6b879f02010-11-04 16:15:23 -0700700 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
701 * of the All apps view on XLarge displays to not take up more space then it needs. Width
702 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
703 * each page to have the same width.
704 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 final int verticalPadding = getPaddingTop() + getPaddingBottom();
706 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700707
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700708 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700709 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700710 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700711 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700712 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
713 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
714 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
715 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 final int childCount = getChildCount();
717 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700718 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700719 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100720 if (child.getVisibility() != GONE) {
721 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700722
Vladimir Marko2824b072013-10-04 16:42:17 +0100723 int childWidthMode;
724 int childHeightMode;
725 int childWidth;
726 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700727
Vladimir Marko2824b072013-10-04 16:42:17 +0100728 if (!lp.isFullScreenPage) {
729 if (lp.width == LayoutParams.WRAP_CONTENT) {
730 childWidthMode = MeasureSpec.AT_MOST;
731 } else {
732 childWidthMode = MeasureSpec.EXACTLY;
733 }
734
735 if (lp.height == LayoutParams.WRAP_CONTENT) {
736 childHeightMode = MeasureSpec.AT_MOST;
737 } else {
738 childHeightMode = MeasureSpec.EXACTLY;
739 }
740
Adam Cohen3b185e22013-10-29 14:45:58 -0700741 childWidth = getViewportWidth() - horizontalPadding
742 - mInsets.left - mInsets.right;
743 childHeight = getViewportHeight() - verticalPadding
744 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100745 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700746 } else {
747 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700748 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100749
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800750 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700751 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700752 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700753 if (referenceChildWidth == 0) {
754 referenceChildWidth = childWidth;
755 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700756
Vladimir Marko2824b072013-10-04 16:42:17 +0100757 final int childWidthMeasureSpec =
758 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
759 final int childHeightMeasureSpec =
760 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
761 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700762 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700763 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700764 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800765 }
766
Sunny Goyalcf25b522015-07-09 00:01:18 -0700767 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700769 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700770 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700771 return;
772 }
773
Winson Chung785d2eb2011-04-14 16:08:02 -0700774 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700776
Adam Cohen7d30a372013-07-01 17:03:59 -0700777 int offsetX = getViewportOffsetX();
778 int offsetY = getViewportOffsetY();
779
780 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700781 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700782
Sunny Goyal70660032015-05-14 00:07:08 -0700783 final int startIndex = mIsRtl ? childCount - 1 : 0;
784 final int endIndex = mIsRtl ? -1 : childCount;
785 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700786
Adam Cohen7d30a372013-07-01 17:03:59 -0700787 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700788
Adam Cohen3b185e22013-10-29 14:45:58 -0700789 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000790 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700791
Sunny Goyal6178f132016-07-11 17:30:03 -0700792 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700793 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
794 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700795 }
796
Adam Cohen0ffac432013-07-10 11:19:26 -0700797 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700798 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700800 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100801 int childTop;
802 if (lp.isFullScreenPage) {
803 childTop = offsetY;
804 } else {
805 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700806 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100807 }
808
Adam Cohen96d30a12013-07-16 18:13:21 -0700809 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700810 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800811
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700813 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800814 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700815
Sunny Goyal6178f132016-07-11 17:30:03 -0700816 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700817 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000818
819 int pageGap = mPageSpacing;
820 int next = i + delta;
821 if (next != endIndex) {
822 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
823 } else {
824 nextLp = null;
825 }
826
827 // Prevent full screen pages from showing in the viewport
828 // when they are not the current page.
829 if (lp.isFullScreenPage) {
830 pageGap = getPaddingLeft();
831 } else if (nextLp != null && nextLp.isFullScreenPage) {
832 pageGap = getPaddingRight();
833 }
834
Sunny Goyala1fbd842015-05-20 13:40:27 -0700835 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 }
837 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700838
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700839 final LayoutTransition transition = getLayoutTransition();
840 // If the transition is running defer updating max scroll, as some empty pages could
841 // still be present, and a max scroll change could cause sudden jumps in scroll.
842 if (transition != null && transition.isRunning()) {
843 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
844
845 @Override
846 public void startTransition(LayoutTransition transition, ViewGroup container,
847 View view, int transitionType) { }
848
849 @Override
850 public void endTransition(LayoutTransition transition, ViewGroup container,
851 View view, int transitionType) {
852 // Wait until all transitions are complete.
853 if (!transition.isRunning()) {
854 transition.removeTransitionListener(this);
855 updateMaxScrollX();
856 }
857 }
858 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700859 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700860 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700861 }
862
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700863 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
864 updateCurrentPageScroll();
865 mFirstLayout = false;
866 }
867
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700868 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700869 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700870 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700871 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700872
873 if (isReordering(true)) {
874 updateDragViewTranslationDuringDrag();
875 }
Winson Chunge3193b92010-09-10 11:44:42 -0700876 }
877
Sunny Goyala1fbd842015-05-20 13:40:27 -0700878 protected int getChildGap() {
879 return 0;
880 }
881
Sunny Goyal316490e2015-06-02 09:38:28 -0700882 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700883 mMaxScrollX = computeMaxScrollX();
884 }
885
886 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700887 int childCount = getChildCount();
888 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700889 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700890 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700891 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700892 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700893 }
894 }
895
Adam Cohen3b185e22013-10-29 14:45:58 -0700896 public void setPageSpacing(int pageSpacing) {
897 mPageSpacing = pageSpacing;
898 requestLayout();
899 }
900
Winson Chunge3193b92010-09-10 11:44:42 -0700901 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700902 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700903 // Update the page indicator, we don't update the page indicator as we
904 // add/remove pages
905 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700906 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700907 }
908
Adam Cohen2591f6a2011-10-25 14:36:40 -0700909 // This ensures that when children are added, they get the correct transforms / alphas
910 // in accordance with any scroll effects.
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700911 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700912 invalidate();
913 }
914
Michael Jurka8b805b12012-04-18 14:23:14 -0700915 @Override
916 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700917 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700918 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700919 }
920
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700921 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700922 // Update the page indicator, we don't update the page indicator as we
923 // add/remove pages
924 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700925 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700926 }
927 }
928
929 @Override
930 public void removeView(View v) {
931 // XXX: We should find a better way to hook into this before the view
932 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700933 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700934 super.removeView(v);
935 }
936 @Override
937 public void removeViewInLayout(View v) {
938 // XXX: We should find a better way to hook into this before the view
939 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700940 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700941 super.removeViewInLayout(v);
942 }
943 @Override
944 public void removeViewAt(int index) {
945 // XXX: We should find a better way to hook into this before the view
946 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700947 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700948 super.removeViewAt(index);
949 }
950 @Override
951 public void removeAllViewsInLayout() {
952 // Update the page indicator, we don't update the page indicator as we
953 // add/remove pages
954 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700955 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700956 }
957
958 super.removeAllViewsInLayout();
959 }
960
Adam Cohen73894962011-10-31 13:17:17 -0700961 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700962 if (index < 0 || index > getChildCount() - 1) return 0;
963
Adam Cohenf698c6e2013-07-17 18:44:25 -0700964 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700965
Adam Cohenf698c6e2013-07-17 18:44:25 -0700966 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700967 }
968
Adam Cohen6f127a62014-06-12 14:54:41 -0700969 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700970 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700971 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700972 }
973
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -0700975 public void draw(Canvas canvas) {
976 super.draw(canvas);
977 if (getPageCount() > 0) {
978 if (!mEdgeGlowLeft.isFinished()) {
979 final int restoreCount = canvas.save();
980 Rect display = mViewport;
981 canvas.translate(display.left, display.top);
982 canvas.rotate(270);
983
Hyunyoung Song0de01172016-10-05 16:27:48 -0700984 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700985 canvas.translate(display.top - sTmpIntPoint[1], 0);
986 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
987 if (mEdgeGlowLeft.draw(canvas)) {
988 postInvalidateOnAnimation();
989 }
990 canvas.restoreToCount(restoreCount);
991 }
992 if (!mEdgeGlowRight.isFinished()) {
993 final int restoreCount = canvas.save();
994 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700995 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700996 canvas.rotate(90);
997
Hyunyoung Song0de01172016-10-05 16:27:48 -0700998 getEdgeVerticalPosition(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700999
Sunny Goyal93264612015-11-23 11:47:50 -08001000 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1001 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001002 if (mEdgeGlowRight.draw(canvas)) {
1003 postInvalidateOnAnimation();
1004 }
1005 canvas.restoreToCount(restoreCount);
1006 }
1007 }
1008 }
1009
1010 /**
1011 * Returns the top and bottom position for the edge effect.
1012 */
Hyunyoung Song0de01172016-10-05 16:27:48 -07001013 protected abstract void getEdgeVerticalPosition(int[] pos);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001014
1015 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001017 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001018 if (page != mCurrentPage || !mScroller.isFinished()) {
1019 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 return true;
1021 }
1022 return false;
1023 }
1024
1025 @Override
1026 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001027 int focusablePage;
1028 if (mNextPage != INVALID_PAGE) {
1029 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001031 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 }
Winson Chung86f77532010-08-24 11:08:22 -07001033 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001035 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 }
1037 return false;
1038 }
1039
1040 @Override
1041 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001042 if (super.dispatchUnhandledMove(focused, direction)) {
1043 return true;
1044 }
1045
1046 if (mIsRtl) {
1047 if (direction == View.FOCUS_LEFT) {
1048 direction = View.FOCUS_RIGHT;
1049 } else if (direction == View.FOCUS_RIGHT) {
1050 direction = View.FOCUS_LEFT;
1051 }
1052 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001054 if (getCurrentPage() > 0) {
1055 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 return true;
1057 }
1058 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001059 if (getCurrentPage() < getPageCount() - 1) {
1060 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 return true;
1062 }
1063 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001064 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 }
1066
1067 @Override
1068 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -07001069 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1070 return;
1071 }
1072
Adam Cohen0ffac432013-07-10 11:19:26 -07001073 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001074 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001075 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001076 }
1077 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001078 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001079 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 }
1081 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001082 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001083 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 }
1085 }
1086 }
1087
1088 /**
1089 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001090 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001091 *
Winson Chung86f77532010-08-24 11:08:22 -07001092 * This happens when live folders requery, and if they're off page, they
1093 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 */
1095 @Override
1096 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001097 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001098 View v = focused;
1099 while (true) {
1100 if (v == current) {
1101 super.focusableViewAvailable(focused);
1102 return;
1103 }
1104 if (v == this) {
1105 return;
1106 }
1107 ViewParent parent = v.getParent();
1108 if (parent instanceof View) {
1109 v = (View)v.getParent();
1110 } else {
1111 return;
1112 }
1113 }
1114 }
1115
1116 /**
1117 * {@inheritDoc}
1118 */
1119 @Override
1120 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1121 if (disallowIntercept) {
1122 // We need to make sure to cancel our long press if
1123 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001124 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001125 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 }
1127 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1128 }
1129
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001130 /**
1131 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1132 */
1133 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001134 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001135 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001136 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001137 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001138 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001139 }
1140
1141 /**
1142 * Return true if a tap at (x, y) should trigger a flip to the next page.
1143 */
1144 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001145 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001146 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001147 }
1148 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001149 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001150 }
Winson Chung52aee602013-01-30 12:01:02 -08001151
Adam Cohen7d30a372013-07-01 17:03:59 -07001152 /** Returns whether x and y originated within the buffered viewport */
1153 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001154 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001155 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001156 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001157 }
1158
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 @Override
1160 public boolean onInterceptTouchEvent(MotionEvent ev) {
1161 /*
1162 * This method JUST determines whether we want to intercept the motion.
1163 * If we return true, onTouchEvent will be called and we do the actual
1164 * scrolling there.
1165 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001166 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001167
Winson Chung45e1d6e2010-11-09 17:19:49 -08001168 // Skip touch handling if there are no pages to swipe
1169 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1170
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 /*
1172 * Shortcut the most recurring case: the user is in the dragging
1173 * state and he is moving his finger. We want to intercept this
1174 * motion.
1175 */
1176 final int action = ev.getAction();
1177 if ((action == MotionEvent.ACTION_MOVE) &&
1178 (mTouchState == TOUCH_STATE_SCROLLING)) {
1179 return true;
1180 }
1181
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 switch (action & MotionEvent.ACTION_MASK) {
1183 case MotionEvent.ACTION_MOVE: {
1184 /*
1185 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1186 * whether the user has moved far enough from his original down touch.
1187 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001188 if (mActivePointerId != INVALID_POINTER) {
1189 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001190 }
1191 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1192 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1193 // i.e. fall through to the next case (don't break)
1194 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1195 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001196 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
1198
1199 case MotionEvent.ACTION_DOWN: {
1200 final float x = ev.getX();
1201 final float y = ev.getY();
1202 // Remember location of down touch
1203 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001204 mDownMotionY = y;
1205 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 mLastMotionX = x;
1207 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001208 float[] p = mapPointFromViewToParent(this, x, y);
1209 mParentDownMotionX = p[0];
1210 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001211 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001212 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001214
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 /*
1216 * If being flinged and user touches the screen, initiate drag;
1217 * otherwise don't. mScroller.isFinished should be false when
1218 * being flinged.
1219 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001220 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001221 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001222
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001223 if (finishedScrolling) {
1224 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001225 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001226 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001227 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -08001228 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001229 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001230 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1231 mTouchState = TOUCH_STATE_SCROLLING;
1232 } else {
1233 mTouchState = TOUCH_STATE_REST;
1234 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001235 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001236
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 break;
1238 }
1239
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001241 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001242 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 break;
1244
1245 case MotionEvent.ACTION_POINTER_UP:
1246 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001247 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 break;
1249 }
1250
1251 /*
1252 * The only time we want to intercept motion events is if we are in the
1253 * drag mode.
1254 */
1255 return mTouchState != TOUCH_STATE_REST;
1256 }
1257
Adam Cohenf8d28232011-02-01 21:47:00 -08001258 protected void determineScrollingStart(MotionEvent ev) {
1259 determineScrollingStart(ev, 1.0f);
1260 }
1261
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 /*
1263 * Determines if we should change the touch state to start scrolling after the
1264 * user moves their touch point too far.
1265 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001266 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001267 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001269 if (pointerIndex == -1) return;
1270
1271 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 final float x = ev.getX(pointerIndex);
1273 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001274 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1275
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277
Adam Cohenf8d28232011-02-01 21:47:00 -08001278 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001280
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001281 if (xMoved) {
1282 // Scroll if the user moved far enough along the X axis
1283 mTouchState = TOUCH_STATE_SCROLLING;
1284 mTotalMotionX += Math.abs(mLastMotionX - x);
1285 mLastMotionX = x;
1286 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001287 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001288 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001289 // Stop listening for things like pinches.
1290 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001291 }
1292 }
1293
1294 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001295 // Try canceling the long press. It could also have been scheduled
1296 // by a distant descendant, so use the mAllowLongPress flag to block
1297 // everything
1298 final View currentPage = getPageAt(mCurrentPage);
1299 if (currentPage != null) {
1300 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 }
1302 }
1303
Adam Cohenb5ba0972011-09-07 18:02:31 -07001304 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001305 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001306
Adam Cohenedb40762013-07-18 16:45:45 -07001307 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001308 int count = getChildCount();
1309
1310 final int totalDistance;
1311
1312 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001313 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001314 adjacentPage = page - 1;
1315 }
1316
1317 if (adjacentPage < 0 || adjacentPage > count - 1) {
1318 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1319 } else {
1320 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1321 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001322
1323 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001324 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1325 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001326 return scrollProgress;
1327 }
1328
Adam Cohenedb40762013-07-18 16:45:45 -07001329 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001330 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001331 return 0;
1332 } else {
1333 return mPageScrolls[index];
1334 }
1335 }
1336
Adam Cohen564a2e72013-10-09 14:47:32 -07001337 // While layout transitions are occurring, a child's position may stray from its baseline
1338 // position. This method returns the magnitude of this stray at any given time.
1339 public int getLayoutTransitionOffsetForPage(int index) {
1340 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1341 return 0;
1342 } else {
1343 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001344
1345 int scrollOffset = 0;
1346 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1347 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001348 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001349 }
1350
Adam Cohen564a2e72013-10-09 14:47:32 -07001351 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1352 return (int) (child.getX() - baselineX);
1353 }
1354 }
1355
Adam Cohenb5ba0972011-09-07 18:02:31 -07001356 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001358 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001359 if (f < 0) {
1360 mEdgeGlowLeft.onPull(-f);
1361 } else if (f > 0) {
1362 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001363 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001364 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001365 }
1366 invalidate();
1367 }
1368
Adam Cohenb5ba0972011-09-07 18:02:31 -07001369 protected void overScroll(float amount) {
1370 dampedOverScroll(amount);
1371 }
1372
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001373 /**
1374 * return true if freescroll has been enabled, false otherwise
1375 */
1376 public boolean enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001377 setEnableFreeScroll(true);
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001378 return true;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001379 }
1380
Winson Chungdc61c4d2015-04-20 18:26:57 -07001381 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001382 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001383 }
1384
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001385 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001386 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001387 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001388 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1389 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1390 } else {
1391 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1392 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1393 }
1394 }
1395
Adam Cohenf9618852013-11-08 06:45:03 -08001396 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001397 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001398 mFreeScroll = freeScroll;
1399
Adam Cohenf9618852013-11-08 06:45:03 -08001400 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001401 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001402 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1404 setCurrentPage(mTempVisiblePagesRange[0]);
1405 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1406 setCurrentPage(mTempVisiblePagesRange[1]);
1407 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001408 } else if (wasFreeScroll) {
1409 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410 }
1411
1412 setEnableOverscroll(!freeScroll);
1413 }
1414
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001415 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416 mAllowOverScroll = enable;
1417 }
1418
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001419 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 if (mDragView != null) {
1421 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1422 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001423 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 int minDistance = Integer.MAX_VALUE;
1425 int minIndex = indexOfChild(mDragView);
1426 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1427 View page = getPageAt(i);
1428 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1429 int d = Math.abs(dragX - pageX);
1430 if (d < minDistance) {
1431 minIndex = i;
1432 minDistance = d;
1433 }
1434 }
1435 return minIndex;
1436 }
1437 return -1;
1438 }
1439
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 @Override
1441 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001442 super.onTouchEvent(ev);
1443
Winson Chung45e1d6e2010-11-09 17:19:49 -08001444 // Skip touch handling if there are no pages to swipe
1445 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1446
Michael Jurkab8f06722010-10-10 15:58:46 -07001447 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001448
1449 final int action = ev.getAction();
1450
1451 switch (action & MotionEvent.ACTION_MASK) {
1452 case MotionEvent.ACTION_DOWN:
1453 /*
1454 * If being flinged and user touches, stop the fling. isFinished
1455 * will be false if being flinged.
1456 */
1457 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001458 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 }
1460
1461 // Remember where the motion event started
1462 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001463 mDownMotionY = mLastMotionY = ev.getY();
1464 mDownScrollX = getScrollX();
1465 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1466 mParentDownMotionX = p[0];
1467 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001468 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001469 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001471
Michael Jurka0142d492010-08-25 17:46:15 -07001472 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001473 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001474 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001475 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 break;
1477
1478 case MotionEvent.ACTION_MOVE:
1479 if (mTouchState == TOUCH_STATE_SCROLLING) {
1480 // Scroll to follow the motion event
1481 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001482
1483 if (pointerIndex == -1) return true;
1484
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001486 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001487
Adam Cohenaefd4e12011-02-14 16:39:38 -08001488 mTotalMotionX += Math.abs(deltaX);
1489
Winson Chungc0844aa2011-02-02 15:25:58 -08001490 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1491 // keep the remainder because we are actually testing if we've moved from the last
1492 // scrolled position (which is discrete).
1493 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001494 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001495 mLastMotionX = x;
1496 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 } else {
1498 awakenScrollBars();
1499 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001500 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1501 // Update the last motion position
1502 mLastMotionX = ev.getX();
1503 mLastMotionY = ev.getY();
1504
1505 // Update the parent down so that our zoom animations take this new movement into
1506 // account
1507 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1508 mParentDownMotionX = pt[0];
1509 mParentDownMotionY = pt[1];
1510 updateDragViewTranslationDuringDrag();
1511
1512 // Find the closest page to the touch point
1513 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001514
Adam Cohen7d30a372013-07-01 17:03:59 -07001515 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1516 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1517 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1518 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1519
Adam Cohenf358a4b2013-07-23 16:47:31 -07001520 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001521 // Do not allow any page to be moved to 0th position.
1522 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001523 mTempVisiblePagesRange[0] = 0;
1524 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001525 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1527 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1528 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1529 mSidePageHoverIndex = pageUnderPointIndex;
1530 mSidePageHoverRunnable = new Runnable() {
1531 @Override
1532 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001533 // Setup the scroll to the correct page before we swap the views
1534 snapToPage(pageUnderPointIndex);
1535
1536 // For each of the pages between the paged view and the drag view,
1537 // animate them from the previous position to the new position in
1538 // the layout (as a result of the drag view moving in the layout)
1539 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1540 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1541 dragViewIndex + 1 : pageUnderPointIndex;
1542 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1543 dragViewIndex - 1 : pageUnderPointIndex;
1544 for (int i = lowerIndex; i <= upperIndex; ++i) {
1545 View v = getChildAt(i);
1546 // dragViewIndex < pageUnderPointIndex, so after we remove the
1547 // drag view all subsequent views to pageUnderPointIndex will
1548 // shift down.
1549 int oldX = getViewportOffsetX() + getChildOffset(i);
1550 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1551
1552 // Animate the view translation from its old position to its new
1553 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001554 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001555 if (anim != null) {
1556 anim.cancel();
1557 }
1558
1559 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001560 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001561 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001562 anim.start();
1563 v.setTag(anim);
1564 }
1565
1566 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001567 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001568 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001569 if (mPageIndicator != null) {
1570 mPageIndicator.setActiveMarker(getNextPage());
1571 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001572 }
1573 };
1574 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1575 }
1576 } else {
1577 removeCallbacks(mSidePageHoverRunnable);
1578 mSidePageHoverIndex = -1;
1579 }
Adam Cohen564976a2010-10-13 18:52:07 -07001580 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001581 determineScrollingStart(ev);
1582 }
1583 break;
1584
1585 case MotionEvent.ACTION_UP:
1586 if (mTouchState == TOUCH_STATE_SCROLLING) {
1587 final int activePointerId = mActivePointerId;
1588 final int pointerIndex = ev.findPointerIndex(activePointerId);
1589 final float x = ev.getX(pointerIndex);
1590 final VelocityTracker velocityTracker = mVelocityTracker;
1591 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1592 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001593 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001594 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001595 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1596 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001597
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001598 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1599
Adam Cohen00481b32011-11-18 12:03:48 -08001600 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001601 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001602
Adam Cohenf358a4b2013-07-23 16:47:31 -07001603 if (!mFreeScroll) {
1604 // In the case that the page is moved far to one direction and then is flung
1605 // in the opposite direction, we use a threshold to determine whether we should
1606 // just return to the starting page, or if we should skip one further.
1607 boolean returnToOriginalPage = false;
1608 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1609 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1610 returnToOriginalPage = true;
1611 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001612
Adam Cohenf358a4b2013-07-23 16:47:31 -07001613 int finalPage;
1614 // We give flings precedence over large moves, which is why we short-circuit our
1615 // test for a large move if a fling has been registered. That is, a large
1616 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001617 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1618 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001619 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1620 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1621 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1622 snapToPageWithVelocity(finalPage, velocityX);
1623 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1624 (isFling && isVelocityXLeft)) &&
1625 mCurrentPage < getChildCount() - 1) {
1626 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1627 snapToPageWithVelocity(finalPage, velocityX);
1628 } else {
1629 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001630 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001631 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001632 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001633 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001634 }
1635
1636 float scaleX = getScaleX();
1637 int vX = (int) (-velocityX * scaleX);
1638 int initialScrollX = (int) (getScrollX() * scaleX);
1639
Adam Cohenf9618852013-11-08 06:45:03 -08001640 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001641 mScroller.fling(initialScrollX,
1642 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001643 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001644 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001645 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001646 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001647 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1648 // at this point we have not moved beyond the touch slop
1649 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1650 // we can just page
1651 int nextPage = Math.max(0, mCurrentPage - 1);
1652 if (nextPage != mCurrentPage) {
1653 snapToPage(nextPage);
1654 } else {
1655 snapToDestination();
1656 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001657 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001658 // at this point we have not moved beyond the touch slop
1659 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1660 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001661 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1662 if (nextPage != mCurrentPage) {
1663 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001664 } else {
1665 snapToDestination();
1666 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001667 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1668 // Update the last motion position
1669 mLastMotionX = ev.getX();
1670 mLastMotionY = ev.getY();
1671
1672 // Update the parent down so that our zoom animations take this new movement into
1673 // account
1674 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1675 mParentDownMotionX = pt[0];
1676 mParentDownMotionY = pt[1];
1677 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001678 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001679 if (!mCancelTap) {
1680 onUnhandledTap(ev);
1681 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001682 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001683
1684 // Remove the callback to wait for the side page hover timeout
1685 removeCallbacks(mSidePageHoverRunnable);
1686 // End any intermediate reordering states
1687 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001688 break;
1689
1690 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001691 if (mTouchState == TOUCH_STATE_SCROLLING) {
1692 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001693 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001694 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001695 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001696 break;
1697
1698 case MotionEvent.ACTION_POINTER_UP:
1699 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001700 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001701 break;
1702 }
1703
1704 return true;
1705 }
1706
Adam Cohen7d30a372013-07-01 17:03:59 -07001707 private void resetTouchState() {
1708 releaseVelocityTracker();
1709 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001710 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001711 mTouchState = TOUCH_STATE_REST;
1712 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001713 mEdgeGlowLeft.onRelease();
1714 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001715 }
1716
Adam Cohenc2d6e892014-10-16 09:49:24 -07001717 /**
1718 * Triggered by scrolling via touch
1719 */
1720 protected void onScrollInteractionBegin() {
1721 }
1722
1723 protected void onScrollInteractionEnd() {
1724 }
1725
Adam Cohen1697b792013-09-17 19:08:21 -07001726 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001727 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001728 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001729
Winson Chung185d7162011-02-28 13:47:29 -08001730 @Override
1731 public boolean onGenericMotionEvent(MotionEvent event) {
1732 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1733 switch (event.getAction()) {
1734 case MotionEvent.ACTION_SCROLL: {
1735 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1736 final float vscroll;
1737 final float hscroll;
1738 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1739 vscroll = 0;
1740 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1741 } else {
1742 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1743 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1744 }
1745 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001746 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001747 : (hscroll > 0 || vscroll > 0);
1748 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001749 scrollRight();
1750 } else {
1751 scrollLeft();
1752 }
1753 return true;
1754 }
1755 }
1756 }
1757 }
1758 return super.onGenericMotionEvent(event);
1759 }
1760
Michael Jurkab8f06722010-10-10 15:58:46 -07001761 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1762 if (mVelocityTracker == null) {
1763 mVelocityTracker = VelocityTracker.obtain();
1764 }
1765 mVelocityTracker.addMovement(ev);
1766 }
1767
1768 private void releaseVelocityTracker() {
1769 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001770 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001771 mVelocityTracker.recycle();
1772 mVelocityTracker = null;
1773 }
1774 }
1775
Winson Chung321e9ee2010-08-09 13:37:56 -07001776 private void onSecondaryPointerUp(MotionEvent ev) {
1777 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1778 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1779 final int pointerId = ev.getPointerId(pointerIndex);
1780 if (pointerId == mActivePointerId) {
1781 // This was our active pointer going up. Choose a new
1782 // active pointer and adjust accordingly.
1783 // TODO: Make this decision more intelligent.
1784 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1785 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1786 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001787 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001788 mActivePointerId = ev.getPointerId(newPointerIndex);
1789 if (mVelocityTracker != null) {
1790 mVelocityTracker.clear();
1791 }
1792 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001793 }
1794
Winson Chung321e9ee2010-08-09 13:37:56 -07001795 @Override
1796 public void requestChildFocus(View child, View focused) {
1797 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001798 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001799 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001800 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001801 }
1802 }
1803
Adam Cohend19d3ca2010-09-15 14:43:42 -07001804 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001805 return getPageNearestToCenterOfScreen(getScrollX());
1806 }
1807
1808 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1809 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001810 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001811 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001812 final int childCount = getChildCount();
1813 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001814 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001815 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001816 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001817 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001818 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1819 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1820 minDistanceFromScreenCenter = distanceFromScreenCenter;
1821 minDistanceFromScreenCenterIndex = i;
1822 }
1823 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001824 return minDistanceFromScreenCenterIndex;
1825 }
1826
1827 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001828 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001829 }
1830
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001831 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001832 public ScrollInterpolator() {
1833 }
1834
1835 public float getInterpolation(float t) {
1836 t -= 1.0f;
1837 return t*t*t*t*t + 1;
1838 }
1839 }
1840
1841 // We want the duration of the page snap animation to be influenced by the distance that
1842 // the screen has to travel, however, we don't want this duration to be effected in a
1843 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1844 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001845 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001846 f -= 0.5f; // center the values about 0.
1847 f *= 0.3f * Math.PI / 2.0f;
1848 return (float) Math.sin(f);
1849 }
1850
Michael Jurka0142d492010-08-25 17:46:15 -07001851 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001852 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001853 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001854
Adam Cohenedb40762013-07-18 16:45:45 -07001855 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001856 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001857 int duration = 0;
1858
Sunny Goyal4d113a52015-05-27 10:05:28 -07001859 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001860 // If the velocity is low enough, then treat this more as an automatic page advance
1861 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001862 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001863 return;
1864 }
1865
1866 // Here we compute a "distance" that will be used in the computation of the overall
1867 // snap duration. This is a function of the actual distance that needs to be traveled;
1868 // we keep this value close to half screen size in order to reduce the variance in snap
1869 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001870 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001871 float distance = halfScreenSize + halfScreenSize *
1872 distanceInfluenceForSnapDuration(distanceRatio);
1873
1874 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001875 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001876
1877 // we want the page's snap velocity to approximately match the velocity at which the
1878 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001879 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1880 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001881
1882 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001883 }
1884
Sunny Goyal1740d902015-05-27 11:14:01 -07001885 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001886 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001887 }
1888
Adam Cohenf9c184a2016-01-15 16:47:43 -08001889 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001890 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001891 }
1892
Michael Jurka0142d492010-08-25 17:46:15 -07001893 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001894 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001895 }
1896
Adam Cohenf9618852013-11-08 06:45:03 -08001897 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1898 snapToPage(whichPage, duration, false, interpolator);
1899 }
1900
1901 protected void snapToPage(int whichPage, int duration, boolean immediate,
1902 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001903 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001904
Adam Cohenedb40762013-07-18 16:45:45 -07001905 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001906 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001907 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001908 }
1909
1910 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001911 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001912 }
Michael Jurka0142d492010-08-25 17:46:15 -07001913
Adam Cohenf9618852013-11-08 06:45:03 -08001914 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1915 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001916 whichPage = validateNewPage(whichPage);
1917
Adam Cohen7d30a372013-07-01 17:03:59 -07001918 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001919
Winson Chung321e9ee2010-08-09 13:37:56 -07001920 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001921 if (immediate) {
1922 duration = 0;
1923 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001924 duration = Math.abs(delta);
1925 }
1926
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001927 if (duration != 0) {
1928 pageBeginTransition();
1929 }
1930
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001932 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001933 }
Adam Cohenf9618852013-11-08 06:45:03 -08001934
1935 if (interpolator != null) {
1936 mScroller.setInterpolator(interpolator);
1937 } else {
1938 mScroller.setInterpolator(mDefaultInterpolator);
1939 }
1940
Sunny Goyalc86df472016-02-25 09:19:38 -08001941 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001942
Adam Cohen674531f2013-12-13 15:07:14 -08001943 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001944
1945 // Trigger a compute() to finish switching pages if necessary
1946 if (immediate) {
1947 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001948 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001949 }
1950
Winson Chung321e9ee2010-08-09 13:37:56 -07001951 invalidate();
1952 }
1953
Winson Chung321e9ee2010-08-09 13:37:56 -07001954 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001955 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001956 }
1957
1958 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001959 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001960 }
1961
Adam Cohendbdff6b2013-09-18 19:09:15 -07001962 @Override
1963 public boolean performLongClick() {
1964 mCancelTap = true;
1965 return super.performLongClick();
1966 }
1967
Winson Chung321e9ee2010-08-09 13:37:56 -07001968 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001969 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001970
1971 SavedState(Parcelable superState) {
1972 super(superState);
1973 }
1974
Adam Cohen091440a2015-03-18 14:16:05 -07001975 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001976 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001977 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001978 }
1979
1980 @Override
1981 public void writeToParcel(Parcel out, int flags) {
1982 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001983 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 }
1985
1986 public static final Parcelable.Creator<SavedState> CREATOR =
1987 new Parcelable.Creator<SavedState>() {
1988 public SavedState createFromParcel(Parcel in) {
1989 return new SavedState(in);
1990 }
1991
1992 public SavedState[] newArray(int size) {
1993 return new SavedState[size];
1994 }
1995 };
1996 }
1997
Adam Cohen7d30a372013-07-01 17:03:59 -07001998 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001999 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002000 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002001 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2002 .translationX(0)
2003 .translationY(0)
2004 .scaleX(1)
2005 .scaleY(1)
2006 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002007 anim.addListener(new AnimatorListenerAdapter() {
2008 @Override
2009 public void onAnimationEnd(Animator animation) {
2010 onPostReorderingAnimationCompleted();
2011 }
2012 });
2013 anim.start();
2014 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002015 }
2016
Sunny Goyal1d08f702015-05-04 15:50:25 -07002017 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002018 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2019 mTouchState = TOUCH_STATE_REORDERING;
2020 mIsReordering = true;
2021
Adam Cohen7d30a372013-07-01 17:03:59 -07002022 // We must invalidate to trigger a redraw to update the layers such that the drag view
2023 // is always drawn on top
2024 invalidate();
2025 }
2026
Adam Cohen091440a2015-03-18 14:16:05 -07002027 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002028 // Trigger the callback when reordering has settled
2029 --mPostReorderingPreZoomInRemainingAnimationCount;
2030 if (mPostReorderingPreZoomInRunnable != null &&
2031 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2032 mPostReorderingPreZoomInRunnable.run();
2033 mPostReorderingPreZoomInRunnable = null;
2034 }
2035 }
2036
Sunny Goyal1d08f702015-05-04 15:50:25 -07002037 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002039 }
2040
Adam Cohenf358a4b2013-07-23 16:47:31 -07002041 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002042 int dragViewIndex = indexOfChild(v);
2043
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002044 // Do not allow the first page to be moved around
2045 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002046
Adam Cohen7d30a372013-07-01 17:03:59 -07002047 mTempVisiblePagesRange[0] = 0;
2048 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002049 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002050 mReorderingStarted = true;
2051
2052 // Check if we are within the reordering range
2053 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002054 dragViewIndex <= mTempVisiblePagesRange[1]) {
2055 // Find the drag view under the pointer
2056 mDragView = getChildAt(dragViewIndex);
2057 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2058 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002059 snapToPage(getPageNearestToCenterOfScreen());
2060 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002061 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002062 return true;
2063 }
2064 return false;
2065 }
2066
2067 boolean isReordering(boolean testTouchState) {
2068 boolean state = mIsReordering;
2069 if (testTouchState) {
2070 state &= (mTouchState == TOUCH_STATE_REORDERING);
2071 }
2072 return state;
2073 }
2074 void endReordering() {
2075 // For simplicity, we call endReordering sometimes even if reordering was never started.
2076 // In that case, we don't want to do anything.
2077 if (!mReorderingStarted) return;
2078 mReorderingStarted = false;
2079
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002080 mPostReorderingPreZoomInRunnable = new Runnable() {
2081 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002082 // If we haven't flung-to-delete the current child,
2083 // then we just animate the drag view back into position
2084 onEndReordering();
2085
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002086 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08002087 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002088 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002089
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002090 mPostReorderingPreZoomInRemainingAnimationCount =
2091 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2092 // Snap to the current page
2093 snapToPage(indexOfChild(mDragView), 0);
2094 // Animate the drag view back to the front position
2095 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002096 }
2097
Winson Chung6a0f57d2011-06-29 20:10:49 -07002098 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002099 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07002100 @Override
2101 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2102 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002103 info.setScrollable(getPageCount() > 1);
2104 if (getCurrentPage() < getPageCount() - 1) {
2105 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2106 }
2107 if (getCurrentPage() > 0) {
2108 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2109 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002110 info.setClassName(getClass().getName());
2111
2112 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2113 // Besides disabling the accessibility long-click, this also prevents this view from getting
2114 // accessibility focus.
2115 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08002116 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002117 }
2118
2119 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002120 public void sendAccessibilityEvent(int eventType) {
2121 // Don't let the view send real scroll events.
2122 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2123 super.sendAccessibilityEvent(eventType);
2124 }
2125 }
2126
2127 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002128 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2129 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002130 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002131 }
2132
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002133 @Override
2134 public boolean performAccessibilityAction(int action, Bundle arguments) {
2135 if (super.performAccessibilityAction(action, arguments)) {
2136 return true;
2137 }
2138 switch (action) {
2139 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2140 if (getCurrentPage() < getPageCount() - 1) {
2141 scrollRight();
2142 return true;
2143 }
2144 } break;
2145 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2146 if (getCurrentPage() > 0) {
2147 scrollLeft();
2148 return true;
2149 }
2150 } break;
2151 }
2152 return false;
2153 }
2154
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002155 protected String getPageIndicatorDescription() {
2156 return getCurrentPageDescription();
2157 }
2158
Adam Cohen0ffac432013-07-10 11:19:26 -07002159 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002160 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002161 getNextPage() + 1, getChildCount());
2162 }
2163
Winson Chungd11265e2011-08-30 13:37:23 -07002164 @Override
2165 public boolean onHoverEvent(android.view.MotionEvent event) {
2166 return true;
2167 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002168}