blob: bf9a421b1331e8e61b7223233e44ba72be20a217 [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) {
Sunny Goyalc487bd32016-05-20 12:48:10 -0700262 mPageIndicator.setOnClickListener(listener);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700263 }
Sunny Goyalc487bd32016-05-20 12:48:10 -0700264 mPageIndicator.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) {
Sunny Goyalc487bd32016-05-20 12:48:10 -0700470 mPageIndicator.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
Sunny Goyal7c786f72016-06-01 14:08:21 -0700650 // If true, the start edge of the page snaps to the start edge of the viewport.
651 public boolean matchStartEdge = false;
652
Adam Cohen96d30a12013-07-16 18:13:21 -0700653 /**
654 * {@inheritDoc}
655 */
656 public LayoutParams(int width, int height) {
657 super(width, height);
658 }
659
Sunny Goyal41b22c02015-05-14 15:20:48 -0700660 public LayoutParams(Context context, AttributeSet attrs) {
661 super(context, attrs);
662 }
663
Adam Cohen96d30a12013-07-16 18:13:21 -0700664 public LayoutParams(ViewGroup.LayoutParams source) {
665 super(source);
666 }
667 }
668
Sunny Goyal41b22c02015-05-14 15:20:48 -0700669 @Override
670 public LayoutParams generateLayoutParams(AttributeSet attrs) {
671 return new LayoutParams(getContext(), attrs);
672 }
673
674 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700675 protected LayoutParams generateDefaultLayoutParams() {
676 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
677 }
678
Sunny Goyal41b22c02015-05-14 15:20:48 -0700679 @Override
680 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
681 return new LayoutParams(p);
682 }
683
684 @Override
685 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
686 return p instanceof LayoutParams;
687 }
688
Adam Cohenedb40762013-07-18 16:45:45 -0700689 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700690 LayoutParams lp = generateDefaultLayoutParams();
691 lp.isFullScreenPage = true;
692 super.addView(page, 0, lp);
693 }
694
Adam Cohen410f3cd2013-09-22 12:09:32 -0700695 public int getNormalChildHeight() {
696 return mNormalChildHeight;
697 }
698
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 @Override
700 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700701 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700702 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
703 return;
704 }
705
Adam Cohen7d30a372013-07-01 17:03:59 -0700706 // We measure the dimensions of the PagedView to be larger than the pages so that when we
707 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700708 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
709 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
710 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700711 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800712 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700713 // viewport, we can be at most one and a half screens offset once we scale down
714 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700715 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
716 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700717
Winson Chungc82d2622013-11-06 13:23:29 -0800718 int parentWidthSize = (int) (2f * maxSize);
719 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700720 int scaledWidthSize, scaledHeightSize;
721 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700722 scaledWidthSize = (int) (parentWidthSize / mMinScale);
723 scaledHeightSize = (int) (parentHeightSize / mMinScale);
724 } else {
725 scaledWidthSize = widthSize;
726 scaledHeightSize = heightSize;
727 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700728 mViewport.set(0, 0, widthSize, heightSize);
729
730 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
731 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
732 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700733 }
734
Winson Chung8aad6102012-05-11 16:27:49 -0700735 // Return early if we aren't given a proper dimension
736 if (widthSize <= 0 || heightSize <= 0) {
737 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
738 return;
739 }
740
Adam Lesinski6b879f02010-11-04 16:15:23 -0700741 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
742 * of the All apps view on XLarge displays to not take up more space then it needs. Width
743 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
744 * each page to have the same width.
745 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 final int verticalPadding = getPaddingTop() + getPaddingBottom();
747 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700748
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700749 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700751 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700752 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700753 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
754 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
755 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
756 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 final int childCount = getChildCount();
758 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700759 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700760 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100761 if (child.getVisibility() != GONE) {
762 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700763
Vladimir Marko2824b072013-10-04 16:42:17 +0100764 int childWidthMode;
765 int childHeightMode;
766 int childWidth;
767 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700768
Vladimir Marko2824b072013-10-04 16:42:17 +0100769 if (!lp.isFullScreenPage) {
770 if (lp.width == LayoutParams.WRAP_CONTENT) {
771 childWidthMode = MeasureSpec.AT_MOST;
772 } else {
773 childWidthMode = MeasureSpec.EXACTLY;
774 }
775
776 if (lp.height == LayoutParams.WRAP_CONTENT) {
777 childHeightMode = MeasureSpec.AT_MOST;
778 } else {
779 childHeightMode = MeasureSpec.EXACTLY;
780 }
781
Adam Cohen3b185e22013-10-29 14:45:58 -0700782 childWidth = getViewportWidth() - horizontalPadding
783 - mInsets.left - mInsets.right;
Sunny Goyal7c786f72016-06-01 14:08:21 -0700784
785 if (lp.matchStartEdge) {
786 childWidth += getPaddingStart();
787 }
Adam Cohen3b185e22013-10-29 14:45:58 -0700788 childHeight = getViewportHeight() - verticalPadding
789 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100790 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700791 } else {
792 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700793 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100794
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800795 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700796 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700797 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700798 if (referenceChildWidth == 0) {
799 referenceChildWidth = childWidth;
800 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700801
Vladimir Marko2824b072013-10-04 16:42:17 +0100802 final int childWidthMeasureSpec =
803 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
804 final int childHeightMeasureSpec =
805 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
806 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700807 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700808 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700809 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800810 }
811
Sunny Goyalcf25b522015-07-09 00:01:18 -0700812 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700814 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700815 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700816 return;
817 }
818
Winson Chung785d2eb2011-04-14 16:08:02 -0700819 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700821
Adam Cohen7d30a372013-07-01 17:03:59 -0700822 int offsetX = getViewportOffsetX();
823 int offsetY = getViewportOffsetY();
824
825 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700826 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700827
Sunny Goyal70660032015-05-14 00:07:08 -0700828 final int startIndex = mIsRtl ? childCount - 1 : 0;
829 final int endIndex = mIsRtl ? -1 : childCount;
830 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700831
Adam Cohen7d30a372013-07-01 17:03:59 -0700832 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700833
Adam Cohen3b185e22013-10-29 14:45:58 -0700834 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000835 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700836
Sunny Goyal7c786f72016-06-01 14:08:21 -0700837 int childLeft = offsetX +
838 ((lp.isFullScreenPage || (!mIsRtl && lp.matchStartEdge)) ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700839 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
840 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700841 }
842
Adam Cohen0ffac432013-07-10 11:19:26 -0700843 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700844 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700846 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100847 int childTop;
848 if (lp.isFullScreenPage) {
849 childTop = offsetY;
850 } else {
851 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700852 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100853 }
854
Adam Cohen96d30a12013-07-16 18:13:21 -0700855 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700856 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800857
Winson Chung785d2eb2011-04-14 16:08:02 -0700858 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700859 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800860 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700861
Sunny Goyal7c786f72016-06-01 14:08:21 -0700862 int scrollOffsetLeft = (lp.isFullScreenPage || (!mIsRtl & lp.matchStartEdge)) ?
863 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700864 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000865
866 int pageGap = mPageSpacing;
867 int next = i + delta;
868 if (next != endIndex) {
869 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
870 } else {
871 nextLp = null;
872 }
873
874 // Prevent full screen pages from showing in the viewport
875 // when they are not the current page.
876 if (lp.isFullScreenPage) {
877 pageGap = getPaddingLeft();
878 } else if (nextLp != null && nextLp.isFullScreenPage) {
879 pageGap = getPaddingRight();
880 }
881
Sunny Goyala1fbd842015-05-20 13:40:27 -0700882 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 }
884 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700885
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700886 final LayoutTransition transition = getLayoutTransition();
887 // If the transition is running defer updating max scroll, as some empty pages could
888 // still be present, and a max scroll change could cause sudden jumps in scroll.
889 if (transition != null && transition.isRunning()) {
890 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
891
892 @Override
893 public void startTransition(LayoutTransition transition, ViewGroup container,
894 View view, int transitionType) { }
895
896 @Override
897 public void endTransition(LayoutTransition transition, ViewGroup container,
898 View view, int transitionType) {
899 // Wait until all transitions are complete.
900 if (!transition.isRunning()) {
901 transition.removeTransitionListener(this);
902 updateMaxScrollX();
903 }
904 }
905 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700906 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700907 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700908 }
909
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700910 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
911 updateCurrentPageScroll();
912 mFirstLayout = false;
913 }
914
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700915 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700916 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700917 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700918 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700919 } else {
920 setCurrentPage(getNextPage());
921 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700922 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700923 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700924
925 if (isReordering(true)) {
926 updateDragViewTranslationDuringDrag();
927 }
Winson Chunge3193b92010-09-10 11:44:42 -0700928 }
929
Sunny Goyala1fbd842015-05-20 13:40:27 -0700930 protected int getChildGap() {
931 return 0;
932 }
933
Sunny Goyal316490e2015-06-02 09:38:28 -0700934 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700935 mMaxScrollX = computeMaxScrollX();
936 }
937
938 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700939 int childCount = getChildCount();
940 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700941 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700942 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700943 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700944 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700945 }
946 }
947
Adam Cohen3b185e22013-10-29 14:45:58 -0700948 public void setPageSpacing(int pageSpacing) {
949 mPageSpacing = pageSpacing;
950 requestLayout();
951 }
952
Sunny Goyal4d113a52015-05-27 10:05:28 -0700953 /**
954 * Called when the center screen changes during scrolling.
955 */
956 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700957
Winson Chunge3193b92010-09-10 11:44:42 -0700958 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700959 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700960 // Update the page indicator, we don't update the page indicator as we
961 // add/remove pages
962 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700963 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700964 }
965
Adam Cohen2591f6a2011-10-25 14:36:40 -0700966 // This ensures that when children are added, they get the correct transforms / alphas
967 // in accordance with any scroll effects.
968 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700969 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700970 invalidate();
971 }
972
Michael Jurka8b805b12012-04-18 14:23:14 -0700973 @Override
974 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700975 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700976 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700977 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700978 }
979
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700980 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700981 // Update the page indicator, we don't update the page indicator as we
982 // add/remove pages
983 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700984 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700985 }
986 }
987
988 @Override
989 public void removeView(View v) {
990 // XXX: We should find a better way to hook into this before the view
991 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700992 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700993 super.removeView(v);
994 }
995 @Override
996 public void removeViewInLayout(View v) {
997 // XXX: We should find a better way to hook into this before the view
998 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700999 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -07001000 super.removeViewInLayout(v);
1001 }
1002 @Override
1003 public void removeViewAt(int index) {
1004 // XXX: We should find a better way to hook into this before the view
1005 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -07001006 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -07001007 super.removeViewAt(index);
1008 }
1009 @Override
1010 public void removeAllViewsInLayout() {
1011 // Update the page indicator, we don't update the page indicator as we
1012 // add/remove pages
1013 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -07001014 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -07001015 }
1016
1017 super.removeAllViewsInLayout();
1018 }
1019
Adam Cohen73894962011-10-31 13:17:17 -07001020 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001021 if (index < 0 || index > getChildCount() - 1) return 0;
1022
Adam Cohenf698c6e2013-07-17 18:44:25 -07001023 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001024
Adam Cohenf698c6e2013-07-17 18:44:25 -07001025 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001026 }
1027
Adam Cohen6f127a62014-06-12 14:54:41 -07001028 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001029 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001030 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001031 }
1032
Michael Jurkadde558b2011-11-09 22:09:06 -08001033 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001034 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001035 range[0] = -1;
1036 range[1] = -1;
1037
Sunny Goyalee688162016-02-12 14:24:21 -08001038 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001039 final int visibleLeft = -getLeft();
1040 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001041 final Matrix pageShiftMatrix = getPageShiftMatrix();
1042 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001043
Sunny Goyalee688162016-02-12 14:24:21 -08001044 for (int i = 0; i < count; i++) {
1045 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001046
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001047 // Verify if the page bounds are within the visible range.
1048 sTmpRectF.left = 0;
1049 sTmpRectF.right = currPage.getMeasuredWidth();
1050 currPage.getMatrix().mapRect(sTmpRectF);
1051 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001052 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001053
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001054 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001055 if (range[0] == -1) {
1056 continue;
1057 } else {
1058 break;
1059 }
1060 }
Sunny Goyalee688162016-02-12 14:24:21 -08001061 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001062 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001063 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001064 }
1065 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001066
Sunny Goyalee688162016-02-12 14:24:21 -08001067 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001068 } else {
1069 range[0] = -1;
1070 range[1] = -1;
1071 }
1072 }
1073
Sunny Goyalee688162016-02-12 14:24:21 -08001074 protected Matrix getPageShiftMatrix() {
1075 return getMatrix();
1076 }
1077
Michael Jurka920d7f42012-05-14 16:29:55 -07001078 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001079 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001080 }
1081
Michael Jurkadde558b2011-11-09 22:09:06 -08001082 @Override
1083 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001084 // Find out which screens are visible; as an optimization we only call draw on them
1085 final int pageCount = getChildCount();
1086 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001087 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001088 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001089
1090 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1091 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1092 // set it for the next frame
1093 mForceScreenScrolled = false;
1094 screenScrolled(screenCenter);
1095 mLastScreenCenter = screenCenter;
1096 }
1097
Michael Jurkadde558b2011-11-09 22:09:06 -08001098 getVisiblePages(mTempVisiblePagesRange);
1099 final int leftScreen = mTempVisiblePagesRange[0];
1100 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001101 if (leftScreen != -1 && rightScreen != -1) {
1102 final long drawingTime = getDrawingTime();
1103 // Clip to the bounds
1104 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001105 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1106 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001107
Adam Cohen7d30a372013-07-01 17:03:59 -07001108 // Draw all the children, leaving the drag view for last
1109 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001110 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001111 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001112 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001113 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001114 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001115 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001116 // Draw the drag view on top (if there is one)
1117 if (mDragView != null) {
1118 drawChild(canvas, mDragView, drawingTime);
1119 }
1120
Winson Chungc6f10b92011-11-14 11:39:07 -08001121 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001122 }
Michael Jurka0142d492010-08-25 17:46:15 -07001123 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 }
1125
1126 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001127 public void draw(Canvas canvas) {
1128 super.draw(canvas);
1129 if (getPageCount() > 0) {
1130 if (!mEdgeGlowLeft.isFinished()) {
1131 final int restoreCount = canvas.save();
1132 Rect display = mViewport;
1133 canvas.translate(display.left, display.top);
1134 canvas.rotate(270);
1135
1136 getEdgeVerticalPostion(sTmpIntPoint);
1137 canvas.translate(display.top - sTmpIntPoint[1], 0);
1138 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1139 if (mEdgeGlowLeft.draw(canvas)) {
1140 postInvalidateOnAnimation();
1141 }
1142 canvas.restoreToCount(restoreCount);
1143 }
1144 if (!mEdgeGlowRight.isFinished()) {
1145 final int restoreCount = canvas.save();
1146 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001147 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001148 canvas.rotate(90);
1149
1150 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001151
Sunny Goyal93264612015-11-23 11:47:50 -08001152 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1153 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001154 if (mEdgeGlowRight.draw(canvas)) {
1155 postInvalidateOnAnimation();
1156 }
1157 canvas.restoreToCount(restoreCount);
1158 }
1159 }
1160 }
1161
1162 /**
1163 * Returns the top and bottom position for the edge effect.
1164 */
1165 protected abstract void getEdgeVerticalPostion(int[] pos);
1166
1167 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001169 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001170 if (page != mCurrentPage || !mScroller.isFinished()) {
1171 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 return true;
1173 }
1174 return false;
1175 }
1176
1177 @Override
1178 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001179 int focusablePage;
1180 if (mNextPage != INVALID_PAGE) {
1181 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001183 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 }
Winson Chung86f77532010-08-24 11:08:22 -07001185 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001187 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 }
1189 return false;
1190 }
1191
1192 @Override
1193 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001194 if (super.dispatchUnhandledMove(focused, direction)) {
1195 return true;
1196 }
1197
1198 if (mIsRtl) {
1199 if (direction == View.FOCUS_LEFT) {
1200 direction = View.FOCUS_RIGHT;
1201 } else if (direction == View.FOCUS_RIGHT) {
1202 direction = View.FOCUS_LEFT;
1203 }
1204 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001206 if (getCurrentPage() > 0) {
1207 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 return true;
1209 }
1210 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001211 if (getCurrentPage() < getPageCount() - 1) {
1212 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 return true;
1214 }
1215 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001216 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 }
1218
1219 @Override
1220 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001221 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001222 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001223 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 }
1225 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001226 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001227 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 }
1229 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001230 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001231 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 }
1233 }
1234 }
1235
1236 /**
1237 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001238 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 *
Winson Chung86f77532010-08-24 11:08:22 -07001240 * This happens when live folders requery, and if they're off page, they
1241 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 */
1243 @Override
1244 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001245 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 View v = focused;
1247 while (true) {
1248 if (v == current) {
1249 super.focusableViewAvailable(focused);
1250 return;
1251 }
1252 if (v == this) {
1253 return;
1254 }
1255 ViewParent parent = v.getParent();
1256 if (parent instanceof View) {
1257 v = (View)v.getParent();
1258 } else {
1259 return;
1260 }
1261 }
1262 }
1263
1264 /**
1265 * {@inheritDoc}
1266 */
1267 @Override
1268 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1269 if (disallowIntercept) {
1270 // We need to make sure to cancel our long press if
1271 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001272 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001273 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 }
1275 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1276 }
1277
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001278 /**
1279 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1280 */
1281 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001282 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001283 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001284 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001285 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001286 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001287 }
1288
1289 /**
1290 * Return true if a tap at (x, y) should trigger a flip to the next page.
1291 */
1292 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001293 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001294 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001295 }
1296 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001297 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001298 }
Winson Chung52aee602013-01-30 12:01:02 -08001299
Adam Cohen7d30a372013-07-01 17:03:59 -07001300 /** Returns whether x and y originated within the buffered viewport */
1301 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001302 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001303 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001304 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001305 }
1306
Winson Chung321e9ee2010-08-09 13:37:56 -07001307 @Override
1308 public boolean onInterceptTouchEvent(MotionEvent ev) {
1309 /*
1310 * This method JUST determines whether we want to intercept the motion.
1311 * If we return true, onTouchEvent will be called and we do the actual
1312 * scrolling there.
1313 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001314 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001315
Winson Chung45e1d6e2010-11-09 17:19:49 -08001316 // Skip touch handling if there are no pages to swipe
1317 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1318
Winson Chung321e9ee2010-08-09 13:37:56 -07001319 /*
1320 * Shortcut the most recurring case: the user is in the dragging
1321 * state and he is moving his finger. We want to intercept this
1322 * motion.
1323 */
1324 final int action = ev.getAction();
1325 if ((action == MotionEvent.ACTION_MOVE) &&
1326 (mTouchState == TOUCH_STATE_SCROLLING)) {
1327 return true;
1328 }
1329
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 switch (action & MotionEvent.ACTION_MASK) {
1331 case MotionEvent.ACTION_MOVE: {
1332 /*
1333 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1334 * whether the user has moved far enough from his original down touch.
1335 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001336 if (mActivePointerId != INVALID_POINTER) {
1337 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001338 }
1339 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1340 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1341 // i.e. fall through to the next case (don't break)
1342 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1343 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001344 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 }
1346
1347 case MotionEvent.ACTION_DOWN: {
1348 final float x = ev.getX();
1349 final float y = ev.getY();
1350 // Remember location of down touch
1351 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001352 mDownMotionY = y;
1353 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 mLastMotionX = x;
1355 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001356 float[] p = mapPointFromViewToParent(this, x, y);
1357 mParentDownMotionX = p[0];
1358 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001359 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001360 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001362
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 /*
1364 * If being flinged and user touches the screen, initiate drag;
1365 * otherwise don't. mScroller.isFinished should be false when
1366 * being flinged.
1367 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001368 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001369 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001370
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001371 if (finishedScrolling) {
1372 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001373 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001374 setCurrentPage(getNextPage());
1375 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001376 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001377 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001378 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1379 mTouchState = TOUCH_STATE_SCROLLING;
1380 } else {
1381 mTouchState = TOUCH_STATE_REST;
1382 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001383 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001384
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 break;
1386 }
1387
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001389 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001390 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 break;
1392
1393 case MotionEvent.ACTION_POINTER_UP:
1394 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001395 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 break;
1397 }
1398
1399 /*
1400 * The only time we want to intercept motion events is if we are in the
1401 * drag mode.
1402 */
1403 return mTouchState != TOUCH_STATE_REST;
1404 }
1405
Adam Cohenf8d28232011-02-01 21:47:00 -08001406 protected void determineScrollingStart(MotionEvent ev) {
1407 determineScrollingStart(ev, 1.0f);
1408 }
1409
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 /*
1411 * Determines if we should change the touch state to start scrolling after the
1412 * user moves their touch point too far.
1413 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001414 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001415 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001417 if (pointerIndex == -1) return;
1418
1419 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 final float x = ev.getX(pointerIndex);
1421 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001422 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001425
Adam Cohenf8d28232011-02-01 21:47:00 -08001426 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001428
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001429 if (xMoved) {
1430 // Scroll if the user moved far enough along the X axis
1431 mTouchState = TOUCH_STATE_SCROLLING;
1432 mTotalMotionX += Math.abs(mLastMotionX - x);
1433 mLastMotionX = x;
1434 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001435 onScrollInteractionBegin();
1436 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001437 // Stop listening for things like pinches.
1438 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001439 }
1440 }
1441
1442 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001443 // Try canceling the long press. It could also have been scheduled
1444 // by a distant descendant, so use the mAllowLongPress flag to block
1445 // everything
1446 final View currentPage = getPageAt(mCurrentPage);
1447 if (currentPage != null) {
1448 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001449 }
1450 }
1451
Adam Cohenb5ba0972011-09-07 18:02:31 -07001452 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001453 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001454
Adam Cohenedb40762013-07-18 16:45:45 -07001455 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001456 int count = getChildCount();
1457
1458 final int totalDistance;
1459
1460 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001461 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001462 adjacentPage = page - 1;
1463 }
1464
1465 if (adjacentPage < 0 || adjacentPage > count - 1) {
1466 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1467 } else {
1468 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1469 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001470
1471 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001472 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1473 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001474 return scrollProgress;
1475 }
1476
Adam Cohenedb40762013-07-18 16:45:45 -07001477 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001478 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001479 return 0;
1480 } else {
1481 return mPageScrolls[index];
1482 }
1483 }
1484
Adam Cohen564a2e72013-10-09 14:47:32 -07001485 // While layout transitions are occurring, a child's position may stray from its baseline
1486 // position. This method returns the magnitude of this stray at any given time.
1487 public int getLayoutTransitionOffsetForPage(int index) {
1488 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1489 return 0;
1490 } else {
1491 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001492
1493 int scrollOffset = 0;
1494 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1495 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001496 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001497 }
1498
Adam Cohen564a2e72013-10-09 14:47:32 -07001499 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1500 return (int) (child.getX() - baselineX);
1501 }
1502 }
1503
Adam Cohenb5ba0972011-09-07 18:02:31 -07001504 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001506 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001507 if (f < 0) {
1508 mEdgeGlowLeft.onPull(-f);
1509 } else if (f > 0) {
1510 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001511 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001512 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001513 }
1514 invalidate();
1515 }
1516
Adam Cohenb5ba0972011-09-07 18:02:31 -07001517 protected void overScroll(float amount) {
1518 dampedOverScroll(amount);
1519 }
1520
Winson Chungdc61c4d2015-04-20 18:26:57 -07001521 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001522 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001523 }
1524
Winson Chungdc61c4d2015-04-20 18:26:57 -07001525 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001526 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001527 }
1528
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001529 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001530 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001531 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001532 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1533 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1534 } else {
1535 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1536 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1537 }
1538 }
1539
Adam Cohenf9618852013-11-08 06:45:03 -08001540 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001541 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001542 mFreeScroll = freeScroll;
1543
Adam Cohenf9618852013-11-08 06:45:03 -08001544 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001545 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001546 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001547 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1548 setCurrentPage(mTempVisiblePagesRange[0]);
1549 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1550 setCurrentPage(mTempVisiblePagesRange[1]);
1551 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001552 } else if (wasFreeScroll) {
1553 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001554 }
1555
1556 setEnableOverscroll(!freeScroll);
1557 }
1558
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001559 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001560 mAllowOverScroll = enable;
1561 }
1562
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001563 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001564 if (mDragView != null) {
1565 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1566 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001567 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001568 int minDistance = Integer.MAX_VALUE;
1569 int minIndex = indexOfChild(mDragView);
1570 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1571 View page = getPageAt(i);
1572 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1573 int d = Math.abs(dragX - pageX);
1574 if (d < minDistance) {
1575 minIndex = i;
1576 minDistance = d;
1577 }
1578 }
1579 return minIndex;
1580 }
1581 return -1;
1582 }
1583
Winson Chung321e9ee2010-08-09 13:37:56 -07001584 @Override
1585 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001586 super.onTouchEvent(ev);
1587
Winson Chung45e1d6e2010-11-09 17:19:49 -08001588 // Skip touch handling if there are no pages to swipe
1589 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1590
Michael Jurkab8f06722010-10-10 15:58:46 -07001591 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001592
1593 final int action = ev.getAction();
1594
1595 switch (action & MotionEvent.ACTION_MASK) {
1596 case MotionEvent.ACTION_DOWN:
1597 /*
1598 * If being flinged and user touches, stop the fling. isFinished
1599 * will be false if being flinged.
1600 */
1601 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001602 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001603 }
1604
1605 // Remember where the motion event started
1606 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001607 mDownMotionY = mLastMotionY = ev.getY();
1608 mDownScrollX = getScrollX();
1609 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1610 mParentDownMotionX = p[0];
1611 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001612 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001613 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001614 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001615
Michael Jurka0142d492010-08-25 17:46:15 -07001616 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001617 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001618 pageBeginMoving();
1619 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001620 break;
1621
1622 case MotionEvent.ACTION_MOVE:
1623 if (mTouchState == TOUCH_STATE_SCROLLING) {
1624 // Scroll to follow the motion event
1625 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001626
1627 if (pointerIndex == -1) return true;
1628
Winson Chung321e9ee2010-08-09 13:37:56 -07001629 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001630 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001631
Adam Cohenaefd4e12011-02-14 16:39:38 -08001632 mTotalMotionX += Math.abs(deltaX);
1633
Winson Chungc0844aa2011-02-02 15:25:58 -08001634 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1635 // keep the remainder because we are actually testing if we've moved from the last
1636 // scrolled position (which is discrete).
1637 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001638 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001639 mLastMotionX = x;
1640 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001641 } else {
1642 awakenScrollBars();
1643 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001644 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1645 // Update the last motion position
1646 mLastMotionX = ev.getX();
1647 mLastMotionY = ev.getY();
1648
1649 // Update the parent down so that our zoom animations take this new movement into
1650 // account
1651 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1652 mParentDownMotionX = pt[0];
1653 mParentDownMotionY = pt[1];
1654 updateDragViewTranslationDuringDrag();
1655
1656 // Find the closest page to the touch point
1657 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001658
Adam Cohen7d30a372013-07-01 17:03:59 -07001659 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1660 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1661 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1662 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1663
Adam Cohenf358a4b2013-07-23 16:47:31 -07001664 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001665 // Do not allow any page to be moved to 0th position.
1666 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001667 mTempVisiblePagesRange[0] = 0;
1668 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001669 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001670 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1671 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1672 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1673 mSidePageHoverIndex = pageUnderPointIndex;
1674 mSidePageHoverRunnable = new Runnable() {
1675 @Override
1676 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 // Setup the scroll to the correct page before we swap the views
1678 snapToPage(pageUnderPointIndex);
1679
1680 // For each of the pages between the paged view and the drag view,
1681 // animate them from the previous position to the new position in
1682 // the layout (as a result of the drag view moving in the layout)
1683 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1684 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1685 dragViewIndex + 1 : pageUnderPointIndex;
1686 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1687 dragViewIndex - 1 : pageUnderPointIndex;
1688 for (int i = lowerIndex; i <= upperIndex; ++i) {
1689 View v = getChildAt(i);
1690 // dragViewIndex < pageUnderPointIndex, so after we remove the
1691 // drag view all subsequent views to pageUnderPointIndex will
1692 // shift down.
1693 int oldX = getViewportOffsetX() + getChildOffset(i);
1694 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1695
1696 // Animate the view translation from its old position to its new
1697 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001698 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001699 if (anim != null) {
1700 anim.cancel();
1701 }
1702
1703 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001704 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001705 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001706 anim.start();
1707 v.setTag(anim);
1708 }
1709
1710 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001711 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001712 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001713 if (mPageIndicator != null) {
1714 mPageIndicator.setActiveMarker(getNextPage());
1715 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001716 }
1717 };
1718 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1719 }
1720 } else {
1721 removeCallbacks(mSidePageHoverRunnable);
1722 mSidePageHoverIndex = -1;
1723 }
Adam Cohen564976a2010-10-13 18:52:07 -07001724 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001725 determineScrollingStart(ev);
1726 }
1727 break;
1728
1729 case MotionEvent.ACTION_UP:
1730 if (mTouchState == TOUCH_STATE_SCROLLING) {
1731 final int activePointerId = mActivePointerId;
1732 final int pointerIndex = ev.findPointerIndex(activePointerId);
1733 final float x = ev.getX(pointerIndex);
1734 final VelocityTracker velocityTracker = mVelocityTracker;
1735 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1736 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001737 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001738 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001739 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1740 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001741
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001742 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1743
Adam Cohen00481b32011-11-18 12:03:48 -08001744 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001745 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001746
Adam Cohenf358a4b2013-07-23 16:47:31 -07001747 if (!mFreeScroll) {
1748 // In the case that the page is moved far to one direction and then is flung
1749 // in the opposite direction, we use a threshold to determine whether we should
1750 // just return to the starting page, or if we should skip one further.
1751 boolean returnToOriginalPage = false;
1752 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1753 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1754 returnToOriginalPage = true;
1755 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001756
Adam Cohenf358a4b2013-07-23 16:47:31 -07001757 int finalPage;
1758 // We give flings precedence over large moves, which is why we short-circuit our
1759 // test for a large move if a fling has been registered. That is, a large
1760 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001761 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1762 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001763 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1764 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1765 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1766 snapToPageWithVelocity(finalPage, velocityX);
1767 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1768 (isFling && isVelocityXLeft)) &&
1769 mCurrentPage < getChildCount() - 1) {
1770 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1771 snapToPageWithVelocity(finalPage, velocityX);
1772 } else {
1773 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001774 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001775 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001776 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001777 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001778 }
1779
1780 float scaleX = getScaleX();
1781 int vX = (int) (-velocityX * scaleX);
1782 int initialScrollX = (int) (getScrollX() * scaleX);
1783
Adam Cohenf9618852013-11-08 06:45:03 -08001784 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001785 mScroller.fling(initialScrollX,
1786 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001787 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001788 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001789 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001790 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001791 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1792 // 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
1795 int nextPage = Math.max(0, mCurrentPage - 1);
1796 if (nextPage != mCurrentPage) {
1797 snapToPage(nextPage);
1798 } else {
1799 snapToDestination();
1800 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001801 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001802 // at this point we have not moved beyond the touch slop
1803 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1804 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001805 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1806 if (nextPage != mCurrentPage) {
1807 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001808 } else {
1809 snapToDestination();
1810 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001811 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1812 // Update the last motion position
1813 mLastMotionX = ev.getX();
1814 mLastMotionY = ev.getY();
1815
1816 // Update the parent down so that our zoom animations take this new movement into
1817 // account
1818 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1819 mParentDownMotionX = pt[0];
1820 mParentDownMotionY = pt[1];
1821 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001822 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001823 if (!mCancelTap) {
1824 onUnhandledTap(ev);
1825 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001826 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001827
1828 // Remove the callback to wait for the side page hover timeout
1829 removeCallbacks(mSidePageHoverRunnable);
1830 // End any intermediate reordering states
1831 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001832 break;
1833
1834 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001835 if (mTouchState == TOUCH_STATE_SCROLLING) {
1836 snapToDestination();
1837 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001838 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001839 break;
1840
1841 case MotionEvent.ACTION_POINTER_UP:
1842 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001843 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001844 break;
1845 }
1846
1847 return true;
1848 }
1849
Adam Cohen7d30a372013-07-01 17:03:59 -07001850 private void resetTouchState() {
1851 releaseVelocityTracker();
1852 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001853 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001854 mTouchState = TOUCH_STATE_REST;
1855 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001856 mEdgeGlowLeft.onRelease();
1857 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001858 }
1859
Adam Cohenc2d6e892014-10-16 09:49:24 -07001860 /**
1861 * Triggered by scrolling via touch
1862 */
1863 protected void onScrollInteractionBegin() {
1864 }
1865
1866 protected void onScrollInteractionEnd() {
1867 }
1868
Adam Cohen1697b792013-09-17 19:08:21 -07001869 protected void onUnhandledTap(MotionEvent ev) {
1870 ((Launcher) getContext()).onClick(this);
1871 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001872
Winson Chung185d7162011-02-28 13:47:29 -08001873 @Override
1874 public boolean onGenericMotionEvent(MotionEvent event) {
1875 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1876 switch (event.getAction()) {
1877 case MotionEvent.ACTION_SCROLL: {
1878 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1879 final float vscroll;
1880 final float hscroll;
1881 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1882 vscroll = 0;
1883 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1884 } else {
1885 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1886 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1887 }
1888 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001889 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001890 : (hscroll > 0 || vscroll > 0);
1891 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001892 scrollRight();
1893 } else {
1894 scrollLeft();
1895 }
1896 return true;
1897 }
1898 }
1899 }
1900 }
1901 return super.onGenericMotionEvent(event);
1902 }
1903
Michael Jurkab8f06722010-10-10 15:58:46 -07001904 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1905 if (mVelocityTracker == null) {
1906 mVelocityTracker = VelocityTracker.obtain();
1907 }
1908 mVelocityTracker.addMovement(ev);
1909 }
1910
1911 private void releaseVelocityTracker() {
1912 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001913 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001914 mVelocityTracker.recycle();
1915 mVelocityTracker = null;
1916 }
1917 }
1918
Winson Chung321e9ee2010-08-09 13:37:56 -07001919 private void onSecondaryPointerUp(MotionEvent ev) {
1920 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1921 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1922 final int pointerId = ev.getPointerId(pointerIndex);
1923 if (pointerId == mActivePointerId) {
1924 // This was our active pointer going up. Choose a new
1925 // active pointer and adjust accordingly.
1926 // TODO: Make this decision more intelligent.
1927 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1928 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1929 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001930 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001931 mActivePointerId = ev.getPointerId(newPointerIndex);
1932 if (mVelocityTracker != null) {
1933 mVelocityTracker.clear();
1934 }
1935 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001936 }
1937
Winson Chung321e9ee2010-08-09 13:37:56 -07001938 @Override
1939 public void requestChildFocus(View child, View focused) {
1940 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001941 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001942 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001943 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001944 }
1945 }
1946
Adam Cohend19d3ca2010-09-15 14:43:42 -07001947 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001948 return getPageNearestToCenterOfScreen(getScrollX());
1949 }
1950
1951 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1952 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001953 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001954 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001955 final int childCount = getChildCount();
1956 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001957 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001958 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001960 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001961 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1962 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1963 minDistanceFromScreenCenter = distanceFromScreenCenter;
1964 minDistanceFromScreenCenterIndex = i;
1965 }
1966 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001967 return minDistanceFromScreenCenterIndex;
1968 }
1969
1970 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001971 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001972 }
1973
Adam Cohene0f66b52010-11-23 15:06:07 -08001974 private static class ScrollInterpolator implements Interpolator {
1975 public ScrollInterpolator() {
1976 }
1977
1978 public float getInterpolation(float t) {
1979 t -= 1.0f;
1980 return t*t*t*t*t + 1;
1981 }
1982 }
1983
1984 // We want the duration of the page snap animation to be influenced by the distance that
1985 // the screen has to travel, however, we don't want this duration to be effected in a
1986 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1987 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001988 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001989 f -= 0.5f; // center the values about 0.
1990 f *= 0.3f * Math.PI / 2.0f;
1991 return (float) Math.sin(f);
1992 }
1993
Michael Jurka0142d492010-08-25 17:46:15 -07001994 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001995 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001996 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001997
Adam Cohenedb40762013-07-18 16:45:45 -07001998 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001999 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002000 int duration = 0;
2001
Sunny Goyal4d113a52015-05-27 10:05:28 -07002002 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002003 // If the velocity is low enough, then treat this more as an automatic page advance
2004 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002005 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002006 return;
2007 }
2008
2009 // Here we compute a "distance" that will be used in the computation of the overall
2010 // snap duration. This is a function of the actual distance that needs to be traveled;
2011 // we keep this value close to half screen size in order to reduce the variance in snap
2012 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002013 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002014 float distance = halfScreenSize + halfScreenSize *
2015 distanceInfluenceForSnapDuration(distanceRatio);
2016
2017 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002018 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002019
2020 // we want the page's snap velocity to approximately match the velocity at which the
2021 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002022 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2023 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002024
2025 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002026 }
2027
Sunny Goyal1740d902015-05-27 11:14:01 -07002028 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002029 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002030 }
2031
Adam Cohenf9c184a2016-01-15 16:47:43 -08002032 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002033 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002034 }
2035
Michael Jurka0142d492010-08-25 17:46:15 -07002036 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002037 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 }
2039
Adam Cohenf9618852013-11-08 06:45:03 -08002040 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2041 snapToPage(whichPage, duration, false, interpolator);
2042 }
2043
2044 protected void snapToPage(int whichPage, int duration, boolean immediate,
2045 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002046 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002047
Adam Cohenedb40762013-07-18 16:45:45 -07002048 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002049 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002050 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002051 }
2052
2053 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002054 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002055 }
Michael Jurka0142d492010-08-25 17:46:15 -07002056
Adam Cohenf9618852013-11-08 06:45:03 -08002057 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2058 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002059 whichPage = validateNewPage(whichPage);
2060
Adam Cohen7d30a372013-07-01 17:03:59 -07002061 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002062
2063 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002064 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002065 if (immediate) {
2066 duration = 0;
2067 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002068 duration = Math.abs(delta);
2069 }
2070
Adam Cohenf358a4b2013-07-23 16:47:31 -07002071 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002072 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002073 }
Adam Cohenf9618852013-11-08 06:45:03 -08002074
2075 if (interpolator != null) {
2076 mScroller.setInterpolator(interpolator);
2077 } else {
2078 mScroller.setInterpolator(mDefaultInterpolator);
2079 }
2080
Sunny Goyalc86df472016-02-25 09:19:38 -08002081 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002082
Adam Cohen674531f2013-12-13 15:07:14 -08002083 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002084
2085 // Trigger a compute() to finish switching pages if necessary
2086 if (immediate) {
2087 computeScroll();
2088 }
2089
2090 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 invalidate();
2092 }
2093
Winson Chung321e9ee2010-08-09 13:37:56 -07002094 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002095 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002096 }
2097
2098 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002099 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002100 }
2101
Adam Cohendbdff6b2013-09-18 19:09:15 -07002102 @Override
2103 public boolean performLongClick() {
2104 mCancelTap = true;
2105 return super.performLongClick();
2106 }
2107
Winson Chung321e9ee2010-08-09 13:37:56 -07002108 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002109 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002110
2111 SavedState(Parcelable superState) {
2112 super(superState);
2113 }
2114
Adam Cohen091440a2015-03-18 14:16:05 -07002115 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002116 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002117 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 }
2119
2120 @Override
2121 public void writeToParcel(Parcel out, int flags) {
2122 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002123 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002124 }
2125
2126 public static final Parcelable.Creator<SavedState> CREATOR =
2127 new Parcelable.Creator<SavedState>() {
2128 public SavedState createFromParcel(Parcel in) {
2129 return new SavedState(in);
2130 }
2131
2132 public SavedState[] newArray(int size) {
2133 return new SavedState[size];
2134 }
2135 };
2136 }
2137
Adam Cohen7d30a372013-07-01 17:03:59 -07002138 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002139 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002140 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002141 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2142 .translationX(0)
2143 .translationY(0)
2144 .scaleX(1)
2145 .scaleY(1)
2146 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002147 anim.addListener(new AnimatorListenerAdapter() {
2148 @Override
2149 public void onAnimationEnd(Animator animation) {
2150 onPostReorderingAnimationCompleted();
2151 }
2152 });
2153 anim.start();
2154 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002155 }
2156
Sunny Goyal1d08f702015-05-04 15:50:25 -07002157 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002158 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2159 mTouchState = TOUCH_STATE_REORDERING;
2160 mIsReordering = true;
2161
Adam Cohen7d30a372013-07-01 17:03:59 -07002162 // We must invalidate to trigger a redraw to update the layers such that the drag view
2163 // is always drawn on top
2164 invalidate();
2165 }
2166
Adam Cohen091440a2015-03-18 14:16:05 -07002167 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002168 // Trigger the callback when reordering has settled
2169 --mPostReorderingPreZoomInRemainingAnimationCount;
2170 if (mPostReorderingPreZoomInRunnable != null &&
2171 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2172 mPostReorderingPreZoomInRunnable.run();
2173 mPostReorderingPreZoomInRunnable = null;
2174 }
2175 }
2176
Sunny Goyal1d08f702015-05-04 15:50:25 -07002177 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002178 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002179 }
2180
Adam Cohenf358a4b2013-07-23 16:47:31 -07002181 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002182 int dragViewIndex = indexOfChild(v);
2183
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002184 // Do not allow the first page to be moved around
2185 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002186
Adam Cohen7d30a372013-07-01 17:03:59 -07002187 mTempVisiblePagesRange[0] = 0;
2188 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002189 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002190 mReorderingStarted = true;
2191
2192 // Check if we are within the reordering range
2193 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002194 dragViewIndex <= mTempVisiblePagesRange[1]) {
2195 // Find the drag view under the pointer
2196 mDragView = getChildAt(dragViewIndex);
2197 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2198 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002199 snapToPage(getPageNearestToCenterOfScreen());
2200 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002201 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002202 return true;
2203 }
2204 return false;
2205 }
2206
2207 boolean isReordering(boolean testTouchState) {
2208 boolean state = mIsReordering;
2209 if (testTouchState) {
2210 state &= (mTouchState == TOUCH_STATE_REORDERING);
2211 }
2212 return state;
2213 }
2214 void endReordering() {
2215 // For simplicity, we call endReordering sometimes even if reordering was never started.
2216 // In that case, we don't want to do anything.
2217 if (!mReorderingStarted) return;
2218 mReorderingStarted = false;
2219
2220 // If we haven't flung-to-delete the current child, then we just animate the drag view
2221 // back into position
2222 final Runnable onCompleteRunnable = new Runnable() {
2223 @Override
2224 public void run() {
2225 onEndReordering();
2226 }
2227 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002228
2229 mPostReorderingPreZoomInRunnable = new Runnable() {
2230 public void run() {
2231 onCompleteRunnable.run();
2232 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002233 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002234 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002235
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002236 mPostReorderingPreZoomInRemainingAnimationCount =
2237 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2238 // Snap to the current page
2239 snapToPage(indexOfChild(mDragView), 0);
2240 // Animate the drag view back to the front position
2241 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002242 }
2243
Winson Chung6a0f57d2011-06-29 20:10:49 -07002244 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002245 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002246 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002247 @Override
2248 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2249 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002250 info.setScrollable(getPageCount() > 1);
2251 if (getCurrentPage() < getPageCount() - 1) {
2252 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2253 }
2254 if (getCurrentPage() > 0) {
2255 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2256 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002257 info.setClassName(getClass().getName());
2258
2259 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2260 // Besides disabling the accessibility long-click, this also prevents this view from getting
2261 // accessibility focus.
2262 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002263 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002264 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2265 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002266 }
2267
2268 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002269 public void sendAccessibilityEvent(int eventType) {
2270 // Don't let the view send real scroll events.
2271 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2272 super.sendAccessibilityEvent(eventType);
2273 }
2274 }
2275
2276 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002277 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2278 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002279 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002280 }
2281
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002282 @Override
2283 public boolean performAccessibilityAction(int action, Bundle arguments) {
2284 if (super.performAccessibilityAction(action, arguments)) {
2285 return true;
2286 }
2287 switch (action) {
2288 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2289 if (getCurrentPage() < getPageCount() - 1) {
2290 scrollRight();
2291 return true;
2292 }
2293 } break;
2294 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2295 if (getCurrentPage() > 0) {
2296 scrollLeft();
2297 return true;
2298 }
2299 } break;
2300 }
2301 return false;
2302 }
2303
Adam Cohen0ffac432013-07-10 11:19:26 -07002304 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002305 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002306 getNextPage() + 1, getChildCount());
2307 }
2308
Winson Chungd11265e2011-08-30 13:37:23 -07002309 @Override
2310 public boolean onHoverEvent(android.view.MotionEvent event) {
2311 return true;
2312 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002313}