blob: 5ac3f0bab9507c0c370b2ab95817f7a2f8790812 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal8bf6f312016-01-23 14:40:35 -080031import android.graphics.RectF;
Sunny Goyal316490e2015-06-02 09:38:28 -070032import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070049import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
53import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070054import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070055import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070056
Winson Chung6a0f57d2011-06-29 20:10:49 -070057import java.util.ArrayList;
58
Winson Chung321e9ee2010-08-09 13:37:56 -070059/**
60 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070061 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070062 */
Michael Jurka8b805b12012-04-18 14:23:14 -070063public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070064 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070065 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070066 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Winson Chung86f77532010-08-24 11:08:22 -070068 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070069 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Vadim Tryshevfedca432015-08-19 17:55:02 -070071 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070072 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070073
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080075 // The page is moved more than halfway, automatically move to the next page on touch up.
76 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080077
Sunny Goyal8e2133b2015-05-06 13:39:07 -070078 private static final float MAX_SCROLL_PROGRESS = 1.0f;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 // The following constants need to be scaled based on density. The scaled versions will be
81 // assigned to the corresponding member variables below.
82 private static final int FLING_THRESHOLD_VELOCITY = 500;
83 private static final int MIN_SNAP_VELOCITY = 1500;
84 private static final int MIN_FLING_VELOCITY = 250;
85
Adam Cohen21cd0022013-10-09 18:57:02 -070086 public static final int INVALID_RESTORE_PAGE = -1001;
87
Adam Cohenf358a4b2013-07-23 16:47:31 -070088 private boolean mFreeScroll = false;
89 private int mFreeScrollMinScrollX = -1;
90 private int mFreeScrollMaxScrollX = -1;
91
Adam Cohen265b9a62011-12-07 14:37:18 -080092 protected int mFlingThresholdVelocity;
93 protected int mMinFlingVelocity;
94 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070097 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070098
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700101 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700102 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700103
Sunny Goyal4ffec482016-02-09 11:28:52 -0800104 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800106 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800107 protected LauncherScroller mScroller;
108 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mParentDownMotionX;
113 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700115 private float mDownMotionY;
116 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700117 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700118 private float mLastMotionX;
119 private float mLastMotionXRemainder;
120 private float mLastMotionY;
121 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700122 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700123
Adam Cohendbdff6b2013-09-18 19:09:15 -0700124 private boolean mCancelTap;
125
Adam Cohenedb40762013-07-18 16:45:45 -0700126 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka0142d492010-08-25 17:46:15 -0700128 protected final static int TOUCH_STATE_REST = 0;
129 protected final static int TOUCH_STATE_SCROLLING = 1;
130 protected final static int TOUCH_STATE_PREV_PAGE = 2;
131 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 protected final static int TOUCH_STATE_REORDERING = 4;
133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700135 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800136
Michael Jurka0142d492010-08-25 17:46:15 -0700137 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka7426c422010-11-11 15:23:47 -0800139 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800141 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800142 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka5f1c5092010-09-03 14:15:02 -0700144 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka0142d492010-08-25 17:46:15 -0700148 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700149 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700150
Patrick Dubroy1262e362010-10-06 15:49:50 -0700151 protected boolean mIsPageMoving = false;
152
Sunny Goyal061380a2016-02-29 15:15:03 -0800153 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700154
Winson Chungd2be3812013-07-16 11:11:32 -0700155 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700157 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700158 // The viewport whether the pages are to be contained (the actual view may be larger than the
159 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800160 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700161 private Rect mViewport = new Rect();
162
163 // Reordering
164 // We use the min scale to determine how much to expand the actually PagedView measured
165 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700166 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700167 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700168 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
169
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700171 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700172 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700174 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 // This variable's scope is only for the duration of startReordering() and endReordering()
176 private boolean mReorderingStarted = false;
177 // This variable's scope is for the duration of startReordering() and after the zoomIn()
178 // animation after endReordering()
179 private boolean mIsReordering;
180 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700181 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700182 private int mPostReorderingPreZoomInRemainingAnimationCount;
183 private Runnable mPostReorderingPreZoomInRunnable;
184
Adam Cohen7d30a372013-07-01 17:03:59 -0700185 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700186 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700187 private static final float[] sTmpPoint = new float[2];
188 private static final int[] sTmpIntPoint = new int[2];
189 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800190 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700191
John Spurlock77e1f472013-09-11 10:09:51 -0400192 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700193 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400194
Sunny Goyal4d113a52015-05-27 10:05:28 -0700195 // Edge effect
196 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
197 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
198
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 public PagedView(Context context) {
200 this(context, null);
201 }
202
203 public PagedView(Context context, AttributeSet attrs) {
204 this(context, attrs, 0);
205 }
206
207 public PagedView(Context context, AttributeSet attrs, int defStyle) {
208 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700209
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 TypedArray a = context.obtainStyledAttributes(attrs,
211 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700212 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 a.recycle();
214
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700216 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700217 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
219
220 /**
221 * Initializes various states for this workspace.
222 */
Michael Jurka0142d492010-08-25 17:46:15 -0700223 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800224 mScroller = new LauncherScroller(getContext());
225 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700226 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227
228 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700229 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800231
Sunny Goyalcf25b522015-07-09 00:01:18 -0700232 float density = getResources().getDisplayMetrics().density;
233 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
234 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
235 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700236 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700237 setWillNotDraw(false);
238 }
239
240 protected void setEdgeGlowColor(int color) {
241 mEdgeGlowLeft.setColor(color);
242 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 }
244
Adam Cohenf9618852013-11-08 06:45:03 -0800245 protected void setDefaultInterpolator(Interpolator interpolator) {
246 mDefaultInterpolator = interpolator;
247 mScroller.setInterpolator(mDefaultInterpolator);
248 }
249
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700250 public void initParentViews(View parent) {
251 if (mPageIndicatorViewId > -1) {
252 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700253 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700254 mPageIndicator.setContentDescription(getCurrentPageDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700255 }
256 }
257
Adam Cohen7d30a372013-07-01 17:03:59 -0700258 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700259 private float[] mapPointFromViewToParent(View v, float x, float y) {
260 sTmpPoint[0] = x;
261 sTmpPoint[1] = y;
262 v.getMatrix().mapPoints(sTmpPoint);
263 sTmpPoint[0] += v.getLeft();
264 sTmpPoint[1] += v.getTop();
265 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700266 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700267 private float[] mapPointFromParentToView(View v, float x, float y) {
268 sTmpPoint[0] = x - v.getLeft();
269 sTmpPoint[1] = y - v.getTop();
270 v.getMatrix().invert(sTmpInvMatrix);
271 sTmpInvMatrix.mapPoints(sTmpPoint);
272 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700273 }
274
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700275 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700276 if (mDragView != null) {
277 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
278 (mDragViewBaselineLeft - mDragView.getLeft());
279 float y = mLastMotionY - mDownMotionY;
280 mDragView.setTranslationX(x);
281 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700282
Adam Cohenf358a4b2013-07-23 16:47:31 -0700283 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
284 + x + ", " + y);
285 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700286 }
287
288 public void setMinScale(float f) {
289 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700290 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700291 requestLayout();
292 }
293
294 @Override
295 public void setScaleX(float scaleX) {
296 super.setScaleX(scaleX);
297 if (isReordering(true)) {
298 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
299 mLastMotionX = p[0];
300 mLastMotionY = p[1];
301 updateDragViewTranslationDuringDrag();
302 }
303 }
304
305 // Convenience methods to get the actual width/height of the PagedView (since it is measured
306 // to be larger to account for the minimum possible scale)
307 int getViewportWidth() {
308 return mViewport.width();
309 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800310 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700311 return mViewport.height();
312 }
313
314 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
315 // PagedView both horizontally and vertically
316 int getViewportOffsetX() {
317 return (getMeasuredWidth() - getViewportWidth()) / 2;
318 }
319
320 int getViewportOffsetY() {
321 return (getMeasuredHeight() - getViewportHeight()) / 2;
322 }
323
Adam Cohenedb40762013-07-18 16:45:45 -0700324 PageIndicator getPageIndicator() {
325 return mPageIndicator;
326 }
327
Adam Cohen674531f2013-12-13 15:07:14 -0800328 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700329 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
330 * that the user was on before entering free scroll mode (e.g. the home screen page they
331 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
332 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700334 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700335 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700337
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700338 /**
339 * Returns the index of page to be shown immediately afterwards.
340 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700341 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700342 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
343 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700344
Adam Cohenf9c184a2016-01-15 16:47:43 -0800345 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700346 return getChildCount();
347 }
348
Sunny Goyal83a8f042015-05-19 12:52:12 -0700349 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 return getChildAt(index);
351 }
352
Adam Cohenae4f1552011-10-20 00:15:42 -0700353 protected int indexToPage(int index) {
354 return index;
355 }
356
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800358 * Updates the scroll of the current page immediately to its final scroll position. We use this
359 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
360 * the previous tab page.
361 */
362 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700363 // If the current page is invalid, just reset the scroll position to zero
364 int newX = 0;
365 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700366 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700367 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800368 scrollTo(newX, 0);
369 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700370 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800371 }
372
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700373 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700374 mScroller.abortAnimation();
375 // We need to clean up the next page here to avoid computeScrollHelper from
376 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700377 if (resetNextPage) {
378 mNextPage = INVALID_PAGE;
379 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700380 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700381
Tony Wickham8f7ead32016-04-07 18:46:44 -0700382 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700383 mScroller.forceFinished(true);
384 // We need to clean up the next page here to avoid computeScrollHelper from
385 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700386 if (resetNextPage) {
387 mNextPage = INVALID_PAGE;
388 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700389 }
390
Adam Cohen6f127a62014-06-12 14:54:41 -0700391 private int validateNewPage(int newPage) {
392 int validatedPage = newPage;
393 // When in free scroll mode, we need to clamp to the free scroll page range.
394 if (mFreeScroll) {
395 getFreeScrollPageRange(mTempVisiblePagesRange);
396 validatedPage = Math.max(mTempVisiblePagesRange[0],
397 Math.min(newPage, mTempVisiblePagesRange[1]));
398 }
399 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700400 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700401 return validatedPage;
402 }
403
Chet Haasebc2f0822012-10-26 17:59:53 -0700404 /**
Winson Chung86f77532010-08-24 11:08:22 -0700405 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700407 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800408 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700409 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800410 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800411 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
412 // the default
413 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800414 return;
415 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700416 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700417 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700418 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700419 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800420 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700421 }
422
Winson Chung8c87cd82013-07-23 16:20:10 -0700423 /**
424 * The restore page will be set in place of the current page at the next (likely first)
425 * layout.
426 */
427 void setRestorePage(int restorePage) {
428 mRestorePage = restorePage;
429 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800430 int getRestorePage() {
431 return mRestorePage;
432 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700433
Adam Cohen674531f2013-12-13 15:07:14 -0800434 /**
435 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
436 * has settled.
437 */
Michael Jurka0142d492010-08-25 17:46:15 -0700438 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800439 updatePageIndicator();
440 }
441
442 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700443 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700444 if (mPageIndicator != null) {
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700445 mPageIndicator.setContentDescription(getCurrentPageDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700446 if (!isReordering(false)) {
447 mPageIndicator.setActiveMarker(getNextPage());
448 }
Winson Chungd2be3812013-07-16 11:11:32 -0700449 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800451 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700452 if (!mIsPageMoving) {
453 mIsPageMoving = true;
454 onPageBeginMoving();
455 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700456 }
457
Michael Jurkace7e05f2011-02-01 22:02:35 -0800458 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700459 if (mIsPageMoving) {
460 mIsPageMoving = false;
461 onPageEndMoving();
462 }
Michael Jurka0142d492010-08-25 17:46:15 -0700463 }
464
Adam Cohen26976d92011-03-22 15:33:33 -0700465 protected boolean isPageMoving() {
466 return mIsPageMoving;
467 }
468
Michael Jurka0142d492010-08-25 17:46:15 -0700469 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700470 protected void onPageBeginMoving() {
471 }
472
473 // a method that subclasses can override to add behavior
474 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700475 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700476 }
477
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 /**
Winson Chung86f77532010-08-24 11:08:22 -0700479 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 *
481 * @param l The listener used to respond to long clicks.
482 */
483 @Override
484 public void setOnLongClickListener(OnLongClickListener l) {
485 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700486 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700488 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
Adam Cohen1697b792013-09-17 19:08:21 -0700490 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 }
492
Sunny Goyalc86df472016-02-25 09:19:38 -0800493 protected int getUnboundedScrollX() {
494 return getScrollX();
495 }
496
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800498 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800499 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800500 }
501
502 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700503 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700504 // In free scroll mode, we clamp the scrollX
505 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700506 // If the scroller is trying to move to a location beyond the maximum allowed
507 // in the free scroll mode, we make sure to end the scroll operation.
508 if (!mScroller.isFinished() &&
509 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700510 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700511 }
512
Adam Cohenf358a4b2013-07-23 16:47:31 -0700513 x = Math.min(x, mFreeScrollMaxScrollX);
514 x = Math.max(x, mFreeScrollMinScrollX);
515 }
516
Sunny Goyal70660032015-05-14 00:07:08 -0700517 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
518 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700519 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700520 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800521 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700522 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700523 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700524 overScroll(x - mMaxScrollX);
525 } else {
526 overScroll(x);
527 }
Adam Cohen68d73932010-11-15 10:50:58 -0800528 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700529 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700530 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800531 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700532 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700533 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700534 overScroll(x);
535 } else {
536 overScroll(x - mMaxScrollX);
537 }
Adam Cohen68d73932010-11-15 10:50:58 -0800538 }
539 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700540 if (mWasInOverscroll) {
541 overScroll(0);
542 mWasInOverscroll = false;
543 }
Adam Cohen68d73932010-11-15 10:50:58 -0800544 super.scrollTo(x, y);
545 }
546
Adam Cohen7d30a372013-07-01 17:03:59 -0700547 // Update the last motion events when scrolling
548 if (isReordering(true)) {
549 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
550 mLastMotionX = p[0];
551 mLastMotionY = p[1];
552 updateDragViewTranslationDuringDrag();
553 }
Michael Jurka0142d492010-08-25 17:46:15 -0700554 }
555
Adam Cohen53805212013-10-01 10:39:23 -0700556 private void sendScrollAccessibilityEvent() {
557 AccessibilityManager am =
558 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
559 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700560 if (mCurrentPage != getNextPage()) {
561 AccessibilityEvent ev =
562 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700563 ev.setScrollable(true);
564 ev.setScrollX(getScrollX());
565 ev.setScrollY(getScrollY());
566 ev.setMaxScrollX(mMaxScrollX);
567 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700568
Vadim Tryshevf4715972015-05-08 17:21:03 -0700569 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700570 }
Adam Cohen53805212013-10-01 10:39:23 -0700571 }
572 }
573
Michael Jurka0142d492010-08-25 17:46:15 -0700574 // we moved this functionality to a helper function so SmoothPagedView can reuse it
575 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800576 return computeScrollHelper(true);
577 }
578
579 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700581 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800583 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700584 float scaleX = mFreeScroll ? getScaleX() : 1f;
585 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
586 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700587 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800588 if (shouldInvalidate) {
589 invalidate();
590 }
Michael Jurka0142d492010-08-25 17:46:15 -0700591 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800592 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700593 sendScrollAccessibilityEvent();
594
Adam Cohen6f127a62014-06-12 14:54:41 -0700595 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700596 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700597 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700598
Adam Cohen73aa9752010-11-24 16:26:58 -0800599 // We don't want to trigger a page end moving unless the page has settled
600 // and the user has stopped scrolling
601 if (mTouchState == TOUCH_STATE_REST) {
602 pageEndMoving();
603 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700604
Adam Cohen7d30a372013-07-01 17:03:59 -0700605 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700606 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700607 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700608 if (am.isEnabled()) {
609 // Notify the user when the page changes
610 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700611 }
Michael Jurka0142d492010-08-25 17:46:15 -0700612 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
Michael Jurka0142d492010-08-25 17:46:15 -0700614 return false;
615 }
616
617 @Override
618 public void computeScroll() {
619 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 }
621
Adam Cohen96d30a12013-07-16 18:13:21 -0700622 public static class LayoutParams extends ViewGroup.LayoutParams {
623 public boolean isFullScreenPage = false;
624
Sunny Goyal7c786f72016-06-01 14:08:21 -0700625 // If true, the start edge of the page snaps to the start edge of the viewport.
626 public boolean matchStartEdge = false;
627
Adam Cohen96d30a12013-07-16 18:13:21 -0700628 /**
629 * {@inheritDoc}
630 */
631 public LayoutParams(int width, int height) {
632 super(width, height);
633 }
634
Sunny Goyal41b22c02015-05-14 15:20:48 -0700635 public LayoutParams(Context context, AttributeSet attrs) {
636 super(context, attrs);
637 }
638
Adam Cohen96d30a12013-07-16 18:13:21 -0700639 public LayoutParams(ViewGroup.LayoutParams source) {
640 super(source);
641 }
642 }
643
Sunny Goyal41b22c02015-05-14 15:20:48 -0700644 @Override
645 public LayoutParams generateLayoutParams(AttributeSet attrs) {
646 return new LayoutParams(getContext(), attrs);
647 }
648
649 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700650 protected LayoutParams generateDefaultLayoutParams() {
651 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
652 }
653
Sunny Goyal41b22c02015-05-14 15:20:48 -0700654 @Override
655 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
656 return new LayoutParams(p);
657 }
658
659 @Override
660 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
661 return p instanceof LayoutParams;
662 }
663
Adam Cohenedb40762013-07-18 16:45:45 -0700664 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700665 LayoutParams lp = generateDefaultLayoutParams();
666 lp.isFullScreenPage = true;
667 super.addView(page, 0, lp);
668 }
669
Adam Cohen410f3cd2013-09-22 12:09:32 -0700670 public int getNormalChildHeight() {
671 return mNormalChildHeight;
672 }
673
Winson Chung321e9ee2010-08-09 13:37:56 -0700674 @Override
675 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700676 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700677 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
678 return;
679 }
680
Adam Cohen7d30a372013-07-01 17:03:59 -0700681 // We measure the dimensions of the PagedView to be larger than the pages so that when we
682 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700683 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
684 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
685 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700686 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800687 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700688 // viewport, we can be at most one and a half screens offset once we scale down
689 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700690 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
691 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700692
Winson Chungc82d2622013-11-06 13:23:29 -0800693 int parentWidthSize = (int) (2f * maxSize);
694 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700695 int scaledWidthSize, scaledHeightSize;
696 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700697 scaledWidthSize = (int) (parentWidthSize / mMinScale);
698 scaledHeightSize = (int) (parentHeightSize / mMinScale);
699 } else {
700 scaledWidthSize = widthSize;
701 scaledHeightSize = heightSize;
702 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700703 mViewport.set(0, 0, widthSize, heightSize);
704
705 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
706 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
707 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 }
709
Winson Chung8aad6102012-05-11 16:27:49 -0700710 // Return early if we aren't given a proper dimension
711 if (widthSize <= 0 || heightSize <= 0) {
712 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
713 return;
714 }
715
Adam Lesinski6b879f02010-11-04 16:15:23 -0700716 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
717 * of the All apps view on XLarge displays to not take up more space then it needs. Width
718 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
719 * each page to have the same width.
720 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 final int verticalPadding = getPaddingTop() + getPaddingBottom();
722 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700723
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700724 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700726 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700727 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700728 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
729 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
730 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
731 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 final int childCount = getChildCount();
733 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700734 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700735 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100736 if (child.getVisibility() != GONE) {
737 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700738
Vladimir Marko2824b072013-10-04 16:42:17 +0100739 int childWidthMode;
740 int childHeightMode;
741 int childWidth;
742 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700743
Vladimir Marko2824b072013-10-04 16:42:17 +0100744 if (!lp.isFullScreenPage) {
745 if (lp.width == LayoutParams.WRAP_CONTENT) {
746 childWidthMode = MeasureSpec.AT_MOST;
747 } else {
748 childWidthMode = MeasureSpec.EXACTLY;
749 }
750
751 if (lp.height == LayoutParams.WRAP_CONTENT) {
752 childHeightMode = MeasureSpec.AT_MOST;
753 } else {
754 childHeightMode = MeasureSpec.EXACTLY;
755 }
756
Adam Cohen3b185e22013-10-29 14:45:58 -0700757 childWidth = getViewportWidth() - horizontalPadding
758 - mInsets.left - mInsets.right;
Sunny Goyal7c786f72016-06-01 14:08:21 -0700759
760 if (lp.matchStartEdge) {
761 childWidth += getPaddingStart();
762 }
Adam Cohen3b185e22013-10-29 14:45:58 -0700763 childHeight = getViewportHeight() - verticalPadding
764 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100765 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700766 } else {
767 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700768 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100769
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800770 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700771 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700772 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700773 if (referenceChildWidth == 0) {
774 referenceChildWidth = childWidth;
775 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700776
Vladimir Marko2824b072013-10-04 16:42:17 +0100777 final int childWidthMeasureSpec =
778 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
779 final int childHeightMeasureSpec =
780 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
781 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700782 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700783 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700784 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800785 }
786
Sunny Goyalcf25b522015-07-09 00:01:18 -0700787 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700789 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700790 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700791 return;
792 }
793
Winson Chung785d2eb2011-04-14 16:08:02 -0700794 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700796
Adam Cohen7d30a372013-07-01 17:03:59 -0700797 int offsetX = getViewportOffsetX();
798 int offsetY = getViewportOffsetY();
799
800 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700801 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700802
Sunny Goyal70660032015-05-14 00:07:08 -0700803 final int startIndex = mIsRtl ? childCount - 1 : 0;
804 final int endIndex = mIsRtl ? -1 : childCount;
805 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700806
Adam Cohen7d30a372013-07-01 17:03:59 -0700807 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700808
Adam Cohen3b185e22013-10-29 14:45:58 -0700809 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000810 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700811
Sunny Goyal7c786f72016-06-01 14:08:21 -0700812 int childLeft = offsetX +
813 ((lp.isFullScreenPage || (!mIsRtl && lp.matchStartEdge)) ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700814 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
815 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700816 }
817
Adam Cohen0ffac432013-07-10 11:19:26 -0700818 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700819 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700821 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100822 int childTop;
823 if (lp.isFullScreenPage) {
824 childTop = offsetY;
825 } else {
826 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700827 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100828 }
829
Adam Cohen96d30a12013-07-16 18:13:21 -0700830 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700831 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800832
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700834 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800835 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700836
Sunny Goyal7c786f72016-06-01 14:08:21 -0700837 int scrollOffsetLeft = (lp.isFullScreenPage || (!mIsRtl & lp.matchStartEdge)) ?
838 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700839 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000840
841 int pageGap = mPageSpacing;
842 int next = i + delta;
843 if (next != endIndex) {
844 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
845 } else {
846 nextLp = null;
847 }
848
849 // Prevent full screen pages from showing in the viewport
850 // when they are not the current page.
851 if (lp.isFullScreenPage) {
852 pageGap = getPaddingLeft();
853 } else if (nextLp != null && nextLp.isFullScreenPage) {
854 pageGap = getPaddingRight();
855 }
856
Sunny Goyala1fbd842015-05-20 13:40:27 -0700857 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 }
859 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700860
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700861 final LayoutTransition transition = getLayoutTransition();
862 // If the transition is running defer updating max scroll, as some empty pages could
863 // still be present, and a max scroll change could cause sudden jumps in scroll.
864 if (transition != null && transition.isRunning()) {
865 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
866
867 @Override
868 public void startTransition(LayoutTransition transition, ViewGroup container,
869 View view, int transitionType) { }
870
871 @Override
872 public void endTransition(LayoutTransition transition, ViewGroup container,
873 View view, int transitionType) {
874 // Wait until all transitions are complete.
875 if (!transition.isRunning()) {
876 transition.removeTransitionListener(this);
877 updateMaxScrollX();
878 }
879 }
880 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700881 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700882 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700883 }
884
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700885 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
886 updateCurrentPageScroll();
887 mFirstLayout = false;
888 }
889
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700890 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700891 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700892 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700893 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700894 } else {
895 setCurrentPage(getNextPage());
896 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700897 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700898 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700899
900 if (isReordering(true)) {
901 updateDragViewTranslationDuringDrag();
902 }
Winson Chunge3193b92010-09-10 11:44:42 -0700903 }
904
Sunny Goyala1fbd842015-05-20 13:40:27 -0700905 protected int getChildGap() {
906 return 0;
907 }
908
Sunny Goyal316490e2015-06-02 09:38:28 -0700909 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700910 mMaxScrollX = computeMaxScrollX();
911 }
912
913 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700914 int childCount = getChildCount();
915 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700916 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700917 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700918 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700919 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700920 }
921 }
922
Adam Cohen3b185e22013-10-29 14:45:58 -0700923 public void setPageSpacing(int pageSpacing) {
924 mPageSpacing = pageSpacing;
925 requestLayout();
926 }
927
Sunny Goyal4d113a52015-05-27 10:05:28 -0700928 /**
929 * Called when the center screen changes during scrolling.
930 */
931 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700932
Winson Chunge3193b92010-09-10 11:44:42 -0700933 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700934 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700935 // Update the page indicator, we don't update the page indicator as we
936 // add/remove pages
937 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700938 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700939 }
940
Adam Cohen2591f6a2011-10-25 14:36:40 -0700941 // This ensures that when children are added, they get the correct transforms / alphas
942 // in accordance with any scroll effects.
943 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700944 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700945 invalidate();
946 }
947
Michael Jurka8b805b12012-04-18 14:23:14 -0700948 @Override
949 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700950 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700951 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700952 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700953 }
954
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700955 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700956 // Update the page indicator, we don't update the page indicator as we
957 // add/remove pages
958 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700959 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700960 }
961 }
962
963 @Override
964 public void removeView(View v) {
965 // XXX: We should find a better way to hook into this before the view
966 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700967 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700968 super.removeView(v);
969 }
970 @Override
971 public void removeViewInLayout(View v) {
972 // XXX: We should find a better way to hook into this before the view
973 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700974 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700975 super.removeViewInLayout(v);
976 }
977 @Override
978 public void removeViewAt(int index) {
979 // XXX: We should find a better way to hook into this before the view
980 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700981 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700982 super.removeViewAt(index);
983 }
984 @Override
985 public void removeAllViewsInLayout() {
986 // Update the page indicator, we don't update the page indicator as we
987 // add/remove pages
988 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700989 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700990 }
991
992 super.removeAllViewsInLayout();
993 }
994
Adam Cohen73894962011-10-31 13:17:17 -0700995 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700996 if (index < 0 || index > getChildCount() - 1) return 0;
997
Adam Cohenf698c6e2013-07-17 18:44:25 -0700998 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700999
Adam Cohenf698c6e2013-07-17 18:44:25 -07001000 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001001 }
1002
Adam Cohen6f127a62014-06-12 14:54:41 -07001003 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001004 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001005 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001006 }
1007
Michael Jurkadde558b2011-11-09 22:09:06 -08001008 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001009 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001010 range[0] = -1;
1011 range[1] = -1;
1012
Sunny Goyalee688162016-02-12 14:24:21 -08001013 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001014 final int visibleLeft = -getLeft();
1015 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001016 final Matrix pageShiftMatrix = getPageShiftMatrix();
1017 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001018
Sunny Goyalee688162016-02-12 14:24:21 -08001019 for (int i = 0; i < count; i++) {
1020 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001021
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001022 // Verify if the page bounds are within the visible range.
1023 sTmpRectF.left = 0;
1024 sTmpRectF.right = currPage.getMeasuredWidth();
1025 currPage.getMatrix().mapRect(sTmpRectF);
1026 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001027 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001028
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001029 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001030 if (range[0] == -1) {
1031 continue;
1032 } else {
1033 break;
1034 }
1035 }
Sunny Goyalee688162016-02-12 14:24:21 -08001036 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001037 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001038 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001039 }
1040 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001041
Sunny Goyalee688162016-02-12 14:24:21 -08001042 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001043 } else {
1044 range[0] = -1;
1045 range[1] = -1;
1046 }
1047 }
1048
Sunny Goyalee688162016-02-12 14:24:21 -08001049 protected Matrix getPageShiftMatrix() {
1050 return getMatrix();
1051 }
1052
Michael Jurka920d7f42012-05-14 16:29:55 -07001053 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001054 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001055 }
1056
Michael Jurkadde558b2011-11-09 22:09:06 -08001057 @Override
1058 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001059 // Find out which screens are visible; as an optimization we only call draw on them
1060 final int pageCount = getChildCount();
1061 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001062 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001063 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001064
1065 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1066 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1067 // set it for the next frame
1068 mForceScreenScrolled = false;
1069 screenScrolled(screenCenter);
1070 mLastScreenCenter = screenCenter;
1071 }
1072
Michael Jurkadde558b2011-11-09 22:09:06 -08001073 getVisiblePages(mTempVisiblePagesRange);
1074 final int leftScreen = mTempVisiblePagesRange[0];
1075 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001076 if (leftScreen != -1 && rightScreen != -1) {
1077 final long drawingTime = getDrawingTime();
1078 // Clip to the bounds
1079 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001080 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1081 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001082
Adam Cohen7d30a372013-07-01 17:03:59 -07001083 // Draw all the children, leaving the drag view for last
1084 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001085 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001086 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001087 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001088 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001089 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001090 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001091 // Draw the drag view on top (if there is one)
1092 if (mDragView != null) {
1093 drawChild(canvas, mDragView, drawingTime);
1094 }
1095
Winson Chungc6f10b92011-11-14 11:39:07 -08001096 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001097 }
Michael Jurka0142d492010-08-25 17:46:15 -07001098 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001099 }
1100
1101 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001102 public void draw(Canvas canvas) {
1103 super.draw(canvas);
1104 if (getPageCount() > 0) {
1105 if (!mEdgeGlowLeft.isFinished()) {
1106 final int restoreCount = canvas.save();
1107 Rect display = mViewport;
1108 canvas.translate(display.left, display.top);
1109 canvas.rotate(270);
1110
1111 getEdgeVerticalPostion(sTmpIntPoint);
1112 canvas.translate(display.top - sTmpIntPoint[1], 0);
1113 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1114 if (mEdgeGlowLeft.draw(canvas)) {
1115 postInvalidateOnAnimation();
1116 }
1117 canvas.restoreToCount(restoreCount);
1118 }
1119 if (!mEdgeGlowRight.isFinished()) {
1120 final int restoreCount = canvas.save();
1121 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001122 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001123 canvas.rotate(90);
1124
1125 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001126
Sunny Goyal93264612015-11-23 11:47:50 -08001127 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1128 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001129 if (mEdgeGlowRight.draw(canvas)) {
1130 postInvalidateOnAnimation();
1131 }
1132 canvas.restoreToCount(restoreCount);
1133 }
1134 }
1135 }
1136
1137 /**
1138 * Returns the top and bottom position for the edge effect.
1139 */
1140 protected abstract void getEdgeVerticalPostion(int[] pos);
1141
1142 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001143 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001144 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001145 if (page != mCurrentPage || !mScroller.isFinished()) {
1146 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001147 return true;
1148 }
1149 return false;
1150 }
1151
1152 @Override
1153 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001154 int focusablePage;
1155 if (mNextPage != INVALID_PAGE) {
1156 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001158 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 }
Winson Chung86f77532010-08-24 11:08:22 -07001160 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001162 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 }
1164 return false;
1165 }
1166
1167 @Override
1168 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001169 if (super.dispatchUnhandledMove(focused, direction)) {
1170 return true;
1171 }
1172
1173 if (mIsRtl) {
1174 if (direction == View.FOCUS_LEFT) {
1175 direction = View.FOCUS_RIGHT;
1176 } else if (direction == View.FOCUS_RIGHT) {
1177 direction = View.FOCUS_LEFT;
1178 }
1179 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001181 if (getCurrentPage() > 0) {
1182 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 return true;
1184 }
1185 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001186 if (getCurrentPage() < getPageCount() - 1) {
1187 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 return true;
1189 }
1190 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001191 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 }
1193
1194 @Override
1195 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001196 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001197 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001198 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 }
1200 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001201 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001202 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 }
1204 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001205 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001206 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 }
1208 }
1209 }
1210
1211 /**
1212 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001213 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 *
Winson Chung86f77532010-08-24 11:08:22 -07001215 * This happens when live folders requery, and if they're off page, they
1216 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 */
1218 @Override
1219 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001220 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 View v = focused;
1222 while (true) {
1223 if (v == current) {
1224 super.focusableViewAvailable(focused);
1225 return;
1226 }
1227 if (v == this) {
1228 return;
1229 }
1230 ViewParent parent = v.getParent();
1231 if (parent instanceof View) {
1232 v = (View)v.getParent();
1233 } else {
1234 return;
1235 }
1236 }
1237 }
1238
1239 /**
1240 * {@inheritDoc}
1241 */
1242 @Override
1243 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1244 if (disallowIntercept) {
1245 // We need to make sure to cancel our long press if
1246 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001247 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001248 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 }
1250 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1251 }
1252
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001253 /**
1254 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1255 */
1256 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001257 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001258 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001259 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001260 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001261 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001262 }
1263
1264 /**
1265 * Return true if a tap at (x, y) should trigger a flip to the next page.
1266 */
1267 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001268 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001269 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001270 }
1271 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001272 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001273 }
Winson Chung52aee602013-01-30 12:01:02 -08001274
Adam Cohen7d30a372013-07-01 17:03:59 -07001275 /** Returns whether x and y originated within the buffered viewport */
1276 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001277 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001278 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001279 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001280 }
1281
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 @Override
1283 public boolean onInterceptTouchEvent(MotionEvent ev) {
1284 /*
1285 * This method JUST determines whether we want to intercept the motion.
1286 * If we return true, onTouchEvent will be called and we do the actual
1287 * scrolling there.
1288 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001289 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001290
Winson Chung45e1d6e2010-11-09 17:19:49 -08001291 // Skip touch handling if there are no pages to swipe
1292 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1293
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 /*
1295 * Shortcut the most recurring case: the user is in the dragging
1296 * state and he is moving his finger. We want to intercept this
1297 * motion.
1298 */
1299 final int action = ev.getAction();
1300 if ((action == MotionEvent.ACTION_MOVE) &&
1301 (mTouchState == TOUCH_STATE_SCROLLING)) {
1302 return true;
1303 }
1304
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 switch (action & MotionEvent.ACTION_MASK) {
1306 case MotionEvent.ACTION_MOVE: {
1307 /*
1308 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1309 * whether the user has moved far enough from his original down touch.
1310 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001311 if (mActivePointerId != INVALID_POINTER) {
1312 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001313 }
1314 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1315 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1316 // i.e. fall through to the next case (don't break)
1317 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1318 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001319 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 }
1321
1322 case MotionEvent.ACTION_DOWN: {
1323 final float x = ev.getX();
1324 final float y = ev.getY();
1325 // Remember location of down touch
1326 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001327 mDownMotionY = y;
1328 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 mLastMotionX = x;
1330 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001331 float[] p = mapPointFromViewToParent(this, x, y);
1332 mParentDownMotionX = p[0];
1333 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001334 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001335 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001337
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 /*
1339 * If being flinged and user touches the screen, initiate drag;
1340 * otherwise don't. mScroller.isFinished should be false when
1341 * being flinged.
1342 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001343 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001344 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001345
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001346 if (finishedScrolling) {
1347 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001348 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001349 setCurrentPage(getNextPage());
1350 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001351 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001352 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001353 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1354 mTouchState = TOUCH_STATE_SCROLLING;
1355 } else {
1356 mTouchState = TOUCH_STATE_REST;
1357 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001358 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001359
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 break;
1361 }
1362
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001364 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001365 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 break;
1367
1368 case MotionEvent.ACTION_POINTER_UP:
1369 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001370 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 break;
1372 }
1373
1374 /*
1375 * The only time we want to intercept motion events is if we are in the
1376 * drag mode.
1377 */
1378 return mTouchState != TOUCH_STATE_REST;
1379 }
1380
Adam Cohenf8d28232011-02-01 21:47:00 -08001381 protected void determineScrollingStart(MotionEvent ev) {
1382 determineScrollingStart(ev, 1.0f);
1383 }
1384
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 /*
1386 * Determines if we should change the touch state to start scrolling after the
1387 * user moves their touch point too far.
1388 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001389 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001390 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001392 if (pointerIndex == -1) return;
1393
1394 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 final float x = ev.getX(pointerIndex);
1396 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1398
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001400
Adam Cohenf8d28232011-02-01 21:47:00 -08001401 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001403
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001404 if (xMoved) {
1405 // Scroll if the user moved far enough along the X axis
1406 mTouchState = TOUCH_STATE_SCROLLING;
1407 mTotalMotionX += Math.abs(mLastMotionX - x);
1408 mLastMotionX = x;
1409 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001410 onScrollInteractionBegin();
1411 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001412 // Stop listening for things like pinches.
1413 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001414 }
1415 }
1416
1417 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001418 // Try canceling the long press. It could also have been scheduled
1419 // by a distant descendant, so use the mAllowLongPress flag to block
1420 // everything
1421 final View currentPage = getPageAt(mCurrentPage);
1422 if (currentPage != null) {
1423 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 }
1425 }
1426
Adam Cohenb5ba0972011-09-07 18:02:31 -07001427 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001428 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001429
Adam Cohenedb40762013-07-18 16:45:45 -07001430 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001431 int count = getChildCount();
1432
1433 final int totalDistance;
1434
1435 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001436 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001437 adjacentPage = page - 1;
1438 }
1439
1440 if (adjacentPage < 0 || adjacentPage > count - 1) {
1441 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1442 } else {
1443 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1444 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001445
1446 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001447 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1448 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001449 return scrollProgress;
1450 }
1451
Adam Cohenedb40762013-07-18 16:45:45 -07001452 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001453 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001454 return 0;
1455 } else {
1456 return mPageScrolls[index];
1457 }
1458 }
1459
Adam Cohen564a2e72013-10-09 14:47:32 -07001460 // While layout transitions are occurring, a child's position may stray from its baseline
1461 // position. This method returns the magnitude of this stray at any given time.
1462 public int getLayoutTransitionOffsetForPage(int index) {
1463 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1464 return 0;
1465 } else {
1466 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001467
1468 int scrollOffset = 0;
1469 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1470 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001471 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001472 }
1473
Adam Cohen564a2e72013-10-09 14:47:32 -07001474 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1475 return (int) (child.getX() - baselineX);
1476 }
1477 }
1478
Adam Cohenb5ba0972011-09-07 18:02:31 -07001479 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001480 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001481 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001482 if (f < 0) {
1483 mEdgeGlowLeft.onPull(-f);
1484 } else if (f > 0) {
1485 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001486 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001487 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001488 }
1489 invalidate();
1490 }
1491
Adam Cohenb5ba0972011-09-07 18:02:31 -07001492 protected void overScroll(float amount) {
1493 dampedOverScroll(amount);
1494 }
1495
Winson Chungdc61c4d2015-04-20 18:26:57 -07001496 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001497 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001498 }
1499
Winson Chungdc61c4d2015-04-20 18:26:57 -07001500 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001501 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001502 }
1503
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001504 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001505 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001506 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001507 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1508 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1509 } else {
1510 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1511 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1512 }
1513 }
1514
Adam Cohenf9618852013-11-08 06:45:03 -08001515 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001516 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001517 mFreeScroll = freeScroll;
1518
Adam Cohenf9618852013-11-08 06:45:03 -08001519 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001520 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001521 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001522 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1523 setCurrentPage(mTempVisiblePagesRange[0]);
1524 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1525 setCurrentPage(mTempVisiblePagesRange[1]);
1526 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001527 } else if (wasFreeScroll) {
1528 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001529 }
1530
1531 setEnableOverscroll(!freeScroll);
1532 }
1533
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001534 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001535 mAllowOverScroll = enable;
1536 }
1537
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001538 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001539 if (mDragView != null) {
1540 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1541 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001542 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001543 int minDistance = Integer.MAX_VALUE;
1544 int minIndex = indexOfChild(mDragView);
1545 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1546 View page = getPageAt(i);
1547 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1548 int d = Math.abs(dragX - pageX);
1549 if (d < minDistance) {
1550 minIndex = i;
1551 minDistance = d;
1552 }
1553 }
1554 return minIndex;
1555 }
1556 return -1;
1557 }
1558
Winson Chung321e9ee2010-08-09 13:37:56 -07001559 @Override
1560 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001561 super.onTouchEvent(ev);
1562
Winson Chung45e1d6e2010-11-09 17:19:49 -08001563 // Skip touch handling if there are no pages to swipe
1564 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1565
Michael Jurkab8f06722010-10-10 15:58:46 -07001566 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001567
1568 final int action = ev.getAction();
1569
1570 switch (action & MotionEvent.ACTION_MASK) {
1571 case MotionEvent.ACTION_DOWN:
1572 /*
1573 * If being flinged and user touches, stop the fling. isFinished
1574 * will be false if being flinged.
1575 */
1576 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001577 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001578 }
1579
1580 // Remember where the motion event started
1581 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001582 mDownMotionY = mLastMotionY = ev.getY();
1583 mDownScrollX = getScrollX();
1584 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1585 mParentDownMotionX = p[0];
1586 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001587 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001588 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001590
Michael Jurka0142d492010-08-25 17:46:15 -07001591 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001592 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001593 pageBeginMoving();
1594 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001595 break;
1596
1597 case MotionEvent.ACTION_MOVE:
1598 if (mTouchState == TOUCH_STATE_SCROLLING) {
1599 // Scroll to follow the motion event
1600 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001601
1602 if (pointerIndex == -1) return true;
1603
Winson Chung321e9ee2010-08-09 13:37:56 -07001604 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001605 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001606
Adam Cohenaefd4e12011-02-14 16:39:38 -08001607 mTotalMotionX += Math.abs(deltaX);
1608
Winson Chungc0844aa2011-02-02 15:25:58 -08001609 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1610 // keep the remainder because we are actually testing if we've moved from the last
1611 // scrolled position (which is discrete).
1612 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001613 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001614 mLastMotionX = x;
1615 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001616 } else {
1617 awakenScrollBars();
1618 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001619 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1620 // Update the last motion position
1621 mLastMotionX = ev.getX();
1622 mLastMotionY = ev.getY();
1623
1624 // Update the parent down so that our zoom animations take this new movement into
1625 // account
1626 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1627 mParentDownMotionX = pt[0];
1628 mParentDownMotionY = pt[1];
1629 updateDragViewTranslationDuringDrag();
1630
1631 // Find the closest page to the touch point
1632 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001633
Adam Cohen7d30a372013-07-01 17:03:59 -07001634 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1635 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1636 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1637 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1638
Adam Cohenf358a4b2013-07-23 16:47:31 -07001639 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001640 // Do not allow any page to be moved to 0th position.
1641 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001642 mTempVisiblePagesRange[0] = 0;
1643 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001644 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001645 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1646 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1647 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1648 mSidePageHoverIndex = pageUnderPointIndex;
1649 mSidePageHoverRunnable = new Runnable() {
1650 @Override
1651 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001652 // Setup the scroll to the correct page before we swap the views
1653 snapToPage(pageUnderPointIndex);
1654
1655 // For each of the pages between the paged view and the drag view,
1656 // animate them from the previous position to the new position in
1657 // the layout (as a result of the drag view moving in the layout)
1658 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1659 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1660 dragViewIndex + 1 : pageUnderPointIndex;
1661 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1662 dragViewIndex - 1 : pageUnderPointIndex;
1663 for (int i = lowerIndex; i <= upperIndex; ++i) {
1664 View v = getChildAt(i);
1665 // dragViewIndex < pageUnderPointIndex, so after we remove the
1666 // drag view all subsequent views to pageUnderPointIndex will
1667 // shift down.
1668 int oldX = getViewportOffsetX() + getChildOffset(i);
1669 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1670
1671 // Animate the view translation from its old position to its new
1672 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001673 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001674 if (anim != null) {
1675 anim.cancel();
1676 }
1677
1678 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001679 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001680 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001681 anim.start();
1682 v.setTag(anim);
1683 }
1684
1685 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001686 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001687 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001688 if (mPageIndicator != null) {
1689 mPageIndicator.setActiveMarker(getNextPage());
1690 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001691 }
1692 };
1693 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1694 }
1695 } else {
1696 removeCallbacks(mSidePageHoverRunnable);
1697 mSidePageHoverIndex = -1;
1698 }
Adam Cohen564976a2010-10-13 18:52:07 -07001699 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001700 determineScrollingStart(ev);
1701 }
1702 break;
1703
1704 case MotionEvent.ACTION_UP:
1705 if (mTouchState == TOUCH_STATE_SCROLLING) {
1706 final int activePointerId = mActivePointerId;
1707 final int pointerIndex = ev.findPointerIndex(activePointerId);
1708 final float x = ev.getX(pointerIndex);
1709 final VelocityTracker velocityTracker = mVelocityTracker;
1710 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1711 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001712 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001713 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001714 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1715 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001716
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001717 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1718
Adam Cohen00481b32011-11-18 12:03:48 -08001719 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001720 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001721
Adam Cohenf358a4b2013-07-23 16:47:31 -07001722 if (!mFreeScroll) {
1723 // In the case that the page is moved far to one direction and then is flung
1724 // in the opposite direction, we use a threshold to determine whether we should
1725 // just return to the starting page, or if we should skip one further.
1726 boolean returnToOriginalPage = false;
1727 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1728 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1729 returnToOriginalPage = true;
1730 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001731
Adam Cohenf358a4b2013-07-23 16:47:31 -07001732 int finalPage;
1733 // We give flings precedence over large moves, which is why we short-circuit our
1734 // test for a large move if a fling has been registered. That is, a large
1735 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001736 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1737 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001738 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1739 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1740 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1741 snapToPageWithVelocity(finalPage, velocityX);
1742 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1743 (isFling && isVelocityXLeft)) &&
1744 mCurrentPage < getChildCount() - 1) {
1745 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1746 snapToPageWithVelocity(finalPage, velocityX);
1747 } else {
1748 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001749 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001750 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001751 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001752 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001753 }
1754
1755 float scaleX = getScaleX();
1756 int vX = (int) (-velocityX * scaleX);
1757 int initialScrollX = (int) (getScrollX() * scaleX);
1758
Adam Cohenf9618852013-11-08 06:45:03 -08001759 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001760 mScroller.fling(initialScrollX,
1761 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001762 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001763 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001764 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001765 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001766 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1767 // at this point we have not moved beyond the touch slop
1768 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1769 // we can just page
1770 int nextPage = Math.max(0, mCurrentPage - 1);
1771 if (nextPage != mCurrentPage) {
1772 snapToPage(nextPage);
1773 } else {
1774 snapToDestination();
1775 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001776 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001777 // at this point we have not moved beyond the touch slop
1778 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1779 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001780 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1781 if (nextPage != mCurrentPage) {
1782 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001783 } else {
1784 snapToDestination();
1785 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001786 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1787 // Update the last motion position
1788 mLastMotionX = ev.getX();
1789 mLastMotionY = ev.getY();
1790
1791 // Update the parent down so that our zoom animations take this new movement into
1792 // account
1793 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1794 mParentDownMotionX = pt[0];
1795 mParentDownMotionY = pt[1];
1796 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001797 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001798 if (!mCancelTap) {
1799 onUnhandledTap(ev);
1800 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001801 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001802
1803 // Remove the callback to wait for the side page hover timeout
1804 removeCallbacks(mSidePageHoverRunnable);
1805 // End any intermediate reordering states
1806 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001807 break;
1808
1809 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001810 if (mTouchState == TOUCH_STATE_SCROLLING) {
1811 snapToDestination();
1812 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001813 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001814 break;
1815
1816 case MotionEvent.ACTION_POINTER_UP:
1817 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001818 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001819 break;
1820 }
1821
1822 return true;
1823 }
1824
Adam Cohen7d30a372013-07-01 17:03:59 -07001825 private void resetTouchState() {
1826 releaseVelocityTracker();
1827 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001828 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 mTouchState = TOUCH_STATE_REST;
1830 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001831 mEdgeGlowLeft.onRelease();
1832 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001833 }
1834
Adam Cohenc2d6e892014-10-16 09:49:24 -07001835 /**
1836 * Triggered by scrolling via touch
1837 */
1838 protected void onScrollInteractionBegin() {
1839 }
1840
1841 protected void onScrollInteractionEnd() {
1842 }
1843
Adam Cohen1697b792013-09-17 19:08:21 -07001844 protected void onUnhandledTap(MotionEvent ev) {
1845 ((Launcher) getContext()).onClick(this);
1846 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001847
Winson Chung185d7162011-02-28 13:47:29 -08001848 @Override
1849 public boolean onGenericMotionEvent(MotionEvent event) {
1850 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1851 switch (event.getAction()) {
1852 case MotionEvent.ACTION_SCROLL: {
1853 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1854 final float vscroll;
1855 final float hscroll;
1856 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1857 vscroll = 0;
1858 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1859 } else {
1860 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1861 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1862 }
1863 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001864 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001865 : (hscroll > 0 || vscroll > 0);
1866 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001867 scrollRight();
1868 } else {
1869 scrollLeft();
1870 }
1871 return true;
1872 }
1873 }
1874 }
1875 }
1876 return super.onGenericMotionEvent(event);
1877 }
1878
Michael Jurkab8f06722010-10-10 15:58:46 -07001879 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1880 if (mVelocityTracker == null) {
1881 mVelocityTracker = VelocityTracker.obtain();
1882 }
1883 mVelocityTracker.addMovement(ev);
1884 }
1885
1886 private void releaseVelocityTracker() {
1887 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001888 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001889 mVelocityTracker.recycle();
1890 mVelocityTracker = null;
1891 }
1892 }
1893
Winson Chung321e9ee2010-08-09 13:37:56 -07001894 private void onSecondaryPointerUp(MotionEvent ev) {
1895 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1896 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1897 final int pointerId = ev.getPointerId(pointerIndex);
1898 if (pointerId == mActivePointerId) {
1899 // This was our active pointer going up. Choose a new
1900 // active pointer and adjust accordingly.
1901 // TODO: Make this decision more intelligent.
1902 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1903 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1904 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001905 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001906 mActivePointerId = ev.getPointerId(newPointerIndex);
1907 if (mVelocityTracker != null) {
1908 mVelocityTracker.clear();
1909 }
1910 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001911 }
1912
Winson Chung321e9ee2010-08-09 13:37:56 -07001913 @Override
1914 public void requestChildFocus(View child, View focused) {
1915 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001916 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001917 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001918 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001919 }
1920 }
1921
Adam Cohend19d3ca2010-09-15 14:43:42 -07001922 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001923 return getPageNearestToCenterOfScreen(getScrollX());
1924 }
1925
1926 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1927 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001928 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001929 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001930 final int childCount = getChildCount();
1931 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001932 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001933 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001934 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001935 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001936 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1937 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1938 minDistanceFromScreenCenter = distanceFromScreenCenter;
1939 minDistanceFromScreenCenterIndex = i;
1940 }
1941 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001942 return minDistanceFromScreenCenterIndex;
1943 }
1944
1945 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001946 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001947 }
1948
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001949 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001950 public ScrollInterpolator() {
1951 }
1952
1953 public float getInterpolation(float t) {
1954 t -= 1.0f;
1955 return t*t*t*t*t + 1;
1956 }
1957 }
1958
1959 // We want the duration of the page snap animation to be influenced by the distance that
1960 // the screen has to travel, however, we don't want this duration to be effected in a
1961 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1962 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001963 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001964 f -= 0.5f; // center the values about 0.
1965 f *= 0.3f * Math.PI / 2.0f;
1966 return (float) Math.sin(f);
1967 }
1968
Michael Jurka0142d492010-08-25 17:46:15 -07001969 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001970 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001971 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001972
Adam Cohenedb40762013-07-18 16:45:45 -07001973 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001974 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001975 int duration = 0;
1976
Sunny Goyal4d113a52015-05-27 10:05:28 -07001977 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001978 // If the velocity is low enough, then treat this more as an automatic page advance
1979 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001980 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001981 return;
1982 }
1983
1984 // Here we compute a "distance" that will be used in the computation of the overall
1985 // snap duration. This is a function of the actual distance that needs to be traveled;
1986 // we keep this value close to half screen size in order to reduce the variance in snap
1987 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001988 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001989 float distance = halfScreenSize + halfScreenSize *
1990 distanceInfluenceForSnapDuration(distanceRatio);
1991
1992 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001993 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001994
1995 // we want the page's snap velocity to approximately match the velocity at which the
1996 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001997 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1998 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001999
2000 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002001 }
2002
Sunny Goyal1740d902015-05-27 11:14:01 -07002003 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002004 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002005 }
2006
Adam Cohenf9c184a2016-01-15 16:47:43 -08002007 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002008 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002009 }
2010
Michael Jurka0142d492010-08-25 17:46:15 -07002011 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002012 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002013 }
2014
Adam Cohenf9618852013-11-08 06:45:03 -08002015 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2016 snapToPage(whichPage, duration, false, interpolator);
2017 }
2018
2019 protected void snapToPage(int whichPage, int duration, boolean immediate,
2020 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002021 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002022
Adam Cohenedb40762013-07-18 16:45:45 -07002023 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002024 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002025 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002026 }
2027
2028 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002029 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002030 }
Michael Jurka0142d492010-08-25 17:46:15 -07002031
Adam Cohenf9618852013-11-08 06:45:03 -08002032 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2033 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002034 whichPage = validateNewPage(whichPage);
2035
Adam Cohen7d30a372013-07-01 17:03:59 -07002036 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002037
2038 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002039 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002040 if (immediate) {
2041 duration = 0;
2042 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002043 duration = Math.abs(delta);
2044 }
2045
Adam Cohenf358a4b2013-07-23 16:47:31 -07002046 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002047 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002048 }
Adam Cohenf9618852013-11-08 06:45:03 -08002049
2050 if (interpolator != null) {
2051 mScroller.setInterpolator(interpolator);
2052 } else {
2053 mScroller.setInterpolator(mDefaultInterpolator);
2054 }
2055
Sunny Goyalc86df472016-02-25 09:19:38 -08002056 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002057
Adam Cohen674531f2013-12-13 15:07:14 -08002058 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002059
2060 // Trigger a compute() to finish switching pages if necessary
2061 if (immediate) {
2062 computeScroll();
2063 }
2064
2065 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002066 invalidate();
2067 }
2068
Winson Chung321e9ee2010-08-09 13:37:56 -07002069 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002070 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002071 }
2072
2073 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002074 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002075 }
2076
Adam Cohendbdff6b2013-09-18 19:09:15 -07002077 @Override
2078 public boolean performLongClick() {
2079 mCancelTap = true;
2080 return super.performLongClick();
2081 }
2082
Winson Chung321e9ee2010-08-09 13:37:56 -07002083 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002084 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002085
2086 SavedState(Parcelable superState) {
2087 super(superState);
2088 }
2089
Adam Cohen091440a2015-03-18 14:16:05 -07002090 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002092 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002093 }
2094
2095 @Override
2096 public void writeToParcel(Parcel out, int flags) {
2097 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002098 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002099 }
2100
2101 public static final Parcelable.Creator<SavedState> CREATOR =
2102 new Parcelable.Creator<SavedState>() {
2103 public SavedState createFromParcel(Parcel in) {
2104 return new SavedState(in);
2105 }
2106
2107 public SavedState[] newArray(int size) {
2108 return new SavedState[size];
2109 }
2110 };
2111 }
2112
Adam Cohen7d30a372013-07-01 17:03:59 -07002113 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002114 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002115 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002116 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2117 .translationX(0)
2118 .translationY(0)
2119 .scaleX(1)
2120 .scaleY(1)
2121 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002122 anim.addListener(new AnimatorListenerAdapter() {
2123 @Override
2124 public void onAnimationEnd(Animator animation) {
2125 onPostReorderingAnimationCompleted();
2126 }
2127 });
2128 anim.start();
2129 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002130 }
2131
Sunny Goyal1d08f702015-05-04 15:50:25 -07002132 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002133 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2134 mTouchState = TOUCH_STATE_REORDERING;
2135 mIsReordering = true;
2136
Adam Cohen7d30a372013-07-01 17:03:59 -07002137 // We must invalidate to trigger a redraw to update the layers such that the drag view
2138 // is always drawn on top
2139 invalidate();
2140 }
2141
Adam Cohen091440a2015-03-18 14:16:05 -07002142 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002143 // Trigger the callback when reordering has settled
2144 --mPostReorderingPreZoomInRemainingAnimationCount;
2145 if (mPostReorderingPreZoomInRunnable != null &&
2146 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2147 mPostReorderingPreZoomInRunnable.run();
2148 mPostReorderingPreZoomInRunnable = null;
2149 }
2150 }
2151
Sunny Goyal1d08f702015-05-04 15:50:25 -07002152 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002153 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002154 }
2155
Adam Cohenf358a4b2013-07-23 16:47:31 -07002156 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002157 int dragViewIndex = indexOfChild(v);
2158
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002159 // Do not allow the first page to be moved around
2160 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002161
Adam Cohen7d30a372013-07-01 17:03:59 -07002162 mTempVisiblePagesRange[0] = 0;
2163 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002164 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002165 mReorderingStarted = true;
2166
2167 // Check if we are within the reordering range
2168 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002169 dragViewIndex <= mTempVisiblePagesRange[1]) {
2170 // Find the drag view under the pointer
2171 mDragView = getChildAt(dragViewIndex);
2172 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2173 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002174 snapToPage(getPageNearestToCenterOfScreen());
2175 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002176 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002177 return true;
2178 }
2179 return false;
2180 }
2181
2182 boolean isReordering(boolean testTouchState) {
2183 boolean state = mIsReordering;
2184 if (testTouchState) {
2185 state &= (mTouchState == TOUCH_STATE_REORDERING);
2186 }
2187 return state;
2188 }
2189 void endReordering() {
2190 // For simplicity, we call endReordering sometimes even if reordering was never started.
2191 // In that case, we don't want to do anything.
2192 if (!mReorderingStarted) return;
2193 mReorderingStarted = false;
2194
2195 // If we haven't flung-to-delete the current child, then we just animate the drag view
2196 // back into position
2197 final Runnable onCompleteRunnable = new Runnable() {
2198 @Override
2199 public void run() {
2200 onEndReordering();
2201 }
2202 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002203
2204 mPostReorderingPreZoomInRunnable = new Runnable() {
2205 public void run() {
2206 onCompleteRunnable.run();
2207 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002208 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002209 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002210
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002211 mPostReorderingPreZoomInRemainingAnimationCount =
2212 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2213 // Snap to the current page
2214 snapToPage(indexOfChild(mDragView), 0);
2215 // Animate the drag view back to the front position
2216 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002217 }
2218
Winson Chung6a0f57d2011-06-29 20:10:49 -07002219 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002220 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002221 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002222 @Override
2223 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2224 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002225 info.setScrollable(getPageCount() > 1);
2226 if (getCurrentPage() < getPageCount() - 1) {
2227 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2228 }
2229 if (getCurrentPage() > 0) {
2230 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2231 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002232 info.setClassName(getClass().getName());
2233
2234 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2235 // Besides disabling the accessibility long-click, this also prevents this view from getting
2236 // accessibility focus.
2237 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002238 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002239 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2240 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002241 }
2242
2243 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002244 public void sendAccessibilityEvent(int eventType) {
2245 // Don't let the view send real scroll events.
2246 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2247 super.sendAccessibilityEvent(eventType);
2248 }
2249 }
2250
2251 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002252 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2253 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002254 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002255 }
2256
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002257 @Override
2258 public boolean performAccessibilityAction(int action, Bundle arguments) {
2259 if (super.performAccessibilityAction(action, arguments)) {
2260 return true;
2261 }
2262 switch (action) {
2263 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2264 if (getCurrentPage() < getPageCount() - 1) {
2265 scrollRight();
2266 return true;
2267 }
2268 } break;
2269 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2270 if (getCurrentPage() > 0) {
2271 scrollLeft();
2272 return true;
2273 }
2274 } break;
2275 }
2276 return false;
2277 }
2278
Adam Cohen0ffac432013-07-10 11:19:26 -07002279 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002280 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002281 getNextPage() + 1, getChildCount());
2282 }
2283
Winson Chungd11265e2011-08-30 13:37:23 -07002284 @Override
2285 public boolean onHoverEvent(android.view.MotionEvent event) {
2286 return true;
2287 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002288}