blob: e1cb082208a07205d2e6a9a0b7a4c0ce1374ff9b [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;
Sunny Goyal4d113a52015-05-27 10:05:28 -070052import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Vadim Tryshevfedca432015-08-19 17:55:02 -070068 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Adam Cohenb64cb5a2011-02-15 13:53:42 -080071 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080072 // The page is moved more than halfway, automatically move to the next page on touch up.
73 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080074
Sunny Goyal8e2133b2015-05-06 13:39:07 -070075 private static final float MAX_SCROLL_PROGRESS = 1.0f;
76
Adam Cohen265b9a62011-12-07 14:37:18 -080077 // The following constants need to be scaled based on density. The scaled versions will be
78 // assigned to the corresponding member variables below.
79 private static final int FLING_THRESHOLD_VELOCITY = 500;
80 private static final int MIN_SNAP_VELOCITY = 1500;
81 private static final int MIN_FLING_VELOCITY = 250;
82
Adam Cohen21cd0022013-10-09 18:57:02 -070083 public static final int INVALID_RESTORE_PAGE = -1001;
84
Adam Cohenf358a4b2013-07-23 16:47:31 -070085 private boolean mFreeScroll = false;
86 private int mFreeScrollMinScrollX = -1;
87 private int mFreeScrollMaxScrollX = -1;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070094 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -070098 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070099 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800103 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800104 protected LauncherScroller mScroller;
105 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mParentDownMotionX;
110 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mDownMotionY;
113 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700114 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700115 private float mLastMotionX;
116 private float mLastMotionXRemainder;
117 private float mLastMotionY;
118 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700119 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700120
Adam Cohendbdff6b2013-09-18 19:09:15 -0700121 private boolean mCancelTap;
122
Adam Cohenedb40762013-07-18 16:45:45 -0700123 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected final static int TOUCH_STATE_REST = 0;
126 protected final static int TOUCH_STATE_SCROLLING = 1;
127 protected final static int TOUCH_STATE_PREV_PAGE = 2;
128 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 protected final static int TOUCH_STATE_REORDERING = 4;
130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700132 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800138 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800139 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700146 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700147
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148 protected boolean mIsPageMoving = false;
149
Sunny Goyal061380a2016-02-29 15:15:03 -0800150 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700151
Winson Chungd2be3812013-07-16 11:11:32 -0700152 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mPageIndicatorViewId;
154 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700155 // The viewport whether the pages are to be contained (the actual view may be larger than the
156 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800157 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700158 private Rect mViewport = new Rect();
159
160 // Reordering
161 // We use the min scale to determine how much to expand the actually PagedView measured
162 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700163 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700164 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700165 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
166
Adam Cohen7d30a372013-07-01 17:03:59 -0700167 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700168 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700169 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 // This variable's scope is only for the duration of startReordering() and endReordering()
173 private boolean mReorderingStarted = false;
174 // This variable's scope is for the duration of startReordering() and after the zoomIn()
175 // animation after endReordering()
176 private boolean mIsReordering;
177 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700178 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700179 private int mPostReorderingPreZoomInRemainingAnimationCount;
180 private Runnable mPostReorderingPreZoomInRunnable;
181
Adam Cohen7d30a372013-07-01 17:03:59 -0700182 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700183 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700184 private static final float[] sTmpPoint = new float[2];
185 private static final int[] sTmpIntPoint = new int[2];
186 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800187 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700188
John Spurlock77e1f472013-09-11 10:09:51 -0400189 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700190 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400191
Sunny Goyal4d113a52015-05-27 10:05:28 -0700192 // Edge effect
193 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
194 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
195
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 public PagedView(Context context) {
197 this(context, null);
198 }
199
200 public PagedView(Context context, AttributeSet attrs) {
201 this(context, attrs, 0);
202 }
203
204 public PagedView(Context context, AttributeSet attrs, int defStyle) {
205 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700206
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 TypedArray a = context.obtainStyledAttributes(attrs,
208 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700209 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 a.recycle();
211
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700213 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700214 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 }
216
217 /**
218 * Initializes various states for this workspace.
219 */
Michael Jurka0142d492010-08-25 17:46:15 -0700220 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800221 mScroller = new LauncherScroller(getContext());
222 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700223 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700224
225 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700226 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800228
Sunny Goyalcf25b522015-07-09 00:01:18 -0700229 float density = getResources().getDisplayMetrics().density;
230 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
231 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
232 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700233 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700234 setWillNotDraw(false);
235 }
236
237 protected void setEdgeGlowColor(int color) {
238 mEdgeGlowLeft.setColor(color);
239 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 }
241
Adam Cohenf9618852013-11-08 06:45:03 -0800242 protected void setDefaultInterpolator(Interpolator interpolator) {
243 mDefaultInterpolator = interpolator;
244 mScroller.setInterpolator(mDefaultInterpolator);
245 }
246
Winson Chungd2be3812013-07-16 11:11:32 -0700247 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700248 super.onAttachedToWindow();
249
Winson Chungd2be3812013-07-16 11:11:32 -0700250 // Hook up the page indicator
251 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700252 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700253 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700254 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700255 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700256
Winson Chung7819a562013-09-19 15:55:45 -0700257 ArrayList<PageIndicator.PageMarkerResources> markers =
258 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700259 for (int i = 0; i < getChildCount(); ++i) {
260 markers.add(getPageIndicatorMarker(i));
261 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700263 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700264
265 OnClickListener listener = getPageIndicatorClickListener();
266 if (listener != null) {
267 mPageIndicator.setOnClickListener(listener);
268 }
Adam Cohen53805212013-10-01 10:39:23 -0700269 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700270 }
271 }
272
Adam Cohen53805212013-10-01 10:39:23 -0700273 protected String getPageIndicatorDescription() {
274 return getCurrentPageDescription();
275 }
276
277 protected OnClickListener getPageIndicatorClickListener() {
278 return null;
279 }
280
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700281 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700282 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700283 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700284 // Unhook the page indicator
285 mPageIndicator = null;
286 }
287
Adam Cohen7d30a372013-07-01 17:03:59 -0700288 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700289 private float[] mapPointFromViewToParent(View v, float x, float y) {
290 sTmpPoint[0] = x;
291 sTmpPoint[1] = y;
292 v.getMatrix().mapPoints(sTmpPoint);
293 sTmpPoint[0] += v.getLeft();
294 sTmpPoint[1] += v.getTop();
295 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700296 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700297 private float[] mapPointFromParentToView(View v, float x, float y) {
298 sTmpPoint[0] = x - v.getLeft();
299 sTmpPoint[1] = y - v.getTop();
300 v.getMatrix().invert(sTmpInvMatrix);
301 sTmpInvMatrix.mapPoints(sTmpPoint);
302 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700303 }
304
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700305 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700306 if (mDragView != null) {
307 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
308 (mDragViewBaselineLeft - mDragView.getLeft());
309 float y = mLastMotionY - mDownMotionY;
310 mDragView.setTranslationX(x);
311 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700312
Adam Cohenf358a4b2013-07-23 16:47:31 -0700313 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
314 + x + ", " + y);
315 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700316 }
317
318 public void setMinScale(float f) {
319 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700320 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700321 requestLayout();
322 }
323
324 @Override
325 public void setScaleX(float scaleX) {
326 super.setScaleX(scaleX);
327 if (isReordering(true)) {
328 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
329 mLastMotionX = p[0];
330 mLastMotionY = p[1];
331 updateDragViewTranslationDuringDrag();
332 }
333 }
334
335 // Convenience methods to get the actual width/height of the PagedView (since it is measured
336 // to be larger to account for the minimum possible scale)
337 int getViewportWidth() {
338 return mViewport.width();
339 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800340 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700341 return mViewport.height();
342 }
343
344 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
345 // PagedView both horizontally and vertically
346 int getViewportOffsetX() {
347 return (getMeasuredWidth() - getViewportWidth()) / 2;
348 }
349
350 int getViewportOffsetY() {
351 return (getMeasuredHeight() - getViewportHeight()) / 2;
352 }
353
Adam Cohenedb40762013-07-18 16:45:45 -0700354 PageIndicator getPageIndicator() {
355 return mPageIndicator;
356 }
Winson Chung7819a562013-09-19 15:55:45 -0700357 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
358 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700359 }
Adam Cohenedb40762013-07-18 16:45:45 -0700360
Adam Cohen674531f2013-12-13 15:07:14 -0800361 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700362 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
363 * that the user was on before entering free scroll mode (e.g. the home screen page they
364 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
365 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700367 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700368 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700370
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700371 /**
372 * Returns the index of page to be shown immediately afterwards.
373 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700374 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700375 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
376 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700377
Adam Cohenf9c184a2016-01-15 16:47:43 -0800378 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 return getChildCount();
380 }
381
Sunny Goyal83a8f042015-05-19 12:52:12 -0700382 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700383 return getChildAt(index);
384 }
385
Adam Cohenae4f1552011-10-20 00:15:42 -0700386 protected int indexToPage(int index) {
387 return index;
388 }
389
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800391 * Updates the scroll of the current page immediately to its final scroll position. We use this
392 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
393 * the previous tab page.
394 */
395 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700396 // If the current page is invalid, just reset the scroll position to zero
397 int newX = 0;
398 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700399 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700400 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800401 scrollTo(newX, 0);
402 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700403 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800404 }
405
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700406 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700407 mScroller.abortAnimation();
408 // We need to clean up the next page here to avoid computeScrollHelper from
409 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700410 if (resetNextPage) {
411 mNextPage = INVALID_PAGE;
412 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700413 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700414
Tony Wickham8f7ead32016-04-07 18:46:44 -0700415 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700416 mScroller.forceFinished(true);
417 // We need to clean up the next page here to avoid computeScrollHelper from
418 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700419 if (resetNextPage) {
420 mNextPage = INVALID_PAGE;
421 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700422 }
423
Adam Cohen6f127a62014-06-12 14:54:41 -0700424 private int validateNewPage(int newPage) {
425 int validatedPage = newPage;
426 // When in free scroll mode, we need to clamp to the free scroll page range.
427 if (mFreeScroll) {
428 getFreeScrollPageRange(mTempVisiblePagesRange);
429 validatedPage = Math.max(mTempVisiblePagesRange[0],
430 Math.min(newPage, mTempVisiblePagesRange[1]));
431 }
432 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickham86930612015-09-09 13:50:40 -0700433 validatedPage = Utilities.boundInRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700434 return validatedPage;
435 }
436
Chet Haasebc2f0822012-10-26 17:59:53 -0700437 /**
Winson Chung86f77532010-08-24 11:08:22 -0700438 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700440 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800441 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700442 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800443 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800444 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
445 // the default
446 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800447 return;
448 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700449 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700450 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700451 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700452 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800453 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
455
Winson Chung8c87cd82013-07-23 16:20:10 -0700456 /**
457 * The restore page will be set in place of the current page at the next (likely first)
458 * layout.
459 */
460 void setRestorePage(int restorePage) {
461 mRestorePage = restorePage;
462 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800463 int getRestorePage() {
464 return mRestorePage;
465 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700466
Adam Cohen674531f2013-12-13 15:07:14 -0800467 /**
468 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
469 * has settled.
470 */
Michael Jurka0142d492010-08-25 17:46:15 -0700471 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800472 updatePageIndicator();
473 }
474
475 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700476 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700477 if (mPageIndicator != null) {
478 mPageIndicator.setContentDescription(getPageIndicatorDescription());
479 if (!isReordering(false)) {
480 mPageIndicator.setActiveMarker(getNextPage());
481 }
Winson Chungd2be3812013-07-16 11:11:32 -0700482 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800484 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700485 if (!mIsPageMoving) {
486 mIsPageMoving = true;
487 onPageBeginMoving();
488 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700489 }
490
Michael Jurkace7e05f2011-02-01 22:02:35 -0800491 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700492 if (mIsPageMoving) {
493 mIsPageMoving = false;
494 onPageEndMoving();
495 }
Michael Jurka0142d492010-08-25 17:46:15 -0700496 }
497
Adam Cohen26976d92011-03-22 15:33:33 -0700498 protected boolean isPageMoving() {
499 return mIsPageMoving;
500 }
501
Michael Jurka0142d492010-08-25 17:46:15 -0700502 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700503 protected void onPageBeginMoving() {
504 }
505
506 // a method that subclasses can override to add behavior
507 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700508 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700509 }
510
Winson Chung321e9ee2010-08-09 13:37:56 -0700511 /**
Winson Chung86f77532010-08-24 11:08:22 -0700512 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 *
514 * @param l The listener used to respond to long clicks.
515 */
516 @Override
517 public void setOnLongClickListener(OnLongClickListener l) {
518 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700519 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700520 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700521 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700522 }
Adam Cohen1697b792013-09-17 19:08:21 -0700523 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525
Sunny Goyalc86df472016-02-25 09:19:38 -0800526 protected int getUnboundedScrollX() {
527 return getScrollX();
528 }
529
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800531 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800532 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800533 }
534
535 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700536 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700537 // In free scroll mode, we clamp the scrollX
538 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700539 // If the scroller is trying to move to a location beyond the maximum allowed
540 // in the free scroll mode, we make sure to end the scroll operation.
541 if (!mScroller.isFinished() &&
542 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700543 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700544 }
545
Adam Cohenf358a4b2013-07-23 16:47:31 -0700546 x = Math.min(x, mFreeScrollMaxScrollX);
547 x = Math.max(x, mFreeScrollMinScrollX);
548 }
549
Sunny Goyal70660032015-05-14 00:07:08 -0700550 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
551 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700552 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700553 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800554 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700555 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700556 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700557 overScroll(x - mMaxScrollX);
558 } else {
559 overScroll(x);
560 }
Adam Cohen68d73932010-11-15 10:50:58 -0800561 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700562 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700563 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800564 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700565 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700566 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700567 overScroll(x);
568 } else {
569 overScroll(x - mMaxScrollX);
570 }
Adam Cohen68d73932010-11-15 10:50:58 -0800571 }
572 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700573 if (mWasInOverscroll) {
574 overScroll(0);
575 mWasInOverscroll = false;
576 }
Adam Cohen68d73932010-11-15 10:50:58 -0800577 super.scrollTo(x, y);
578 }
579
Adam Cohen7d30a372013-07-01 17:03:59 -0700580 // Update the last motion events when scrolling
581 if (isReordering(true)) {
582 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
583 mLastMotionX = p[0];
584 mLastMotionY = p[1];
585 updateDragViewTranslationDuringDrag();
586 }
Michael Jurka0142d492010-08-25 17:46:15 -0700587 }
588
Adam Cohen53805212013-10-01 10:39:23 -0700589 private void sendScrollAccessibilityEvent() {
590 AccessibilityManager am =
591 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
592 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700593 if (mCurrentPage != getNextPage()) {
594 AccessibilityEvent ev =
595 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700596 ev.setScrollable(true);
597 ev.setScrollX(getScrollX());
598 ev.setScrollY(getScrollY());
599 ev.setMaxScrollX(mMaxScrollX);
600 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700601
Vadim Tryshevf4715972015-05-08 17:21:03 -0700602 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700603 }
Adam Cohen53805212013-10-01 10:39:23 -0700604 }
605 }
606
Michael Jurka0142d492010-08-25 17:46:15 -0700607 // we moved this functionality to a helper function so SmoothPagedView can reuse it
608 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800609 return computeScrollHelper(true);
610 }
611
612 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700614 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800616 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700617 float scaleX = mFreeScroll ? getScaleX() : 1f;
618 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
619 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700620 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800621 if (shouldInvalidate) {
622 invalidate();
623 }
Michael Jurka0142d492010-08-25 17:46:15 -0700624 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800625 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700626 sendScrollAccessibilityEvent();
627
Adam Cohen6f127a62014-06-12 14:54:41 -0700628 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700629 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700630 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700631
Adam Cohen73aa9752010-11-24 16:26:58 -0800632 // We don't want to trigger a page end moving unless the page has settled
633 // and the user has stopped scrolling
634 if (mTouchState == TOUCH_STATE_REST) {
635 pageEndMoving();
636 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700637
Adam Cohen7d30a372013-07-01 17:03:59 -0700638 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700639 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700640 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700641 if (am.isEnabled()) {
642 // Notify the user when the page changes
643 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700644 }
Michael Jurka0142d492010-08-25 17:46:15 -0700645 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
Michael Jurka0142d492010-08-25 17:46:15 -0700647 return false;
648 }
649
650 @Override
651 public void computeScroll() {
652 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 }
654
Adam Cohen96d30a12013-07-16 18:13:21 -0700655 public static class LayoutParams extends ViewGroup.LayoutParams {
656 public boolean isFullScreenPage = false;
657
658 /**
659 * {@inheritDoc}
660 */
661 public LayoutParams(int width, int height) {
662 super(width, height);
663 }
664
Sunny Goyal41b22c02015-05-14 15:20:48 -0700665 public LayoutParams(Context context, AttributeSet attrs) {
666 super(context, attrs);
667 }
668
Adam Cohen96d30a12013-07-16 18:13:21 -0700669 public LayoutParams(ViewGroup.LayoutParams source) {
670 super(source);
671 }
672 }
673
Sunny Goyal41b22c02015-05-14 15:20:48 -0700674 @Override
675 public LayoutParams generateLayoutParams(AttributeSet attrs) {
676 return new LayoutParams(getContext(), attrs);
677 }
678
679 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700680 protected LayoutParams generateDefaultLayoutParams() {
681 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
682 }
683
Sunny Goyal41b22c02015-05-14 15:20:48 -0700684 @Override
685 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
686 return new LayoutParams(p);
687 }
688
689 @Override
690 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
691 return p instanceof LayoutParams;
692 }
693
Adam Cohenedb40762013-07-18 16:45:45 -0700694 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700695 LayoutParams lp = generateDefaultLayoutParams();
696 lp.isFullScreenPage = true;
697 super.addView(page, 0, lp);
698 }
699
Adam Cohen410f3cd2013-09-22 12:09:32 -0700700 public int getNormalChildHeight() {
701 return mNormalChildHeight;
702 }
703
Winson Chung321e9ee2010-08-09 13:37:56 -0700704 @Override
705 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700706 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700707 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
708 return;
709 }
710
Adam Cohen7d30a372013-07-01 17:03:59 -0700711 // We measure the dimensions of the PagedView to be larger than the pages so that when we
712 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700713 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
714 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
715 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700716 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800717 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700718 // viewport, we can be at most one and a half screens offset once we scale down
719 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700720 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
721 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700722
Winson Chungc82d2622013-11-06 13:23:29 -0800723 int parentWidthSize = (int) (2f * maxSize);
724 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700725 int scaledWidthSize, scaledHeightSize;
726 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700727 scaledWidthSize = (int) (parentWidthSize / mMinScale);
728 scaledHeightSize = (int) (parentHeightSize / mMinScale);
729 } else {
730 scaledWidthSize = widthSize;
731 scaledHeightSize = heightSize;
732 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700733 mViewport.set(0, 0, widthSize, heightSize);
734
735 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
736 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
737 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 }
739
Winson Chung8aad6102012-05-11 16:27:49 -0700740 // Return early if we aren't given a proper dimension
741 if (widthSize <= 0 || heightSize <= 0) {
742 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
743 return;
744 }
745
Adam Lesinski6b879f02010-11-04 16:15:23 -0700746 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
747 * of the All apps view on XLarge displays to not take up more space then it needs. Width
748 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
749 * each page to have the same width.
750 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 final int verticalPadding = getPaddingTop() + getPaddingBottom();
752 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700753
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700754 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700756 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700757 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700758 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
759 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
760 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
761 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 final int childCount = getChildCount();
763 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700764 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700765 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100766 if (child.getVisibility() != GONE) {
767 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700768
Vladimir Marko2824b072013-10-04 16:42:17 +0100769 int childWidthMode;
770 int childHeightMode;
771 int childWidth;
772 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700773
Vladimir Marko2824b072013-10-04 16:42:17 +0100774 if (!lp.isFullScreenPage) {
775 if (lp.width == LayoutParams.WRAP_CONTENT) {
776 childWidthMode = MeasureSpec.AT_MOST;
777 } else {
778 childWidthMode = MeasureSpec.EXACTLY;
779 }
780
781 if (lp.height == LayoutParams.WRAP_CONTENT) {
782 childHeightMode = MeasureSpec.AT_MOST;
783 } else {
784 childHeightMode = MeasureSpec.EXACTLY;
785 }
786
Adam Cohen3b185e22013-10-29 14:45:58 -0700787 childWidth = getViewportWidth() - horizontalPadding
788 - mInsets.left - mInsets.right;
789 childHeight = getViewportHeight() - verticalPadding
790 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100791 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700792 } else {
793 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700794 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100795
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800796 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700797 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700798 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700799 if (referenceChildWidth == 0) {
800 referenceChildWidth = childWidth;
801 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700802
Vladimir Marko2824b072013-10-04 16:42:17 +0100803 final int childWidthMeasureSpec =
804 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
805 final int childHeightMeasureSpec =
806 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
807 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700808 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700809 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700810 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800811 }
812
Sunny Goyalcf25b522015-07-09 00:01:18 -0700813 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700815 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700816 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700817 return;
818 }
819
Winson Chung785d2eb2011-04-14 16:08:02 -0700820 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700822
Adam Cohen7d30a372013-07-01 17:03:59 -0700823 int offsetX = getViewportOffsetX();
824 int offsetY = getViewportOffsetY();
825
826 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700827 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700828
Sunny Goyal70660032015-05-14 00:07:08 -0700829 final int startIndex = mIsRtl ? childCount - 1 : 0;
830 final int endIndex = mIsRtl ? -1 : childCount;
831 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700832
Adam Cohen7d30a372013-07-01 17:03:59 -0700833 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700834
Adam Cohen3b185e22013-10-29 14:45:58 -0700835 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000836 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700837
838 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700839 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
840 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700841 }
842
Adam Cohen0ffac432013-07-10 11:19:26 -0700843 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700844 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700846 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100847 int childTop;
848 if (lp.isFullScreenPage) {
849 childTop = offsetY;
850 } else {
851 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700852 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100853 }
854
Adam Cohen96d30a12013-07-16 18:13:21 -0700855 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700856 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800857
Winson Chung785d2eb2011-04-14 16:08:02 -0700858 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700859 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800860 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700861
Adam Cohen3b185e22013-10-29 14:45:58 -0700862 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
863 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000864
865 int pageGap = mPageSpacing;
866 int next = i + delta;
867 if (next != endIndex) {
868 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
869 } else {
870 nextLp = null;
871 }
872
873 // Prevent full screen pages from showing in the viewport
874 // when they are not the current page.
875 if (lp.isFullScreenPage) {
876 pageGap = getPaddingLeft();
877 } else if (nextLp != null && nextLp.isFullScreenPage) {
878 pageGap = getPaddingRight();
879 }
880
Sunny Goyala1fbd842015-05-20 13:40:27 -0700881 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 }
883 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700884
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700885 final LayoutTransition transition = getLayoutTransition();
886 // If the transition is running defer updating max scroll, as some empty pages could
887 // still be present, and a max scroll change could cause sudden jumps in scroll.
888 if (transition != null && transition.isRunning()) {
889 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
890
891 @Override
892 public void startTransition(LayoutTransition transition, ViewGroup container,
893 View view, int transitionType) { }
894
895 @Override
896 public void endTransition(LayoutTransition transition, ViewGroup container,
897 View view, int transitionType) {
898 // Wait until all transitions are complete.
899 if (!transition.isRunning()) {
900 transition.removeTransitionListener(this);
901 updateMaxScrollX();
902 }
903 }
904 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700905 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700906 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700907 }
908
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700909 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
910 updateCurrentPageScroll();
911 mFirstLayout = false;
912 }
913
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700914 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700915 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700916 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700917 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700918 } else {
919 setCurrentPage(getNextPage());
920 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700921 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700922 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700923
924 if (isReordering(true)) {
925 updateDragViewTranslationDuringDrag();
926 }
Winson Chunge3193b92010-09-10 11:44:42 -0700927 }
928
Sunny Goyala1fbd842015-05-20 13:40:27 -0700929 protected int getChildGap() {
930 return 0;
931 }
932
Sunny Goyal316490e2015-06-02 09:38:28 -0700933 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700934 int childCount = getChildCount();
935 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700936 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700937 mMaxScrollX = getScrollForPage(index);
938 } else {
939 mMaxScrollX = 0;
940 }
941 }
942
Adam Cohen3b185e22013-10-29 14:45:58 -0700943 public void setPageSpacing(int pageSpacing) {
944 mPageSpacing = pageSpacing;
945 requestLayout();
946 }
947
Sunny Goyal4d113a52015-05-27 10:05:28 -0700948 /**
949 * Called when the center screen changes during scrolling.
950 */
951 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700952
Winson Chunge3193b92010-09-10 11:44:42 -0700953 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700954 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700955 // Update the page indicator, we don't update the page indicator as we
956 // add/remove pages
957 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700958 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700959 mPageIndicator.addMarker(pageIndex,
960 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700961 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700962 }
963
Adam Cohen2591f6a2011-10-25 14:36:40 -0700964 // This ensures that when children are added, they get the correct transforms / alphas
965 // in accordance with any scroll effects.
966 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700967 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700968 invalidate();
969 }
970
Michael Jurka8b805b12012-04-18 14:23:14 -0700971 @Override
972 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700973 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700974 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700975 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700976 }
977
Winson Chungd2be3812013-07-16 11:11:32 -0700978 private void removeMarkerForView(int index) {
979 // Update the page indicator, we don't update the page indicator as we
980 // add/remove pages
981 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700982 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700983 }
984 }
985
986 @Override
987 public void removeView(View v) {
988 // XXX: We should find a better way to hook into this before the view
989 // gets removed form its parent...
990 removeMarkerForView(indexOfChild(v));
991 super.removeView(v);
992 }
993 @Override
994 public void removeViewInLayout(View v) {
995 // XXX: We should find a better way to hook into this before the view
996 // gets removed form its parent...
997 removeMarkerForView(indexOfChild(v));
998 super.removeViewInLayout(v);
999 }
1000 @Override
1001 public void removeViewAt(int index) {
1002 // XXX: We should find a better way to hook into this before the view
1003 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001004 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001005 super.removeViewAt(index);
1006 }
1007 @Override
1008 public void removeAllViewsInLayout() {
1009 // Update the page indicator, we don't update the page indicator as we
1010 // add/remove pages
1011 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001012 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001013 }
1014
1015 super.removeAllViewsInLayout();
1016 }
1017
Adam Cohen73894962011-10-31 13:17:17 -07001018 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001019 if (index < 0 || index > getChildCount() - 1) return 0;
1020
Adam Cohenf698c6e2013-07-17 18:44:25 -07001021 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001022
Adam Cohenf698c6e2013-07-17 18:44:25 -07001023 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001024 }
1025
Adam Cohen6f127a62014-06-12 14:54:41 -07001026 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001027 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001028 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001029 }
1030
Michael Jurkadde558b2011-11-09 22:09:06 -08001031 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001032 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001033 range[0] = -1;
1034 range[1] = -1;
1035
Sunny Goyalee688162016-02-12 14:24:21 -08001036 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001037 final int visibleLeft = -getLeft();
1038 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001039 final Matrix pageShiftMatrix = getPageShiftMatrix();
1040 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001041
Sunny Goyalee688162016-02-12 14:24:21 -08001042 for (int i = 0; i < count; i++) {
1043 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001044
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001045 // Verify if the page bounds are within the visible range.
1046 sTmpRectF.left = 0;
1047 sTmpRectF.right = currPage.getMeasuredWidth();
1048 currPage.getMatrix().mapRect(sTmpRectF);
1049 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001050 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001051
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001052 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001053 if (range[0] == -1) {
1054 continue;
1055 } else {
1056 break;
1057 }
1058 }
Sunny Goyalee688162016-02-12 14:24:21 -08001059 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001060 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001061 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001062 }
1063 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001064
Sunny Goyalee688162016-02-12 14:24:21 -08001065 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001066 } else {
1067 range[0] = -1;
1068 range[1] = -1;
1069 }
1070 }
1071
Sunny Goyalee688162016-02-12 14:24:21 -08001072 protected Matrix getPageShiftMatrix() {
1073 return getMatrix();
1074 }
1075
Michael Jurka920d7f42012-05-14 16:29:55 -07001076 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001077 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001078 }
1079
Michael Jurkadde558b2011-11-09 22:09:06 -08001080 @Override
1081 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001082 // Find out which screens are visible; as an optimization we only call draw on them
1083 final int pageCount = getChildCount();
1084 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001085 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001086 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001087
1088 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1089 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1090 // set it for the next frame
1091 mForceScreenScrolled = false;
1092 screenScrolled(screenCenter);
1093 mLastScreenCenter = screenCenter;
1094 }
1095
Michael Jurkadde558b2011-11-09 22:09:06 -08001096 getVisiblePages(mTempVisiblePagesRange);
1097 final int leftScreen = mTempVisiblePagesRange[0];
1098 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001099 if (leftScreen != -1 && rightScreen != -1) {
1100 final long drawingTime = getDrawingTime();
1101 // Clip to the bounds
1102 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001103 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1104 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001105
Adam Cohen7d30a372013-07-01 17:03:59 -07001106 // Draw all the children, leaving the drag view for last
1107 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001108 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001109 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001110 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001111 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001112 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001113 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001114 // Draw the drag view on top (if there is one)
1115 if (mDragView != null) {
1116 drawChild(canvas, mDragView, drawingTime);
1117 }
1118
Winson Chungc6f10b92011-11-14 11:39:07 -08001119 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001120 }
Michael Jurka0142d492010-08-25 17:46:15 -07001121 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 }
1123
1124 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001125 public void draw(Canvas canvas) {
1126 super.draw(canvas);
1127 if (getPageCount() > 0) {
1128 if (!mEdgeGlowLeft.isFinished()) {
1129 final int restoreCount = canvas.save();
1130 Rect display = mViewport;
1131 canvas.translate(display.left, display.top);
1132 canvas.rotate(270);
1133
1134 getEdgeVerticalPostion(sTmpIntPoint);
1135 canvas.translate(display.top - sTmpIntPoint[1], 0);
1136 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1137 if (mEdgeGlowLeft.draw(canvas)) {
1138 postInvalidateOnAnimation();
1139 }
1140 canvas.restoreToCount(restoreCount);
1141 }
1142 if (!mEdgeGlowRight.isFinished()) {
1143 final int restoreCount = canvas.save();
1144 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001145 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001146 canvas.rotate(90);
1147
1148 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001149
Sunny Goyal93264612015-11-23 11:47:50 -08001150 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1151 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001152 if (mEdgeGlowRight.draw(canvas)) {
1153 postInvalidateOnAnimation();
1154 }
1155 canvas.restoreToCount(restoreCount);
1156 }
1157 }
1158 }
1159
1160 /**
1161 * Returns the top and bottom position for the edge effect.
1162 */
1163 protected abstract void getEdgeVerticalPostion(int[] pos);
1164
1165 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001166 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001167 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001168 if (page != mCurrentPage || !mScroller.isFinished()) {
1169 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 return true;
1171 }
1172 return false;
1173 }
1174
1175 @Override
1176 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001177 int focusablePage;
1178 if (mNextPage != INVALID_PAGE) {
1179 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001181 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 }
Winson Chung86f77532010-08-24 11:08:22 -07001183 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001185 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 }
1187 return false;
1188 }
1189
1190 @Override
1191 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001192 if (super.dispatchUnhandledMove(focused, direction)) {
1193 return true;
1194 }
1195
1196 if (mIsRtl) {
1197 if (direction == View.FOCUS_LEFT) {
1198 direction = View.FOCUS_RIGHT;
1199 } else if (direction == View.FOCUS_RIGHT) {
1200 direction = View.FOCUS_LEFT;
1201 }
1202 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001204 if (getCurrentPage() > 0) {
1205 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 return true;
1207 }
1208 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001209 if (getCurrentPage() < getPageCount() - 1) {
1210 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 return true;
1212 }
1213 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001214 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
1216
1217 @Override
1218 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001219 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001220 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001221 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 }
1223 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001224 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001225 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 }
1227 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001229 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 }
1231 }
1232 }
1233
1234 /**
1235 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001236 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 *
Winson Chung86f77532010-08-24 11:08:22 -07001238 * This happens when live folders requery, and if they're off page, they
1239 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 */
1241 @Override
1242 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001243 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 View v = focused;
1245 while (true) {
1246 if (v == current) {
1247 super.focusableViewAvailable(focused);
1248 return;
1249 }
1250 if (v == this) {
1251 return;
1252 }
1253 ViewParent parent = v.getParent();
1254 if (parent instanceof View) {
1255 v = (View)v.getParent();
1256 } else {
1257 return;
1258 }
1259 }
1260 }
1261
1262 /**
1263 * {@inheritDoc}
1264 */
1265 @Override
1266 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1267 if (disallowIntercept) {
1268 // We need to make sure to cancel our long press if
1269 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001270 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001271 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1274 }
1275
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001276 /**
1277 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1278 */
1279 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001280 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001281 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001282 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001283 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001284 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001285 }
1286
1287 /**
1288 * Return true if a tap at (x, y) should trigger a flip to the next page.
1289 */
1290 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001291 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001292 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001293 }
1294 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001295 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001296 }
Winson Chung52aee602013-01-30 12:01:02 -08001297
Adam Cohen7d30a372013-07-01 17:03:59 -07001298 /** Returns whether x and y originated within the buffered viewport */
1299 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001300 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001301 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001302 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001303 }
1304
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 @Override
1306 public boolean onInterceptTouchEvent(MotionEvent ev) {
1307 /*
1308 * This method JUST determines whether we want to intercept the motion.
1309 * If we return true, onTouchEvent will be called and we do the actual
1310 * scrolling there.
1311 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001312 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001313
Winson Chung45e1d6e2010-11-09 17:19:49 -08001314 // Skip touch handling if there are no pages to swipe
1315 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1316
Winson Chung321e9ee2010-08-09 13:37:56 -07001317 /*
1318 * Shortcut the most recurring case: the user is in the dragging
1319 * state and he is moving his finger. We want to intercept this
1320 * motion.
1321 */
1322 final int action = ev.getAction();
1323 if ((action == MotionEvent.ACTION_MOVE) &&
1324 (mTouchState == TOUCH_STATE_SCROLLING)) {
1325 return true;
1326 }
1327
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 switch (action & MotionEvent.ACTION_MASK) {
1329 case MotionEvent.ACTION_MOVE: {
1330 /*
1331 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1332 * whether the user has moved far enough from his original down touch.
1333 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001334 if (mActivePointerId != INVALID_POINTER) {
1335 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001336 }
1337 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1338 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1339 // i.e. fall through to the next case (don't break)
1340 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1341 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001342 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 }
1344
1345 case MotionEvent.ACTION_DOWN: {
1346 final float x = ev.getX();
1347 final float y = ev.getY();
1348 // Remember location of down touch
1349 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001350 mDownMotionY = y;
1351 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 mLastMotionX = x;
1353 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001354 float[] p = mapPointFromViewToParent(this, x, y);
1355 mParentDownMotionX = p[0];
1356 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001357 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001358 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001360
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 /*
1362 * If being flinged and user touches the screen, initiate drag;
1363 * otherwise don't. mScroller.isFinished should be false when
1364 * being flinged.
1365 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001366 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001367 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001368
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001369 if (finishedScrolling) {
1370 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001371 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001372 setCurrentPage(getNextPage());
1373 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001374 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001375 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001376 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1377 mTouchState = TOUCH_STATE_SCROLLING;
1378 } else {
1379 mTouchState = TOUCH_STATE_REST;
1380 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001381 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001382
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 break;
1384 }
1385
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001387 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001388 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 break;
1390
1391 case MotionEvent.ACTION_POINTER_UP:
1392 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001393 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 break;
1395 }
1396
1397 /*
1398 * The only time we want to intercept motion events is if we are in the
1399 * drag mode.
1400 */
1401 return mTouchState != TOUCH_STATE_REST;
1402 }
1403
Adam Cohenf8d28232011-02-01 21:47:00 -08001404 protected void determineScrollingStart(MotionEvent ev) {
1405 determineScrollingStart(ev, 1.0f);
1406 }
1407
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 /*
1409 * Determines if we should change the touch state to start scrolling after the
1410 * user moves their touch point too far.
1411 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001412 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001413 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001415 if (pointerIndex == -1) return;
1416
1417 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 final float x = ev.getX(pointerIndex);
1419 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001420 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1421
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001423
Adam Cohenf8d28232011-02-01 21:47:00 -08001424 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001426
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001427 if (xMoved) {
1428 // Scroll if the user moved far enough along the X axis
1429 mTouchState = TOUCH_STATE_SCROLLING;
1430 mTotalMotionX += Math.abs(mLastMotionX - x);
1431 mLastMotionX = x;
1432 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001433 onScrollInteractionBegin();
1434 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001435 // Stop listening for things like pinches.
1436 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001437 }
1438 }
1439
1440 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001441 // Try canceling the long press. It could also have been scheduled
1442 // by a distant descendant, so use the mAllowLongPress flag to block
1443 // everything
1444 final View currentPage = getPageAt(mCurrentPage);
1445 if (currentPage != null) {
1446 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001447 }
1448 }
1449
Adam Cohenb5ba0972011-09-07 18:02:31 -07001450 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001451 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001452
Adam Cohenedb40762013-07-18 16:45:45 -07001453 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001454 int count = getChildCount();
1455
1456 final int totalDistance;
1457
1458 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001459 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001460 adjacentPage = page - 1;
1461 }
1462
1463 if (adjacentPage < 0 || adjacentPage > count - 1) {
1464 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1465 } else {
1466 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1467 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001468
1469 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001470 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1471 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001472 return scrollProgress;
1473 }
1474
Adam Cohenedb40762013-07-18 16:45:45 -07001475 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001476 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001477 return 0;
1478 } else {
1479 return mPageScrolls[index];
1480 }
1481 }
1482
Adam Cohen564a2e72013-10-09 14:47:32 -07001483 // While layout transitions are occurring, a child's position may stray from its baseline
1484 // position. This method returns the magnitude of this stray at any given time.
1485 public int getLayoutTransitionOffsetForPage(int index) {
1486 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1487 return 0;
1488 } else {
1489 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001490
1491 int scrollOffset = 0;
1492 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1493 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001494 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001495 }
1496
Adam Cohen564a2e72013-10-09 14:47:32 -07001497 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1498 return (int) (child.getX() - baselineX);
1499 }
1500 }
1501
Adam Cohenb5ba0972011-09-07 18:02:31 -07001502 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001503 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001504 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001505 if (f < 0) {
1506 mEdgeGlowLeft.onPull(-f);
1507 } else if (f > 0) {
1508 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001509 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001510 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001511 }
1512 invalidate();
1513 }
1514
Adam Cohenb5ba0972011-09-07 18:02:31 -07001515 protected void overScroll(float amount) {
1516 dampedOverScroll(amount);
1517 }
1518
Winson Chungdc61c4d2015-04-20 18:26:57 -07001519 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001520 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001521 }
1522
Winson Chungdc61c4d2015-04-20 18:26:57 -07001523 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001524 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001525 }
1526
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001527 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001528 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001529 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001530 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1531 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1532 } else {
1533 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1534 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1535 }
1536 }
1537
Adam Cohenf9618852013-11-08 06:45:03 -08001538 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001539 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001540 mFreeScroll = freeScroll;
1541
Adam Cohenf9618852013-11-08 06:45:03 -08001542 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001543 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001544 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001545 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1546 setCurrentPage(mTempVisiblePagesRange[0]);
1547 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1548 setCurrentPage(mTempVisiblePagesRange[1]);
1549 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001550 } else if (wasFreeScroll) {
1551 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001552 }
1553
1554 setEnableOverscroll(!freeScroll);
1555 }
1556
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001557 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001558 mAllowOverScroll = enable;
1559 }
1560
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001561 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001562 if (mDragView != null) {
1563 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1564 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001565 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001566 int minDistance = Integer.MAX_VALUE;
1567 int minIndex = indexOfChild(mDragView);
1568 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1569 View page = getPageAt(i);
1570 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1571 int d = Math.abs(dragX - pageX);
1572 if (d < minDistance) {
1573 minIndex = i;
1574 minDistance = d;
1575 }
1576 }
1577 return minIndex;
1578 }
1579 return -1;
1580 }
1581
Winson Chung321e9ee2010-08-09 13:37:56 -07001582 @Override
1583 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001584 super.onTouchEvent(ev);
1585
Winson Chung45e1d6e2010-11-09 17:19:49 -08001586 // Skip touch handling if there are no pages to swipe
1587 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1588
Michael Jurkab8f06722010-10-10 15:58:46 -07001589 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001590
1591 final int action = ev.getAction();
1592
1593 switch (action & MotionEvent.ACTION_MASK) {
1594 case MotionEvent.ACTION_DOWN:
1595 /*
1596 * If being flinged and user touches, stop the fling. isFinished
1597 * will be false if being flinged.
1598 */
1599 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001600 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001601 }
1602
1603 // Remember where the motion event started
1604 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001605 mDownMotionY = mLastMotionY = ev.getY();
1606 mDownScrollX = getScrollX();
1607 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1608 mParentDownMotionX = p[0];
1609 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001610 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001611 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001612 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001613
Michael Jurka0142d492010-08-25 17:46:15 -07001614 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001615 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001616 pageBeginMoving();
1617 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001618 break;
1619
1620 case MotionEvent.ACTION_MOVE:
1621 if (mTouchState == TOUCH_STATE_SCROLLING) {
1622 // Scroll to follow the motion event
1623 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001624
1625 if (pointerIndex == -1) return true;
1626
Winson Chung321e9ee2010-08-09 13:37:56 -07001627 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001628 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001629
Adam Cohenaefd4e12011-02-14 16:39:38 -08001630 mTotalMotionX += Math.abs(deltaX);
1631
Winson Chungc0844aa2011-02-02 15:25:58 -08001632 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1633 // keep the remainder because we are actually testing if we've moved from the last
1634 // scrolled position (which is discrete).
1635 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001636 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001637 mLastMotionX = x;
1638 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001639 } else {
1640 awakenScrollBars();
1641 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001642 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1643 // Update the last motion position
1644 mLastMotionX = ev.getX();
1645 mLastMotionY = ev.getY();
1646
1647 // Update the parent down so that our zoom animations take this new movement into
1648 // account
1649 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1650 mParentDownMotionX = pt[0];
1651 mParentDownMotionY = pt[1];
1652 updateDragViewTranslationDuringDrag();
1653
1654 // Find the closest page to the touch point
1655 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001656
Adam Cohen7d30a372013-07-01 17:03:59 -07001657 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1658 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1659 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1660 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1661
Adam Cohenf358a4b2013-07-23 16:47:31 -07001662 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001663 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 mTempVisiblePagesRange[0] = 0;
1665 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001666 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001667 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1668 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1669 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1670 mSidePageHoverIndex = pageUnderPointIndex;
1671 mSidePageHoverRunnable = new Runnable() {
1672 @Override
1673 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001674 // Setup the scroll to the correct page before we swap the views
1675 snapToPage(pageUnderPointIndex);
1676
1677 // For each of the pages between the paged view and the drag view,
1678 // animate them from the previous position to the new position in
1679 // the layout (as a result of the drag view moving in the layout)
1680 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1681 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1682 dragViewIndex + 1 : pageUnderPointIndex;
1683 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1684 dragViewIndex - 1 : pageUnderPointIndex;
1685 for (int i = lowerIndex; i <= upperIndex; ++i) {
1686 View v = getChildAt(i);
1687 // dragViewIndex < pageUnderPointIndex, so after we remove the
1688 // drag view all subsequent views to pageUnderPointIndex will
1689 // shift down.
1690 int oldX = getViewportOffsetX() + getChildOffset(i);
1691 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1692
1693 // Animate the view translation from its old position to its new
1694 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001695 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001696 if (anim != null) {
1697 anim.cancel();
1698 }
1699
1700 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001701 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001702 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001703 anim.start();
1704 v.setTag(anim);
1705 }
1706
1707 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001709 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001710 if (mPageIndicator != null) {
1711 mPageIndicator.setActiveMarker(getNextPage());
1712 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001713 }
1714 };
1715 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1716 }
1717 } else {
1718 removeCallbacks(mSidePageHoverRunnable);
1719 mSidePageHoverIndex = -1;
1720 }
Adam Cohen564976a2010-10-13 18:52:07 -07001721 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001722 determineScrollingStart(ev);
1723 }
1724 break;
1725
1726 case MotionEvent.ACTION_UP:
1727 if (mTouchState == TOUCH_STATE_SCROLLING) {
1728 final int activePointerId = mActivePointerId;
1729 final int pointerIndex = ev.findPointerIndex(activePointerId);
1730 final float x = ev.getX(pointerIndex);
1731 final VelocityTracker velocityTracker = mVelocityTracker;
1732 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1733 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001734 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001735 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001736 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1737 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001738
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001739 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1740
Adam Cohen00481b32011-11-18 12:03:48 -08001741 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001742 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001743
Adam Cohenf358a4b2013-07-23 16:47:31 -07001744 if (!mFreeScroll) {
1745 // In the case that the page is moved far to one direction and then is flung
1746 // in the opposite direction, we use a threshold to determine whether we should
1747 // just return to the starting page, or if we should skip one further.
1748 boolean returnToOriginalPage = false;
1749 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1750 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1751 returnToOriginalPage = true;
1752 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001753
Adam Cohenf358a4b2013-07-23 16:47:31 -07001754 int finalPage;
1755 // We give flings precedence over large moves, which is why we short-circuit our
1756 // test for a large move if a fling has been registered. That is, a large
1757 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001758 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1759 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001760 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1761 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1762 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1763 snapToPageWithVelocity(finalPage, velocityX);
1764 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1765 (isFling && isVelocityXLeft)) &&
1766 mCurrentPage < getChildCount() - 1) {
1767 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1768 snapToPageWithVelocity(finalPage, velocityX);
1769 } else {
1770 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001771 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001772 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001773 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001774 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001775 }
1776
1777 float scaleX = getScaleX();
1778 int vX = (int) (-velocityX * scaleX);
1779 int initialScrollX = (int) (getScrollX() * scaleX);
1780
Adam Cohenf9618852013-11-08 06:45:03 -08001781 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001782 mScroller.fling(initialScrollX,
1783 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001784 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001785 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001786 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001787 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001788 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1789 // at this point we have not moved beyond the touch slop
1790 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1791 // we can just page
1792 int nextPage = Math.max(0, mCurrentPage - 1);
1793 if (nextPage != mCurrentPage) {
1794 snapToPage(nextPage);
1795 } else {
1796 snapToDestination();
1797 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001798 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001799 // at this point we have not moved beyond the touch slop
1800 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1801 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001802 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1803 if (nextPage != mCurrentPage) {
1804 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001805 } else {
1806 snapToDestination();
1807 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001808 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1809 // Update the last motion position
1810 mLastMotionX = ev.getX();
1811 mLastMotionY = ev.getY();
1812
1813 // Update the parent down so that our zoom animations take this new movement into
1814 // account
1815 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1816 mParentDownMotionX = pt[0];
1817 mParentDownMotionY = pt[1];
1818 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001819 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001820 if (!mCancelTap) {
1821 onUnhandledTap(ev);
1822 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001823 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001824
1825 // Remove the callback to wait for the side page hover timeout
1826 removeCallbacks(mSidePageHoverRunnable);
1827 // End any intermediate reordering states
1828 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001829 break;
1830
1831 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001832 if (mTouchState == TOUCH_STATE_SCROLLING) {
1833 snapToDestination();
1834 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001835 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001836 break;
1837
1838 case MotionEvent.ACTION_POINTER_UP:
1839 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001840 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001841 break;
1842 }
1843
1844 return true;
1845 }
1846
Adam Cohen7d30a372013-07-01 17:03:59 -07001847 private void resetTouchState() {
1848 releaseVelocityTracker();
1849 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001850 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001851 mTouchState = TOUCH_STATE_REST;
1852 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001853 mEdgeGlowLeft.onRelease();
1854 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001855 }
1856
Adam Cohenc2d6e892014-10-16 09:49:24 -07001857 /**
1858 * Triggered by scrolling via touch
1859 */
1860 protected void onScrollInteractionBegin() {
1861 }
1862
1863 protected void onScrollInteractionEnd() {
1864 }
1865
Adam Cohen1697b792013-09-17 19:08:21 -07001866 protected void onUnhandledTap(MotionEvent ev) {
1867 ((Launcher) getContext()).onClick(this);
1868 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001869
Winson Chung185d7162011-02-28 13:47:29 -08001870 @Override
1871 public boolean onGenericMotionEvent(MotionEvent event) {
1872 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1873 switch (event.getAction()) {
1874 case MotionEvent.ACTION_SCROLL: {
1875 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1876 final float vscroll;
1877 final float hscroll;
1878 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1879 vscroll = 0;
1880 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1881 } else {
1882 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1883 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1884 }
1885 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001886 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001887 : (hscroll > 0 || vscroll > 0);
1888 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001889 scrollRight();
1890 } else {
1891 scrollLeft();
1892 }
1893 return true;
1894 }
1895 }
1896 }
1897 }
1898 return super.onGenericMotionEvent(event);
1899 }
1900
Michael Jurkab8f06722010-10-10 15:58:46 -07001901 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1902 if (mVelocityTracker == null) {
1903 mVelocityTracker = VelocityTracker.obtain();
1904 }
1905 mVelocityTracker.addMovement(ev);
1906 }
1907
1908 private void releaseVelocityTracker() {
1909 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001910 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001911 mVelocityTracker.recycle();
1912 mVelocityTracker = null;
1913 }
1914 }
1915
Winson Chung321e9ee2010-08-09 13:37:56 -07001916 private void onSecondaryPointerUp(MotionEvent ev) {
1917 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1918 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1919 final int pointerId = ev.getPointerId(pointerIndex);
1920 if (pointerId == mActivePointerId) {
1921 // This was our active pointer going up. Choose a new
1922 // active pointer and adjust accordingly.
1923 // TODO: Make this decision more intelligent.
1924 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1925 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1926 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001927 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 mActivePointerId = ev.getPointerId(newPointerIndex);
1929 if (mVelocityTracker != null) {
1930 mVelocityTracker.clear();
1931 }
1932 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001933 }
1934
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 @Override
1936 public void requestChildFocus(View child, View focused) {
1937 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001938 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001939 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001940 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 }
1942 }
1943
Adam Cohend19d3ca2010-09-15 14:43:42 -07001944 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001945 return getPageNearestToCenterOfScreen(getScrollX());
1946 }
1947
1948 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1949 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001950 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001951 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001952 final int childCount = getChildCount();
1953 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001954 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001955 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001956 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001957 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001958 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1959 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1960 minDistanceFromScreenCenter = distanceFromScreenCenter;
1961 minDistanceFromScreenCenterIndex = i;
1962 }
1963 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001964 return minDistanceFromScreenCenterIndex;
1965 }
1966
1967 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001968 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001969 }
1970
Adam Cohene0f66b52010-11-23 15:06:07 -08001971 private static class ScrollInterpolator implements Interpolator {
1972 public ScrollInterpolator() {
1973 }
1974
1975 public float getInterpolation(float t) {
1976 t -= 1.0f;
1977 return t*t*t*t*t + 1;
1978 }
1979 }
1980
1981 // We want the duration of the page snap animation to be influenced by the distance that
1982 // the screen has to travel, however, we don't want this duration to be effected in a
1983 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1984 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001985 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001986 f -= 0.5f; // center the values about 0.
1987 f *= 0.3f * Math.PI / 2.0f;
1988 return (float) Math.sin(f);
1989 }
1990
Michael Jurka0142d492010-08-25 17:46:15 -07001991 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001992 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001993 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001994
Adam Cohenedb40762013-07-18 16:45:45 -07001995 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001996 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001997 int duration = 0;
1998
Sunny Goyal4d113a52015-05-27 10:05:28 -07001999 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002000 // If the velocity is low enough, then treat this more as an automatic page advance
2001 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002002 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002003 return;
2004 }
2005
2006 // Here we compute a "distance" that will be used in the computation of the overall
2007 // snap duration. This is a function of the actual distance that needs to be traveled;
2008 // we keep this value close to half screen size in order to reduce the variance in snap
2009 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002010 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002011 float distance = halfScreenSize + halfScreenSize *
2012 distanceInfluenceForSnapDuration(distanceRatio);
2013
2014 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002015 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002016
2017 // we want the page's snap velocity to approximately match the velocity at which the
2018 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002019 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2020 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002021
2022 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002023 }
2024
Sunny Goyal1740d902015-05-27 11:14:01 -07002025 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002026 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002027 }
2028
Adam Cohenf9c184a2016-01-15 16:47:43 -08002029 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002030 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002031 }
2032
Michael Jurka0142d492010-08-25 17:46:15 -07002033 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002034 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002035 }
2036
Adam Cohenf9618852013-11-08 06:45:03 -08002037 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2038 snapToPage(whichPage, duration, false, interpolator);
2039 }
2040
2041 protected void snapToPage(int whichPage, int duration, boolean immediate,
2042 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002043 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002044
Adam Cohenedb40762013-07-18 16:45:45 -07002045 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002046 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002047 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002048 }
2049
2050 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002051 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002052 }
Michael Jurka0142d492010-08-25 17:46:15 -07002053
Adam Cohenf9618852013-11-08 06:45:03 -08002054 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2055 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002056 whichPage = validateNewPage(whichPage);
2057
Adam Cohen7d30a372013-07-01 17:03:59 -07002058 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002059
2060 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002061 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002062 if (immediate) {
2063 duration = 0;
2064 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002065 duration = Math.abs(delta);
2066 }
2067
Adam Cohenf358a4b2013-07-23 16:47:31 -07002068 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002069 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002070 }
Adam Cohenf9618852013-11-08 06:45:03 -08002071
2072 if (interpolator != null) {
2073 mScroller.setInterpolator(interpolator);
2074 } else {
2075 mScroller.setInterpolator(mDefaultInterpolator);
2076 }
2077
Sunny Goyalc86df472016-02-25 09:19:38 -08002078 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002079
Adam Cohen674531f2013-12-13 15:07:14 -08002080 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002081
2082 // Trigger a compute() to finish switching pages if necessary
2083 if (immediate) {
2084 computeScroll();
2085 }
2086
2087 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002088 invalidate();
2089 }
2090
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002092 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002093 }
2094
2095 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002096 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002097 }
2098
Adam Cohendbdff6b2013-09-18 19:09:15 -07002099 @Override
2100 public boolean performLongClick() {
2101 mCancelTap = true;
2102 return super.performLongClick();
2103 }
2104
Winson Chung321e9ee2010-08-09 13:37:56 -07002105 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002106 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002107
2108 SavedState(Parcelable superState) {
2109 super(superState);
2110 }
2111
Adam Cohen091440a2015-03-18 14:16:05 -07002112 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002113 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002114 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002115 }
2116
2117 @Override
2118 public void writeToParcel(Parcel out, int flags) {
2119 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002120 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002121 }
2122
2123 public static final Parcelable.Creator<SavedState> CREATOR =
2124 new Parcelable.Creator<SavedState>() {
2125 public SavedState createFromParcel(Parcel in) {
2126 return new SavedState(in);
2127 }
2128
2129 public SavedState[] newArray(int size) {
2130 return new SavedState[size];
2131 }
2132 };
2133 }
2134
Adam Cohen7d30a372013-07-01 17:03:59 -07002135 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002136 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002137 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002138 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2139 .translationX(0)
2140 .translationY(0)
2141 .scaleX(1)
2142 .scaleY(1)
2143 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002144 anim.addListener(new AnimatorListenerAdapter() {
2145 @Override
2146 public void onAnimationEnd(Animator animation) {
2147 onPostReorderingAnimationCompleted();
2148 }
2149 });
2150 anim.start();
2151 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002152 }
2153
Sunny Goyal1d08f702015-05-04 15:50:25 -07002154 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002155 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2156 mTouchState = TOUCH_STATE_REORDERING;
2157 mIsReordering = true;
2158
Adam Cohen7d30a372013-07-01 17:03:59 -07002159 // We must invalidate to trigger a redraw to update the layers such that the drag view
2160 // is always drawn on top
2161 invalidate();
2162 }
2163
Adam Cohen091440a2015-03-18 14:16:05 -07002164 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002165 // Trigger the callback when reordering has settled
2166 --mPostReorderingPreZoomInRemainingAnimationCount;
2167 if (mPostReorderingPreZoomInRunnable != null &&
2168 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2169 mPostReorderingPreZoomInRunnable.run();
2170 mPostReorderingPreZoomInRunnable = null;
2171 }
2172 }
2173
Sunny Goyal1d08f702015-05-04 15:50:25 -07002174 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002175 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002176 }
2177
Adam Cohenf358a4b2013-07-23 16:47:31 -07002178 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002179 int dragViewIndex = indexOfChild(v);
2180
Adam Cohen327acfe2014-06-06 11:52:52 -07002181 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002182
Adam Cohen7d30a372013-07-01 17:03:59 -07002183 mTempVisiblePagesRange[0] = 0;
2184 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002185 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002186 mReorderingStarted = true;
2187
2188 // Check if we are within the reordering range
2189 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002190 dragViewIndex <= mTempVisiblePagesRange[1]) {
2191 // Find the drag view under the pointer
2192 mDragView = getChildAt(dragViewIndex);
2193 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2194 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002195 snapToPage(getPageNearestToCenterOfScreen());
2196 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002197 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002198 return true;
2199 }
2200 return false;
2201 }
2202
2203 boolean isReordering(boolean testTouchState) {
2204 boolean state = mIsReordering;
2205 if (testTouchState) {
2206 state &= (mTouchState == TOUCH_STATE_REORDERING);
2207 }
2208 return state;
2209 }
2210 void endReordering() {
2211 // For simplicity, we call endReordering sometimes even if reordering was never started.
2212 // In that case, we don't want to do anything.
2213 if (!mReorderingStarted) return;
2214 mReorderingStarted = false;
2215
2216 // If we haven't flung-to-delete the current child, then we just animate the drag view
2217 // back into position
2218 final Runnable onCompleteRunnable = new Runnable() {
2219 @Override
2220 public void run() {
2221 onEndReordering();
2222 }
2223 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002224
2225 mPostReorderingPreZoomInRunnable = new Runnable() {
2226 public void run() {
2227 onCompleteRunnable.run();
2228 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002229 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002230 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002231
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002232 mPostReorderingPreZoomInRemainingAnimationCount =
2233 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2234 // Snap to the current page
2235 snapToPage(indexOfChild(mDragView), 0);
2236 // Animate the drag view back to the front position
2237 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002238 }
2239
Winson Chung6a0f57d2011-06-29 20:10:49 -07002240 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002241 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002242 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002243 @Override
2244 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2245 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002246 info.setScrollable(getPageCount() > 1);
2247 if (getCurrentPage() < getPageCount() - 1) {
2248 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2249 }
2250 if (getCurrentPage() > 0) {
2251 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2252 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002253 info.setClassName(getClass().getName());
2254
2255 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2256 // Besides disabling the accessibility long-click, this also prevents this view from getting
2257 // accessibility focus.
2258 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002259 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002260 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2261 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002262 }
2263
2264 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002265 public void sendAccessibilityEvent(int eventType) {
2266 // Don't let the view send real scroll events.
2267 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2268 super.sendAccessibilityEvent(eventType);
2269 }
2270 }
2271
2272 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002273 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2274 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002275 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002276 }
2277
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002278 @Override
2279 public boolean performAccessibilityAction(int action, Bundle arguments) {
2280 if (super.performAccessibilityAction(action, arguments)) {
2281 return true;
2282 }
2283 switch (action) {
2284 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2285 if (getCurrentPage() < getPageCount() - 1) {
2286 scrollRight();
2287 return true;
2288 }
2289 } break;
2290 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2291 if (getCurrentPage() > 0) {
2292 scrollLeft();
2293 return true;
2294 }
2295 } break;
2296 }
2297 return false;
2298 }
2299
Adam Cohen0ffac432013-07-10 11:19:26 -07002300 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002301 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002302 getNextPage() + 1, getChildCount());
2303 }
2304
Winson Chungd11265e2011-08-30 13:37:23 -07002305 @Override
2306 public boolean onHoverEvent(android.view.MotionEvent event) {
2307 return true;
2308 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002309}