blob: 51c1dbd13fc49fd14ddedc74e1ad9864e7fa78c6 [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;
45import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080050import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070051import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Winson Chung6a0f57d2011-06-29 20:10:49 -070053import java.util.ArrayList;
54
Winson Chung321e9ee2010-08-09 13:37:56 -070055/**
56 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070057 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070058 */
Michael Jurka8b805b12012-04-18 14:23:14 -070059public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070060 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070061 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070062 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070063
Winson Chung86f77532010-08-24 11:08:22 -070064 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070065 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Vadim Tryshevfedca432015-08-19 17:55:02 -070067 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070068 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb64cb5a2011-02-15 13:53:42 -080070 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080071 // The page is moved more than halfway, automatically move to the next page on touch up.
72 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080073
Sunny Goyal8e2133b2015-05-06 13:39:07 -070074 private static final float MAX_SCROLL_PROGRESS = 1.0f;
75
Adam Cohen265b9a62011-12-07 14:37:18 -080076 // The following constants need to be scaled based on density. The scaled versions will be
77 // assigned to the corresponding member variables below.
78 private static final int FLING_THRESHOLD_VELOCITY = 500;
79 private static final int MIN_SNAP_VELOCITY = 1500;
80 private static final int MIN_FLING_VELOCITY = 250;
81
Adam Cohen21cd0022013-10-09 18:57:02 -070082 public static final int INVALID_RESTORE_PAGE = -1001;
83
Adam Cohenf358a4b2013-07-23 16:47:31 -070084 private boolean mFreeScroll = false;
85 private int mFreeScrollMinScrollX = -1;
86 private int mFreeScrollMaxScrollX = -1;
87
Adam Cohen265b9a62011-12-07 14:37:18 -080088 protected int mFlingThresholdVelocity;
89 protected int mMinFlingVelocity;
90 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070093 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070094
95 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -070096 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070097 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070098
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mNextPage = INVALID_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700100 private int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800101 protected LauncherScroller mScroller;
102 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Adam Cohen7d30a372013-07-01 17:03:59 -0700106 private float mParentDownMotionX;
107 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mDownMotionY;
110 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700111 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700112 private float mLastMotionX;
113 private float mLastMotionXRemainder;
114 private float mLastMotionY;
115 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700116 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700117
Adam Cohendbdff6b2013-09-18 19:09:15 -0700118 private boolean mCancelTap;
119
Adam Cohenedb40762013-07-18 16:45:45 -0700120 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka0142d492010-08-25 17:46:15 -0700122 protected final static int TOUCH_STATE_REST = 0;
123 protected final static int TOUCH_STATE_SCROLLING = 1;
124 protected final static int TOUCH_STATE_PREV_PAGE = 2;
125 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700126 protected final static int TOUCH_STATE_REORDERING = 4;
127
Michael Jurka0142d492010-08-25 17:46:15 -0700128 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700129 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800135 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800136 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka5f1c5092010-09-03 14:15:02 -0700138 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Winson Chung86f77532010-08-24 11:08:22 -0700142 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka0142d492010-08-25 17:46:15 -0700144 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700145 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700146
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147 protected boolean mIsPageMoving = false;
148
Adam Cohenc2d6e892014-10-16 09:49:24 -0700149 private boolean mWasInOverscroll = false;
150
Winson Chungd2be3812013-07-16 11:11:32 -0700151 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700152 @Thunk int mPageIndicatorViewId;
153 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700154 // The viewport whether the pages are to be contained (the actual view may be larger than the
155 // viewport)
156 private Rect mViewport = new Rect();
157
158 // Reordering
159 // We use the min scale to determine how much to expand the actually PagedView measured
160 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700161 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700162 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700163 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
164
Adam Cohen7d30a372013-07-01 17:03:59 -0700165 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700166 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700167 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700168 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700169 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 // This variable's scope is only for the duration of startReordering() and endReordering()
171 private boolean mReorderingStarted = false;
172 // This variable's scope is for the duration of startReordering() and after the zoomIn()
173 // animation after endReordering()
174 private boolean mIsReordering;
175 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700176 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700177 private int mPostReorderingPreZoomInRemainingAnimationCount;
178 private Runnable mPostReorderingPreZoomInRunnable;
179
Adam Cohen7d30a372013-07-01 17:03:59 -0700180 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700181 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700182 private static final float[] sTmpPoint = new float[2];
183 private static final int[] sTmpIntPoint = new int[2];
184 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800185 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700186
John Spurlock77e1f472013-09-11 10:09:51 -0400187 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700188 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400189
Sunny Goyal4d113a52015-05-27 10:05:28 -0700190 // Edge effect
191 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
192 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
193
Winson Chung86f77532010-08-24 11:08:22 -0700194 public interface PageSwitchListener {
195 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 }
197
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 public PagedView(Context context) {
199 this(context, null);
200 }
201
202 public PagedView(Context context, AttributeSet attrs) {
203 this(context, attrs, 0);
204 }
205
206 public PagedView(Context context, AttributeSet attrs, int defStyle) {
207 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700208
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 TypedArray a = context.obtainStyledAttributes(attrs,
210 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700211 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 a.recycle();
213
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700215 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700216 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 }
218
219 /**
220 * Initializes various states for this workspace.
221 */
Michael Jurka0142d492010-08-25 17:46:15 -0700222 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800223 mScroller = new LauncherScroller(getContext());
224 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700225 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226
227 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700228 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800230
Sunny Goyalcf25b522015-07-09 00:01:18 -0700231 float density = getResources().getDisplayMetrics().density;
232 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
233 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
234 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700235 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700236 setWillNotDraw(false);
237 }
238
239 protected void setEdgeGlowColor(int color) {
240 mEdgeGlowLeft.setColor(color);
241 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 }
243
Adam Cohenf9618852013-11-08 06:45:03 -0800244 protected void setDefaultInterpolator(Interpolator interpolator) {
245 mDefaultInterpolator = interpolator;
246 mScroller.setInterpolator(mDefaultInterpolator);
247 }
248
Winson Chungd2be3812013-07-16 11:11:32 -0700249 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700250 super.onAttachedToWindow();
251
Winson Chungd2be3812013-07-16 11:11:32 -0700252 // Hook up the page indicator
253 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700254 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700255 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700256 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700257 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700258
Winson Chung7819a562013-09-19 15:55:45 -0700259 ArrayList<PageIndicator.PageMarkerResources> markers =
260 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700261 for (int i = 0; i < getChildCount(); ++i) {
262 markers.add(getPageIndicatorMarker(i));
263 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700265 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700266
267 OnClickListener listener = getPageIndicatorClickListener();
268 if (listener != null) {
269 mPageIndicator.setOnClickListener(listener);
270 }
Adam Cohen53805212013-10-01 10:39:23 -0700271 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700272 }
273 }
274
Adam Cohen53805212013-10-01 10:39:23 -0700275 protected String getPageIndicatorDescription() {
276 return getCurrentPageDescription();
277 }
278
279 protected OnClickListener getPageIndicatorClickListener() {
280 return null;
281 }
282
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700283 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700284 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700285 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700286 // Unhook the page indicator
287 mPageIndicator = null;
288 }
289
Adam Cohen7d30a372013-07-01 17:03:59 -0700290 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700291 private float[] mapPointFromViewToParent(View v, float x, float y) {
292 sTmpPoint[0] = x;
293 sTmpPoint[1] = y;
294 v.getMatrix().mapPoints(sTmpPoint);
295 sTmpPoint[0] += v.getLeft();
296 sTmpPoint[1] += v.getTop();
297 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700298 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700299 private float[] mapPointFromParentToView(View v, float x, float y) {
300 sTmpPoint[0] = x - v.getLeft();
301 sTmpPoint[1] = y - v.getTop();
302 v.getMatrix().invert(sTmpInvMatrix);
303 sTmpInvMatrix.mapPoints(sTmpPoint);
304 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700305 }
306
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700307 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700308 if (mDragView != null) {
309 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
310 (mDragViewBaselineLeft - mDragView.getLeft());
311 float y = mLastMotionY - mDownMotionY;
312 mDragView.setTranslationX(x);
313 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700314
Adam Cohenf358a4b2013-07-23 16:47:31 -0700315 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
316 + x + ", " + y);
317 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700318 }
319
320 public void setMinScale(float f) {
321 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700322 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700323 requestLayout();
324 }
325
326 @Override
327 public void setScaleX(float scaleX) {
328 super.setScaleX(scaleX);
329 if (isReordering(true)) {
330 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
331 mLastMotionX = p[0];
332 mLastMotionY = p[1];
333 updateDragViewTranslationDuringDrag();
334 }
335 }
336
337 // Convenience methods to get the actual width/height of the PagedView (since it is measured
338 // to be larger to account for the minimum possible scale)
339 int getViewportWidth() {
340 return mViewport.width();
341 }
342 int getViewportHeight() {
343 return mViewport.height();
344 }
345
346 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
347 // PagedView both horizontally and vertically
348 int getViewportOffsetX() {
349 return (getMeasuredWidth() - getViewportWidth()) / 2;
350 }
351
352 int getViewportOffsetY() {
353 return (getMeasuredHeight() - getViewportHeight()) / 2;
354 }
355
Adam Cohenedb40762013-07-18 16:45:45 -0700356 PageIndicator getPageIndicator() {
357 return mPageIndicator;
358 }
Winson Chung7819a562013-09-19 15:55:45 -0700359 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
360 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700361 }
Adam Cohenedb40762013-07-18 16:45:45 -0700362
Adam Cohen674531f2013-12-13 15:07:14 -0800363 /**
364 * Add a page change listener which will be called when a page is _finished_ listening.
365 *
366 */
Winson Chung86f77532010-08-24 11:08:22 -0700367 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
368 mPageSwitchListener = pageSwitchListener;
369 if (mPageSwitchListener != null) {
370 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372 }
373
374 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700375 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
376 * that the user was on before entering free scroll mode (e.g. the home screen page they
377 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
378 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700380 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700381 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700382 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700383
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700384 /**
385 * Returns the index of page to be shown immediately afterwards.
386 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700387 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700388 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
389 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700390
Winson Chung86f77532010-08-24 11:08:22 -0700391 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 return getChildCount();
393 }
394
Sunny Goyal83a8f042015-05-19 12:52:12 -0700395 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 return getChildAt(index);
397 }
398
Adam Cohenae4f1552011-10-20 00:15:42 -0700399 protected int indexToPage(int index) {
400 return index;
401 }
402
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800404 * Updates the scroll of the current page immediately to its final scroll position. We use this
405 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
406 * the previous tab page.
407 */
408 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700409 // If the current page is invalid, just reset the scroll position to zero
410 int newX = 0;
411 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700412 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700413 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800414 scrollTo(newX, 0);
415 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700416 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800417 }
418
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700419 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700420 mScroller.abortAnimation();
421 // We need to clean up the next page here to avoid computeScrollHelper from
422 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700423 if (resetNextPage) {
424 mNextPage = INVALID_PAGE;
425 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700426 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700427
428 private void forceFinishScroller() {
429 mScroller.forceFinished(true);
430 // We need to clean up the next page here to avoid computeScrollHelper from
431 // updating current page on the pass.
432 mNextPage = INVALID_PAGE;
433 }
434
Adam Cohen6f127a62014-06-12 14:54:41 -0700435 private int validateNewPage(int newPage) {
436 int validatedPage = newPage;
437 // When in free scroll mode, we need to clamp to the free scroll page range.
438 if (mFreeScroll) {
439 getFreeScrollPageRange(mTempVisiblePagesRange);
440 validatedPage = Math.max(mTempVisiblePagesRange[0],
441 Math.min(newPage, mTempVisiblePagesRange[1]));
442 }
443 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickham86930612015-09-09 13:50:40 -0700444 validatedPage = Utilities.boundInRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700445 return validatedPage;
446 }
447
Chet Haasebc2f0822012-10-26 17:59:53 -0700448 /**
Winson Chung86f77532010-08-24 11:08:22 -0700449 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700451 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800452 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700453 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800454 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800455 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
456 // the default
457 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800458 return;
459 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700460 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700461 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700462 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700463 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800464 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 }
466
Winson Chung8c87cd82013-07-23 16:20:10 -0700467 /**
468 * The restore page will be set in place of the current page at the next (likely first)
469 * layout.
470 */
471 void setRestorePage(int restorePage) {
472 mRestorePage = restorePage;
473 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800474 int getRestorePage() {
475 return mRestorePage;
476 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700477
Adam Cohen674531f2013-12-13 15:07:14 -0800478 /**
479 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
480 * has settled.
481 */
Michael Jurka0142d492010-08-25 17:46:15 -0700482 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700483 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800484 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700485 }
Winson Chungd2be3812013-07-16 11:11:32 -0700486
Adam Cohen674531f2013-12-13 15:07:14 -0800487 updatePageIndicator();
488 }
489
490 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700491 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700492 if (mPageIndicator != null) {
493 mPageIndicator.setContentDescription(getPageIndicatorDescription());
494 if (!isReordering(false)) {
495 mPageIndicator.setActiveMarker(getNextPage());
496 }
Winson Chungd2be3812013-07-16 11:11:32 -0700497 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800499 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700500 if (!mIsPageMoving) {
501 mIsPageMoving = true;
502 onPageBeginMoving();
503 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700504 }
505
Michael Jurkace7e05f2011-02-01 22:02:35 -0800506 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700507 if (mIsPageMoving) {
508 mIsPageMoving = false;
509 onPageEndMoving();
510 }
Michael Jurka0142d492010-08-25 17:46:15 -0700511 }
512
Adam Cohen26976d92011-03-22 15:33:33 -0700513 protected boolean isPageMoving() {
514 return mIsPageMoving;
515 }
516
Michael Jurka0142d492010-08-25 17:46:15 -0700517 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700518 protected void onPageBeginMoving() {
519 }
520
521 // a method that subclasses can override to add behavior
522 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700523 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700524 }
525
Winson Chung321e9ee2010-08-09 13:37:56 -0700526 /**
Winson Chung86f77532010-08-24 11:08:22 -0700527 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700528 *
529 * @param l The listener used to respond to long clicks.
530 */
531 @Override
532 public void setOnLongClickListener(OnLongClickListener l) {
533 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700534 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700536 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 }
Adam Cohen1697b792013-09-17 19:08:21 -0700538 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700539 }
540
541 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800542 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700543 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800544 }
545
546 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700547 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700548 // In free scroll mode, we clamp the scrollX
549 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700550 // If the scroller is trying to move to a location beyond the maximum allowed
551 // in the free scroll mode, we make sure to end the scroll operation.
552 if (!mScroller.isFinished() &&
553 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
554 forceFinishScroller();
555 }
556
Adam Cohenf358a4b2013-07-23 16:47:31 -0700557 x = Math.min(x, mFreeScrollMaxScrollX);
558 x = Math.max(x, mFreeScrollMinScrollX);
559 }
560
Sunny Goyal70660032015-05-14 00:07:08 -0700561 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
562 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700563 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700564 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800565 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700566 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700567 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700568 overScroll(x - mMaxScrollX);
569 } else {
570 overScroll(x);
571 }
Adam Cohen68d73932010-11-15 10:50:58 -0800572 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700573 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700574 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800575 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700576 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700577 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700578 overScroll(x);
579 } else {
580 overScroll(x - mMaxScrollX);
581 }
Adam Cohen68d73932010-11-15 10:50:58 -0800582 }
583 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700584 if (mWasInOverscroll) {
585 overScroll(0);
586 mWasInOverscroll = false;
587 }
Adam Cohen68d73932010-11-15 10:50:58 -0800588 super.scrollTo(x, y);
589 }
590
Adam Cohen7d30a372013-07-01 17:03:59 -0700591 // Update the last motion events when scrolling
592 if (isReordering(true)) {
593 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
594 mLastMotionX = p[0];
595 mLastMotionY = p[1];
596 updateDragViewTranslationDuringDrag();
597 }
Michael Jurka0142d492010-08-25 17:46:15 -0700598 }
599
Adam Cohen53805212013-10-01 10:39:23 -0700600 private void sendScrollAccessibilityEvent() {
601 AccessibilityManager am =
602 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
603 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700604 if (mCurrentPage != getNextPage()) {
605 AccessibilityEvent ev =
606 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700607 ev.setScrollable(true);
608 ev.setScrollX(getScrollX());
609 ev.setScrollY(getScrollY());
610 ev.setMaxScrollX(mMaxScrollX);
611 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700612
Vadim Tryshevf4715972015-05-08 17:21:03 -0700613 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700614 }
Adam Cohen53805212013-10-01 10:39:23 -0700615 }
616 }
617
Michael Jurka0142d492010-08-25 17:46:15 -0700618 // we moved this functionality to a helper function so SmoothPagedView can reuse it
619 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700621 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700623 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700624 float scaleX = mFreeScroll ? getScaleX() : 1f;
625 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
626 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700627 }
Michael Jurka0142d492010-08-25 17:46:15 -0700628 invalidate();
629 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700630 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700631 sendScrollAccessibilityEvent();
632
Adam Cohen6f127a62014-06-12 14:54:41 -0700633 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700634 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700635 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700636
Adam Cohen73aa9752010-11-24 16:26:58 -0800637 // We don't want to trigger a page end moving unless the page has settled
638 // and the user has stopped scrolling
639 if (mTouchState == TOUCH_STATE_REST) {
640 pageEndMoving();
641 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700642
Adam Cohen7d30a372013-07-01 17:03:59 -0700643 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700644 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700645 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700646 if (am.isEnabled()) {
647 // Notify the user when the page changes
648 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700649 }
Michael Jurka0142d492010-08-25 17:46:15 -0700650 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 }
Michael Jurka0142d492010-08-25 17:46:15 -0700652 return false;
653 }
654
655 @Override
656 public void computeScroll() {
657 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 }
659
Adam Cohen96d30a12013-07-16 18:13:21 -0700660 public static class LayoutParams extends ViewGroup.LayoutParams {
661 public boolean isFullScreenPage = false;
662
663 /**
664 * {@inheritDoc}
665 */
666 public LayoutParams(int width, int height) {
667 super(width, height);
668 }
669
Sunny Goyal41b22c02015-05-14 15:20:48 -0700670 public LayoutParams(Context context, AttributeSet attrs) {
671 super(context, attrs);
672 }
673
Adam Cohen96d30a12013-07-16 18:13:21 -0700674 public LayoutParams(ViewGroup.LayoutParams source) {
675 super(source);
676 }
677 }
678
Sunny Goyal41b22c02015-05-14 15:20:48 -0700679 @Override
680 public LayoutParams generateLayoutParams(AttributeSet attrs) {
681 return new LayoutParams(getContext(), attrs);
682 }
683
684 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700685 protected LayoutParams generateDefaultLayoutParams() {
686 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
687 }
688
Sunny Goyal41b22c02015-05-14 15:20:48 -0700689 @Override
690 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
691 return new LayoutParams(p);
692 }
693
694 @Override
695 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
696 return p instanceof LayoutParams;
697 }
698
Adam Cohenedb40762013-07-18 16:45:45 -0700699 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700700 LayoutParams lp = generateDefaultLayoutParams();
701 lp.isFullScreenPage = true;
702 super.addView(page, 0, lp);
703 }
704
Adam Cohen410f3cd2013-09-22 12:09:32 -0700705 public int getNormalChildHeight() {
706 return mNormalChildHeight;
707 }
708
Winson Chung321e9ee2010-08-09 13:37:56 -0700709 @Override
710 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700711 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700712 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
713 return;
714 }
715
Adam Cohen7d30a372013-07-01 17:03:59 -0700716 // We measure the dimensions of the PagedView to be larger than the pages so that when we
717 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700718 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
719 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
720 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700721 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800722 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700723 // viewport, we can be at most one and a half screens offset once we scale down
724 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700725 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
726 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700727
Winson Chungc82d2622013-11-06 13:23:29 -0800728 int parentWidthSize = (int) (2f * maxSize);
729 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700730 int scaledWidthSize, scaledHeightSize;
731 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700732 scaledWidthSize = (int) (parentWidthSize / mMinScale);
733 scaledHeightSize = (int) (parentHeightSize / mMinScale);
734 } else {
735 scaledWidthSize = widthSize;
736 scaledHeightSize = heightSize;
737 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700738 mViewport.set(0, 0, widthSize, heightSize);
739
740 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
741 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
742 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 }
744
Winson Chung8aad6102012-05-11 16:27:49 -0700745 // Return early if we aren't given a proper dimension
746 if (widthSize <= 0 || heightSize <= 0) {
747 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
748 return;
749 }
750
Adam Lesinski6b879f02010-11-04 16:15:23 -0700751 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
752 * of the All apps view on XLarge displays to not take up more space then it needs. Width
753 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
754 * each page to have the same width.
755 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 final int verticalPadding = getPaddingTop() + getPaddingBottom();
757 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700758
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700759 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700761 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700762 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700763 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
764 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
765 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
766 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 final int childCount = getChildCount();
768 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700769 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700770 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100771 if (child.getVisibility() != GONE) {
772 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700773
Vladimir Marko2824b072013-10-04 16:42:17 +0100774 int childWidthMode;
775 int childHeightMode;
776 int childWidth;
777 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700778
Vladimir Marko2824b072013-10-04 16:42:17 +0100779 if (!lp.isFullScreenPage) {
780 if (lp.width == LayoutParams.WRAP_CONTENT) {
781 childWidthMode = MeasureSpec.AT_MOST;
782 } else {
783 childWidthMode = MeasureSpec.EXACTLY;
784 }
785
786 if (lp.height == LayoutParams.WRAP_CONTENT) {
787 childHeightMode = MeasureSpec.AT_MOST;
788 } else {
789 childHeightMode = MeasureSpec.EXACTLY;
790 }
791
Adam Cohen3b185e22013-10-29 14:45:58 -0700792 childWidth = getViewportWidth() - horizontalPadding
793 - mInsets.left - mInsets.right;
794 childHeight = getViewportHeight() - verticalPadding
795 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100796 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700797 } else {
798 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700799 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100800
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800801 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700802 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700803 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700804 if (referenceChildWidth == 0) {
805 referenceChildWidth = childWidth;
806 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700807
Vladimir Marko2824b072013-10-04 16:42:17 +0100808 final int childWidthMeasureSpec =
809 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
810 final int childHeightMeasureSpec =
811 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
812 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700813 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700814 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700815 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800816 }
817
Sunny Goyalcf25b522015-07-09 00:01:18 -0700818 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700820 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700821 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700822 return;
823 }
824
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700827
Adam Cohen7d30a372013-07-01 17:03:59 -0700828 int offsetX = getViewportOffsetX();
829 int offsetY = getViewportOffsetY();
830
831 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700832 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700833
Sunny Goyal70660032015-05-14 00:07:08 -0700834 final int startIndex = mIsRtl ? childCount - 1 : 0;
835 final int endIndex = mIsRtl ? -1 : childCount;
836 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700837
Adam Cohen7d30a372013-07-01 17:03:59 -0700838 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700839
Adam Cohen3b185e22013-10-29 14:45:58 -0700840 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000841 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700842
843 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700844 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
845 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700846 }
847
Adam Cohen0ffac432013-07-10 11:19:26 -0700848 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700849 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700851 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100852 int childTop;
853 if (lp.isFullScreenPage) {
854 childTop = offsetY;
855 } else {
856 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700857 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100858 }
859
Adam Cohen96d30a12013-07-16 18:13:21 -0700860 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700861 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800862
Winson Chung785d2eb2011-04-14 16:08:02 -0700863 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700864 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800865 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700866
Adam Cohen3b185e22013-10-29 14:45:58 -0700867 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
868 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000869
870 int pageGap = mPageSpacing;
871 int next = i + delta;
872 if (next != endIndex) {
873 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
874 } else {
875 nextLp = null;
876 }
877
878 // Prevent full screen pages from showing in the viewport
879 // when they are not the current page.
880 if (lp.isFullScreenPage) {
881 pageGap = getPaddingLeft();
882 } else if (nextLp != null && nextLp.isFullScreenPage) {
883 pageGap = getPaddingRight();
884 }
885
Sunny Goyala1fbd842015-05-20 13:40:27 -0700886 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 }
888 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700889
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700890 final LayoutTransition transition = getLayoutTransition();
891 // If the transition is running defer updating max scroll, as some empty pages could
892 // still be present, and a max scroll change could cause sudden jumps in scroll.
893 if (transition != null && transition.isRunning()) {
894 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
895
896 @Override
897 public void startTransition(LayoutTransition transition, ViewGroup container,
898 View view, int transitionType) { }
899
900 @Override
901 public void endTransition(LayoutTransition transition, ViewGroup container,
902 View view, int transitionType) {
903 // Wait until all transitions are complete.
904 if (!transition.isRunning()) {
905 transition.removeTransitionListener(this);
906 updateMaxScrollX();
907 }
908 }
909 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700910 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700911 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700912 }
913
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700914 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
915 updateCurrentPageScroll();
916 mFirstLayout = false;
917 }
918
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700919 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700920 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700921 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700922 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700923 } else {
924 setCurrentPage(getNextPage());
925 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700926 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700927 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700928
929 if (isReordering(true)) {
930 updateDragViewTranslationDuringDrag();
931 }
Winson Chunge3193b92010-09-10 11:44:42 -0700932 }
933
Sunny Goyala1fbd842015-05-20 13:40:27 -0700934 protected int getChildGap() {
935 return 0;
936 }
937
Sunny Goyal316490e2015-06-02 09:38:28 -0700938 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700939 int childCount = getChildCount();
940 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700941 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700942 mMaxScrollX = getScrollForPage(index);
943 } else {
944 mMaxScrollX = 0;
945 }
946 }
947
Adam Cohen3b185e22013-10-29 14:45:58 -0700948 public void setPageSpacing(int pageSpacing) {
949 mPageSpacing = pageSpacing;
950 requestLayout();
951 }
952
Sunny Goyal4d113a52015-05-27 10:05:28 -0700953 /**
954 * Called when the center screen changes during scrolling.
955 */
956 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700957
Winson Chunge3193b92010-09-10 11:44:42 -0700958 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700959 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700960 // Update the page indicator, we don't update the page indicator as we
961 // add/remove pages
962 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700963 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700964 mPageIndicator.addMarker(pageIndex,
965 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700966 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700967 }
968
Adam Cohen2591f6a2011-10-25 14:36:40 -0700969 // This ensures that when children are added, they get the correct transforms / alphas
970 // in accordance with any scroll effects.
971 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700972 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700973 invalidate();
974 }
975
Michael Jurka8b805b12012-04-18 14:23:14 -0700976 @Override
977 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700978 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700979 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700981 }
982
Winson Chungd2be3812013-07-16 11:11:32 -0700983 private void removeMarkerForView(int index) {
984 // Update the page indicator, we don't update the page indicator as we
985 // add/remove pages
986 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700987 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700988 }
989 }
990
991 @Override
992 public void removeView(View v) {
993 // XXX: We should find a better way to hook into this before the view
994 // gets removed form its parent...
995 removeMarkerForView(indexOfChild(v));
996 super.removeView(v);
997 }
998 @Override
999 public void removeViewInLayout(View v) {
1000 // XXX: We should find a better way to hook into this before the view
1001 // gets removed form its parent...
1002 removeMarkerForView(indexOfChild(v));
1003 super.removeViewInLayout(v);
1004 }
1005 @Override
1006 public void removeViewAt(int index) {
1007 // XXX: We should find a better way to hook into this before the view
1008 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001009 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001010 super.removeViewAt(index);
1011 }
1012 @Override
1013 public void removeAllViewsInLayout() {
1014 // Update the page indicator, we don't update the page indicator as we
1015 // add/remove pages
1016 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001017 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001018 }
1019
1020 super.removeAllViewsInLayout();
1021 }
1022
Adam Cohen73894962011-10-31 13:17:17 -07001023 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001024 if (index < 0 || index > getChildCount() - 1) return 0;
1025
Adam Cohenf698c6e2013-07-17 18:44:25 -07001026 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001027
Adam Cohenf698c6e2013-07-17 18:44:25 -07001028 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001029 }
1030
Adam Cohen6f127a62014-06-12 14:54:41 -07001031 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001032 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001033 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001034 }
1035
Michael Jurkadde558b2011-11-09 22:09:06 -08001036 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001037 final int pageCount = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001038 range[0] = -1;
1039 range[1] = -1;
1040
Michael Jurkac4fb9172010-09-02 17:19:20 -07001041 if (pageCount > 0) {
Tony Wickham29d853c2015-09-08 10:35:56 -07001042 int lastVisiblePageIndex = 0;
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001043 final int visibleLeft = -getLeft();
1044 final int visibleRight = visibleLeft + getViewportWidth();
Adam Cohen7d30a372013-07-01 17:03:59 -07001045
Tony Wickham29d853c2015-09-08 10:35:56 -07001046 for (int currPageIndex = 0; currPageIndex < pageCount; currPageIndex++) {
1047 View currPage = getPageAt(currPageIndex);
Winson Chungc9ca2982013-07-19 12:07:38 -07001048
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001049 // Verify if the page bounds are within the visible range.
1050 sTmpRectF.left = 0;
1051 sTmpRectF.right = currPage.getMeasuredWidth();
1052 currPage.getMatrix().mapRect(sTmpRectF);
1053 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
1054 getMatrix().mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001055
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001056 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001057 if (range[0] == -1) {
1058 continue;
1059 } else {
1060 break;
1061 }
1062 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001063 if (range[0] < 0) {
Tony Wickham29d853c2015-09-08 10:35:56 -07001064 range[0] = currPageIndex;
Winson Chungc9ca2982013-07-19 12:07:38 -07001065 }
Tony Wickham29d853c2015-09-08 10:35:56 -07001066 lastVisiblePageIndex = currPageIndex;
Winson Chungc9ca2982013-07-19 12:07:38 -07001067 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001068
Tony Wickham29d853c2015-09-08 10:35:56 -07001069 range[1] = lastVisiblePageIndex;
Michael Jurkadde558b2011-11-09 22:09:06 -08001070 } else {
1071 range[0] = -1;
1072 range[1] = -1;
1073 }
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();
Adam Cohenf8d28232011-02-01 21:47:00 -08001435 }
1436 }
1437
1438 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001439 // Try canceling the long press. It could also have been scheduled
1440 // by a distant descendant, so use the mAllowLongPress flag to block
1441 // everything
1442 final View currentPage = getPageAt(mCurrentPage);
1443 if (currentPage != null) {
1444 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 }
1446 }
1447
Adam Cohenb5ba0972011-09-07 18:02:31 -07001448 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001449 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001450
Adam Cohenedb40762013-07-18 16:45:45 -07001451 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001452 int count = getChildCount();
1453
1454 final int totalDistance;
1455
1456 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001457 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001458 adjacentPage = page - 1;
1459 }
1460
1461 if (adjacentPage < 0 || adjacentPage > count - 1) {
1462 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1463 } else {
1464 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1465 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001466
1467 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001468 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1469 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001470 return scrollProgress;
1471 }
1472
Adam Cohenedb40762013-07-18 16:45:45 -07001473 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001474 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001475 return 0;
1476 } else {
1477 return mPageScrolls[index];
1478 }
1479 }
1480
Adam Cohen564a2e72013-10-09 14:47:32 -07001481 // While layout transitions are occurring, a child's position may stray from its baseline
1482 // position. This method returns the magnitude of this stray at any given time.
1483 public int getLayoutTransitionOffsetForPage(int index) {
1484 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1485 return 0;
1486 } else {
1487 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001488
1489 int scrollOffset = 0;
1490 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1491 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001492 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001493 }
1494
Adam Cohen564a2e72013-10-09 14:47:32 -07001495 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1496 return (int) (child.getX() - baselineX);
1497 }
1498 }
1499
Adam Cohenb5ba0972011-09-07 18:02:31 -07001500 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001501 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001502 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001503 if (f < 0) {
1504 mEdgeGlowLeft.onPull(-f);
1505 } else if (f > 0) {
1506 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001507 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001508 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001509 }
1510 invalidate();
1511 }
1512
Adam Cohenb5ba0972011-09-07 18:02:31 -07001513 protected void overScroll(float amount) {
1514 dampedOverScroll(amount);
1515 }
1516
Winson Chungdc61c4d2015-04-20 18:26:57 -07001517 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001518 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001519 }
1520
Winson Chungdc61c4d2015-04-20 18:26:57 -07001521 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001522 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001523 }
1524
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001525 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001526 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001527 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001528 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1529 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1530 } else {
1531 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1532 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1533 }
1534 }
1535
Adam Cohenf9618852013-11-08 06:45:03 -08001536 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001537 mFreeScroll = freeScroll;
1538
Adam Cohenf9618852013-11-08 06:45:03 -08001539 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001540 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001541 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001542 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1543 setCurrentPage(mTempVisiblePagesRange[0]);
1544 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1545 setCurrentPage(mTempVisiblePagesRange[1]);
1546 }
1547 }
1548
1549 setEnableOverscroll(!freeScroll);
1550 }
1551
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001552 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001553 mAllowOverScroll = enable;
1554 }
1555
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001556 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001557 if (mDragView != null) {
1558 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1559 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001560 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001561 int minDistance = Integer.MAX_VALUE;
1562 int minIndex = indexOfChild(mDragView);
1563 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1564 View page = getPageAt(i);
1565 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1566 int d = Math.abs(dragX - pageX);
1567 if (d < minDistance) {
1568 minIndex = i;
1569 minDistance = d;
1570 }
1571 }
1572 return minIndex;
1573 }
1574 return -1;
1575 }
1576
Winson Chung321e9ee2010-08-09 13:37:56 -07001577 @Override
1578 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001579 super.onTouchEvent(ev);
1580
Winson Chung45e1d6e2010-11-09 17:19:49 -08001581 // Skip touch handling if there are no pages to swipe
1582 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1583
Michael Jurkab8f06722010-10-10 15:58:46 -07001584 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001585
1586 final int action = ev.getAction();
1587
1588 switch (action & MotionEvent.ACTION_MASK) {
1589 case MotionEvent.ACTION_DOWN:
1590 /*
1591 * If being flinged and user touches, stop the fling. isFinished
1592 * will be false if being flinged.
1593 */
1594 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001595 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001596 }
1597
1598 // Remember where the motion event started
1599 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001600 mDownMotionY = mLastMotionY = ev.getY();
1601 mDownScrollX = getScrollX();
1602 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1603 mParentDownMotionX = p[0];
1604 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001605 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001606 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001608
Michael Jurka0142d492010-08-25 17:46:15 -07001609 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001610 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001611 pageBeginMoving();
1612 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001613 break;
1614
1615 case MotionEvent.ACTION_MOVE:
1616 if (mTouchState == TOUCH_STATE_SCROLLING) {
1617 // Scroll to follow the motion event
1618 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001619
1620 if (pointerIndex == -1) return true;
1621
Winson Chung321e9ee2010-08-09 13:37:56 -07001622 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001623 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001624
Adam Cohenaefd4e12011-02-14 16:39:38 -08001625 mTotalMotionX += Math.abs(deltaX);
1626
Winson Chungc0844aa2011-02-02 15:25:58 -08001627 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1628 // keep the remainder because we are actually testing if we've moved from the last
1629 // scrolled position (which is discrete).
1630 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001631 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001632 mLastMotionX = x;
1633 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 } else {
1635 awakenScrollBars();
1636 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001637 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1638 // Update the last motion position
1639 mLastMotionX = ev.getX();
1640 mLastMotionY = ev.getY();
1641
1642 // Update the parent down so that our zoom animations take this new movement into
1643 // account
1644 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1645 mParentDownMotionX = pt[0];
1646 mParentDownMotionY = pt[1];
1647 updateDragViewTranslationDuringDrag();
1648
1649 // Find the closest page to the touch point
1650 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001651
Adam Cohen7d30a372013-07-01 17:03:59 -07001652 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1653 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1654 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1655 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1656
Adam Cohenf358a4b2013-07-23 16:47:31 -07001657 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001658 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001659 mTempVisiblePagesRange[0] = 0;
1660 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001661 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001662 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1663 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1664 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1665 mSidePageHoverIndex = pageUnderPointIndex;
1666 mSidePageHoverRunnable = new Runnable() {
1667 @Override
1668 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001669 // Setup the scroll to the correct page before we swap the views
1670 snapToPage(pageUnderPointIndex);
1671
1672 // For each of the pages between the paged view and the drag view,
1673 // animate them from the previous position to the new position in
1674 // the layout (as a result of the drag view moving in the layout)
1675 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1676 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1677 dragViewIndex + 1 : pageUnderPointIndex;
1678 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1679 dragViewIndex - 1 : pageUnderPointIndex;
1680 for (int i = lowerIndex; i <= upperIndex; ++i) {
1681 View v = getChildAt(i);
1682 // dragViewIndex < pageUnderPointIndex, so after we remove the
1683 // drag view all subsequent views to pageUnderPointIndex will
1684 // shift down.
1685 int oldX = getViewportOffsetX() + getChildOffset(i);
1686 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1687
1688 // Animate the view translation from its old position to its new
1689 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001690 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001691 if (anim != null) {
1692 anim.cancel();
1693 }
1694
1695 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001696 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001697 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001698 anim.start();
1699 v.setTag(anim);
1700 }
1701
1702 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001703 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001704 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001705 if (mPageIndicator != null) {
1706 mPageIndicator.setActiveMarker(getNextPage());
1707 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 }
1709 };
1710 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1711 }
1712 } else {
1713 removeCallbacks(mSidePageHoverRunnable);
1714 mSidePageHoverIndex = -1;
1715 }
Adam Cohen564976a2010-10-13 18:52:07 -07001716 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001717 determineScrollingStart(ev);
1718 }
1719 break;
1720
1721 case MotionEvent.ACTION_UP:
1722 if (mTouchState == TOUCH_STATE_SCROLLING) {
1723 final int activePointerId = mActivePointerId;
1724 final int pointerIndex = ev.findPointerIndex(activePointerId);
1725 final float x = ev.getX(pointerIndex);
1726 final VelocityTracker velocityTracker = mVelocityTracker;
1727 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1728 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001729 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001730 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001731 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1732 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001733
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001734 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1735
Adam Cohen00481b32011-11-18 12:03:48 -08001736 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001737 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001738
Adam Cohenf358a4b2013-07-23 16:47:31 -07001739 if (!mFreeScroll) {
1740 // In the case that the page is moved far to one direction and then is flung
1741 // in the opposite direction, we use a threshold to determine whether we should
1742 // just return to the starting page, or if we should skip one further.
1743 boolean returnToOriginalPage = false;
1744 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1745 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1746 returnToOriginalPage = true;
1747 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001748
Adam Cohenf358a4b2013-07-23 16:47:31 -07001749 int finalPage;
1750 // We give flings precedence over large moves, which is why we short-circuit our
1751 // test for a large move if a fling has been registered. That is, a large
1752 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001753 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1754 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001755 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1756 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1757 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1758 snapToPageWithVelocity(finalPage, velocityX);
1759 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1760 (isFling && isVelocityXLeft)) &&
1761 mCurrentPage < getChildCount() - 1) {
1762 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1763 snapToPageWithVelocity(finalPage, velocityX);
1764 } else {
1765 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001766 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001767 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001768 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001769 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001770 }
1771
1772 float scaleX = getScaleX();
1773 int vX = (int) (-velocityX * scaleX);
1774 int initialScrollX = (int) (getScrollX() * scaleX);
1775
Adam Cohenf9618852013-11-08 06:45:03 -08001776 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001777 mScroller.fling(initialScrollX,
1778 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1779 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001780 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001781 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001782 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1783 // at this point we have not moved beyond the touch slop
1784 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1785 // we can just page
1786 int nextPage = Math.max(0, mCurrentPage - 1);
1787 if (nextPage != mCurrentPage) {
1788 snapToPage(nextPage);
1789 } else {
1790 snapToDestination();
1791 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001792 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 // at this point we have not moved beyond the touch slop
1794 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1795 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001796 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1797 if (nextPage != mCurrentPage) {
1798 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001799 } else {
1800 snapToDestination();
1801 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001802 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1803 // Update the last motion position
1804 mLastMotionX = ev.getX();
1805 mLastMotionY = ev.getY();
1806
1807 // Update the parent down so that our zoom animations take this new movement into
1808 // account
1809 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1810 mParentDownMotionX = pt[0];
1811 mParentDownMotionY = pt[1];
1812 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001813 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001814 if (!mCancelTap) {
1815 onUnhandledTap(ev);
1816 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001817 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001818
1819 // Remove the callback to wait for the side page hover timeout
1820 removeCallbacks(mSidePageHoverRunnable);
1821 // End any intermediate reordering states
1822 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001823 break;
1824
1825 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001826 if (mTouchState == TOUCH_STATE_SCROLLING) {
1827 snapToDestination();
1828 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001830 break;
1831
1832 case MotionEvent.ACTION_POINTER_UP:
1833 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001834 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001835 break;
1836 }
1837
1838 return true;
1839 }
1840
Adam Cohen7d30a372013-07-01 17:03:59 -07001841 private void resetTouchState() {
1842 releaseVelocityTracker();
1843 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001844 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001845 mTouchState = TOUCH_STATE_REST;
1846 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001847 mEdgeGlowLeft.onRelease();
1848 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001849 }
1850
Adam Cohenc2d6e892014-10-16 09:49:24 -07001851 /**
1852 * Triggered by scrolling via touch
1853 */
1854 protected void onScrollInteractionBegin() {
1855 }
1856
1857 protected void onScrollInteractionEnd() {
1858 }
1859
Adam Cohen1697b792013-09-17 19:08:21 -07001860 protected void onUnhandledTap(MotionEvent ev) {
1861 ((Launcher) getContext()).onClick(this);
1862 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001863
Winson Chung185d7162011-02-28 13:47:29 -08001864 @Override
1865 public boolean onGenericMotionEvent(MotionEvent event) {
1866 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1867 switch (event.getAction()) {
1868 case MotionEvent.ACTION_SCROLL: {
1869 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1870 final float vscroll;
1871 final float hscroll;
1872 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1873 vscroll = 0;
1874 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1875 } else {
1876 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1877 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1878 }
1879 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001880 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001881 : (hscroll > 0 || vscroll > 0);
1882 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001883 scrollRight();
1884 } else {
1885 scrollLeft();
1886 }
1887 return true;
1888 }
1889 }
1890 }
1891 }
1892 return super.onGenericMotionEvent(event);
1893 }
1894
Michael Jurkab8f06722010-10-10 15:58:46 -07001895 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1896 if (mVelocityTracker == null) {
1897 mVelocityTracker = VelocityTracker.obtain();
1898 }
1899 mVelocityTracker.addMovement(ev);
1900 }
1901
1902 private void releaseVelocityTracker() {
1903 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001904 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001905 mVelocityTracker.recycle();
1906 mVelocityTracker = null;
1907 }
1908 }
1909
Winson Chung321e9ee2010-08-09 13:37:56 -07001910 private void onSecondaryPointerUp(MotionEvent ev) {
1911 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1912 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1913 final int pointerId = ev.getPointerId(pointerIndex);
1914 if (pointerId == mActivePointerId) {
1915 // This was our active pointer going up. Choose a new
1916 // active pointer and adjust accordingly.
1917 // TODO: Make this decision more intelligent.
1918 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1919 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1920 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001921 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001922 mActivePointerId = ev.getPointerId(newPointerIndex);
1923 if (mVelocityTracker != null) {
1924 mVelocityTracker.clear();
1925 }
1926 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001927 }
1928
Winson Chung321e9ee2010-08-09 13:37:56 -07001929 @Override
1930 public void requestChildFocus(View child, View focused) {
1931 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001932 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001933 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001934 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 }
1936 }
1937
Adam Cohend19d3ca2010-09-15 14:43:42 -07001938 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001939 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001940 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001941 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 final int childCount = getChildCount();
1943 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001944 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001945 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001946 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001947 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1949 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1950 minDistanceFromScreenCenter = distanceFromScreenCenter;
1951 minDistanceFromScreenCenterIndex = i;
1952 }
1953 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001954 return minDistanceFromScreenCenterIndex;
1955 }
1956
1957 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001958 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 }
1960
Adam Cohene0f66b52010-11-23 15:06:07 -08001961 private static class ScrollInterpolator implements Interpolator {
1962 public ScrollInterpolator() {
1963 }
1964
1965 public float getInterpolation(float t) {
1966 t -= 1.0f;
1967 return t*t*t*t*t + 1;
1968 }
1969 }
1970
1971 // We want the duration of the page snap animation to be influenced by the distance that
1972 // the screen has to travel, however, we don't want this duration to be effected in a
1973 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1974 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001975 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001976 f -= 0.5f; // center the values about 0.
1977 f *= 0.3f * Math.PI / 2.0f;
1978 return (float) Math.sin(f);
1979 }
1980
Michael Jurka0142d492010-08-25 17:46:15 -07001981 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001982 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001983 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001984
Adam Cohenedb40762013-07-18 16:45:45 -07001985 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001986 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001987 int duration = 0;
1988
Sunny Goyal4d113a52015-05-27 10:05:28 -07001989 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001990 // If the velocity is low enough, then treat this more as an automatic page advance
1991 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001992 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001993 return;
1994 }
1995
1996 // Here we compute a "distance" that will be used in the computation of the overall
1997 // snap duration. This is a function of the actual distance that needs to be traveled;
1998 // we keep this value close to half screen size in order to reduce the variance in snap
1999 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002000 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002001 float distance = halfScreenSize + halfScreenSize *
2002 distanceInfluenceForSnapDuration(distanceRatio);
2003
2004 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002005 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002006
2007 // we want the page's snap velocity to approximately match the velocity at which the
2008 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002009 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2010 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002011
2012 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002013 }
2014
Sunny Goyal1740d902015-05-27 11:14:01 -07002015 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002016 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002017 }
2018
Adam Cohen7d30a372013-07-01 17:03:59 -07002019 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002020 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002021 }
2022
Michael Jurka0142d492010-08-25 17:46:15 -07002023 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002024 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 }
2026
Adam Cohenf9618852013-11-08 06:45:03 -08002027 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2028 snapToPage(whichPage, duration, false, interpolator);
2029 }
2030
2031 protected void snapToPage(int whichPage, int duration, boolean immediate,
2032 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002033 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002034
Adam Cohenedb40762013-07-18 16:45:45 -07002035 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002036 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002037 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002038 }
2039
2040 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002041 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002042 }
Michael Jurka0142d492010-08-25 17:46:15 -07002043
Adam Cohenf9618852013-11-08 06:45:03 -08002044 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2045 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002046 whichPage = validateNewPage(whichPage);
2047
Adam Cohen7d30a372013-07-01 17:03:59 -07002048 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002049
2050 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002051 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002052 if (immediate) {
2053 duration = 0;
2054 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002055 duration = Math.abs(delta);
2056 }
2057
Adam Cohenf358a4b2013-07-23 16:47:31 -07002058 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002059 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002060 }
Adam Cohenf9618852013-11-08 06:45:03 -08002061
2062 if (interpolator != null) {
2063 mScroller.setInterpolator(interpolator);
2064 } else {
2065 mScroller.setInterpolator(mDefaultInterpolator);
2066 }
2067
Sunny Goyal4d113a52015-05-27 10:05:28 -07002068 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002069
Adam Cohen674531f2013-12-13 15:07:14 -08002070 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002071
2072 // Trigger a compute() to finish switching pages if necessary
2073 if (immediate) {
2074 computeScroll();
2075 }
2076
2077 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002078 invalidate();
2079 }
2080
Winson Chung321e9ee2010-08-09 13:37:56 -07002081 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002082 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002083 }
2084
2085 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002086 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002087 }
2088
Adam Cohendbdff6b2013-09-18 19:09:15 -07002089 @Override
2090 public boolean performLongClick() {
2091 mCancelTap = true;
2092 return super.performLongClick();
2093 }
2094
Winson Chung321e9ee2010-08-09 13:37:56 -07002095 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002096 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002097
2098 SavedState(Parcelable superState) {
2099 super(superState);
2100 }
2101
Adam Cohen091440a2015-03-18 14:16:05 -07002102 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002103 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002104 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002105 }
2106
2107 @Override
2108 public void writeToParcel(Parcel out, int flags) {
2109 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002110 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002111 }
2112
2113 public static final Parcelable.Creator<SavedState> CREATOR =
2114 new Parcelable.Creator<SavedState>() {
2115 public SavedState createFromParcel(Parcel in) {
2116 return new SavedState(in);
2117 }
2118
2119 public SavedState[] newArray(int size) {
2120 return new SavedState[size];
2121 }
2122 };
2123 }
2124
Adam Cohen7d30a372013-07-01 17:03:59 -07002125 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002126 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002127 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002128 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2129 .translationX(0)
2130 .translationY(0)
2131 .scaleX(1)
2132 .scaleY(1)
2133 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002134 anim.addListener(new AnimatorListenerAdapter() {
2135 @Override
2136 public void onAnimationEnd(Animator animation) {
2137 onPostReorderingAnimationCompleted();
2138 }
2139 });
2140 anim.start();
2141 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002142 }
2143
Sunny Goyal1d08f702015-05-04 15:50:25 -07002144 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002145 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2146 mTouchState = TOUCH_STATE_REORDERING;
2147 mIsReordering = true;
2148
Adam Cohen7d30a372013-07-01 17:03:59 -07002149 // We must invalidate to trigger a redraw to update the layers such that the drag view
2150 // is always drawn on top
2151 invalidate();
2152 }
2153
Adam Cohen091440a2015-03-18 14:16:05 -07002154 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002155 // Trigger the callback when reordering has settled
2156 --mPostReorderingPreZoomInRemainingAnimationCount;
2157 if (mPostReorderingPreZoomInRunnable != null &&
2158 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2159 mPostReorderingPreZoomInRunnable.run();
2160 mPostReorderingPreZoomInRunnable = null;
2161 }
2162 }
2163
Sunny Goyal1d08f702015-05-04 15:50:25 -07002164 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002165 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002166 }
2167
Adam Cohenf358a4b2013-07-23 16:47:31 -07002168 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002169 int dragViewIndex = indexOfChild(v);
2170
Adam Cohen327acfe2014-06-06 11:52:52 -07002171 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002172
Adam Cohen7d30a372013-07-01 17:03:59 -07002173 mTempVisiblePagesRange[0] = 0;
2174 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002175 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002176 mReorderingStarted = true;
2177
2178 // Check if we are within the reordering range
2179 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002180 dragViewIndex <= mTempVisiblePagesRange[1]) {
2181 // Find the drag view under the pointer
2182 mDragView = getChildAt(dragViewIndex);
2183 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2184 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002185 snapToPage(getPageNearestToCenterOfScreen());
2186 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002187 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002188 return true;
2189 }
2190 return false;
2191 }
2192
2193 boolean isReordering(boolean testTouchState) {
2194 boolean state = mIsReordering;
2195 if (testTouchState) {
2196 state &= (mTouchState == TOUCH_STATE_REORDERING);
2197 }
2198 return state;
2199 }
2200 void endReordering() {
2201 // For simplicity, we call endReordering sometimes even if reordering was never started.
2202 // In that case, we don't want to do anything.
2203 if (!mReorderingStarted) return;
2204 mReorderingStarted = false;
2205
2206 // If we haven't flung-to-delete the current child, then we just animate the drag view
2207 // back into position
2208 final Runnable onCompleteRunnable = new Runnable() {
2209 @Override
2210 public void run() {
2211 onEndReordering();
2212 }
2213 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002214
2215 mPostReorderingPreZoomInRunnable = new Runnable() {
2216 public void run() {
2217 onCompleteRunnable.run();
2218 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002219 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002220 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002221
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002222 mPostReorderingPreZoomInRemainingAnimationCount =
2223 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2224 // Snap to the current page
2225 snapToPage(indexOfChild(mDragView), 0);
2226 // Animate the drag view back to the front position
2227 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002228 }
2229
Winson Chung6a0f57d2011-06-29 20:10:49 -07002230 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002231 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002232 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002233 @Override
2234 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2235 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002236 info.setScrollable(getPageCount() > 1);
2237 if (getCurrentPage() < getPageCount() - 1) {
2238 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2239 }
2240 if (getCurrentPage() > 0) {
2241 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2242 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002243 info.setClassName(getClass().getName());
2244
2245 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2246 // Besides disabling the accessibility long-click, this also prevents this view from getting
2247 // accessibility focus.
2248 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002249 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002250 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2251 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002252 }
2253
2254 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002255 public void sendAccessibilityEvent(int eventType) {
2256 // Don't let the view send real scroll events.
2257 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2258 super.sendAccessibilityEvent(eventType);
2259 }
2260 }
2261
2262 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002263 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2264 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002265 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002266 }
2267
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002268 @Override
2269 public boolean performAccessibilityAction(int action, Bundle arguments) {
2270 if (super.performAccessibilityAction(action, arguments)) {
2271 return true;
2272 }
2273 switch (action) {
2274 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2275 if (getCurrentPage() < getPageCount() - 1) {
2276 scrollRight();
2277 return true;
2278 }
2279 } break;
2280 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2281 if (getCurrentPage() > 0) {
2282 scrollLeft();
2283 return true;
2284 }
2285 } break;
2286 }
2287 return false;
2288 }
2289
Adam Cohen0ffac432013-07-10 11:19:26 -07002290 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002291 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002292 getNextPage() + 1, getChildCount());
2293 }
2294
Winson Chungd11265e2011-08-30 13:37:23 -07002295 @Override
2296 public boolean onHoverEvent(android.view.MotionEvent event) {
2297 return true;
2298 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002299}