blob: 0b9bf09dfb120b0183a45cd4af24451fe749bec0 [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;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700101 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700102
Sunny Goyal4ffec482016-02-09 11:28:52 -0800103 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800105 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800106 protected LauncherScroller mScroller;
107 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700109 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Adam Cohen7d30a372013-07-01 17:03:59 -0700111 private float mParentDownMotionX;
112 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700114 private float mDownMotionY;
115 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700116 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700117 private float mLastMotionX;
118 private float mLastMotionXRemainder;
119 private float mLastMotionY;
120 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700121 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700122
Adam Cohendbdff6b2013-09-18 19:09:15 -0700123 private boolean mCancelTap;
124
Adam Cohenedb40762013-07-18 16:45:45 -0700125 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Michael Jurka0142d492010-08-25 17:46:15 -0700127 protected final static int TOUCH_STATE_REST = 0;
128 protected final static int TOUCH_STATE_SCROLLING = 1;
129 protected final static int TOUCH_STATE_PREV_PAGE = 2;
130 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 protected final static int TOUCH_STATE_REORDERING = 4;
132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700134 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800135
Michael Jurka0142d492010-08-25 17:46:15 -0700136 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka7426c422010-11-11 15:23:47 -0800138 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800140 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800141 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka5f1c5092010-09-03 14:15:02 -0700145 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147 protected boolean mIsPageMoving = false;
148
Sunny Goyal061380a2016-02-29 15:15:03 -0800149 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700150
Winson Chungd2be3812013-07-16 11:11:32 -0700151 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700152 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700153 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700154 // The viewport whether the pages are to be contained (the actual view may be larger than the
155 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800156 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700157 private Rect mViewport = new Rect();
158
159 // Reordering
160 // We use the min scale to determine how much to expand the actually PagedView measured
161 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700162 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700163 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700164 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
165
Adam Cohen7d30a372013-07-01 17:03:59 -0700166 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700167 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700168 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700171 // This variable's scope is only for the duration of startReordering() and endReordering()
172 private boolean mReorderingStarted = false;
173 // This variable's scope is for the duration of startReordering() and after the zoomIn()
174 // animation after endReordering()
175 private boolean mIsReordering;
176 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700177 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 private int mPostReorderingPreZoomInRemainingAnimationCount;
179 private Runnable mPostReorderingPreZoomInRunnable;
180
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700182 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700183 private static final float[] sTmpPoint = new float[2];
184 private static final int[] sTmpIntPoint = new int[2];
185 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800186 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700187
John Spurlock77e1f472013-09-11 10:09:51 -0400188 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700189 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400190
Sunny Goyal4d113a52015-05-27 10:05:28 -0700191 // Edge effect
192 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
193 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
194
Winson Chung321e9ee2010-08-09 13:37:56 -0700195 public PagedView(Context context) {
196 this(context, null);
197 }
198
199 public PagedView(Context context, AttributeSet attrs) {
200 this(context, attrs, 0);
201 }
202
203 public PagedView(Context context, AttributeSet attrs, int defStyle) {
204 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700205
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 TypedArray a = context.obtainStyledAttributes(attrs,
207 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700208 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 a.recycle();
210
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700212 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700213 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 }
215
216 /**
217 * Initializes various states for this workspace.
218 */
Michael Jurka0142d492010-08-25 17:46:15 -0700219 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800220 mScroller = new LauncherScroller(getContext());
221 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700222 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223
224 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700225 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800227
Sunny Goyalcf25b522015-07-09 00:01:18 -0700228 float density = getResources().getDisplayMetrics().density;
229 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
230 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
231 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700232 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700233 setWillNotDraw(false);
234 }
235
236 protected void setEdgeGlowColor(int color) {
237 mEdgeGlowLeft.setColor(color);
238 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 }
240
Adam Cohenf9618852013-11-08 06:45:03 -0800241 protected void setDefaultInterpolator(Interpolator interpolator) {
242 mDefaultInterpolator = interpolator;
243 mScroller.setInterpolator(mDefaultInterpolator);
244 }
245
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700246 public void initParentViews(View parent) {
247 if (mPageIndicatorViewId > -1) {
248 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700249 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700250 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700251 }
252 }
253
Adam Cohen7d30a372013-07-01 17:03:59 -0700254 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700255 private float[] mapPointFromViewToParent(View v, float x, float y) {
256 sTmpPoint[0] = x;
257 sTmpPoint[1] = y;
258 v.getMatrix().mapPoints(sTmpPoint);
259 sTmpPoint[0] += v.getLeft();
260 sTmpPoint[1] += v.getTop();
261 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700262 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700263 private float[] mapPointFromParentToView(View v, float x, float y) {
264 sTmpPoint[0] = x - v.getLeft();
265 sTmpPoint[1] = y - v.getTop();
266 v.getMatrix().invert(sTmpInvMatrix);
267 sTmpInvMatrix.mapPoints(sTmpPoint);
268 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700269 }
270
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700271 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700272 if (mDragView != null) {
273 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
274 (mDragViewBaselineLeft - mDragView.getLeft());
275 float y = mLastMotionY - mDownMotionY;
276 mDragView.setTranslationX(x);
277 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700278
Adam Cohenf358a4b2013-07-23 16:47:31 -0700279 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
280 + x + ", " + y);
281 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700282 }
283
284 public void setMinScale(float f) {
285 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700286 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700287 requestLayout();
288 }
289
290 @Override
291 public void setScaleX(float scaleX) {
292 super.setScaleX(scaleX);
293 if (isReordering(true)) {
294 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
295 mLastMotionX = p[0];
296 mLastMotionY = p[1];
297 updateDragViewTranslationDuringDrag();
298 }
299 }
300
301 // Convenience methods to get the actual width/height of the PagedView (since it is measured
302 // to be larger to account for the minimum possible scale)
303 int getViewportWidth() {
304 return mViewport.width();
305 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800306 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700307 return mViewport.height();
308 }
309
310 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
311 // PagedView both horizontally and vertically
312 int getViewportOffsetX() {
313 return (getMeasuredWidth() - getViewportWidth()) / 2;
314 }
315
316 int getViewportOffsetY() {
317 return (getMeasuredHeight() - getViewportHeight()) / 2;
318 }
319
Hyunyoung Song77441692016-06-27 22:00:48 -0700320 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700321 return mPageIndicator;
322 }
323
Adam Cohen674531f2013-12-13 15:07:14 -0800324 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700325 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
326 * that the user was on before entering free scroll mode (e.g. the home screen page they
327 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
328 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700330 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700331 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700332 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700333
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700334 /**
335 * Returns the index of page to be shown immediately afterwards.
336 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700337 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700338 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
339 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700340
Adam Cohenf9c184a2016-01-15 16:47:43 -0800341 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 return getChildCount();
343 }
344
Sunny Goyal83a8f042015-05-19 12:52:12 -0700345 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700346 return getChildAt(index);
347 }
348
Adam Cohenae4f1552011-10-20 00:15:42 -0700349 protected int indexToPage(int index) {
350 return index;
351 }
352
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800354 * Updates the scroll of the current page immediately to its final scroll position. We use this
355 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
356 * the previous tab page.
357 */
358 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700359 // If the current page is invalid, just reset the scroll position to zero
360 int newX = 0;
361 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700362 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700363 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800364 scrollTo(newX, 0);
365 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700366 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800367 }
368
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700369 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700370 mScroller.abortAnimation();
371 // We need to clean up the next page here to avoid computeScrollHelper from
372 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700373 if (resetNextPage) {
374 mNextPage = INVALID_PAGE;
375 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700376 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700377
Tony Wickham8f7ead32016-04-07 18:46:44 -0700378 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700379 mScroller.forceFinished(true);
380 // We need to clean up the next page here to avoid computeScrollHelper from
381 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700382 if (resetNextPage) {
383 mNextPage = INVALID_PAGE;
384 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700385 }
386
Adam Cohen6f127a62014-06-12 14:54:41 -0700387 private int validateNewPage(int newPage) {
388 int validatedPage = newPage;
389 // When in free scroll mode, we need to clamp to the free scroll page range.
390 if (mFreeScroll) {
391 getFreeScrollPageRange(mTempVisiblePagesRange);
392 validatedPage = Math.max(mTempVisiblePagesRange[0],
393 Math.min(newPage, mTempVisiblePagesRange[1]));
394 }
395 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700396 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700397 return validatedPage;
398 }
399
Chet Haasebc2f0822012-10-26 17:59:53 -0700400 /**
Winson Chung86f77532010-08-24 11:08:22 -0700401 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700403 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800404 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700405 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800406 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800407 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
408 // the default
409 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800410 return;
411 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700412 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700413 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700414 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700415 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800416 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
418
Winson Chung8c87cd82013-07-23 16:20:10 -0700419 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800420 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
421 * has settled.
422 */
Michael Jurka0142d492010-08-25 17:46:15 -0700423 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800424 updatePageIndicator();
425 }
426
427 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700428 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700429 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700430 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700431 if (!isReordering(false)) {
432 mPageIndicator.setActiveMarker(getNextPage());
433 }
Winson Chungd2be3812013-07-16 11:11:32 -0700434 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800436 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700437 if (!mIsPageMoving) {
438 mIsPageMoving = true;
439 onPageBeginMoving();
440 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700441 }
442
Michael Jurkace7e05f2011-02-01 22:02:35 -0800443 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700444 if (mIsPageMoving) {
445 mIsPageMoving = false;
446 onPageEndMoving();
447 }
Michael Jurka0142d492010-08-25 17:46:15 -0700448 }
449
Adam Cohen26976d92011-03-22 15:33:33 -0700450 protected boolean isPageMoving() {
451 return mIsPageMoving;
452 }
453
Michael Jurka0142d492010-08-25 17:46:15 -0700454 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700455 protected void onPageBeginMoving() {
456 }
457
458 // a method that subclasses can override to add behavior
459 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700460 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700461 }
462
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 /**
Winson Chung86f77532010-08-24 11:08:22 -0700464 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 *
466 * @param l The listener used to respond to long clicks.
467 */
468 @Override
469 public void setOnLongClickListener(OnLongClickListener l) {
470 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700471 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700473 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
Adam Cohen1697b792013-09-17 19:08:21 -0700475 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 }
477
Sunny Goyalc86df472016-02-25 09:19:38 -0800478 protected int getUnboundedScrollX() {
479 return getScrollX();
480 }
481
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800483 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800484 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800485 }
486
487 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700488 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700489 // In free scroll mode, we clamp the scrollX
490 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700491 // If the scroller is trying to move to a location beyond the maximum allowed
492 // in the free scroll mode, we make sure to end the scroll operation.
493 if (!mScroller.isFinished() &&
494 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700495 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700496 }
497
Adam Cohenf358a4b2013-07-23 16:47:31 -0700498 x = Math.min(x, mFreeScrollMaxScrollX);
499 x = Math.max(x, mFreeScrollMinScrollX);
500 }
501
Sunny Goyal70660032015-05-14 00:07:08 -0700502 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
503 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700504 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700505 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800506 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700507 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700508 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700509 overScroll(x - mMaxScrollX);
510 } else {
511 overScroll(x);
512 }
Adam Cohen68d73932010-11-15 10:50:58 -0800513 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700514 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700515 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800516 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700517 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700518 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700519 overScroll(x);
520 } else {
521 overScroll(x - mMaxScrollX);
522 }
Adam Cohen68d73932010-11-15 10:50:58 -0800523 }
524 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700525 if (mWasInOverscroll) {
526 overScroll(0);
527 mWasInOverscroll = false;
528 }
Adam Cohen68d73932010-11-15 10:50:58 -0800529 super.scrollTo(x, y);
530 }
531
Adam Cohen7d30a372013-07-01 17:03:59 -0700532 // Update the last motion events when scrolling
533 if (isReordering(true)) {
534 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
535 mLastMotionX = p[0];
536 mLastMotionY = p[1];
537 updateDragViewTranslationDuringDrag();
538 }
Michael Jurka0142d492010-08-25 17:46:15 -0700539 }
540
Adam Cohen53805212013-10-01 10:39:23 -0700541 private void sendScrollAccessibilityEvent() {
542 AccessibilityManager am =
543 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
544 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700545 if (mCurrentPage != getNextPage()) {
546 AccessibilityEvent ev =
547 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700548 ev.setScrollable(true);
549 ev.setScrollX(getScrollX());
550 ev.setScrollY(getScrollY());
551 ev.setMaxScrollX(mMaxScrollX);
552 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700553
Vadim Tryshevf4715972015-05-08 17:21:03 -0700554 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700555 }
Adam Cohen53805212013-10-01 10:39:23 -0700556 }
557 }
558
Michael Jurka0142d492010-08-25 17:46:15 -0700559 // we moved this functionality to a helper function so SmoothPagedView can reuse it
560 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800561 return computeScrollHelper(true);
562 }
563
564 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700566 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800568 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700569 float scaleX = mFreeScroll ? getScaleX() : 1f;
570 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
571 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700572 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800573 if (shouldInvalidate) {
574 invalidate();
575 }
Michael Jurka0142d492010-08-25 17:46:15 -0700576 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800577 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700578 sendScrollAccessibilityEvent();
579
Adam Cohen6f127a62014-06-12 14:54:41 -0700580 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700581 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700582 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700583
Adam Cohen73aa9752010-11-24 16:26:58 -0800584 // We don't want to trigger a page end moving unless the page has settled
585 // and the user has stopped scrolling
586 if (mTouchState == TOUCH_STATE_REST) {
587 pageEndMoving();
588 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700589
Adam Cohen7d30a372013-07-01 17:03:59 -0700590 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700591 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700592 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700593 if (am.isEnabled()) {
594 // Notify the user when the page changes
595 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700596 }
Michael Jurka0142d492010-08-25 17:46:15 -0700597 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
Michael Jurka0142d492010-08-25 17:46:15 -0700599 return false;
600 }
601
602 @Override
603 public void computeScroll() {
604 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 }
606
Adam Cohen96d30a12013-07-16 18:13:21 -0700607 public static class LayoutParams extends ViewGroup.LayoutParams {
608 public boolean isFullScreenPage = false;
609
Sunny Goyal7c786f72016-06-01 14:08:21 -0700610 // If true, the start edge of the page snaps to the start edge of the viewport.
611 public boolean matchStartEdge = false;
612
Adam Cohen96d30a12013-07-16 18:13:21 -0700613 /**
614 * {@inheritDoc}
615 */
616 public LayoutParams(int width, int height) {
617 super(width, height);
618 }
619
Sunny Goyal41b22c02015-05-14 15:20:48 -0700620 public LayoutParams(Context context, AttributeSet attrs) {
621 super(context, attrs);
622 }
623
Adam Cohen96d30a12013-07-16 18:13:21 -0700624 public LayoutParams(ViewGroup.LayoutParams source) {
625 super(source);
626 }
627 }
628
Sunny Goyal41b22c02015-05-14 15:20:48 -0700629 @Override
630 public LayoutParams generateLayoutParams(AttributeSet attrs) {
631 return new LayoutParams(getContext(), attrs);
632 }
633
634 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700635 protected LayoutParams generateDefaultLayoutParams() {
636 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
637 }
638
Sunny Goyal41b22c02015-05-14 15:20:48 -0700639 @Override
640 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
641 return new LayoutParams(p);
642 }
643
644 @Override
645 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
646 return p instanceof LayoutParams;
647 }
648
Adam Cohenedb40762013-07-18 16:45:45 -0700649 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700650 LayoutParams lp = generateDefaultLayoutParams();
651 lp.isFullScreenPage = true;
652 super.addView(page, 0, lp);
653 }
654
Adam Cohen410f3cd2013-09-22 12:09:32 -0700655 public int getNormalChildHeight() {
656 return mNormalChildHeight;
657 }
658
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 @Override
660 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700661 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700662 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
663 return;
664 }
665
Adam Cohen7d30a372013-07-01 17:03:59 -0700666 // We measure the dimensions of the PagedView to be larger than the pages so that when we
667 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700668 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
669 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
670 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700671 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800672 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700673 // viewport, we can be at most one and a half screens offset once we scale down
674 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700675 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
676 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700677
Winson Chungc82d2622013-11-06 13:23:29 -0800678 int parentWidthSize = (int) (2f * maxSize);
679 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700680 int scaledWidthSize, scaledHeightSize;
681 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700682 scaledWidthSize = (int) (parentWidthSize / mMinScale);
683 scaledHeightSize = (int) (parentHeightSize / mMinScale);
684 } else {
685 scaledWidthSize = widthSize;
686 scaledHeightSize = heightSize;
687 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700688 mViewport.set(0, 0, widthSize, heightSize);
689
690 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
691 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
692 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700693 }
694
Winson Chung8aad6102012-05-11 16:27:49 -0700695 // Return early if we aren't given a proper dimension
696 if (widthSize <= 0 || heightSize <= 0) {
697 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
698 return;
699 }
700
Adam Lesinski6b879f02010-11-04 16:15:23 -0700701 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
702 * of the All apps view on XLarge displays to not take up more space then it needs. Width
703 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
704 * each page to have the same width.
705 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int verticalPadding = getPaddingTop() + getPaddingBottom();
707 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700708
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700709 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700711 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700712 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700713 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
714 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
715 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
716 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 final int childCount = getChildCount();
718 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700719 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700720 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100721 if (child.getVisibility() != GONE) {
722 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700723
Vladimir Marko2824b072013-10-04 16:42:17 +0100724 int childWidthMode;
725 int childHeightMode;
726 int childWidth;
727 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700728
Vladimir Marko2824b072013-10-04 16:42:17 +0100729 if (!lp.isFullScreenPage) {
730 if (lp.width == LayoutParams.WRAP_CONTENT) {
731 childWidthMode = MeasureSpec.AT_MOST;
732 } else {
733 childWidthMode = MeasureSpec.EXACTLY;
734 }
735
736 if (lp.height == LayoutParams.WRAP_CONTENT) {
737 childHeightMode = MeasureSpec.AT_MOST;
738 } else {
739 childHeightMode = MeasureSpec.EXACTLY;
740 }
741
Adam Cohen3b185e22013-10-29 14:45:58 -0700742 childWidth = getViewportWidth() - horizontalPadding
743 - mInsets.left - mInsets.right;
744 childHeight = getViewportHeight() - verticalPadding
745 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100746 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700747 } else {
748 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700749 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100750
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800751 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700752 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700753 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700754 if (referenceChildWidth == 0) {
755 referenceChildWidth = childWidth;
756 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700757
Vladimir Marko2824b072013-10-04 16:42:17 +0100758 final int childWidthMeasureSpec =
759 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
760 final int childHeightMeasureSpec =
761 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
762 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700763 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700764 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700765 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800766 }
767
Sunny Goyalcf25b522015-07-09 00:01:18 -0700768 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700770 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700771 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700772 return;
773 }
774
Winson Chung785d2eb2011-04-14 16:08:02 -0700775 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700777
Adam Cohen7d30a372013-07-01 17:03:59 -0700778 int offsetX = getViewportOffsetX();
779 int offsetY = getViewportOffsetY();
780
781 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700782 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700783
Sunny Goyal70660032015-05-14 00:07:08 -0700784 final int startIndex = mIsRtl ? childCount - 1 : 0;
785 final int endIndex = mIsRtl ? -1 : childCount;
786 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700787
Adam Cohen7d30a372013-07-01 17:03:59 -0700788 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700789
Adam Cohen3b185e22013-10-29 14:45:58 -0700790 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000791 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700792
Sunny Goyal6178f132016-07-11 17:30:03 -0700793 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700794 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
795 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700796 }
797
Adam Cohen0ffac432013-07-10 11:19:26 -0700798 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700799 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700801 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100802 int childTop;
803 if (lp.isFullScreenPage) {
804 childTop = offsetY;
805 } else {
806 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700807 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100808 }
809
Adam Cohen96d30a12013-07-16 18:13:21 -0700810 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700811 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800812
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700814 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800815 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700816
Sunny Goyal6178f132016-07-11 17:30:03 -0700817 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -0700818 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000819
820 int pageGap = mPageSpacing;
821 int next = i + delta;
822 if (next != endIndex) {
823 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
824 } else {
825 nextLp = null;
826 }
827
828 // Prevent full screen pages from showing in the viewport
829 // when they are not the current page.
830 if (lp.isFullScreenPage) {
831 pageGap = getPaddingLeft();
832 } else if (nextLp != null && nextLp.isFullScreenPage) {
833 pageGap = getPaddingRight();
834 }
835
Sunny Goyala1fbd842015-05-20 13:40:27 -0700836 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 }
838 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700839
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700840 final LayoutTransition transition = getLayoutTransition();
841 // If the transition is running defer updating max scroll, as some empty pages could
842 // still be present, and a max scroll change could cause sudden jumps in scroll.
843 if (transition != null && transition.isRunning()) {
844 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
845
846 @Override
847 public void startTransition(LayoutTransition transition, ViewGroup container,
848 View view, int transitionType) { }
849
850 @Override
851 public void endTransition(LayoutTransition transition, ViewGroup container,
852 View view, int transitionType) {
853 // Wait until all transitions are complete.
854 if (!transition.isRunning()) {
855 transition.removeTransitionListener(this);
856 updateMaxScrollX();
857 }
858 }
859 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700860 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700861 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700862 }
863
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700864 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
865 updateCurrentPageScroll();
866 mFirstLayout = false;
867 }
868
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700869 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700870 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700871 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700872 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700873
874 if (isReordering(true)) {
875 updateDragViewTranslationDuringDrag();
876 }
Winson Chunge3193b92010-09-10 11:44:42 -0700877 }
878
Sunny Goyala1fbd842015-05-20 13:40:27 -0700879 protected int getChildGap() {
880 return 0;
881 }
882
Sunny Goyal316490e2015-06-02 09:38:28 -0700883 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700884 mMaxScrollX = computeMaxScrollX();
885 }
886
887 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700888 int childCount = getChildCount();
889 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700890 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700891 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700892 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700893 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700894 }
895 }
896
Adam Cohen3b185e22013-10-29 14:45:58 -0700897 public void setPageSpacing(int pageSpacing) {
898 mPageSpacing = pageSpacing;
899 requestLayout();
900 }
901
Sunny Goyal4d113a52015-05-27 10:05:28 -0700902 /**
903 * Called when the center screen changes during scrolling.
904 */
905 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700906
Winson Chunge3193b92010-09-10 11:44:42 -0700907 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700908 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700909 // Update the page indicator, we don't update the page indicator as we
910 // add/remove pages
911 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700912 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700913 }
914
Adam Cohen2591f6a2011-10-25 14:36:40 -0700915 // This ensures that when children are added, they get the correct transforms / alphas
916 // in accordance with any scroll effects.
917 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700918 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700919 invalidate();
920 }
921
Michael Jurka8b805b12012-04-18 14:23:14 -0700922 @Override
923 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700924 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700925 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700926 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700927 }
928
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700929 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700930 // Update the page indicator, we don't update the page indicator as we
931 // add/remove pages
932 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700933 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700934 }
935 }
936
937 @Override
938 public void removeView(View v) {
939 // XXX: We should find a better way to hook into this before the view
940 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700941 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700942 super.removeView(v);
943 }
944 @Override
945 public void removeViewInLayout(View v) {
946 // XXX: We should find a better way to hook into this before the view
947 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700948 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700949 super.removeViewInLayout(v);
950 }
951 @Override
952 public void removeViewAt(int index) {
953 // XXX: We should find a better way to hook into this before the view
954 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700955 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700956 super.removeViewAt(index);
957 }
958 @Override
959 public void removeAllViewsInLayout() {
960 // Update the page indicator, we don't update the page indicator as we
961 // add/remove pages
962 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700963 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700964 }
965
966 super.removeAllViewsInLayout();
967 }
968
Adam Cohen73894962011-10-31 13:17:17 -0700969 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700970 if (index < 0 || index > getChildCount() - 1) return 0;
971
Adam Cohenf698c6e2013-07-17 18:44:25 -0700972 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -0700973
Adam Cohenf698c6e2013-07-17 18:44:25 -0700974 return offset;
Adam Cohen73894962011-10-31 13:17:17 -0700975 }
976
Adam Cohen6f127a62014-06-12 14:54:41 -0700977 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -0700978 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700979 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 }
981
Michael Jurkadde558b2011-11-09 22:09:06 -0800982 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -0800983 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -0700984 range[0] = -1;
985 range[1] = -1;
986
Sunny Goyalee688162016-02-12 14:24:21 -0800987 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800988 final int visibleLeft = -getLeft();
989 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -0800990 final Matrix pageShiftMatrix = getPageShiftMatrix();
991 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -0700992
Sunny Goyalee688162016-02-12 14:24:21 -0800993 for (int i = 0; i < count; i++) {
994 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -0700995
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800996 // Verify if the page bounds are within the visible range.
997 sTmpRectF.left = 0;
998 sTmpRectF.right = currPage.getMeasuredWidth();
999 currPage.getMatrix().mapRect(sTmpRectF);
1000 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001001 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001002
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001003 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001004 if (range[0] == -1) {
1005 continue;
1006 } else {
1007 break;
1008 }
1009 }
Sunny Goyalee688162016-02-12 14:24:21 -08001010 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001011 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001012 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001013 }
1014 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001015
Sunny Goyalee688162016-02-12 14:24:21 -08001016 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001017 } else {
1018 range[0] = -1;
1019 range[1] = -1;
1020 }
1021 }
1022
Sunny Goyalee688162016-02-12 14:24:21 -08001023 protected Matrix getPageShiftMatrix() {
1024 return getMatrix();
1025 }
1026
Michael Jurka920d7f42012-05-14 16:29:55 -07001027 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001028 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001029 }
1030
Michael Jurkadde558b2011-11-09 22:09:06 -08001031 @Override
1032 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001033 // Find out which screens are visible; as an optimization we only call draw on them
1034 final int pageCount = getChildCount();
1035 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001036 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001037 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001038
1039 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1040 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1041 // set it for the next frame
1042 mForceScreenScrolled = false;
1043 screenScrolled(screenCenter);
1044 mLastScreenCenter = screenCenter;
1045 }
1046
Michael Jurkadde558b2011-11-09 22:09:06 -08001047 getVisiblePages(mTempVisiblePagesRange);
1048 final int leftScreen = mTempVisiblePagesRange[0];
1049 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001050 if (leftScreen != -1 && rightScreen != -1) {
1051 final long drawingTime = getDrawingTime();
1052 // Clip to the bounds
1053 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001054 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1055 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001056
Adam Cohen7d30a372013-07-01 17:03:59 -07001057 // Draw all the children, leaving the drag view for last
1058 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001059 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001060 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001061 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001062 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001063 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001064 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001065 // Draw the drag view on top (if there is one)
1066 if (mDragView != null) {
1067 drawChild(canvas, mDragView, drawingTime);
1068 }
1069
Winson Chungc6f10b92011-11-14 11:39:07 -08001070 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001071 }
Michael Jurka0142d492010-08-25 17:46:15 -07001072 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074
1075 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001076 public void draw(Canvas canvas) {
1077 super.draw(canvas);
1078 if (getPageCount() > 0) {
1079 if (!mEdgeGlowLeft.isFinished()) {
1080 final int restoreCount = canvas.save();
1081 Rect display = mViewport;
1082 canvas.translate(display.left, display.top);
1083 canvas.rotate(270);
1084
1085 getEdgeVerticalPostion(sTmpIntPoint);
1086 canvas.translate(display.top - sTmpIntPoint[1], 0);
1087 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1088 if (mEdgeGlowLeft.draw(canvas)) {
1089 postInvalidateOnAnimation();
1090 }
1091 canvas.restoreToCount(restoreCount);
1092 }
1093 if (!mEdgeGlowRight.isFinished()) {
1094 final int restoreCount = canvas.save();
1095 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001096 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001097 canvas.rotate(90);
1098
1099 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001100
Sunny Goyal93264612015-11-23 11:47:50 -08001101 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1102 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001103 if (mEdgeGlowRight.draw(canvas)) {
1104 postInvalidateOnAnimation();
1105 }
1106 canvas.restoreToCount(restoreCount);
1107 }
1108 }
1109 }
1110
1111 /**
1112 * Returns the top and bottom position for the edge effect.
1113 */
1114 protected abstract void getEdgeVerticalPostion(int[] pos);
1115
1116 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001118 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001119 if (page != mCurrentPage || !mScroller.isFinished()) {
1120 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 return true;
1122 }
1123 return false;
1124 }
1125
1126 @Override
1127 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001128 int focusablePage;
1129 if (mNextPage != INVALID_PAGE) {
1130 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001132 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001133 }
Winson Chung86f77532010-08-24 11:08:22 -07001134 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001136 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 }
1138 return false;
1139 }
1140
1141 @Override
1142 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001143 if (super.dispatchUnhandledMove(focused, direction)) {
1144 return true;
1145 }
1146
1147 if (mIsRtl) {
1148 if (direction == View.FOCUS_LEFT) {
1149 direction = View.FOCUS_RIGHT;
1150 } else if (direction == View.FOCUS_RIGHT) {
1151 direction = View.FOCUS_LEFT;
1152 }
1153 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001154 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001155 if (getCurrentPage() > 0) {
1156 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 return true;
1158 }
1159 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001160 if (getCurrentPage() < getPageCount() - 1) {
1161 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 return true;
1163 }
1164 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001165 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001166 }
1167
1168 @Override
1169 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001170 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001171 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001172 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 }
1174 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001175 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001176 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 }
1178 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001179 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001180 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 }
1182 }
1183 }
1184
1185 /**
1186 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001187 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 *
Winson Chung86f77532010-08-24 11:08:22 -07001189 * This happens when live folders requery, and if they're off page, they
1190 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 */
1192 @Override
1193 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001194 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 View v = focused;
1196 while (true) {
1197 if (v == current) {
1198 super.focusableViewAvailable(focused);
1199 return;
1200 }
1201 if (v == this) {
1202 return;
1203 }
1204 ViewParent parent = v.getParent();
1205 if (parent instanceof View) {
1206 v = (View)v.getParent();
1207 } else {
1208 return;
1209 }
1210 }
1211 }
1212
1213 /**
1214 * {@inheritDoc}
1215 */
1216 @Override
1217 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1218 if (disallowIntercept) {
1219 // We need to make sure to cancel our long press if
1220 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001221 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001222 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 }
1224 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1225 }
1226
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001227 /**
1228 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1229 */
1230 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001231 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001232 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001233 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001234 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001235 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001236 }
1237
1238 /**
1239 * Return true if a tap at (x, y) should trigger a flip to the next page.
1240 */
1241 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001242 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001243 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001244 }
1245 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001246 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001247 }
Winson Chung52aee602013-01-30 12:01:02 -08001248
Adam Cohen7d30a372013-07-01 17:03:59 -07001249 /** Returns whether x and y originated within the buffered viewport */
1250 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001251 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001252 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001253 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001254 }
1255
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 @Override
1257 public boolean onInterceptTouchEvent(MotionEvent ev) {
1258 /*
1259 * This method JUST determines whether we want to intercept the motion.
1260 * If we return true, onTouchEvent will be called and we do the actual
1261 * scrolling there.
1262 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001263 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001264
Winson Chung45e1d6e2010-11-09 17:19:49 -08001265 // Skip touch handling if there are no pages to swipe
1266 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1267
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 /*
1269 * Shortcut the most recurring case: the user is in the dragging
1270 * state and he is moving his finger. We want to intercept this
1271 * motion.
1272 */
1273 final int action = ev.getAction();
1274 if ((action == MotionEvent.ACTION_MOVE) &&
1275 (mTouchState == TOUCH_STATE_SCROLLING)) {
1276 return true;
1277 }
1278
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 switch (action & MotionEvent.ACTION_MASK) {
1280 case MotionEvent.ACTION_MOVE: {
1281 /*
1282 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1283 * whether the user has moved far enough from his original down touch.
1284 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001285 if (mActivePointerId != INVALID_POINTER) {
1286 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001287 }
1288 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1289 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1290 // i.e. fall through to the next case (don't break)
1291 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1292 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001293 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 }
1295
1296 case MotionEvent.ACTION_DOWN: {
1297 final float x = ev.getX();
1298 final float y = ev.getY();
1299 // Remember location of down touch
1300 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001301 mDownMotionY = y;
1302 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 mLastMotionX = x;
1304 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001305 float[] p = mapPointFromViewToParent(this, x, y);
1306 mParentDownMotionX = p[0];
1307 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001308 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001309 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001311
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 /*
1313 * If being flinged and user touches the screen, initiate drag;
1314 * otherwise don't. mScroller.isFinished should be false when
1315 * being flinged.
1316 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001317 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001318 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001319
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001320 if (finishedScrolling) {
1321 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001322 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001323 setCurrentPage(getNextPage());
1324 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001325 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001326 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001327 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1328 mTouchState = TOUCH_STATE_SCROLLING;
1329 } else {
1330 mTouchState = TOUCH_STATE_REST;
1331 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001332 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001333
Winson Chung321e9ee2010-08-09 13:37:56 -07001334 break;
1335 }
1336
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001338 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001339 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 break;
1341
1342 case MotionEvent.ACTION_POINTER_UP:
1343 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001344 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 break;
1346 }
1347
1348 /*
1349 * The only time we want to intercept motion events is if we are in the
1350 * drag mode.
1351 */
1352 return mTouchState != TOUCH_STATE_REST;
1353 }
1354
Adam Cohenf8d28232011-02-01 21:47:00 -08001355 protected void determineScrollingStart(MotionEvent ev) {
1356 determineScrollingStart(ev, 1.0f);
1357 }
1358
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 /*
1360 * Determines if we should change the touch state to start scrolling after the
1361 * user moves their touch point too far.
1362 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001363 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001364 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001366 if (pointerIndex == -1) return;
1367
1368 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 final float x = ev.getX(pointerIndex);
1370 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001371 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1372
Winson Chung321e9ee2010-08-09 13:37:56 -07001373 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001374
Adam Cohenf8d28232011-02-01 21:47:00 -08001375 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001377
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001378 if (xMoved) {
1379 // Scroll if the user moved far enough along the X axis
1380 mTouchState = TOUCH_STATE_SCROLLING;
1381 mTotalMotionX += Math.abs(mLastMotionX - x);
1382 mLastMotionX = x;
1383 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001384 onScrollInteractionBegin();
1385 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001386 // Stop listening for things like pinches.
1387 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001388 }
1389 }
1390
1391 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001392 // Try canceling the long press. It could also have been scheduled
1393 // by a distant descendant, so use the mAllowLongPress flag to block
1394 // everything
1395 final View currentPage = getPageAt(mCurrentPage);
1396 if (currentPage != null) {
1397 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 }
1399 }
1400
Adam Cohenb5ba0972011-09-07 18:02:31 -07001401 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001402 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001403
Adam Cohenedb40762013-07-18 16:45:45 -07001404 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001405 int count = getChildCount();
1406
1407 final int totalDistance;
1408
1409 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001410 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001411 adjacentPage = page - 1;
1412 }
1413
1414 if (adjacentPage < 0 || adjacentPage > count - 1) {
1415 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1416 } else {
1417 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1418 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001419
1420 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001421 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1422 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001423 return scrollProgress;
1424 }
1425
Adam Cohenedb40762013-07-18 16:45:45 -07001426 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001427 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001428 return 0;
1429 } else {
1430 return mPageScrolls[index];
1431 }
1432 }
1433
Adam Cohen564a2e72013-10-09 14:47:32 -07001434 // While layout transitions are occurring, a child's position may stray from its baseline
1435 // position. This method returns the magnitude of this stray at any given time.
1436 public int getLayoutTransitionOffsetForPage(int index) {
1437 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1438 return 0;
1439 } else {
1440 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001441
1442 int scrollOffset = 0;
1443 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1444 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001445 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001446 }
1447
Adam Cohen564a2e72013-10-09 14:47:32 -07001448 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1449 return (int) (child.getX() - baselineX);
1450 }
1451 }
1452
Adam Cohenb5ba0972011-09-07 18:02:31 -07001453 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001454 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001455 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001456 if (f < 0) {
1457 mEdgeGlowLeft.onPull(-f);
1458 } else if (f > 0) {
1459 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001460 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001461 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001462 }
1463 invalidate();
1464 }
1465
Adam Cohenb5ba0972011-09-07 18:02:31 -07001466 protected void overScroll(float amount) {
1467 dampedOverScroll(amount);
1468 }
1469
Winson Chungdc61c4d2015-04-20 18:26:57 -07001470 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001471 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001472 }
1473
Winson Chungdc61c4d2015-04-20 18:26:57 -07001474 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001475 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001476 }
1477
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001478 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001479 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001480 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001481 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1482 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1483 } else {
1484 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1485 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1486 }
1487 }
1488
Adam Cohenf9618852013-11-08 06:45:03 -08001489 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001490 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001491 mFreeScroll = freeScroll;
1492
Adam Cohenf9618852013-11-08 06:45:03 -08001493 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001494 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001495 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001496 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1497 setCurrentPage(mTempVisiblePagesRange[0]);
1498 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1499 setCurrentPage(mTempVisiblePagesRange[1]);
1500 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001501 } else if (wasFreeScroll) {
1502 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001503 }
1504
1505 setEnableOverscroll(!freeScroll);
1506 }
1507
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001508 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001509 mAllowOverScroll = enable;
1510 }
1511
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001512 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001513 if (mDragView != null) {
1514 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1515 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001516 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001517 int minDistance = Integer.MAX_VALUE;
1518 int minIndex = indexOfChild(mDragView);
1519 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1520 View page = getPageAt(i);
1521 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1522 int d = Math.abs(dragX - pageX);
1523 if (d < minDistance) {
1524 minIndex = i;
1525 minDistance = d;
1526 }
1527 }
1528 return minIndex;
1529 }
1530 return -1;
1531 }
1532
Winson Chung321e9ee2010-08-09 13:37:56 -07001533 @Override
1534 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001535 super.onTouchEvent(ev);
1536
Winson Chung45e1d6e2010-11-09 17:19:49 -08001537 // Skip touch handling if there are no pages to swipe
1538 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1539
Michael Jurkab8f06722010-10-10 15:58:46 -07001540 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001541
1542 final int action = ev.getAction();
1543
1544 switch (action & MotionEvent.ACTION_MASK) {
1545 case MotionEvent.ACTION_DOWN:
1546 /*
1547 * If being flinged and user touches, stop the fling. isFinished
1548 * will be false if being flinged.
1549 */
1550 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001551 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 }
1553
1554 // Remember where the motion event started
1555 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001556 mDownMotionY = mLastMotionY = ev.getY();
1557 mDownScrollX = getScrollX();
1558 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1559 mParentDownMotionX = p[0];
1560 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001561 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001562 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001563 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001564
Michael Jurka0142d492010-08-25 17:46:15 -07001565 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001566 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001567 pageBeginMoving();
1568 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 break;
1570
1571 case MotionEvent.ACTION_MOVE:
1572 if (mTouchState == TOUCH_STATE_SCROLLING) {
1573 // Scroll to follow the motion event
1574 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001575
1576 if (pointerIndex == -1) return true;
1577
Winson Chung321e9ee2010-08-09 13:37:56 -07001578 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001579 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001580
Adam Cohenaefd4e12011-02-14 16:39:38 -08001581 mTotalMotionX += Math.abs(deltaX);
1582
Winson Chungc0844aa2011-02-02 15:25:58 -08001583 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1584 // keep the remainder because we are actually testing if we've moved from the last
1585 // scrolled position (which is discrete).
1586 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001587 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001588 mLastMotionX = x;
1589 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 } else {
1591 awakenScrollBars();
1592 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001593 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1594 // Update the last motion position
1595 mLastMotionX = ev.getX();
1596 mLastMotionY = ev.getY();
1597
1598 // Update the parent down so that our zoom animations take this new movement into
1599 // account
1600 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1601 mParentDownMotionX = pt[0];
1602 mParentDownMotionY = pt[1];
1603 updateDragViewTranslationDuringDrag();
1604
1605 // Find the closest page to the touch point
1606 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001607
Adam Cohen7d30a372013-07-01 17:03:59 -07001608 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1609 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1610 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1611 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1612
Adam Cohenf358a4b2013-07-23 16:47:31 -07001613 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001614 // Do not allow any page to be moved to 0th position.
1615 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001616 mTempVisiblePagesRange[0] = 0;
1617 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001618 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001619 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1620 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1621 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1622 mSidePageHoverIndex = pageUnderPointIndex;
1623 mSidePageHoverRunnable = new Runnable() {
1624 @Override
1625 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001626 // Setup the scroll to the correct page before we swap the views
1627 snapToPage(pageUnderPointIndex);
1628
1629 // For each of the pages between the paged view and the drag view,
1630 // animate them from the previous position to the new position in
1631 // the layout (as a result of the drag view moving in the layout)
1632 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1633 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1634 dragViewIndex + 1 : pageUnderPointIndex;
1635 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1636 dragViewIndex - 1 : pageUnderPointIndex;
1637 for (int i = lowerIndex; i <= upperIndex; ++i) {
1638 View v = getChildAt(i);
1639 // dragViewIndex < pageUnderPointIndex, so after we remove the
1640 // drag view all subsequent views to pageUnderPointIndex will
1641 // shift down.
1642 int oldX = getViewportOffsetX() + getChildOffset(i);
1643 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1644
1645 // Animate the view translation from its old position to its new
1646 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001647 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001648 if (anim != null) {
1649 anim.cancel();
1650 }
1651
1652 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001653 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001654 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001655 anim.start();
1656 v.setTag(anim);
1657 }
1658
1659 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001660 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001661 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001662 if (mPageIndicator != null) {
1663 mPageIndicator.setActiveMarker(getNextPage());
1664 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001665 }
1666 };
1667 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1668 }
1669 } else {
1670 removeCallbacks(mSidePageHoverRunnable);
1671 mSidePageHoverIndex = -1;
1672 }
Adam Cohen564976a2010-10-13 18:52:07 -07001673 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001674 determineScrollingStart(ev);
1675 }
1676 break;
1677
1678 case MotionEvent.ACTION_UP:
1679 if (mTouchState == TOUCH_STATE_SCROLLING) {
1680 final int activePointerId = mActivePointerId;
1681 final int pointerIndex = ev.findPointerIndex(activePointerId);
1682 final float x = ev.getX(pointerIndex);
1683 final VelocityTracker velocityTracker = mVelocityTracker;
1684 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1685 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001686 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001687 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001688 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1689 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001690
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001691 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1692
Adam Cohen00481b32011-11-18 12:03:48 -08001693 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001694 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001695
Adam Cohenf358a4b2013-07-23 16:47:31 -07001696 if (!mFreeScroll) {
1697 // In the case that the page is moved far to one direction and then is flung
1698 // in the opposite direction, we use a threshold to determine whether we should
1699 // just return to the starting page, or if we should skip one further.
1700 boolean returnToOriginalPage = false;
1701 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1702 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1703 returnToOriginalPage = true;
1704 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001705
Adam Cohenf358a4b2013-07-23 16:47:31 -07001706 int finalPage;
1707 // We give flings precedence over large moves, which is why we short-circuit our
1708 // test for a large move if a fling has been registered. That is, a large
1709 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001710 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1711 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001712 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1713 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1714 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1715 snapToPageWithVelocity(finalPage, velocityX);
1716 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1717 (isFling && isVelocityXLeft)) &&
1718 mCurrentPage < getChildCount() - 1) {
1719 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1720 snapToPageWithVelocity(finalPage, velocityX);
1721 } else {
1722 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001723 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001724 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001725 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001726 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001727 }
1728
1729 float scaleX = getScaleX();
1730 int vX = (int) (-velocityX * scaleX);
1731 int initialScrollX = (int) (getScrollX() * scaleX);
1732
Adam Cohenf9618852013-11-08 06:45:03 -08001733 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001734 mScroller.fling(initialScrollX,
1735 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001736 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001737 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001738 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001739 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001740 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1741 // at this point we have not moved beyond the touch slop
1742 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1743 // we can just page
1744 int nextPage = Math.max(0, mCurrentPage - 1);
1745 if (nextPage != mCurrentPage) {
1746 snapToPage(nextPage);
1747 } else {
1748 snapToDestination();
1749 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001750 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001751 // at this point we have not moved beyond the touch slop
1752 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1753 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001754 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1755 if (nextPage != mCurrentPage) {
1756 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001757 } else {
1758 snapToDestination();
1759 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001760 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1761 // Update the last motion position
1762 mLastMotionX = ev.getX();
1763 mLastMotionY = ev.getY();
1764
1765 // Update the parent down so that our zoom animations take this new movement into
1766 // account
1767 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1768 mParentDownMotionX = pt[0];
1769 mParentDownMotionY = pt[1];
1770 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001771 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001772 if (!mCancelTap) {
1773 onUnhandledTap(ev);
1774 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001775 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001776
1777 // Remove the callback to wait for the side page hover timeout
1778 removeCallbacks(mSidePageHoverRunnable);
1779 // End any intermediate reordering states
1780 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 break;
1782
1783 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001784 if (mTouchState == TOUCH_STATE_SCROLLING) {
1785 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001786 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001787 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001788 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001789 break;
1790
1791 case MotionEvent.ACTION_POINTER_UP:
1792 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001793 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001794 break;
1795 }
1796
1797 return true;
1798 }
1799
Adam Cohen7d30a372013-07-01 17:03:59 -07001800 private void resetTouchState() {
1801 releaseVelocityTracker();
1802 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001803 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001804 mTouchState = TOUCH_STATE_REST;
1805 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001806 mEdgeGlowLeft.onRelease();
1807 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001808 }
1809
Adam Cohenc2d6e892014-10-16 09:49:24 -07001810 /**
1811 * Triggered by scrolling via touch
1812 */
1813 protected void onScrollInteractionBegin() {
1814 }
1815
1816 protected void onScrollInteractionEnd() {
1817 }
1818
Adam Cohen1697b792013-09-17 19:08:21 -07001819 protected void onUnhandledTap(MotionEvent ev) {
1820 ((Launcher) getContext()).onClick(this);
1821 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001822
Winson Chung185d7162011-02-28 13:47:29 -08001823 @Override
1824 public boolean onGenericMotionEvent(MotionEvent event) {
1825 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1826 switch (event.getAction()) {
1827 case MotionEvent.ACTION_SCROLL: {
1828 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1829 final float vscroll;
1830 final float hscroll;
1831 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1832 vscroll = 0;
1833 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1834 } else {
1835 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1836 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1837 }
1838 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001839 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001840 : (hscroll > 0 || vscroll > 0);
1841 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001842 scrollRight();
1843 } else {
1844 scrollLeft();
1845 }
1846 return true;
1847 }
1848 }
1849 }
1850 }
1851 return super.onGenericMotionEvent(event);
1852 }
1853
Michael Jurkab8f06722010-10-10 15:58:46 -07001854 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1855 if (mVelocityTracker == null) {
1856 mVelocityTracker = VelocityTracker.obtain();
1857 }
1858 mVelocityTracker.addMovement(ev);
1859 }
1860
1861 private void releaseVelocityTracker() {
1862 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001863 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001864 mVelocityTracker.recycle();
1865 mVelocityTracker = null;
1866 }
1867 }
1868
Winson Chung321e9ee2010-08-09 13:37:56 -07001869 private void onSecondaryPointerUp(MotionEvent ev) {
1870 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1871 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1872 final int pointerId = ev.getPointerId(pointerIndex);
1873 if (pointerId == mActivePointerId) {
1874 // This was our active pointer going up. Choose a new
1875 // active pointer and adjust accordingly.
1876 // TODO: Make this decision more intelligent.
1877 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1878 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1879 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001880 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001881 mActivePointerId = ev.getPointerId(newPointerIndex);
1882 if (mVelocityTracker != null) {
1883 mVelocityTracker.clear();
1884 }
1885 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001886 }
1887
Winson Chung321e9ee2010-08-09 13:37:56 -07001888 @Override
1889 public void requestChildFocus(View child, View focused) {
1890 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001891 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001892 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001893 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001894 }
1895 }
1896
Adam Cohend19d3ca2010-09-15 14:43:42 -07001897 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001898 return getPageNearestToCenterOfScreen(getScrollX());
1899 }
1900
1901 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1902 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001903 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001904 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001905 final int childCount = getChildCount();
1906 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001907 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001908 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001909 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001910 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001911 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1912 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1913 minDistanceFromScreenCenter = distanceFromScreenCenter;
1914 minDistanceFromScreenCenterIndex = i;
1915 }
1916 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001917 return minDistanceFromScreenCenterIndex;
1918 }
1919
1920 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001921 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001922 }
1923
Hyunyoung Song74b5af32016-06-08 16:29:32 -07001924 public static class ScrollInterpolator implements Interpolator {
Adam Cohene0f66b52010-11-23 15:06:07 -08001925 public ScrollInterpolator() {
1926 }
1927
1928 public float getInterpolation(float t) {
1929 t -= 1.0f;
1930 return t*t*t*t*t + 1;
1931 }
1932 }
1933
1934 // We want the duration of the page snap animation to be influenced by the distance that
1935 // the screen has to travel, however, we don't want this duration to be effected in a
1936 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1937 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001938 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001939 f -= 0.5f; // center the values about 0.
1940 f *= 0.3f * Math.PI / 2.0f;
1941 return (float) Math.sin(f);
1942 }
1943
Michael Jurka0142d492010-08-25 17:46:15 -07001944 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001945 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001946 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001947
Adam Cohenedb40762013-07-18 16:45:45 -07001948 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001949 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001950 int duration = 0;
1951
Sunny Goyal4d113a52015-05-27 10:05:28 -07001952 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001953 // If the velocity is low enough, then treat this more as an automatic page advance
1954 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001955 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001956 return;
1957 }
1958
1959 // Here we compute a "distance" that will be used in the computation of the overall
1960 // snap duration. This is a function of the actual distance that needs to be traveled;
1961 // we keep this value close to half screen size in order to reduce the variance in snap
1962 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001963 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001964 float distance = halfScreenSize + halfScreenSize *
1965 distanceInfluenceForSnapDuration(distanceRatio);
1966
1967 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001968 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001969
1970 // we want the page's snap velocity to approximately match the velocity at which the
1971 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001972 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1973 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001974
1975 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001976 }
1977
Sunny Goyal1740d902015-05-27 11:14:01 -07001978 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001979 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001980 }
1981
Adam Cohenf9c184a2016-01-15 16:47:43 -08001982 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001983 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001984 }
1985
Michael Jurka0142d492010-08-25 17:46:15 -07001986 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001987 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001988 }
1989
Adam Cohenf9618852013-11-08 06:45:03 -08001990 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1991 snapToPage(whichPage, duration, false, interpolator);
1992 }
1993
1994 protected void snapToPage(int whichPage, int duration, boolean immediate,
1995 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001996 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001997
Adam Cohenedb40762013-07-18 16:45:45 -07001998 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001999 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002000 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002001 }
2002
2003 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002004 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002005 }
Michael Jurka0142d492010-08-25 17:46:15 -07002006
Adam Cohenf9618852013-11-08 06:45:03 -08002007 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2008 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002009 whichPage = validateNewPage(whichPage);
2010
Adam Cohen7d30a372013-07-01 17:03:59 -07002011 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002012
2013 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002014 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 if (immediate) {
2016 duration = 0;
2017 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002018 duration = Math.abs(delta);
2019 }
2020
Adam Cohenf358a4b2013-07-23 16:47:31 -07002021 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002022 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002023 }
Adam Cohenf9618852013-11-08 06:45:03 -08002024
2025 if (interpolator != null) {
2026 mScroller.setInterpolator(interpolator);
2027 } else {
2028 mScroller.setInterpolator(mDefaultInterpolator);
2029 }
2030
Sunny Goyalc86df472016-02-25 09:19:38 -08002031 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002032
Adam Cohen674531f2013-12-13 15:07:14 -08002033 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002034
2035 // Trigger a compute() to finish switching pages if necessary
2036 if (immediate) {
2037 computeScroll();
2038 }
2039
2040 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002041 invalidate();
2042 }
2043
Winson Chung321e9ee2010-08-09 13:37:56 -07002044 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002045 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002046 }
2047
2048 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002049 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002050 }
2051
Adam Cohendbdff6b2013-09-18 19:09:15 -07002052 @Override
2053 public boolean performLongClick() {
2054 mCancelTap = true;
2055 return super.performLongClick();
2056 }
2057
Winson Chung321e9ee2010-08-09 13:37:56 -07002058 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002059 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002060
2061 SavedState(Parcelable superState) {
2062 super(superState);
2063 }
2064
Adam Cohen091440a2015-03-18 14:16:05 -07002065 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002066 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002067 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002068 }
2069
2070 @Override
2071 public void writeToParcel(Parcel out, int flags) {
2072 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002073 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002074 }
2075
2076 public static final Parcelable.Creator<SavedState> CREATOR =
2077 new Parcelable.Creator<SavedState>() {
2078 public SavedState createFromParcel(Parcel in) {
2079 return new SavedState(in);
2080 }
2081
2082 public SavedState[] newArray(int size) {
2083 return new SavedState[size];
2084 }
2085 };
2086 }
2087
Adam Cohen7d30a372013-07-01 17:03:59 -07002088 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002089 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002090 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002091 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2092 .translationX(0)
2093 .translationY(0)
2094 .scaleX(1)
2095 .scaleY(1)
2096 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002097 anim.addListener(new AnimatorListenerAdapter() {
2098 @Override
2099 public void onAnimationEnd(Animator animation) {
2100 onPostReorderingAnimationCompleted();
2101 }
2102 });
2103 anim.start();
2104 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002105 }
2106
Sunny Goyal1d08f702015-05-04 15:50:25 -07002107 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002108 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2109 mTouchState = TOUCH_STATE_REORDERING;
2110 mIsReordering = true;
2111
Adam Cohen7d30a372013-07-01 17:03:59 -07002112 // We must invalidate to trigger a redraw to update the layers such that the drag view
2113 // is always drawn on top
2114 invalidate();
2115 }
2116
Adam Cohen091440a2015-03-18 14:16:05 -07002117 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002118 // Trigger the callback when reordering has settled
2119 --mPostReorderingPreZoomInRemainingAnimationCount;
2120 if (mPostReorderingPreZoomInRunnable != null &&
2121 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2122 mPostReorderingPreZoomInRunnable.run();
2123 mPostReorderingPreZoomInRunnable = null;
2124 }
2125 }
2126
Sunny Goyal1d08f702015-05-04 15:50:25 -07002127 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002128 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002129 }
2130
Adam Cohenf358a4b2013-07-23 16:47:31 -07002131 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002132 int dragViewIndex = indexOfChild(v);
2133
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002134 // Do not allow the first page to be moved around
2135 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002136
Adam Cohen7d30a372013-07-01 17:03:59 -07002137 mTempVisiblePagesRange[0] = 0;
2138 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002139 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002140 mReorderingStarted = true;
2141
2142 // Check if we are within the reordering range
2143 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002144 dragViewIndex <= mTempVisiblePagesRange[1]) {
2145 // Find the drag view under the pointer
2146 mDragView = getChildAt(dragViewIndex);
2147 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2148 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002149 snapToPage(getPageNearestToCenterOfScreen());
2150 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002151 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002152 return true;
2153 }
2154 return false;
2155 }
2156
2157 boolean isReordering(boolean testTouchState) {
2158 boolean state = mIsReordering;
2159 if (testTouchState) {
2160 state &= (mTouchState == TOUCH_STATE_REORDERING);
2161 }
2162 return state;
2163 }
2164 void endReordering() {
2165 // For simplicity, we call endReordering sometimes even if reordering was never started.
2166 // In that case, we don't want to do anything.
2167 if (!mReorderingStarted) return;
2168 mReorderingStarted = false;
2169
2170 // If we haven't flung-to-delete the current child, then we just animate the drag view
2171 // back into position
2172 final Runnable onCompleteRunnable = new Runnable() {
2173 @Override
2174 public void run() {
2175 onEndReordering();
2176 }
2177 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002178
2179 mPostReorderingPreZoomInRunnable = new Runnable() {
2180 public void run() {
2181 onCompleteRunnable.run();
2182 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002183 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002184 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002185
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002186 mPostReorderingPreZoomInRemainingAnimationCount =
2187 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2188 // Snap to the current page
2189 snapToPage(indexOfChild(mDragView), 0);
2190 // Animate the drag view back to the front position
2191 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002192 }
2193
Winson Chung6a0f57d2011-06-29 20:10:49 -07002194 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002195 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002196 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002197 @Override
2198 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2199 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002200 info.setScrollable(getPageCount() > 1);
2201 if (getCurrentPage() < getPageCount() - 1) {
2202 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2203 }
2204 if (getCurrentPage() > 0) {
2205 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2206 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002207 info.setClassName(getClass().getName());
2208
2209 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2210 // Besides disabling the accessibility long-click, this also prevents this view from getting
2211 // accessibility focus.
2212 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002213 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002214 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2215 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002216 }
2217
2218 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002219 public void sendAccessibilityEvent(int eventType) {
2220 // Don't let the view send real scroll events.
2221 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2222 super.sendAccessibilityEvent(eventType);
2223 }
2224 }
2225
2226 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002227 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2228 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002229 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002230 }
2231
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002232 @Override
2233 public boolean performAccessibilityAction(int action, Bundle arguments) {
2234 if (super.performAccessibilityAction(action, arguments)) {
2235 return true;
2236 }
2237 switch (action) {
2238 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2239 if (getCurrentPage() < getPageCount() - 1) {
2240 scrollRight();
2241 return true;
2242 }
2243 } break;
2244 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2245 if (getCurrentPage() > 0) {
2246 scrollLeft();
2247 return true;
2248 }
2249 } break;
2250 }
2251 return false;
2252 }
2253
Sunny Goyal53fe1f22016-07-08 08:47:07 -07002254 protected String getPageIndicatorDescription() {
2255 return getCurrentPageDescription();
2256 }
2257
Adam Cohen0ffac432013-07-10 11:19:26 -07002258 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002259 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002260 getNextPage() + 1, getChildCount());
2261 }
2262
Winson Chungd11265e2011-08-30 13:37:23 -07002263 @Override
2264 public boolean onHoverEvent(android.view.MotionEvent event) {
2265 return true;
2266 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002267}