blob: 86a1a397c2ec953873b67da381e6418255306753 [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;
157 @Thunk 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
Winson Chungd2be3812013-07-16 11:11:32 -0700250 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700251 super.onAttachedToWindow();
252
Winson Chungd2be3812013-07-16 11:11:32 -0700253 // Hook up the page indicator
254 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700255 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700256 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700257 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700258 mPageIndicator.setMarkersCount(getChildCount());
Adam Cohenfbdf4272013-10-09 13:02:21 -0700259
260 OnClickListener listener = getPageIndicatorClickListener();
261 if (listener != null) {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700262 mPageIndicator.getView().setOnClickListener(listener);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700263 }
Tony Wickhamf549dab2016-05-16 09:54:06 -0700264 mPageIndicator.getView().setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700265 }
266 }
267
Adam Cohen53805212013-10-01 10:39:23 -0700268 protected String getPageIndicatorDescription() {
269 return getCurrentPageDescription();
270 }
271
272 protected OnClickListener getPageIndicatorClickListener() {
273 return null;
274 }
275
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700276 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700277 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700278 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700279 // Unhook the page indicator
280 mPageIndicator = null;
281 }
282
Adam Cohen7d30a372013-07-01 17:03:59 -0700283 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700284 private float[] mapPointFromViewToParent(View v, float x, float y) {
285 sTmpPoint[0] = x;
286 sTmpPoint[1] = y;
287 v.getMatrix().mapPoints(sTmpPoint);
288 sTmpPoint[0] += v.getLeft();
289 sTmpPoint[1] += v.getTop();
290 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700291 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700292 private float[] mapPointFromParentToView(View v, float x, float y) {
293 sTmpPoint[0] = x - v.getLeft();
294 sTmpPoint[1] = y - v.getTop();
295 v.getMatrix().invert(sTmpInvMatrix);
296 sTmpInvMatrix.mapPoints(sTmpPoint);
297 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700298 }
299
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700300 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700301 if (mDragView != null) {
302 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
303 (mDragViewBaselineLeft - mDragView.getLeft());
304 float y = mLastMotionY - mDownMotionY;
305 mDragView.setTranslationX(x);
306 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700307
Adam Cohenf358a4b2013-07-23 16:47:31 -0700308 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
309 + x + ", " + y);
310 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700311 }
312
313 public void setMinScale(float f) {
314 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700315 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700316 requestLayout();
317 }
318
319 @Override
320 public void setScaleX(float scaleX) {
321 super.setScaleX(scaleX);
322 if (isReordering(true)) {
323 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
324 mLastMotionX = p[0];
325 mLastMotionY = p[1];
326 updateDragViewTranslationDuringDrag();
327 }
328 }
329
330 // Convenience methods to get the actual width/height of the PagedView (since it is measured
331 // to be larger to account for the minimum possible scale)
332 int getViewportWidth() {
333 return mViewport.width();
334 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800335 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700336 return mViewport.height();
337 }
338
339 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
340 // PagedView both horizontally and vertically
341 int getViewportOffsetX() {
342 return (getMeasuredWidth() - getViewportWidth()) / 2;
343 }
344
345 int getViewportOffsetY() {
346 return (getMeasuredHeight() - getViewportHeight()) / 2;
347 }
348
Adam Cohenedb40762013-07-18 16:45:45 -0700349 PageIndicator getPageIndicator() {
350 return mPageIndicator;
351 }
352
Adam Cohen674531f2013-12-13 15:07:14 -0800353 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700354 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
355 * that the user was on before entering free scroll mode (e.g. the home screen page they
356 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
357 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700359 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700360 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700362
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700363 /**
364 * Returns the index of page to be shown immediately afterwards.
365 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700366 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700367 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
368 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700369
Adam Cohenf9c184a2016-01-15 16:47:43 -0800370 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 return getChildCount();
372 }
373
Sunny Goyal83a8f042015-05-19 12:52:12 -0700374 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 return getChildAt(index);
376 }
377
Adam Cohenae4f1552011-10-20 00:15:42 -0700378 protected int indexToPage(int index) {
379 return index;
380 }
381
Winson Chung321e9ee2010-08-09 13:37:56 -0700382 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800383 * Updates the scroll of the current page immediately to its final scroll position. We use this
384 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
385 * the previous tab page.
386 */
387 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700388 // If the current page is invalid, just reset the scroll position to zero
389 int newX = 0;
390 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700391 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700392 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800393 scrollTo(newX, 0);
394 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700395 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800396 }
397
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700398 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700399 mScroller.abortAnimation();
400 // We need to clean up the next page here to avoid computeScrollHelper from
401 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700402 if (resetNextPage) {
403 mNextPage = INVALID_PAGE;
404 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700405 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700406
Tony Wickham8f7ead32016-04-07 18:46:44 -0700407 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700408 mScroller.forceFinished(true);
409 // We need to clean up the next page here to avoid computeScrollHelper from
410 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700411 if (resetNextPage) {
412 mNextPage = INVALID_PAGE;
413 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700414 }
415
Adam Cohen6f127a62014-06-12 14:54:41 -0700416 private int validateNewPage(int newPage) {
417 int validatedPage = newPage;
418 // When in free scroll mode, we need to clamp to the free scroll page range.
419 if (mFreeScroll) {
420 getFreeScrollPageRange(mTempVisiblePagesRange);
421 validatedPage = Math.max(mTempVisiblePagesRange[0],
422 Math.min(newPage, mTempVisiblePagesRange[1]));
423 }
424 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700425 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700426 return validatedPage;
427 }
428
Chet Haasebc2f0822012-10-26 17:59:53 -0700429 /**
Winson Chung86f77532010-08-24 11:08:22 -0700430 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700432 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800433 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700434 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800435 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800436 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
437 // the default
438 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800439 return;
440 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700441 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700442 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700443 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700444 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800445 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 }
447
Winson Chung8c87cd82013-07-23 16:20:10 -0700448 /**
449 * The restore page will be set in place of the current page at the next (likely first)
450 * layout.
451 */
452 void setRestorePage(int restorePage) {
453 mRestorePage = restorePage;
454 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800455 int getRestorePage() {
456 return mRestorePage;
457 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700458
Adam Cohen674531f2013-12-13 15:07:14 -0800459 /**
460 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
461 * has settled.
462 */
Michael Jurka0142d492010-08-25 17:46:15 -0700463 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800464 updatePageIndicator();
465 }
466
467 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700468 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700469 if (mPageIndicator != null) {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700470 mPageIndicator.getView().setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700471 if (!isReordering(false)) {
472 mPageIndicator.setActiveMarker(getNextPage());
473 }
Winson Chungd2be3812013-07-16 11:11:32 -0700474 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800476 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700477 if (!mIsPageMoving) {
478 mIsPageMoving = true;
479 onPageBeginMoving();
480 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700481 }
482
Michael Jurkace7e05f2011-02-01 22:02:35 -0800483 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700484 if (mIsPageMoving) {
485 mIsPageMoving = false;
486 onPageEndMoving();
487 }
Michael Jurka0142d492010-08-25 17:46:15 -0700488 }
489
Adam Cohen26976d92011-03-22 15:33:33 -0700490 protected boolean isPageMoving() {
491 return mIsPageMoving;
492 }
493
Michael Jurka0142d492010-08-25 17:46:15 -0700494 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700495 protected void onPageBeginMoving() {
496 }
497
498 // a method that subclasses can override to add behavior
499 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700501 }
502
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 /**
Winson Chung86f77532010-08-24 11:08:22 -0700504 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 *
506 * @param l The listener used to respond to long clicks.
507 */
508 @Override
509 public void setOnLongClickListener(OnLongClickListener l) {
510 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700511 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700513 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 }
Adam Cohen1697b792013-09-17 19:08:21 -0700515 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700516 }
517
Sunny Goyalc86df472016-02-25 09:19:38 -0800518 protected int getUnboundedScrollX() {
519 return getScrollX();
520 }
521
Winson Chung321e9ee2010-08-09 13:37:56 -0700522 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800523 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800524 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800525 }
526
527 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700528 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700529 // In free scroll mode, we clamp the scrollX
530 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700531 // If the scroller is trying to move to a location beyond the maximum allowed
532 // in the free scroll mode, we make sure to end the scroll operation.
533 if (!mScroller.isFinished() &&
534 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700535 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700536 }
537
Adam Cohenf358a4b2013-07-23 16:47:31 -0700538 x = Math.min(x, mFreeScrollMaxScrollX);
539 x = Math.max(x, mFreeScrollMinScrollX);
540 }
541
Sunny Goyal70660032015-05-14 00:07:08 -0700542 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
543 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700544 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700545 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800546 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700547 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700548 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700549 overScroll(x - mMaxScrollX);
550 } else {
551 overScroll(x);
552 }
Adam Cohen68d73932010-11-15 10:50:58 -0800553 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700554 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700555 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800556 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700557 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700558 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700559 overScroll(x);
560 } else {
561 overScroll(x - mMaxScrollX);
562 }
Adam Cohen68d73932010-11-15 10:50:58 -0800563 }
564 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700565 if (mWasInOverscroll) {
566 overScroll(0);
567 mWasInOverscroll = false;
568 }
Adam Cohen68d73932010-11-15 10:50:58 -0800569 super.scrollTo(x, y);
570 }
571
Adam Cohen7d30a372013-07-01 17:03:59 -0700572 // Update the last motion events when scrolling
573 if (isReordering(true)) {
574 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
575 mLastMotionX = p[0];
576 mLastMotionY = p[1];
577 updateDragViewTranslationDuringDrag();
578 }
Michael Jurka0142d492010-08-25 17:46:15 -0700579 }
580
Adam Cohen53805212013-10-01 10:39:23 -0700581 private void sendScrollAccessibilityEvent() {
582 AccessibilityManager am =
583 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
584 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700585 if (mCurrentPage != getNextPage()) {
586 AccessibilityEvent ev =
587 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700588 ev.setScrollable(true);
589 ev.setScrollX(getScrollX());
590 ev.setScrollY(getScrollY());
591 ev.setMaxScrollX(mMaxScrollX);
592 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700593
Vadim Tryshevf4715972015-05-08 17:21:03 -0700594 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700595 }
Adam Cohen53805212013-10-01 10:39:23 -0700596 }
597 }
598
Michael Jurka0142d492010-08-25 17:46:15 -0700599 // we moved this functionality to a helper function so SmoothPagedView can reuse it
600 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800601 return computeScrollHelper(true);
602 }
603
604 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700606 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700607 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800608 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700609 float scaleX = mFreeScroll ? getScaleX() : 1f;
610 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
611 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700612 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800613 if (shouldInvalidate) {
614 invalidate();
615 }
Michael Jurka0142d492010-08-25 17:46:15 -0700616 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800617 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700618 sendScrollAccessibilityEvent();
619
Adam Cohen6f127a62014-06-12 14:54:41 -0700620 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700621 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700622 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700623
Adam Cohen73aa9752010-11-24 16:26:58 -0800624 // We don't want to trigger a page end moving unless the page has settled
625 // and the user has stopped scrolling
626 if (mTouchState == TOUCH_STATE_REST) {
627 pageEndMoving();
628 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700629
Adam Cohen7d30a372013-07-01 17:03:59 -0700630 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700631 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700632 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700633 if (am.isEnabled()) {
634 // Notify the user when the page changes
635 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700636 }
Michael Jurka0142d492010-08-25 17:46:15 -0700637 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
Michael Jurka0142d492010-08-25 17:46:15 -0700639 return false;
640 }
641
642 @Override
643 public void computeScroll() {
644 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 }
646
Adam Cohen96d30a12013-07-16 18:13:21 -0700647 public static class LayoutParams extends ViewGroup.LayoutParams {
648 public boolean isFullScreenPage = false;
649
650 /**
651 * {@inheritDoc}
652 */
653 public LayoutParams(int width, int height) {
654 super(width, height);
655 }
656
Sunny Goyal41b22c02015-05-14 15:20:48 -0700657 public LayoutParams(Context context, AttributeSet attrs) {
658 super(context, attrs);
659 }
660
Adam Cohen96d30a12013-07-16 18:13:21 -0700661 public LayoutParams(ViewGroup.LayoutParams source) {
662 super(source);
663 }
664 }
665
Sunny Goyal41b22c02015-05-14 15:20:48 -0700666 @Override
667 public LayoutParams generateLayoutParams(AttributeSet attrs) {
668 return new LayoutParams(getContext(), attrs);
669 }
670
671 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700672 protected LayoutParams generateDefaultLayoutParams() {
673 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
674 }
675
Sunny Goyal41b22c02015-05-14 15:20:48 -0700676 @Override
677 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
678 return new LayoutParams(p);
679 }
680
681 @Override
682 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
683 return p instanceof LayoutParams;
684 }
685
Adam Cohenedb40762013-07-18 16:45:45 -0700686 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700687 LayoutParams lp = generateDefaultLayoutParams();
688 lp.isFullScreenPage = true;
689 super.addView(page, 0, lp);
690 }
691
Adam Cohen410f3cd2013-09-22 12:09:32 -0700692 public int getNormalChildHeight() {
693 return mNormalChildHeight;
694 }
695
Winson Chung321e9ee2010-08-09 13:37:56 -0700696 @Override
697 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700698 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700699 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
700 return;
701 }
702
Adam Cohen7d30a372013-07-01 17:03:59 -0700703 // We measure the dimensions of the PagedView to be larger than the pages so that when we
704 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700705 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
706 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
707 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700708 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800709 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700710 // viewport, we can be at most one and a half screens offset once we scale down
711 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700712 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
713 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700714
Winson Chungc82d2622013-11-06 13:23:29 -0800715 int parentWidthSize = (int) (2f * maxSize);
716 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700717 int scaledWidthSize, scaledHeightSize;
718 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700719 scaledWidthSize = (int) (parentWidthSize / mMinScale);
720 scaledHeightSize = (int) (parentHeightSize / mMinScale);
721 } else {
722 scaledWidthSize = widthSize;
723 scaledHeightSize = heightSize;
724 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700725 mViewport.set(0, 0, widthSize, heightSize);
726
727 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
728 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
729 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 }
731
Winson Chung8aad6102012-05-11 16:27:49 -0700732 // Return early if we aren't given a proper dimension
733 if (widthSize <= 0 || heightSize <= 0) {
734 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
735 return;
736 }
737
Adam Lesinski6b879f02010-11-04 16:15:23 -0700738 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
739 * of the All apps view on XLarge displays to not take up more space then it needs. Width
740 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
741 * each page to have the same width.
742 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700743 final int verticalPadding = getPaddingTop() + getPaddingBottom();
744 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700745
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700746 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700748 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700749 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700750 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
751 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
752 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
753 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 final int childCount = getChildCount();
755 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700756 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700757 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100758 if (child.getVisibility() != GONE) {
759 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700760
Vladimir Marko2824b072013-10-04 16:42:17 +0100761 int childWidthMode;
762 int childHeightMode;
763 int childWidth;
764 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700765
Vladimir Marko2824b072013-10-04 16:42:17 +0100766 if (!lp.isFullScreenPage) {
767 if (lp.width == LayoutParams.WRAP_CONTENT) {
768 childWidthMode = MeasureSpec.AT_MOST;
769 } else {
770 childWidthMode = MeasureSpec.EXACTLY;
771 }
772
773 if (lp.height == LayoutParams.WRAP_CONTENT) {
774 childHeightMode = MeasureSpec.AT_MOST;
775 } else {
776 childHeightMode = MeasureSpec.EXACTLY;
777 }
778
Adam Cohen3b185e22013-10-29 14:45:58 -0700779 childWidth = getViewportWidth() - horizontalPadding
780 - mInsets.left - mInsets.right;
781 childHeight = getViewportHeight() - verticalPadding
782 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100783 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700784 } else {
785 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700786 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100787
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800788 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700789 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700790 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700791 if (referenceChildWidth == 0) {
792 referenceChildWidth = childWidth;
793 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700794
Vladimir Marko2824b072013-10-04 16:42:17 +0100795 final int childWidthMeasureSpec =
796 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
797 final int childHeightMeasureSpec =
798 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
799 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700800 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700801 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700802 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800803 }
804
Sunny Goyalcf25b522015-07-09 00:01:18 -0700805 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700807 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700808 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700809 return;
810 }
811
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700814
Adam Cohen7d30a372013-07-01 17:03:59 -0700815 int offsetX = getViewportOffsetX();
816 int offsetY = getViewportOffsetY();
817
818 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700819 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700820
Sunny Goyal70660032015-05-14 00:07:08 -0700821 final int startIndex = mIsRtl ? childCount - 1 : 0;
822 final int endIndex = mIsRtl ? -1 : childCount;
823 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700824
Adam Cohen7d30a372013-07-01 17:03:59 -0700825 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700826
Adam Cohen3b185e22013-10-29 14:45:58 -0700827 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000828 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700829
830 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700831 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
832 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700833 }
834
Adam Cohen0ffac432013-07-10 11:19:26 -0700835 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700836 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700838 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100839 int childTop;
840 if (lp.isFullScreenPage) {
841 childTop = offsetY;
842 } else {
843 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700844 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100845 }
846
Adam Cohen96d30a12013-07-16 18:13:21 -0700847 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700848 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800849
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700851 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800852 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700853
Adam Cohen3b185e22013-10-29 14:45:58 -0700854 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
855 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000856
857 int pageGap = mPageSpacing;
858 int next = i + delta;
859 if (next != endIndex) {
860 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
861 } else {
862 nextLp = null;
863 }
864
865 // Prevent full screen pages from showing in the viewport
866 // when they are not the current page.
867 if (lp.isFullScreenPage) {
868 pageGap = getPaddingLeft();
869 } else if (nextLp != null && nextLp.isFullScreenPage) {
870 pageGap = getPaddingRight();
871 }
872
Sunny Goyala1fbd842015-05-20 13:40:27 -0700873 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 }
875 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700876
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700877 final LayoutTransition transition = getLayoutTransition();
878 // If the transition is running defer updating max scroll, as some empty pages could
879 // still be present, and a max scroll change could cause sudden jumps in scroll.
880 if (transition != null && transition.isRunning()) {
881 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
882
883 @Override
884 public void startTransition(LayoutTransition transition, ViewGroup container,
885 View view, int transitionType) { }
886
887 @Override
888 public void endTransition(LayoutTransition transition, ViewGroup container,
889 View view, int transitionType) {
890 // Wait until all transitions are complete.
891 if (!transition.isRunning()) {
892 transition.removeTransitionListener(this);
893 updateMaxScrollX();
894 }
895 }
896 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700897 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700898 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700899 }
900
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700901 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
902 updateCurrentPageScroll();
903 mFirstLayout = false;
904 }
905
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700906 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700907 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700908 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700909 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700910 } else {
911 setCurrentPage(getNextPage());
912 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700913 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700914 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700915
916 if (isReordering(true)) {
917 updateDragViewTranslationDuringDrag();
918 }
Winson Chunge3193b92010-09-10 11:44:42 -0700919 }
920
Sunny Goyala1fbd842015-05-20 13:40:27 -0700921 protected int getChildGap() {
922 return 0;
923 }
924
Sunny Goyal316490e2015-06-02 09:38:28 -0700925 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700926 mMaxScrollX = computeMaxScrollX();
927 }
928
929 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700930 int childCount = getChildCount();
931 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700932 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700933 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700934 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700935 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700936 }
937 }
938
Adam Cohen3b185e22013-10-29 14:45:58 -0700939 public void setPageSpacing(int pageSpacing) {
940 mPageSpacing = pageSpacing;
941 requestLayout();
942 }
943
Sunny Goyal4d113a52015-05-27 10:05:28 -0700944 /**
945 * Called when the center screen changes during scrolling.
946 */
947 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700948
Winson Chunge3193b92010-09-10 11:44:42 -0700949 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700950 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700951 // Update the page indicator, we don't update the page indicator as we
952 // add/remove pages
953 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700954 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700955 }
956
Adam Cohen2591f6a2011-10-25 14:36:40 -0700957 // This ensures that when children are added, they get the correct transforms / alphas
958 // in accordance with any scroll effects.
959 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700960 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700961 invalidate();
962 }
963
Michael Jurka8b805b12012-04-18 14:23:14 -0700964 @Override
965 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700966 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700967 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700968 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700969 }
970
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700971 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700972 // Update the page indicator, we don't update the page indicator as we
973 // add/remove pages
974 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700975 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700976 }
977 }
978
979 @Override
980 public void removeView(View v) {
981 // XXX: We should find a better way to hook into this before the view
982 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700983 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700984 super.removeView(v);
985 }
986 @Override
987 public void removeViewInLayout(View v) {
988 // XXX: We should find a better way to hook into this before the view
989 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700990 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700991 super.removeViewInLayout(v);
992 }
993 @Override
994 public void removeViewAt(int index) {
995 // XXX: We should find a better way to hook into this before the view
996 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700997 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700998 super.removeViewAt(index);
999 }
1000 @Override
1001 public void removeAllViewsInLayout() {
1002 // Update the page indicator, we don't update the page indicator as we
1003 // add/remove pages
1004 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -07001005 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -07001006 }
1007
1008 super.removeAllViewsInLayout();
1009 }
1010
Adam Cohen73894962011-10-31 13:17:17 -07001011 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001012 if (index < 0 || index > getChildCount() - 1) return 0;
1013
Adam Cohenf698c6e2013-07-17 18:44:25 -07001014 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001015
Adam Cohenf698c6e2013-07-17 18:44:25 -07001016 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001017 }
1018
Adam Cohen6f127a62014-06-12 14:54:41 -07001019 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001020 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001021 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001022 }
1023
Michael Jurkadde558b2011-11-09 22:09:06 -08001024 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001025 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001026 range[0] = -1;
1027 range[1] = -1;
1028
Sunny Goyalee688162016-02-12 14:24:21 -08001029 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001030 final int visibleLeft = -getLeft();
1031 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001032 final Matrix pageShiftMatrix = getPageShiftMatrix();
1033 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001034
Sunny Goyalee688162016-02-12 14:24:21 -08001035 for (int i = 0; i < count; i++) {
1036 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001037
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001038 // Verify if the page bounds are within the visible range.
1039 sTmpRectF.left = 0;
1040 sTmpRectF.right = currPage.getMeasuredWidth();
1041 currPage.getMatrix().mapRect(sTmpRectF);
1042 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001043 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001044
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001045 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001046 if (range[0] == -1) {
1047 continue;
1048 } else {
1049 break;
1050 }
1051 }
Sunny Goyalee688162016-02-12 14:24:21 -08001052 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001053 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001054 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001055 }
1056 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001057
Sunny Goyalee688162016-02-12 14:24:21 -08001058 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001059 } else {
1060 range[0] = -1;
1061 range[1] = -1;
1062 }
1063 }
1064
Sunny Goyalee688162016-02-12 14:24:21 -08001065 protected Matrix getPageShiftMatrix() {
1066 return getMatrix();
1067 }
1068
Michael Jurka920d7f42012-05-14 16:29:55 -07001069 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001070 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001071 }
1072
Michael Jurkadde558b2011-11-09 22:09:06 -08001073 @Override
1074 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001075 // Find out which screens are visible; as an optimization we only call draw on them
1076 final int pageCount = getChildCount();
1077 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001078 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001079 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001080
1081 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1082 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1083 // set it for the next frame
1084 mForceScreenScrolled = false;
1085 screenScrolled(screenCenter);
1086 mLastScreenCenter = screenCenter;
1087 }
1088
Michael Jurkadde558b2011-11-09 22:09:06 -08001089 getVisiblePages(mTempVisiblePagesRange);
1090 final int leftScreen = mTempVisiblePagesRange[0];
1091 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001092 if (leftScreen != -1 && rightScreen != -1) {
1093 final long drawingTime = getDrawingTime();
1094 // Clip to the bounds
1095 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001096 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1097 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001098
Adam Cohen7d30a372013-07-01 17:03:59 -07001099 // Draw all the children, leaving the drag view for last
1100 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001101 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001102 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001103 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001104 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001105 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001106 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001107 // Draw the drag view on top (if there is one)
1108 if (mDragView != null) {
1109 drawChild(canvas, mDragView, drawingTime);
1110 }
1111
Winson Chungc6f10b92011-11-14 11:39:07 -08001112 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001113 }
Michael Jurka0142d492010-08-25 17:46:15 -07001114 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 }
1116
1117 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001118 public void draw(Canvas canvas) {
1119 super.draw(canvas);
1120 if (getPageCount() > 0) {
1121 if (!mEdgeGlowLeft.isFinished()) {
1122 final int restoreCount = canvas.save();
1123 Rect display = mViewport;
1124 canvas.translate(display.left, display.top);
1125 canvas.rotate(270);
1126
1127 getEdgeVerticalPostion(sTmpIntPoint);
1128 canvas.translate(display.top - sTmpIntPoint[1], 0);
1129 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1130 if (mEdgeGlowLeft.draw(canvas)) {
1131 postInvalidateOnAnimation();
1132 }
1133 canvas.restoreToCount(restoreCount);
1134 }
1135 if (!mEdgeGlowRight.isFinished()) {
1136 final int restoreCount = canvas.save();
1137 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001138 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001139 canvas.rotate(90);
1140
1141 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001142
Sunny Goyal93264612015-11-23 11:47:50 -08001143 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1144 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001145 if (mEdgeGlowRight.draw(canvas)) {
1146 postInvalidateOnAnimation();
1147 }
1148 canvas.restoreToCount(restoreCount);
1149 }
1150 }
1151 }
1152
1153 /**
1154 * Returns the top and bottom position for the edge effect.
1155 */
1156 protected abstract void getEdgeVerticalPostion(int[] pos);
1157
1158 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001160 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001161 if (page != mCurrentPage || !mScroller.isFinished()) {
1162 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 return true;
1164 }
1165 return false;
1166 }
1167
1168 @Override
1169 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001170 int focusablePage;
1171 if (mNextPage != INVALID_PAGE) {
1172 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001174 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 }
Winson Chung86f77532010-08-24 11:08:22 -07001176 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001178 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 }
1180 return false;
1181 }
1182
1183 @Override
1184 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001185 if (super.dispatchUnhandledMove(focused, direction)) {
1186 return true;
1187 }
1188
1189 if (mIsRtl) {
1190 if (direction == View.FOCUS_LEFT) {
1191 direction = View.FOCUS_RIGHT;
1192 } else if (direction == View.FOCUS_RIGHT) {
1193 direction = View.FOCUS_LEFT;
1194 }
1195 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001197 if (getCurrentPage() > 0) {
1198 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 return true;
1200 }
1201 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001202 if (getCurrentPage() < getPageCount() - 1) {
1203 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 return true;
1205 }
1206 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001207 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 }
1209
1210 @Override
1211 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001212 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001213 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001214 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
1216 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001218 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 }
1220 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001221 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001222 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 }
1224 }
1225 }
1226
1227 /**
1228 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001229 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 *
Winson Chung86f77532010-08-24 11:08:22 -07001231 * This happens when live folders requery, and if they're off page, they
1232 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 */
1234 @Override
1235 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001236 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 View v = focused;
1238 while (true) {
1239 if (v == current) {
1240 super.focusableViewAvailable(focused);
1241 return;
1242 }
1243 if (v == this) {
1244 return;
1245 }
1246 ViewParent parent = v.getParent();
1247 if (parent instanceof View) {
1248 v = (View)v.getParent();
1249 } else {
1250 return;
1251 }
1252 }
1253 }
1254
1255 /**
1256 * {@inheritDoc}
1257 */
1258 @Override
1259 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1260 if (disallowIntercept) {
1261 // We need to make sure to cancel our long press if
1262 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001263 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001264 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 }
1266 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1267 }
1268
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001269 /**
1270 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1271 */
1272 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001273 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001274 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001275 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001276 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001277 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001278 }
1279
1280 /**
1281 * Return true if a tap at (x, y) should trigger a flip to the next page.
1282 */
1283 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001284 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001285 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001286 }
1287 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001288 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001289 }
Winson Chung52aee602013-01-30 12:01:02 -08001290
Adam Cohen7d30a372013-07-01 17:03:59 -07001291 /** Returns whether x and y originated within the buffered viewport */
1292 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001293 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001294 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001295 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001296 }
1297
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 @Override
1299 public boolean onInterceptTouchEvent(MotionEvent ev) {
1300 /*
1301 * This method JUST determines whether we want to intercept the motion.
1302 * If we return true, onTouchEvent will be called and we do the actual
1303 * scrolling there.
1304 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001305 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001306
Winson Chung45e1d6e2010-11-09 17:19:49 -08001307 // Skip touch handling if there are no pages to swipe
1308 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1309
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 /*
1311 * Shortcut the most recurring case: the user is in the dragging
1312 * state and he is moving his finger. We want to intercept this
1313 * motion.
1314 */
1315 final int action = ev.getAction();
1316 if ((action == MotionEvent.ACTION_MOVE) &&
1317 (mTouchState == TOUCH_STATE_SCROLLING)) {
1318 return true;
1319 }
1320
Winson Chung321e9ee2010-08-09 13:37:56 -07001321 switch (action & MotionEvent.ACTION_MASK) {
1322 case MotionEvent.ACTION_MOVE: {
1323 /*
1324 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1325 * whether the user has moved far enough from his original down touch.
1326 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001327 if (mActivePointerId != INVALID_POINTER) {
1328 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001329 }
1330 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1331 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1332 // i.e. fall through to the next case (don't break)
1333 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1334 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001335 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 }
1337
1338 case MotionEvent.ACTION_DOWN: {
1339 final float x = ev.getX();
1340 final float y = ev.getY();
1341 // Remember location of down touch
1342 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 mDownMotionY = y;
1344 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 mLastMotionX = x;
1346 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001347 float[] p = mapPointFromViewToParent(this, x, y);
1348 mParentDownMotionX = p[0];
1349 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001350 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001351 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001353
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 /*
1355 * If being flinged and user touches the screen, initiate drag;
1356 * otherwise don't. mScroller.isFinished should be false when
1357 * being flinged.
1358 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001359 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001360 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001361
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001362 if (finishedScrolling) {
1363 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001364 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001365 setCurrentPage(getNextPage());
1366 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001367 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001368 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001369 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1370 mTouchState = TOUCH_STATE_SCROLLING;
1371 } else {
1372 mTouchState = TOUCH_STATE_REST;
1373 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001374 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001375
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 break;
1377 }
1378
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001380 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001381 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 break;
1383
1384 case MotionEvent.ACTION_POINTER_UP:
1385 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001386 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 break;
1388 }
1389
1390 /*
1391 * The only time we want to intercept motion events is if we are in the
1392 * drag mode.
1393 */
1394 return mTouchState != TOUCH_STATE_REST;
1395 }
1396
Adam Cohenf8d28232011-02-01 21:47:00 -08001397 protected void determineScrollingStart(MotionEvent ev) {
1398 determineScrollingStart(ev, 1.0f);
1399 }
1400
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 /*
1402 * Determines if we should change the touch state to start scrolling after the
1403 * user moves their touch point too far.
1404 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001405 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001406 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001408 if (pointerIndex == -1) return;
1409
1410 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 final float x = ev.getX(pointerIndex);
1412 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001413 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001416
Adam Cohenf8d28232011-02-01 21:47:00 -08001417 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001419
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001420 if (xMoved) {
1421 // Scroll if the user moved far enough along the X axis
1422 mTouchState = TOUCH_STATE_SCROLLING;
1423 mTotalMotionX += Math.abs(mLastMotionX - x);
1424 mLastMotionX = x;
1425 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001426 onScrollInteractionBegin();
1427 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001428 // Stop listening for things like pinches.
1429 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001430 }
1431 }
1432
1433 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001434 // Try canceling the long press. It could also have been scheduled
1435 // by a distant descendant, so use the mAllowLongPress flag to block
1436 // everything
1437 final View currentPage = getPageAt(mCurrentPage);
1438 if (currentPage != null) {
1439 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 }
1441 }
1442
Adam Cohenb5ba0972011-09-07 18:02:31 -07001443 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001445
Adam Cohenedb40762013-07-18 16:45:45 -07001446 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001447 int count = getChildCount();
1448
1449 final int totalDistance;
1450
1451 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001452 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001453 adjacentPage = page - 1;
1454 }
1455
1456 if (adjacentPage < 0 || adjacentPage > count - 1) {
1457 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1458 } else {
1459 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1460 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001461
1462 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001463 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1464 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001465 return scrollProgress;
1466 }
1467
Adam Cohenedb40762013-07-18 16:45:45 -07001468 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001469 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001470 return 0;
1471 } else {
1472 return mPageScrolls[index];
1473 }
1474 }
1475
Adam Cohen564a2e72013-10-09 14:47:32 -07001476 // While layout transitions are occurring, a child's position may stray from its baseline
1477 // position. This method returns the magnitude of this stray at any given time.
1478 public int getLayoutTransitionOffsetForPage(int index) {
1479 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1480 return 0;
1481 } else {
1482 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001483
1484 int scrollOffset = 0;
1485 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1486 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001487 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001488 }
1489
Adam Cohen564a2e72013-10-09 14:47:32 -07001490 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1491 return (int) (child.getX() - baselineX);
1492 }
1493 }
1494
Adam Cohenb5ba0972011-09-07 18:02:31 -07001495 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001496 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001497 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001498 if (f < 0) {
1499 mEdgeGlowLeft.onPull(-f);
1500 } else if (f > 0) {
1501 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001502 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001503 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001504 }
1505 invalidate();
1506 }
1507
Adam Cohenb5ba0972011-09-07 18:02:31 -07001508 protected void overScroll(float amount) {
1509 dampedOverScroll(amount);
1510 }
1511
Winson Chungdc61c4d2015-04-20 18:26:57 -07001512 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001513 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001514 }
1515
Winson Chungdc61c4d2015-04-20 18:26:57 -07001516 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001517 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001518 }
1519
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001520 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001521 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001522 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001523 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1524 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1525 } else {
1526 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1527 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1528 }
1529 }
1530
Adam Cohenf9618852013-11-08 06:45:03 -08001531 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001532 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001533 mFreeScroll = freeScroll;
1534
Adam Cohenf9618852013-11-08 06:45:03 -08001535 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001536 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001537 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001538 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1539 setCurrentPage(mTempVisiblePagesRange[0]);
1540 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1541 setCurrentPage(mTempVisiblePagesRange[1]);
1542 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001543 } else if (wasFreeScroll) {
1544 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001545 }
1546
1547 setEnableOverscroll(!freeScroll);
1548 }
1549
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001550 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001551 mAllowOverScroll = enable;
1552 }
1553
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001554 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001555 if (mDragView != null) {
1556 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1557 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001558 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001559 int minDistance = Integer.MAX_VALUE;
1560 int minIndex = indexOfChild(mDragView);
1561 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1562 View page = getPageAt(i);
1563 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1564 int d = Math.abs(dragX - pageX);
1565 if (d < minDistance) {
1566 minIndex = i;
1567 minDistance = d;
1568 }
1569 }
1570 return minIndex;
1571 }
1572 return -1;
1573 }
1574
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 @Override
1576 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001577 super.onTouchEvent(ev);
1578
Winson Chung45e1d6e2010-11-09 17:19:49 -08001579 // Skip touch handling if there are no pages to swipe
1580 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1581
Michael Jurkab8f06722010-10-10 15:58:46 -07001582 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001583
1584 final int action = ev.getAction();
1585
1586 switch (action & MotionEvent.ACTION_MASK) {
1587 case MotionEvent.ACTION_DOWN:
1588 /*
1589 * If being flinged and user touches, stop the fling. isFinished
1590 * will be false if being flinged.
1591 */
1592 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001593 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001594 }
1595
1596 // Remember where the motion event started
1597 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001598 mDownMotionY = mLastMotionY = ev.getY();
1599 mDownScrollX = getScrollX();
1600 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1601 mParentDownMotionX = p[0];
1602 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001603 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001604 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001605 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001606
Michael Jurka0142d492010-08-25 17:46:15 -07001607 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001608 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001609 pageBeginMoving();
1610 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001611 break;
1612
1613 case MotionEvent.ACTION_MOVE:
1614 if (mTouchState == TOUCH_STATE_SCROLLING) {
1615 // Scroll to follow the motion event
1616 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001617
1618 if (pointerIndex == -1) return true;
1619
Winson Chung321e9ee2010-08-09 13:37:56 -07001620 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001621 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001622
Adam Cohenaefd4e12011-02-14 16:39:38 -08001623 mTotalMotionX += Math.abs(deltaX);
1624
Winson Chungc0844aa2011-02-02 15:25:58 -08001625 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1626 // keep the remainder because we are actually testing if we've moved from the last
1627 // scrolled position (which is discrete).
1628 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001629 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001630 mLastMotionX = x;
1631 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001632 } else {
1633 awakenScrollBars();
1634 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001635 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1636 // Update the last motion position
1637 mLastMotionX = ev.getX();
1638 mLastMotionY = ev.getY();
1639
1640 // Update the parent down so that our zoom animations take this new movement into
1641 // account
1642 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1643 mParentDownMotionX = pt[0];
1644 mParentDownMotionY = pt[1];
1645 updateDragViewTranslationDuringDrag();
1646
1647 // Find the closest page to the touch point
1648 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001649
Adam Cohen7d30a372013-07-01 17:03:59 -07001650 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1651 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1652 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1653 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1654
Adam Cohenf358a4b2013-07-23 16:47:31 -07001655 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001656 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001657 mTempVisiblePagesRange[0] = 0;
1658 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001659 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001660 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1661 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1662 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1663 mSidePageHoverIndex = pageUnderPointIndex;
1664 mSidePageHoverRunnable = new Runnable() {
1665 @Override
1666 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001667 // Setup the scroll to the correct page before we swap the views
1668 snapToPage(pageUnderPointIndex);
1669
1670 // For each of the pages between the paged view and the drag view,
1671 // animate them from the previous position to the new position in
1672 // the layout (as a result of the drag view moving in the layout)
1673 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1674 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1675 dragViewIndex + 1 : pageUnderPointIndex;
1676 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1677 dragViewIndex - 1 : pageUnderPointIndex;
1678 for (int i = lowerIndex; i <= upperIndex; ++i) {
1679 View v = getChildAt(i);
1680 // dragViewIndex < pageUnderPointIndex, so after we remove the
1681 // drag view all subsequent views to pageUnderPointIndex will
1682 // shift down.
1683 int oldX = getViewportOffsetX() + getChildOffset(i);
1684 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1685
1686 // Animate the view translation from its old position to its new
1687 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001688 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001689 if (anim != null) {
1690 anim.cancel();
1691 }
1692
1693 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001694 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001695 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001696 anim.start();
1697 v.setTag(anim);
1698 }
1699
1700 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001701 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001702 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001703 if (mPageIndicator != null) {
1704 mPageIndicator.setActiveMarker(getNextPage());
1705 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001706 }
1707 };
1708 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1709 }
1710 } else {
1711 removeCallbacks(mSidePageHoverRunnable);
1712 mSidePageHoverIndex = -1;
1713 }
Adam Cohen564976a2010-10-13 18:52:07 -07001714 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001715 determineScrollingStart(ev);
1716 }
1717 break;
1718
1719 case MotionEvent.ACTION_UP:
1720 if (mTouchState == TOUCH_STATE_SCROLLING) {
1721 final int activePointerId = mActivePointerId;
1722 final int pointerIndex = ev.findPointerIndex(activePointerId);
1723 final float x = ev.getX(pointerIndex);
1724 final VelocityTracker velocityTracker = mVelocityTracker;
1725 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1726 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001727 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001728 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001729 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1730 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001731
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001732 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1733
Adam Cohen00481b32011-11-18 12:03:48 -08001734 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001735 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001736
Adam Cohenf358a4b2013-07-23 16:47:31 -07001737 if (!mFreeScroll) {
1738 // In the case that the page is moved far to one direction and then is flung
1739 // in the opposite direction, we use a threshold to determine whether we should
1740 // just return to the starting page, or if we should skip one further.
1741 boolean returnToOriginalPage = false;
1742 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1743 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1744 returnToOriginalPage = true;
1745 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001746
Adam Cohenf358a4b2013-07-23 16:47:31 -07001747 int finalPage;
1748 // We give flings precedence over large moves, which is why we short-circuit our
1749 // test for a large move if a fling has been registered. That is, a large
1750 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001751 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1752 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001753 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1754 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1755 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1756 snapToPageWithVelocity(finalPage, velocityX);
1757 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1758 (isFling && isVelocityXLeft)) &&
1759 mCurrentPage < getChildCount() - 1) {
1760 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1761 snapToPageWithVelocity(finalPage, velocityX);
1762 } else {
1763 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001764 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001765 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001766 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001767 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001768 }
1769
1770 float scaleX = getScaleX();
1771 int vX = (int) (-velocityX * scaleX);
1772 int initialScrollX = (int) (getScrollX() * scaleX);
1773
Adam Cohenf9618852013-11-08 06:45:03 -08001774 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001775 mScroller.fling(initialScrollX,
1776 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001777 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001778 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001779 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001780 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001781 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1782 // at this point we have not moved beyond the touch slop
1783 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1784 // we can just page
1785 int nextPage = Math.max(0, mCurrentPage - 1);
1786 if (nextPage != mCurrentPage) {
1787 snapToPage(nextPage);
1788 } else {
1789 snapToDestination();
1790 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001791 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001792 // at this point we have not moved beyond the touch slop
1793 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1794 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001795 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1796 if (nextPage != mCurrentPage) {
1797 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001798 } else {
1799 snapToDestination();
1800 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001801 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1802 // Update the last motion position
1803 mLastMotionX = ev.getX();
1804 mLastMotionY = ev.getY();
1805
1806 // Update the parent down so that our zoom animations take this new movement into
1807 // account
1808 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1809 mParentDownMotionX = pt[0];
1810 mParentDownMotionY = pt[1];
1811 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001812 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001813 if (!mCancelTap) {
1814 onUnhandledTap(ev);
1815 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001816 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001817
1818 // Remove the callback to wait for the side page hover timeout
1819 removeCallbacks(mSidePageHoverRunnable);
1820 // End any intermediate reordering states
1821 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001822 break;
1823
1824 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001825 if (mTouchState == TOUCH_STATE_SCROLLING) {
1826 snapToDestination();
1827 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001828 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001829 break;
1830
1831 case MotionEvent.ACTION_POINTER_UP:
1832 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001833 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001834 break;
1835 }
1836
1837 return true;
1838 }
1839
Adam Cohen7d30a372013-07-01 17:03:59 -07001840 private void resetTouchState() {
1841 releaseVelocityTracker();
1842 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001843 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001844 mTouchState = TOUCH_STATE_REST;
1845 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001846 mEdgeGlowLeft.onRelease();
1847 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001848 }
1849
Adam Cohenc2d6e892014-10-16 09:49:24 -07001850 /**
1851 * Triggered by scrolling via touch
1852 */
1853 protected void onScrollInteractionBegin() {
1854 }
1855
1856 protected void onScrollInteractionEnd() {
1857 }
1858
Adam Cohen1697b792013-09-17 19:08:21 -07001859 protected void onUnhandledTap(MotionEvent ev) {
1860 ((Launcher) getContext()).onClick(this);
1861 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001862
Winson Chung185d7162011-02-28 13:47:29 -08001863 @Override
1864 public boolean onGenericMotionEvent(MotionEvent event) {
1865 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1866 switch (event.getAction()) {
1867 case MotionEvent.ACTION_SCROLL: {
1868 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1869 final float vscroll;
1870 final float hscroll;
1871 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1872 vscroll = 0;
1873 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1874 } else {
1875 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1876 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1877 }
1878 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001879 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001880 : (hscroll > 0 || vscroll > 0);
1881 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001882 scrollRight();
1883 } else {
1884 scrollLeft();
1885 }
1886 return true;
1887 }
1888 }
1889 }
1890 }
1891 return super.onGenericMotionEvent(event);
1892 }
1893
Michael Jurkab8f06722010-10-10 15:58:46 -07001894 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1895 if (mVelocityTracker == null) {
1896 mVelocityTracker = VelocityTracker.obtain();
1897 }
1898 mVelocityTracker.addMovement(ev);
1899 }
1900
1901 private void releaseVelocityTracker() {
1902 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001903 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001904 mVelocityTracker.recycle();
1905 mVelocityTracker = null;
1906 }
1907 }
1908
Winson Chung321e9ee2010-08-09 13:37:56 -07001909 private void onSecondaryPointerUp(MotionEvent ev) {
1910 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1911 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1912 final int pointerId = ev.getPointerId(pointerIndex);
1913 if (pointerId == mActivePointerId) {
1914 // This was our active pointer going up. Choose a new
1915 // active pointer and adjust accordingly.
1916 // TODO: Make this decision more intelligent.
1917 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1918 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1919 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001920 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001921 mActivePointerId = ev.getPointerId(newPointerIndex);
1922 if (mVelocityTracker != null) {
1923 mVelocityTracker.clear();
1924 }
1925 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001926 }
1927
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 @Override
1929 public void requestChildFocus(View child, View focused) {
1930 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001931 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001932 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001933 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001934 }
1935 }
1936
Adam Cohend19d3ca2010-09-15 14:43:42 -07001937 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001938 return getPageNearestToCenterOfScreen(getScrollX());
1939 }
1940
1941 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1942 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001943 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001944 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001945 final int childCount = getChildCount();
1946 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001947 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001948 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001949 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001950 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001951 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1952 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1953 minDistanceFromScreenCenter = distanceFromScreenCenter;
1954 minDistanceFromScreenCenterIndex = i;
1955 }
1956 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001957 return minDistanceFromScreenCenterIndex;
1958 }
1959
1960 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001961 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001962 }
1963
Adam Cohene0f66b52010-11-23 15:06:07 -08001964 private static class ScrollInterpolator implements Interpolator {
1965 public ScrollInterpolator() {
1966 }
1967
1968 public float getInterpolation(float t) {
1969 t -= 1.0f;
1970 return t*t*t*t*t + 1;
1971 }
1972 }
1973
1974 // We want the duration of the page snap animation to be influenced by the distance that
1975 // the screen has to travel, however, we don't want this duration to be effected in a
1976 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1977 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001978 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001979 f -= 0.5f; // center the values about 0.
1980 f *= 0.3f * Math.PI / 2.0f;
1981 return (float) Math.sin(f);
1982 }
1983
Michael Jurka0142d492010-08-25 17:46:15 -07001984 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001985 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001986 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001987
Adam Cohenedb40762013-07-18 16:45:45 -07001988 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001989 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001990 int duration = 0;
1991
Sunny Goyal4d113a52015-05-27 10:05:28 -07001992 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001993 // If the velocity is low enough, then treat this more as an automatic page advance
1994 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001995 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001996 return;
1997 }
1998
1999 // Here we compute a "distance" that will be used in the computation of the overall
2000 // snap duration. This is a function of the actual distance that needs to be traveled;
2001 // we keep this value close to half screen size in order to reduce the variance in snap
2002 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002003 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002004 float distance = halfScreenSize + halfScreenSize *
2005 distanceInfluenceForSnapDuration(distanceRatio);
2006
2007 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002008 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002009
2010 // we want the page's snap velocity to approximately match the velocity at which the
2011 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002012 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2013 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002014
2015 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002016 }
2017
Sunny Goyal1740d902015-05-27 11:14:01 -07002018 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002019 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002020 }
2021
Adam Cohenf9c184a2016-01-15 16:47:43 -08002022 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002023 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002024 }
2025
Michael Jurka0142d492010-08-25 17:46:15 -07002026 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002027 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002028 }
2029
Adam Cohenf9618852013-11-08 06:45:03 -08002030 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2031 snapToPage(whichPage, duration, false, interpolator);
2032 }
2033
2034 protected void snapToPage(int whichPage, int duration, boolean immediate,
2035 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002036 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002037
Adam Cohenedb40762013-07-18 16:45:45 -07002038 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002039 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002040 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002041 }
2042
2043 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002044 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002045 }
Michael Jurka0142d492010-08-25 17:46:15 -07002046
Adam Cohenf9618852013-11-08 06:45:03 -08002047 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2048 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002049 whichPage = validateNewPage(whichPage);
2050
Adam Cohen7d30a372013-07-01 17:03:59 -07002051 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002052
2053 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002054 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002055 if (immediate) {
2056 duration = 0;
2057 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002058 duration = Math.abs(delta);
2059 }
2060
Adam Cohenf358a4b2013-07-23 16:47:31 -07002061 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002062 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002063 }
Adam Cohenf9618852013-11-08 06:45:03 -08002064
2065 if (interpolator != null) {
2066 mScroller.setInterpolator(interpolator);
2067 } else {
2068 mScroller.setInterpolator(mDefaultInterpolator);
2069 }
2070
Sunny Goyalc86df472016-02-25 09:19:38 -08002071 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002072
Adam Cohen674531f2013-12-13 15:07:14 -08002073 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002074
2075 // Trigger a compute() to finish switching pages if necessary
2076 if (immediate) {
2077 computeScroll();
2078 }
2079
2080 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002081 invalidate();
2082 }
2083
Winson Chung321e9ee2010-08-09 13:37:56 -07002084 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002085 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002086 }
2087
2088 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002089 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002090 }
2091
Adam Cohendbdff6b2013-09-18 19:09:15 -07002092 @Override
2093 public boolean performLongClick() {
2094 mCancelTap = true;
2095 return super.performLongClick();
2096 }
2097
Winson Chung321e9ee2010-08-09 13:37:56 -07002098 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002099 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002100
2101 SavedState(Parcelable superState) {
2102 super(superState);
2103 }
2104
Adam Cohen091440a2015-03-18 14:16:05 -07002105 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002106 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002107 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002108 }
2109
2110 @Override
2111 public void writeToParcel(Parcel out, int flags) {
2112 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002113 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002114 }
2115
2116 public static final Parcelable.Creator<SavedState> CREATOR =
2117 new Parcelable.Creator<SavedState>() {
2118 public SavedState createFromParcel(Parcel in) {
2119 return new SavedState(in);
2120 }
2121
2122 public SavedState[] newArray(int size) {
2123 return new SavedState[size];
2124 }
2125 };
2126 }
2127
Adam Cohen7d30a372013-07-01 17:03:59 -07002128 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002129 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002130 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002131 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2132 .translationX(0)
2133 .translationY(0)
2134 .scaleX(1)
2135 .scaleY(1)
2136 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002137 anim.addListener(new AnimatorListenerAdapter() {
2138 @Override
2139 public void onAnimationEnd(Animator animation) {
2140 onPostReorderingAnimationCompleted();
2141 }
2142 });
2143 anim.start();
2144 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002145 }
2146
Sunny Goyal1d08f702015-05-04 15:50:25 -07002147 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002148 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2149 mTouchState = TOUCH_STATE_REORDERING;
2150 mIsReordering = true;
2151
Adam Cohen7d30a372013-07-01 17:03:59 -07002152 // We must invalidate to trigger a redraw to update the layers such that the drag view
2153 // is always drawn on top
2154 invalidate();
2155 }
2156
Adam Cohen091440a2015-03-18 14:16:05 -07002157 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002158 // Trigger the callback when reordering has settled
2159 --mPostReorderingPreZoomInRemainingAnimationCount;
2160 if (mPostReorderingPreZoomInRunnable != null &&
2161 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2162 mPostReorderingPreZoomInRunnable.run();
2163 mPostReorderingPreZoomInRunnable = null;
2164 }
2165 }
2166
Sunny Goyal1d08f702015-05-04 15:50:25 -07002167 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002168 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002169 }
2170
Adam Cohenf358a4b2013-07-23 16:47:31 -07002171 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002172 int dragViewIndex = indexOfChild(v);
2173
Adam Cohen327acfe2014-06-06 11:52:52 -07002174 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002175
Adam Cohen7d30a372013-07-01 17:03:59 -07002176 mTempVisiblePagesRange[0] = 0;
2177 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002178 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002179 mReorderingStarted = true;
2180
2181 // Check if we are within the reordering range
2182 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002183 dragViewIndex <= mTempVisiblePagesRange[1]) {
2184 // Find the drag view under the pointer
2185 mDragView = getChildAt(dragViewIndex);
2186 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2187 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002188 snapToPage(getPageNearestToCenterOfScreen());
2189 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002190 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002191 return true;
2192 }
2193 return false;
2194 }
2195
2196 boolean isReordering(boolean testTouchState) {
2197 boolean state = mIsReordering;
2198 if (testTouchState) {
2199 state &= (mTouchState == TOUCH_STATE_REORDERING);
2200 }
2201 return state;
2202 }
2203 void endReordering() {
2204 // For simplicity, we call endReordering sometimes even if reordering was never started.
2205 // In that case, we don't want to do anything.
2206 if (!mReorderingStarted) return;
2207 mReorderingStarted = false;
2208
2209 // If we haven't flung-to-delete the current child, then we just animate the drag view
2210 // back into position
2211 final Runnable onCompleteRunnable = new Runnable() {
2212 @Override
2213 public void run() {
2214 onEndReordering();
2215 }
2216 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002217
2218 mPostReorderingPreZoomInRunnable = new Runnable() {
2219 public void run() {
2220 onCompleteRunnable.run();
2221 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002222 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002223 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002224
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002225 mPostReorderingPreZoomInRemainingAnimationCount =
2226 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2227 // Snap to the current page
2228 snapToPage(indexOfChild(mDragView), 0);
2229 // Animate the drag view back to the front position
2230 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002231 }
2232
Winson Chung6a0f57d2011-06-29 20:10:49 -07002233 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002234 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002235 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002236 @Override
2237 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2238 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002239 info.setScrollable(getPageCount() > 1);
2240 if (getCurrentPage() < getPageCount() - 1) {
2241 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2242 }
2243 if (getCurrentPage() > 0) {
2244 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2245 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002246 info.setClassName(getClass().getName());
2247
2248 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2249 // Besides disabling the accessibility long-click, this also prevents this view from getting
2250 // accessibility focus.
2251 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002252 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002253 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2254 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002255 }
2256
2257 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002258 public void sendAccessibilityEvent(int eventType) {
2259 // Don't let the view send real scroll events.
2260 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2261 super.sendAccessibilityEvent(eventType);
2262 }
2263 }
2264
2265 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002266 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2267 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002268 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002269 }
2270
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002271 @Override
2272 public boolean performAccessibilityAction(int action, Bundle arguments) {
2273 if (super.performAccessibilityAction(action, arguments)) {
2274 return true;
2275 }
2276 switch (action) {
2277 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2278 if (getCurrentPage() < getPageCount() - 1) {
2279 scrollRight();
2280 return true;
2281 }
2282 } break;
2283 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2284 if (getCurrentPage() > 0) {
2285 scrollLeft();
2286 return true;
2287 }
2288 } break;
2289 }
2290 return false;
2291 }
2292
Adam Cohen0ffac432013-07-10 11:19:26 -07002293 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002294 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002295 getNextPage() + 1, getChildCount());
2296 }
2297
Winson Chungd11265e2011-08-30 13:37:23 -07002298 @Override
2299 public boolean onHoverEvent(android.view.MotionEvent event) {
2300 return true;
2301 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002302}