blob: 8d5d8dd4d5133868b98bfffb8c0380cb689bbdb5 [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;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070024import android.animation.ValueAnimator;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ValueAnimator.AnimatorUpdateListener;
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;
30import android.graphics.PointF;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070047import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohen7d30a372013-07-01 17:03:59 -070049import android.view.animation.AnimationUtils;
50import android.view.animation.DecelerateInterpolator;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Adam Cohen7d30a372013-07-01 17:03:59 -070052import android.view.animation.LinearInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chungc58497e2013-09-03 17:48:37 -070056interface Page {
57 public int getPageChildCount();
58 public View getChildOnPageAt(int i);
59 public void removeAllViewsOnPage();
60 public void removeViewOnPageAt(int i);
61 public int indexOfChildOnPage(View v);
62}
63
Winson Chung321e9ee2010-08-09 13:37:56 -070064/**
65 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070066 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070067 */
Michael Jurka8b805b12012-04-18 14:23:14 -070068public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070069 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070070 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070071 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Winson Chung86f77532010-08-24 11:08:22 -070073 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070074 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070075
Adam Cohen7d30a372013-07-01 17:03:59 -070076 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070077 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Adam Cohenb5ba0972011-09-07 18:02:31 -070080 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070081 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Adam Cohen265b9a62011-12-07 14:37:18 -080087 // The following constants need to be scaled based on density. The scaled versions will be
88 // assigned to the corresponding member variables below.
89 private static final int FLING_THRESHOLD_VELOCITY = 500;
90 private static final int MIN_SNAP_VELOCITY = 1500;
91 private static final int MIN_FLING_VELOCITY = 250;
92
Adam Cohen7d30a372013-07-01 17:03:59 -070093 // We are disabling touch interaction of the widget region for factory ROM.
94 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070095 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070096 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070097
Adam Cohen21cd0022013-10-09 18:57:02 -070098 public static final int INVALID_RESTORE_PAGE = -1001;
99
Adam Cohenf358a4b2013-07-23 16:47:31 -0700100 private boolean mFreeScroll = false;
101 private int mFreeScrollMinScrollX = -1;
102 private int mFreeScrollMaxScrollX = -1;
103
Winson Chung8aad6102012-05-11 16:27:49 -0700104 static final int AUTOMATIC_PAGE_SPACING = -1;
105
Adam Cohen265b9a62011-12-07 14:37:18 -0800106 protected int mFlingThresholdVelocity;
107 protected int mMinFlingVelocity;
108 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700109
Adam Cohenb5ba0972011-09-07 18:02:31 -0700110 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700111 protected float mSmoothingTime;
112 protected float mTouchX;
113
114 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700115 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700116
117 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700118 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700119 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700120
Michael Jurka0142d492010-08-25 17:46:15 -0700121 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800122 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800123 protected LauncherScroller mScroller;
124 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700126 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Adam Cohen7d30a372013-07-01 17:03:59 -0700128 private float mParentDownMotionX;
129 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 private float mDownMotionY;
132 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700133 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800135 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800137 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700138 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700139
Adam Cohendbdff6b2013-09-18 19:09:15 -0700140 private boolean mCancelTap;
141
Adam Cohenedb40762013-07-18 16:45:45 -0700142 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka0142d492010-08-25 17:46:15 -0700144 protected final static int TOUCH_STATE_REST = 0;
145 protected final static int TOUCH_STATE_SCROLLING = 1;
146 protected final static int TOUCH_STATE_PREV_PAGE = 2;
147 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700148 protected final static int TOUCH_STATE_REORDERING = 4;
149
Adam Cohene45440e2010-10-14 18:33:38 -0700150 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700153 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800154
Winson Chung321e9ee2010-08-09 13:37:56 -0700155
Michael Jurka0142d492010-08-25 17:46:15 -0700156 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Michael Jurka7426c422010-11-11 15:23:47 -0800158 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 private int mPagingTouchSlop;
160 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700161 protected int mPageLayoutPaddingTop;
162 protected int mPageLayoutPaddingBottom;
163 protected int mPageLayoutPaddingLeft;
164 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700165 protected int mPageLayoutWidthGap;
166 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700167 protected int mCellCountX = 0;
168 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700169 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800170 protected boolean mAllowOverScroll = true;
171 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800172 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700173 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700174
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800176 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
177 // the screens from continuing to translate beyond the normal bounds.
178 protected int mOverScrollX;
179
Michael Jurka5f1c5092010-09-03 14:15:02 -0700180 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700181
Michael Jurka5f1c5092010-09-03 14:15:02 -0700182 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Winson Chung86f77532010-08-24 11:08:22 -0700184 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700185
Michael Jurkae326f182011-11-21 14:05:46 -0800186 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700187
Michael Jurka0142d492010-08-25 17:46:15 -0700188 // If true, syncPages and syncPageItems will be called to refresh pages
189 protected boolean mContentIsRefreshable = true;
190
191 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700192 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700193
194 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
195 // to switch to a new page
196 protected boolean mUsePagingTouchSlop = true;
197
Michael Jurka8b805b12012-04-18 14:23:14 -0700198 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700199 // (SmoothPagedView does this)
200 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700201 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700202
Patrick Dubroy1262e362010-10-06 15:49:50 -0700203 protected boolean mIsPageMoving = false;
204
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 // All syncs and layout passes are deferred until data is ready.
206 protected boolean mIsDataReady = false;
207
Adam Cohen7d30a372013-07-01 17:03:59 -0700208 protected boolean mAllowLongPress = true;
209
Winson Chungd2be3812013-07-16 11:11:32 -0700210 // Page Indicator
211 private int mPageIndicatorViewId;
212 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700213 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700214
Adam Cohen7d30a372013-07-01 17:03:59 -0700215 // The viewport whether the pages are to be contained (the actual view may be larger than the
216 // viewport)
217 private Rect mViewport = new Rect();
218
219 // Reordering
220 // We use the min scale to determine how much to expand the actually PagedView measured
221 // dimensions such that when we are zoomed out, the view is not clipped
222 private int REORDERING_DROP_REPOSITION_DURATION = 200;
223 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
224 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700225 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700226 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700227 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700228 protected View mDragView;
229 protected AnimatorSet mZoomInOutAnim;
230 private Runnable mSidePageHoverRunnable;
231 private int mSidePageHoverIndex = -1;
232 // This variable's scope is only for the duration of startReordering() and endReordering()
233 private boolean mReorderingStarted = false;
234 // This variable's scope is for the duration of startReordering() and after the zoomIn()
235 // animation after endReordering()
236 private boolean mIsReordering;
237 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
238 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
239 private int mPostReorderingPreZoomInRemainingAnimationCount;
240 private Runnable mPostReorderingPreZoomInRunnable;
241
Adam Cohen7d30a372013-07-01 17:03:59 -0700242 // Convenience/caching
243 private Matrix mTmpInvMatrix = new Matrix();
244 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700245 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700246 private Rect mTmpRect = new Rect();
247 private Rect mAltTmpRect = new Rect();
248
249 // Fling to delete
250 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
251 private float FLING_TO_DELETE_FRICTION = 0.035f;
252 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
253 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
254 protected int mFlingToDeleteThresholdVelocity = -1400;
255 // Drag to delete
256 private boolean mDeferringForDelete = false;
257 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
258 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
259
260 // Drop to delete
261 private View mDeleteDropTarget;
262
Adam Cohen7d30a372013-07-01 17:03:59 -0700263 // Bouncer
264 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700265
John Spurlock77e1f472013-09-11 10:09:51 -0400266 protected final Rect mInsets = new Rect();
267
Winson Chung86f77532010-08-24 11:08:22 -0700268 public interface PageSwitchListener {
269 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 }
271
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 public PagedView(Context context) {
273 this(context, null);
274 }
275
276 public PagedView(Context context, AttributeSet attrs) {
277 this(context, attrs, 0);
278 }
279
280 public PagedView(Context context, AttributeSet attrs, int defStyle) {
281 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700282
Adam Cohen9c4949e2010-10-05 12:27:22 -0700283 TypedArray a = context.obtainStyledAttributes(attrs,
284 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700285
Adam Cohen9c4949e2010-10-05 12:27:22 -0700286 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800287 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700288 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800289 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700290 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800291 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700292 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800293 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700294 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700295 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700296 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700297 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700298 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700299 a.recycle();
300
Winson Chung321e9ee2010-08-09 13:37:56 -0700301 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700302 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
304
305 /**
306 * Initializes various states for this workspace.
307 */
Michael Jurka0142d492010-08-25 17:46:15 -0700308 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700309 mDirtyPageContent = new ArrayList<Boolean>();
310 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800311 mScroller = new LauncherScroller(getContext());
312 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700313 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700314 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700315
316 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700317 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
319 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700320 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800321
Adam Cohen7d30a372013-07-01 17:03:59 -0700322 // Scale the fling-to-delete threshold by the density
323 mFlingToDeleteThresholdVelocity =
324 (int) (mFlingToDeleteThresholdVelocity * mDensity);
325
Adam Cohen265b9a62011-12-07 14:37:18 -0800326 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
327 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
328 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 }
331
Adam Cohenf9618852013-11-08 06:45:03 -0800332 protected void setDefaultInterpolator(Interpolator interpolator) {
333 mDefaultInterpolator = interpolator;
334 mScroller.setInterpolator(mDefaultInterpolator);
335 }
336
Winson Chungd2be3812013-07-16 11:11:32 -0700337 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700338 super.onAttachedToWindow();
339
Winson Chungd2be3812013-07-16 11:11:32 -0700340 // Hook up the page indicator
341 ViewGroup parent = (ViewGroup) getParent();
342 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
343 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700344 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700345
Winson Chung7819a562013-09-19 15:55:45 -0700346 ArrayList<PageIndicator.PageMarkerResources> markers =
347 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700348 for (int i = 0; i < getChildCount(); ++i) {
349 markers.add(getPageIndicatorMarker(i));
350 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700351
Winson Chungc58497e2013-09-03 17:48:37 -0700352 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700353
354 OnClickListener listener = getPageIndicatorClickListener();
355 if (listener != null) {
356 mPageIndicator.setOnClickListener(listener);
357 }
Adam Cohen53805212013-10-01 10:39:23 -0700358 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700359 }
360 }
361
Adam Cohen53805212013-10-01 10:39:23 -0700362 protected String getPageIndicatorDescription() {
363 return getCurrentPageDescription();
364 }
365
366 protected OnClickListener getPageIndicatorClickListener() {
367 return null;
368 }
369
Winson Chungd2be3812013-07-16 11:11:32 -0700370 protected void onDetachedFromWindow() {
371 // Unhook the page indicator
372 mPageIndicator = null;
373 }
374
Adam Cohen7d30a372013-07-01 17:03:59 -0700375 void setDeleteDropTarget(View v) {
376 mDeleteDropTarget = v;
377 }
378
379 // Convenience methods to map points from self to parent and vice versa
380 float[] mapPointFromViewToParent(View v, float x, float y) {
381 mTmpPoint[0] = x;
382 mTmpPoint[1] = y;
383 v.getMatrix().mapPoints(mTmpPoint);
384 mTmpPoint[0] += v.getLeft();
385 mTmpPoint[1] += v.getTop();
386 return mTmpPoint;
387 }
388 float[] mapPointFromParentToView(View v, float x, float y) {
389 mTmpPoint[0] = x - v.getLeft();
390 mTmpPoint[1] = y - v.getTop();
391 v.getMatrix().invert(mTmpInvMatrix);
392 mTmpInvMatrix.mapPoints(mTmpPoint);
393 return mTmpPoint;
394 }
395
396 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700397 if (mDragView != null) {
398 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
399 (mDragViewBaselineLeft - mDragView.getLeft());
400 float y = mLastMotionY - mDownMotionY;
401 mDragView.setTranslationX(x);
402 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700403
Adam Cohenf358a4b2013-07-23 16:47:31 -0700404 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
405 + x + ", " + y);
406 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700407 }
408
409 public void setMinScale(float f) {
410 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700411 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700412 requestLayout();
413 }
414
415 @Override
416 public void setScaleX(float scaleX) {
417 super.setScaleX(scaleX);
418 if (isReordering(true)) {
419 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
420 mLastMotionX = p[0];
421 mLastMotionY = p[1];
422 updateDragViewTranslationDuringDrag();
423 }
424 }
425
426 // Convenience methods to get the actual width/height of the PagedView (since it is measured
427 // to be larger to account for the minimum possible scale)
428 int getViewportWidth() {
429 return mViewport.width();
430 }
431 int getViewportHeight() {
432 return mViewport.height();
433 }
434
435 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
436 // PagedView both horizontally and vertically
437 int getViewportOffsetX() {
438 return (getMeasuredWidth() - getViewportWidth()) / 2;
439 }
440
441 int getViewportOffsetY() {
442 return (getMeasuredHeight() - getViewportHeight()) / 2;
443 }
444
Adam Cohenedb40762013-07-18 16:45:45 -0700445 PageIndicator getPageIndicator() {
446 return mPageIndicator;
447 }
Winson Chung7819a562013-09-19 15:55:45 -0700448 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
449 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700450 }
Adam Cohenedb40762013-07-18 16:45:45 -0700451
Adam Cohen674531f2013-12-13 15:07:14 -0800452 /**
453 * Add a page change listener which will be called when a page is _finished_ listening.
454 *
455 */
Winson Chung86f77532010-08-24 11:08:22 -0700456 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
457 mPageSwitchListener = pageSwitchListener;
458 if (mPageSwitchListener != null) {
459 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 }
461 }
462
463 /**
Winson Chung52aee602013-01-30 12:01:02 -0800464 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
465 */
466 public boolean isLayoutRtl() {
467 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
468 }
469
470 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700471 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
472 * out pages.
473 */
474 protected void setDataIsReady() {
475 mIsDataReady = true;
476 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700477
Winson Chungf0ea4d32011-06-06 14:27:16 -0700478 protected boolean isDataReady() {
479 return mIsDataReady;
480 }
481
482 /**
Winson Chung86f77532010-08-24 11:08:22 -0700483 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 *
Winson Chung86f77532010-08-24 11:08:22 -0700485 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 */
Winson Chung86f77532010-08-24 11:08:22 -0700487 int getCurrentPage() {
488 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700490
Winson Chung360e63f2012-04-27 13:48:05 -0700491 int getNextPage() {
492 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
493 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700494
Winson Chung86f77532010-08-24 11:08:22 -0700495 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 return getChildCount();
497 }
498
Winson Chung86f77532010-08-24 11:08:22 -0700499 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 return getChildAt(index);
501 }
502
Adam Cohenae4f1552011-10-20 00:15:42 -0700503 protected int indexToPage(int index) {
504 return index;
505 }
506
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800508 * Updates the scroll of the current page immediately to its final scroll position. We use this
509 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
510 * the previous tab page.
511 */
512 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 // If the current page is invalid, just reset the scroll position to zero
514 int newX = 0;
515 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700516 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700517 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800518 scrollTo(newX, 0);
519 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700520 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800521 }
522
523 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700524 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700525 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
526 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700527 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700528 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700529 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800530 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700531 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700532 }
533
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700534 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700535 mScroller.abortAnimation();
536 // We need to clean up the next page here to avoid computeScrollHelper from
537 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700538 if (resetNextPage) {
539 mNextPage = INVALID_PAGE;
540 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700541 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700542
543 private void forceFinishScroller() {
544 mScroller.forceFinished(true);
545 // We need to clean up the next page here to avoid computeScrollHelper from
546 // updating current page on the pass.
547 mNextPage = INVALID_PAGE;
548 }
549
Chet Haasebc2f0822012-10-26 17:59:53 -0700550 /**
Winson Chung86f77532010-08-24 11:08:22 -0700551 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 */
Winson Chung86f77532010-08-24 11:08:22 -0700553 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800554 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700555 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800556 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800557 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
558 // the default
559 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800560 return;
561 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700562 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700563 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chung181c3dc2013-07-17 15:36:20 -0700564 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700565 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800566 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 }
568
Winson Chung8c87cd82013-07-23 16:20:10 -0700569 /**
570 * The restore page will be set in place of the current page at the next (likely first)
571 * layout.
572 */
573 void setRestorePage(int restorePage) {
574 mRestorePage = restorePage;
575 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800576 int getRestorePage() {
577 return mRestorePage;
578 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700579
Adam Cohen674531f2013-12-13 15:07:14 -0800580 /**
581 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
582 * has settled.
583 */
Michael Jurka0142d492010-08-25 17:46:15 -0700584 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700585 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800586 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700587 }
Winson Chungd2be3812013-07-16 11:11:32 -0700588
Adam Cohen674531f2013-12-13 15:07:14 -0800589 updatePageIndicator();
590 }
591
592 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700593 // Update the page indicator (when we aren't reordering)
594 if (mPageIndicator != null && !isReordering(false)) {
595 mPageIndicator.setActiveMarker(getNextPage());
596 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800598 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700599 if (!mIsPageMoving) {
600 mIsPageMoving = true;
601 onPageBeginMoving();
602 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700603 }
604
Michael Jurkace7e05f2011-02-01 22:02:35 -0800605 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700606 if (mIsPageMoving) {
607 mIsPageMoving = false;
608 onPageEndMoving();
609 }
Michael Jurka0142d492010-08-25 17:46:15 -0700610 }
611
Adam Cohen26976d92011-03-22 15:33:33 -0700612 protected boolean isPageMoving() {
613 return mIsPageMoving;
614 }
615
Michael Jurka0142d492010-08-25 17:46:15 -0700616 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700617 protected void onPageBeginMoving() {
618 }
619
620 // a method that subclasses can override to add behavior
621 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700622 }
623
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 /**
Winson Chung86f77532010-08-24 11:08:22 -0700625 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 *
627 * @param l The listener used to respond to long clicks.
628 */
629 @Override
630 public void setOnLongClickListener(OnLongClickListener l) {
631 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700632 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700633 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 }
Adam Cohen1697b792013-09-17 19:08:21 -0700636 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 }
638
639 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800640 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700641 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800642 }
643
644 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700645 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700646 // In free scroll mode, we clamp the scrollX
647 if (mFreeScroll) {
648 x = Math.min(x, mFreeScrollMaxScrollX);
649 x = Math.max(x, mFreeScrollMinScrollX);
650 }
651
Adam Cohen0ffac432013-07-10 11:19:26 -0700652 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800653 mUnboundedScrollX = x;
654
Adam Cohen0ffac432013-07-10 11:19:26 -0700655 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
656 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
657 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800658 super.scrollTo(0, y);
659 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700660 if (isRtl) {
661 overScroll(x - mMaxScrollX);
662 } else {
663 overScroll(x);
664 }
Adam Cohen68d73932010-11-15 10:50:58 -0800665 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700666 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800667 super.scrollTo(mMaxScrollX, y);
668 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700669 if (isRtl) {
670 overScroll(x);
671 } else {
672 overScroll(x - mMaxScrollX);
673 }
Adam Cohen68d73932010-11-15 10:50:58 -0800674 }
675 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800676 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800677 super.scrollTo(x, y);
678 }
679
Michael Jurka0142d492010-08-25 17:46:15 -0700680 mTouchX = x;
681 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700682
683 // Update the last motion events when scrolling
684 if (isReordering(true)) {
685 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
686 mLastMotionX = p[0];
687 mLastMotionY = p[1];
688 updateDragViewTranslationDuringDrag();
689 }
Michael Jurka0142d492010-08-25 17:46:15 -0700690 }
691
Adam Cohen53805212013-10-01 10:39:23 -0700692 private void sendScrollAccessibilityEvent() {
693 AccessibilityManager am =
694 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
695 if (am.isEnabled()) {
696 AccessibilityEvent ev =
697 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700698 ev.setItemCount(getChildCount());
699 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700700 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700701
Alan Viverette254139a2013-10-08 13:13:46 -0700702 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700703 if (getNextPage() >= mCurrentPage) {
704 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
705 } else {
706 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
707 }
708
709 ev.setAction(action);
710 sendAccessibilityEventUnchecked(ev);
711 }
712 }
713
Michael Jurka0142d492010-08-25 17:46:15 -0700714 // we moved this functionality to a helper function so SmoothPagedView can reuse it
715 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700717 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 if (getScrollX() != mScroller.getCurrX()
719 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700720 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700721 float scaleX = mFreeScroll ? getScaleX() : 1f;
722 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
723 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700724 }
Michael Jurka0142d492010-08-25 17:46:15 -0700725 invalidate();
726 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700727 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700728 sendScrollAccessibilityEvent();
729
Winson Chung86f77532010-08-24 11:08:22 -0700730 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700731 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700732 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700733
Winson Chung9c0565f2013-07-19 13:49:06 -0700734 // Load the associated pages if necessary
735 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
736 loadAssociatedPages(mCurrentPage);
737 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
738 }
739
Adam Cohen73aa9752010-11-24 16:26:58 -0800740 // We don't want to trigger a page end moving unless the page has settled
741 // and the user has stopped scrolling
742 if (mTouchState == TOUCH_STATE_REST) {
743 pageEndMoving();
744 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700745
Adam Cohen7d30a372013-07-01 17:03:59 -0700746 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700747 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700748 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700749 if (am.isEnabled()) {
750 // Notify the user when the page changes
751 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700752 }
Michael Jurka0142d492010-08-25 17:46:15 -0700753 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 }
Michael Jurka0142d492010-08-25 17:46:15 -0700755 return false;
756 }
757
758 @Override
759 public void computeScroll() {
760 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700761 }
762
Adam Cohen7d30a372013-07-01 17:03:59 -0700763 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
764 return mTopAlignPageWhenShrinkingForBouncer;
765 }
766
Adam Cohen96d30a12013-07-16 18:13:21 -0700767 public static class LayoutParams extends ViewGroup.LayoutParams {
768 public boolean isFullScreenPage = false;
769
770 /**
771 * {@inheritDoc}
772 */
773 public LayoutParams(int width, int height) {
774 super(width, height);
775 }
776
777 public LayoutParams(ViewGroup.LayoutParams source) {
778 super(source);
779 }
780 }
781
782 protected LayoutParams generateDefaultLayoutParams() {
783 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
784 }
785
Adam Cohenedb40762013-07-18 16:45:45 -0700786 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700787 LayoutParams lp = generateDefaultLayoutParams();
788 lp.isFullScreenPage = true;
789 super.addView(page, 0, lp);
790 }
791
Adam Cohen410f3cd2013-09-22 12:09:32 -0700792 public int getNormalChildHeight() {
793 return mNormalChildHeight;
794 }
795
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 @Override
797 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700799 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
800 return;
801 }
802
Adam Cohen7d30a372013-07-01 17:03:59 -0700803 // We measure the dimensions of the PagedView to be larger than the pages so that when we
804 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700805 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
806 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
807 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700808 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800809 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700810 // viewport, we can be at most one and a half screens offset once we scale down
811 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700812 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
813 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700814
Winson Chungc82d2622013-11-06 13:23:29 -0800815 int parentWidthSize = (int) (2f * maxSize);
816 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700817 int scaledWidthSize, scaledHeightSize;
818 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700819 scaledWidthSize = (int) (parentWidthSize / mMinScale);
820 scaledHeightSize = (int) (parentHeightSize / mMinScale);
821 } else {
822 scaledWidthSize = widthSize;
823 scaledHeightSize = heightSize;
824 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700825 mViewport.set(0, 0, widthSize, heightSize);
826
827 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
828 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
829 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 }
831
Winson Chung8aad6102012-05-11 16:27:49 -0700832 // Return early if we aren't given a proper dimension
833 if (widthSize <= 0 || heightSize <= 0) {
834 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
835 return;
836 }
837
Adam Lesinski6b879f02010-11-04 16:15:23 -0700838 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
839 * of the All apps view on XLarge displays to not take up more space then it needs. Width
840 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
841 * each page to have the same width.
842 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700843 final int verticalPadding = getPaddingTop() + getPaddingBottom();
844 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700845
846 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700847 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700848 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700849 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
850 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
851 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
852 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 final int childCount = getChildCount();
854 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700855 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700856 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100857 if (child.getVisibility() != GONE) {
858 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700859
Vladimir Marko2824b072013-10-04 16:42:17 +0100860 int childWidthMode;
861 int childHeightMode;
862 int childWidth;
863 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700864
Vladimir Marko2824b072013-10-04 16:42:17 +0100865 if (!lp.isFullScreenPage) {
866 if (lp.width == LayoutParams.WRAP_CONTENT) {
867 childWidthMode = MeasureSpec.AT_MOST;
868 } else {
869 childWidthMode = MeasureSpec.EXACTLY;
870 }
871
872 if (lp.height == LayoutParams.WRAP_CONTENT) {
873 childHeightMode = MeasureSpec.AT_MOST;
874 } else {
875 childHeightMode = MeasureSpec.EXACTLY;
876 }
877
Adam Cohen3b185e22013-10-29 14:45:58 -0700878 childWidth = getViewportWidth() - horizontalPadding
879 - mInsets.left - mInsets.right;
880 childHeight = getViewportHeight() - verticalPadding
881 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100882 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700883 } else {
884 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700885 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100886
Adam Cohen3b185e22013-10-29 14:45:58 -0700887 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
888 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700889 }
890
Vladimir Marko2824b072013-10-04 16:42:17 +0100891 final int childWidthMeasureSpec =
892 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
893 final int childHeightMeasureSpec =
894 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
895 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700896 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700897 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700898 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800899 }
900
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700902 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700903 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700904 return;
905 }
906
Winson Chung785d2eb2011-04-14 16:08:02 -0700907 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700908 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909
Adam Cohen7d30a372013-07-01 17:03:59 -0700910 int offsetX = getViewportOffsetX();
911 int offsetY = getViewportOffsetY();
912
913 // Update the viewport offsets
914 mViewport.offset(offsetX, offsetY);
915
Adam Cohen0ffac432013-07-10 11:19:26 -0700916 final boolean isRtl = isLayoutRtl();
917
918 final int startIndex = isRtl ? childCount - 1 : 0;
919 final int endIndex = isRtl ? -1 : childCount;
920 final int delta = isRtl ? -1 : 1;
921
Adam Cohen7d30a372013-07-01 17:03:59 -0700922 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700923
Adam Cohen3b185e22013-10-29 14:45:58 -0700924 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000925 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700926
927 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700928 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
929 mPageScrolls = new int[getChildCount()];
930 }
931
Adam Cohen0ffac432013-07-10 11:19:26 -0700932 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700933 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700935 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100936 int childTop;
937 if (lp.isFullScreenPage) {
938 childTop = offsetY;
939 } else {
940 childTop = offsetY + getPaddingTop() + mInsets.top;
941 if (mCenterPagesVertically) {
942 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
943 }
944 }
945
Adam Cohen96d30a12013-07-16 18:13:21 -0700946 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700947 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800948
Winson Chung785d2eb2011-04-14 16:08:02 -0700949 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700950 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800951 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700952
Adam Cohen3b185e22013-10-29 14:45:58 -0700953 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
954 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000955
956 int pageGap = mPageSpacing;
957 int next = i + delta;
958 if (next != endIndex) {
959 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
960 } else {
961 nextLp = null;
962 }
963
964 // Prevent full screen pages from showing in the viewport
965 // when they are not the current page.
966 if (lp.isFullScreenPage) {
967 pageGap = getPaddingLeft();
968 } else if (nextLp != null && nextLp.isFullScreenPage) {
969 pageGap = getPaddingRight();
970 }
971
972 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 }
974 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700975
976 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
977 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800978 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700979 setHorizontalScrollBarEnabled(true);
980 mFirstLayout = false;
981 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700982
983 if (childCount > 0) {
984 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700985 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700986 } else {
987 mMaxScrollX = 0;
988 }
989
990 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
991 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700992 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700993 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700994 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700995 } else {
996 setCurrentPage(getNextPage());
997 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700998 }
999 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001000
1001 if (isReordering(true)) {
1002 updateDragViewTranslationDuringDrag();
1003 }
Winson Chunge3193b92010-09-10 11:44:42 -07001004 }
1005
Adam Cohen3b185e22013-10-29 14:45:58 -07001006 public void setPageSpacing(int pageSpacing) {
1007 mPageSpacing = pageSpacing;
1008 requestLayout();
1009 }
1010
Adam Cohenf34bab52010-09-30 14:11:56 -07001011 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001012 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1013
1014 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001015 for (int i = 0; i < getChildCount(); i++) {
1016 View child = getChildAt(i);
1017 if (child != null) {
1018 float scrollProgress = getScrollProgress(screenCenter, child, i);
1019 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001020 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001021 }
1022 }
1023 invalidate();
1024 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001025 }
1026
Winson Chungc58497e2013-09-03 17:48:37 -07001027 protected void enablePagedViewAnimations() {
1028 mAllowPagedViewAnimations = true;
1029
1030 }
1031 protected void disablePagedViewAnimations() {
1032 mAllowPagedViewAnimations = false;
1033 }
1034
Winson Chunge3193b92010-09-10 11:44:42 -07001035 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001036 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001037 // Update the page indicator, we don't update the page indicator as we
1038 // add/remove pages
1039 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001040 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001041 mPageIndicator.addMarker(pageIndex,
1042 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001043 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001044 }
1045
Adam Cohen2591f6a2011-10-25 14:36:40 -07001046 // This ensures that when children are added, they get the correct transforms / alphas
1047 // in accordance with any scroll effects.
1048 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001049 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001050 invalidate();
1051 }
1052
Michael Jurka8b805b12012-04-18 14:23:14 -07001053 @Override
1054 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001055 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001056 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001057 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001058 }
1059
Winson Chungd2be3812013-07-16 11:11:32 -07001060 private void removeMarkerForView(int index) {
1061 // Update the page indicator, we don't update the page indicator as we
1062 // add/remove pages
1063 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001064 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001065 }
1066 }
1067
1068 @Override
1069 public void removeView(View v) {
1070 // XXX: We should find a better way to hook into this before the view
1071 // gets removed form its parent...
1072 removeMarkerForView(indexOfChild(v));
1073 super.removeView(v);
1074 }
1075 @Override
1076 public void removeViewInLayout(View v) {
1077 // XXX: We should find a better way to hook into this before the view
1078 // gets removed form its parent...
1079 removeMarkerForView(indexOfChild(v));
1080 super.removeViewInLayout(v);
1081 }
1082 @Override
1083 public void removeViewAt(int index) {
1084 // XXX: We should find a better way to hook into this before the view
1085 // gets removed form its parent...
1086 removeViewAt(index);
1087 super.removeViewAt(index);
1088 }
1089 @Override
1090 public void removeAllViewsInLayout() {
1091 // Update the page indicator, we don't update the page indicator as we
1092 // add/remove pages
1093 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001094 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001095 }
1096
1097 super.removeAllViewsInLayout();
1098 }
1099
Adam Cohen73894962011-10-31 13:17:17 -07001100 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001101 if (index < 0 || index > getChildCount() - 1) return 0;
1102
Adam Cohenf698c6e2013-07-17 18:44:25 -07001103 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001104
Adam Cohenf698c6e2013-07-17 18:44:25 -07001105 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001106 }
1107
Adam Cohenf358a4b2013-07-23 16:47:31 -07001108 protected void getOverviewModePages(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001109 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001110 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001111 }
1112
Michael Jurkadde558b2011-11-09 22:09:06 -08001113 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001114 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001115 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001116
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001117 range[0] = -1;
1118 range[1] = -1;
1119
Michael Jurkac4fb9172010-09-02 17:19:20 -07001120 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001121 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001122 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001123
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001124 int count = getChildCount();
1125 for (int i = 0; i < count; i++) {
1126 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001127
Winson Chungc9ca2982013-07-19 12:07:38 -07001128 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001129 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001130 if (mTmpIntPoint[0] > viewportWidth) {
1131 if (range[0] == -1) {
1132 continue;
1133 } else {
1134 break;
1135 }
1136 }
1137
Adam Cohen6a678da2013-09-24 10:58:01 -07001138 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001139 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1140 if (mTmpIntPoint[0] < 0) {
1141 if (range[0] == -1) {
1142 continue;
1143 } else {
1144 break;
1145 }
1146 }
1147 curScreen = i;
1148 if (range[0] < 0) {
1149 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001150 }
1151 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001152
1153 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001154 } else {
1155 range[0] = -1;
1156 range[1] = -1;
1157 }
1158 }
1159
Michael Jurka920d7f42012-05-14 16:29:55 -07001160 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001161 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001162 }
1163
Michael Jurkadde558b2011-11-09 22:09:06 -08001164 @Override
1165 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001166 // Find out which screens are visible; as an optimization we only call draw on them
1167 final int pageCount = getChildCount();
1168 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001169 int halfScreenSize = getViewportWidth() / 2;
1170 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1171 // Otherwise it is equal to the scaled overscroll position.
1172 int screenCenter = mOverScrollX + halfScreenSize;
1173
1174 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1175 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1176 // set it for the next frame
1177 mForceScreenScrolled = false;
1178 screenScrolled(screenCenter);
1179 mLastScreenCenter = screenCenter;
1180 }
1181
Michael Jurkadde558b2011-11-09 22:09:06 -08001182 getVisiblePages(mTempVisiblePagesRange);
1183 final int leftScreen = mTempVisiblePagesRange[0];
1184 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001185 if (leftScreen != -1 && rightScreen != -1) {
1186 final long drawingTime = getDrawingTime();
1187 // Clip to the bounds
1188 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001189 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1190 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001191
Adam Cohen7d30a372013-07-01 17:03:59 -07001192 // Draw all the children, leaving the drag view for last
1193 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001194 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001195 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001196 if (mForceDrawAllChildrenNextFrame ||
1197 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001198 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001199 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001200 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001201 // Draw the drag view on top (if there is one)
1202 if (mDragView != null) {
1203 drawChild(canvas, mDragView, drawingTime);
1204 }
1205
Michael Jurka5e368ff2012-05-14 23:13:15 -07001206 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001207 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001208 }
Michael Jurka0142d492010-08-25 17:46:15 -07001209 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 }
1211
1212 @Override
1213 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001214 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001215 if (page != mCurrentPage || !mScroller.isFinished()) {
1216 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 return true;
1218 }
1219 return false;
1220 }
1221
1222 @Override
1223 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001224 int focusablePage;
1225 if (mNextPage != INVALID_PAGE) {
1226 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001228 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 }
Winson Chung86f77532010-08-24 11:08:22 -07001230 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001232 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 }
1234 return false;
1235 }
1236
1237 @Override
1238 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001239 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001241 if (getCurrentPage() > 0) {
1242 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 return true;
1244 }
1245 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001246 if (getCurrentPage() < getPageCount() - 1) {
1247 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 return true;
1249 }
1250 }
1251 return super.dispatchUnhandledMove(focused, direction);
1252 }
1253
1254 @Override
1255 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001256 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001257 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001258 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 }
1260 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001261 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001262 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 }
1264 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001265 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001266 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 }
1268 }
1269 }
1270
1271 /**
1272 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001273 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 *
Winson Chung86f77532010-08-24 11:08:22 -07001275 * This happens when live folders requery, and if they're off page, they
1276 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 */
1278 @Override
1279 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001280 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 View v = focused;
1282 while (true) {
1283 if (v == current) {
1284 super.focusableViewAvailable(focused);
1285 return;
1286 }
1287 if (v == this) {
1288 return;
1289 }
1290 ViewParent parent = v.getParent();
1291 if (parent instanceof View) {
1292 v = (View)v.getParent();
1293 } else {
1294 return;
1295 }
1296 }
1297 }
1298
1299 /**
1300 * {@inheritDoc}
1301 */
1302 @Override
1303 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1304 if (disallowIntercept) {
1305 // We need to make sure to cancel our long press if
1306 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001307 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001308 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 }
1310 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1311 }
1312
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001313 /**
1314 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1315 */
1316 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001317 if (isLayoutRtl()) {
1318 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001319 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001320 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001321 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001322 }
1323
1324 /**
1325 * Return true if a tap at (x, y) should trigger a flip to the next page.
1326 */
1327 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001328 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001329 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001330 }
1331 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001332 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001333 }
Winson Chung52aee602013-01-30 12:01:02 -08001334
Adam Cohen7d30a372013-07-01 17:03:59 -07001335 /** Returns whether x and y originated within the buffered viewport */
1336 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1337 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1338 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1339 return mTmpRect.contains(x, y);
1340 }
1341
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 @Override
1343 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 if (DISABLE_TOUCH_INTERACTION) {
1345 return false;
1346 }
1347
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 /*
1349 * This method JUST determines whether we want to intercept the motion.
1350 * If we return true, onTouchEvent will be called and we do the actual
1351 * scrolling there.
1352 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001353 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001354
Winson Chung45e1d6e2010-11-09 17:19:49 -08001355 // Skip touch handling if there are no pages to swipe
1356 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1357
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 /*
1359 * Shortcut the most recurring case: the user is in the dragging
1360 * state and he is moving his finger. We want to intercept this
1361 * motion.
1362 */
1363 final int action = ev.getAction();
1364 if ((action == MotionEvent.ACTION_MOVE) &&
1365 (mTouchState == TOUCH_STATE_SCROLLING)) {
1366 return true;
1367 }
1368
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 switch (action & MotionEvent.ACTION_MASK) {
1370 case MotionEvent.ACTION_MOVE: {
1371 /*
1372 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1373 * whether the user has moved far enough from his original down touch.
1374 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001375 if (mActivePointerId != INVALID_POINTER) {
1376 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001377 }
1378 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1379 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1380 // i.e. fall through to the next case (don't break)
1381 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1382 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001383 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001384 }
1385
1386 case MotionEvent.ACTION_DOWN: {
1387 final float x = ev.getX();
1388 final float y = ev.getY();
1389 // Remember location of down touch
1390 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001391 mDownMotionY = y;
1392 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 mLastMotionX = x;
1394 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 float[] p = mapPointFromViewToParent(this, x, y);
1396 mParentDownMotionX = p[0];
1397 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001398 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001399 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001401
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 /*
1403 * If being flinged and user touches the screen, initiate drag;
1404 * otherwise don't. mScroller.isFinished should be false when
1405 * being flinged.
1406 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001407 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001408 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001409
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001410 if (finishedScrolling) {
1411 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001412 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001413 setCurrentPage(getNextPage());
1414 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001415 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001416 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001417 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1418 mTouchState = TOUCH_STATE_SCROLLING;
1419 } else {
1420 mTouchState = TOUCH_STATE_REST;
1421 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001422 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001423
Winson Chung86f77532010-08-24 11:08:22 -07001424 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001426 if (!DISABLE_TOUCH_SIDE_PAGES) {
1427 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1428 if (getChildCount() > 0) {
1429 if (hitsPreviousPage(x, y)) {
1430 mTouchState = TOUCH_STATE_PREV_PAGE;
1431 } else if (hitsNextPage(x, y)) {
1432 mTouchState = TOUCH_STATE_NEXT_PAGE;
1433 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 }
1435 }
1436 }
1437 break;
1438 }
1439
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001441 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 break;
1444
1445 case MotionEvent.ACTION_POINTER_UP:
1446 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001447 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 break;
1449 }
1450
1451 /*
1452 * The only time we want to intercept motion events is if we are in the
1453 * drag mode.
1454 */
1455 return mTouchState != TOUCH_STATE_REST;
1456 }
1457
Adam Cohenf8d28232011-02-01 21:47:00 -08001458 protected void determineScrollingStart(MotionEvent ev) {
1459 determineScrollingStart(ev, 1.0f);
1460 }
1461
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 /*
1463 * Determines if we should change the touch state to start scrolling after the
1464 * user moves their touch point too far.
1465 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001466 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001467 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001469 if (pointerIndex == -1) return;
1470
1471 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 final float x = ev.getX(pointerIndex);
1473 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001474 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1475
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 final int xDiff = (int) Math.abs(x - mLastMotionX);
1477 final int yDiff = (int) Math.abs(y - mLastMotionY);
1478
Adam Cohenf8d28232011-02-01 21:47:00 -08001479 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001480 boolean xPaged = xDiff > mPagingTouchSlop;
1481 boolean xMoved = xDiff > touchSlop;
1482 boolean yMoved = yDiff > touchSlop;
1483
Adam Cohenf8d28232011-02-01 21:47:00 -08001484 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001485 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 // Scroll if the user moved far enough along the X axis
1487 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001488 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001490 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001491 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001492 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1493 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001495 }
1496 }
1497
Adam Cohen7d30a372013-07-01 17:03:59 -07001498 protected float getMaxScrollProgress() {
1499 return 1.0f;
1500 }
1501
Adam Cohenf8d28232011-02-01 21:47:00 -08001502 protected void cancelCurrentPageLongPress() {
1503 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001504 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001505 // Try canceling the long press. It could also have been scheduled
1506 // by a distant descendant, so use the mAllowLongPress flag to block
1507 // everything
1508 final View currentPage = getPageAt(mCurrentPage);
1509 if (currentPage != null) {
1510 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 }
1512 }
1513 }
1514
Adam Cohen7d30a372013-07-01 17:03:59 -07001515 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1516 final int halfScreenSize = getViewportWidth() / 2;
1517
1518 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1519 screenCenter = Math.max(halfScreenSize, screenCenter);
1520
1521 return getScrollProgress(screenCenter, v, page);
1522 }
1523
Adam Cohenb5ba0972011-09-07 18:02:31 -07001524 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001525 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001526
Adam Cohenedb40762013-07-18 16:45:45 -07001527 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001528 int count = getChildCount();
1529
1530 final int totalDistance;
1531
1532 int adjacentPage = page + 1;
1533 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1534 adjacentPage = page - 1;
1535 }
1536
1537 if (adjacentPage < 0 || adjacentPage > count - 1) {
1538 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1539 } else {
1540 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1541 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001542
1543 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001544 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1545 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001546 return scrollProgress;
1547 }
1548
Adam Cohenedb40762013-07-18 16:45:45 -07001549 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001550 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001551 return 0;
1552 } else {
1553 return mPageScrolls[index];
1554 }
1555 }
1556
Adam Cohen564a2e72013-10-09 14:47:32 -07001557 // While layout transitions are occurring, a child's position may stray from its baseline
1558 // position. This method returns the magnitude of this stray at any given time.
1559 public int getLayoutTransitionOffsetForPage(int index) {
1560 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1561 return 0;
1562 } else {
1563 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001564
1565 int scrollOffset = 0;
1566 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1567 if (!lp.isFullScreenPage) {
1568 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1569 }
1570
Adam Cohen564a2e72013-10-09 14:47:32 -07001571 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1572 return (int) (child.getX() - baselineX);
1573 }
1574 }
1575
Adam Cohene0f66b52010-11-23 15:06:07 -08001576 // This curve determines how the effect of scrolling over the limits of the page dimishes
1577 // as the user pulls further and further from the bounds
1578 private float overScrollInfluenceCurve(float f) {
1579 f -= 1.0f;
1580 return f * f * f + 1.0f;
1581 }
1582
Adam Cohenb5ba0972011-09-07 18:02:31 -07001583 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001584 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001585
1586 // We want to reach the max over scroll effect when the user has
1587 // over scrolled half the size of the screen
1588 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1589
1590 if (f == 0) return;
1591
1592 // Clamp this factor, f, to -1 < f < 1
1593 if (Math.abs(f) >= 1) {
1594 f /= Math.abs(f);
1595 }
1596
1597 int overScrollAmount = (int) Math.round(f * screenSize);
1598 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001599 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001600 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001601 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001602 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001603 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001604 }
1605 invalidate();
1606 }
1607
1608 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001609 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001610
1611 float f = (amount / screenSize);
1612
1613 if (f == 0) return;
1614 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1615
Adam Cohen7bfc9792011-01-28 13:52:37 -08001616 // Clamp this factor, f, to -1 < f < 1
1617 if (Math.abs(f) >= 1) {
1618 f /= Math.abs(f);
1619 }
1620
Adam Cohene0f66b52010-11-23 15:06:07 -08001621 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001622 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001623 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001624 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001625 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001626 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001627 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001628 }
1629 invalidate();
1630 }
1631
Adam Cohenb5ba0972011-09-07 18:02:31 -07001632 protected void overScroll(float amount) {
1633 dampedOverScroll(amount);
1634 }
1635
Michael Jurkac5b262c2011-01-12 20:24:50 -08001636 protected float maxOverScroll() {
1637 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001638 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001639 float f = 1.0f;
1640 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1641 return OVERSCROLL_DAMP_FACTOR * f;
1642 }
1643
Adam Cohenf358a4b2013-07-23 16:47:31 -07001644 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001645 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001646 }
1647
Adam Cohenf9618852013-11-08 06:45:03 -08001648 protected void disableFreeScroll() {
1649 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001650 }
1651
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001652 void updateFreescrollBounds() {
1653 getOverviewModePages(mTempVisiblePagesRange);
1654 if (isLayoutRtl()) {
1655 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1656 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1657 } else {
1658 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1659 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1660 }
1661 }
1662
Adam Cohenf9618852013-11-08 06:45:03 -08001663 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001664 mFreeScroll = freeScroll;
1665
Adam Cohenf9618852013-11-08 06:45:03 -08001666 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001667 updateFreescrollBounds();
1668 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001669 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1670 setCurrentPage(mTempVisiblePagesRange[0]);
1671 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1672 setCurrentPage(mTempVisiblePagesRange[1]);
1673 }
1674 }
1675
1676 setEnableOverscroll(!freeScroll);
1677 }
1678
1679 private void setEnableOverscroll(boolean enable) {
1680 mAllowOverScroll = enable;
1681 }
1682
1683 int getNearestHoverOverPageIndex() {
1684 if (mDragView != null) {
1685 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1686 + mDragView.getTranslationX());
1687 getOverviewModePages(mTempVisiblePagesRange);
1688 int minDistance = Integer.MAX_VALUE;
1689 int minIndex = indexOfChild(mDragView);
1690 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1691 View page = getPageAt(i);
1692 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1693 int d = Math.abs(dragX - pageX);
1694 if (d < minDistance) {
1695 minIndex = i;
1696 minDistance = d;
1697 }
1698 }
1699 return minIndex;
1700 }
1701 return -1;
1702 }
1703
Winson Chung321e9ee2010-08-09 13:37:56 -07001704 @Override
1705 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001706 if (DISABLE_TOUCH_INTERACTION) {
1707 return false;
1708 }
1709
Adam Cohen1697b792013-09-17 19:08:21 -07001710 super.onTouchEvent(ev);
1711
Winson Chung45e1d6e2010-11-09 17:19:49 -08001712 // Skip touch handling if there are no pages to swipe
1713 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1714
Michael Jurkab8f06722010-10-10 15:58:46 -07001715 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001716
1717 final int action = ev.getAction();
1718
1719 switch (action & MotionEvent.ACTION_MASK) {
1720 case MotionEvent.ACTION_DOWN:
1721 /*
1722 * If being flinged and user touches, stop the fling. isFinished
1723 * will be false if being flinged.
1724 */
1725 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001726 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001727 }
1728
1729 // Remember where the motion event started
1730 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001731 mDownMotionY = mLastMotionY = ev.getY();
1732 mDownScrollX = getScrollX();
1733 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1734 mParentDownMotionX = p[0];
1735 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001736 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001737 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001738 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001739
Michael Jurka0142d492010-08-25 17:46:15 -07001740 if (mTouchState == TOUCH_STATE_SCROLLING) {
1741 pageBeginMoving();
1742 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001743 break;
1744
1745 case MotionEvent.ACTION_MOVE:
1746 if (mTouchState == TOUCH_STATE_SCROLLING) {
1747 // Scroll to follow the motion event
1748 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001749
1750 if (pointerIndex == -1) return true;
1751
Winson Chung321e9ee2010-08-09 13:37:56 -07001752 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001753 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001754
Adam Cohenaefd4e12011-02-14 16:39:38 -08001755 mTotalMotionX += Math.abs(deltaX);
1756
Winson Chungc0844aa2011-02-02 15:25:58 -08001757 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1758 // keep the remainder because we are actually testing if we've moved from the last
1759 // scrolled position (which is discrete).
1760 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001761 mTouchX += deltaX;
1762 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1763 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001764 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001765 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001766 } else {
1767 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001768 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001769 mLastMotionX = x;
1770 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001771 } else {
1772 awakenScrollBars();
1773 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001774 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1775 // Update the last motion position
1776 mLastMotionX = ev.getX();
1777 mLastMotionY = ev.getY();
1778
1779 // Update the parent down so that our zoom animations take this new movement into
1780 // account
1781 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1782 mParentDownMotionX = pt[0];
1783 mParentDownMotionY = pt[1];
1784 updateDragViewTranslationDuringDrag();
1785
1786 // Find the closest page to the touch point
1787 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001788
1789 // Change the drag view if we are hovering over the drop target
1790 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1791 (int) mParentDownMotionX, (int) mParentDownMotionY);
1792 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1793
Adam Cohen7d30a372013-07-01 17:03:59 -07001794 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1795 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1796 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1797 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1798
Adam Cohenf358a4b2013-07-23 16:47:31 -07001799 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1800 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1801 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001802 mTempVisiblePagesRange[0] = 0;
1803 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001804 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001805 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1806 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1807 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1808 mSidePageHoverIndex = pageUnderPointIndex;
1809 mSidePageHoverRunnable = new Runnable() {
1810 @Override
1811 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001812 // Setup the scroll to the correct page before we swap the views
1813 snapToPage(pageUnderPointIndex);
1814
1815 // For each of the pages between the paged view and the drag view,
1816 // animate them from the previous position to the new position in
1817 // the layout (as a result of the drag view moving in the layout)
1818 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1819 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1820 dragViewIndex + 1 : pageUnderPointIndex;
1821 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1822 dragViewIndex - 1 : pageUnderPointIndex;
1823 for (int i = lowerIndex; i <= upperIndex; ++i) {
1824 View v = getChildAt(i);
1825 // dragViewIndex < pageUnderPointIndex, so after we remove the
1826 // drag view all subsequent views to pageUnderPointIndex will
1827 // shift down.
1828 int oldX = getViewportOffsetX() + getChildOffset(i);
1829 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1830
1831 // Animate the view translation from its old position to its new
1832 // position
1833 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1834 if (anim != null) {
1835 anim.cancel();
1836 }
1837
1838 v.setTranslationX(oldX - newX);
1839 anim = new AnimatorSet();
1840 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1841 anim.playTogether(
1842 ObjectAnimator.ofFloat(v, "translationX", 0f));
1843 anim.start();
1844 v.setTag(anim);
1845 }
1846
1847 removeView(mDragView);
1848 onRemoveView(mDragView, false);
1849 addView(mDragView, pageUnderPointIndex);
1850 onAddView(mDragView, pageUnderPointIndex);
1851 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001852 if (mPageIndicator != null) {
1853 mPageIndicator.setActiveMarker(getNextPage());
1854 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001855 }
1856 };
1857 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1858 }
1859 } else {
1860 removeCallbacks(mSidePageHoverRunnable);
1861 mSidePageHoverIndex = -1;
1862 }
Adam Cohen564976a2010-10-13 18:52:07 -07001863 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001864 determineScrollingStart(ev);
1865 }
1866 break;
1867
1868 case MotionEvent.ACTION_UP:
1869 if (mTouchState == TOUCH_STATE_SCROLLING) {
1870 final int activePointerId = mActivePointerId;
1871 final int pointerIndex = ev.findPointerIndex(activePointerId);
1872 final float x = ev.getX(pointerIndex);
1873 final VelocityTracker velocityTracker = mVelocityTracker;
1874 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1875 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001876 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001877 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001878 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1879 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001880
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001881 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1882
Adam Cohen00481b32011-11-18 12:03:48 -08001883 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001884 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001885
Adam Cohenf358a4b2013-07-23 16:47:31 -07001886 if (!mFreeScroll) {
1887 // In the case that the page is moved far to one direction and then is flung
1888 // in the opposite direction, we use a threshold to determine whether we should
1889 // just return to the starting page, or if we should skip one further.
1890 boolean returnToOriginalPage = false;
1891 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1892 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1893 returnToOriginalPage = true;
1894 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001895
Adam Cohenf358a4b2013-07-23 16:47:31 -07001896 int finalPage;
1897 // We give flings precedence over large moves, which is why we short-circuit our
1898 // test for a large move if a fling has been registered. That is, a large
1899 // move to the left and fling to the right will register as a fling to the right.
1900 final boolean isRtl = isLayoutRtl();
1901 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1902 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1903 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1904 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1905 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1906 snapToPageWithVelocity(finalPage, velocityX);
1907 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1908 (isFling && isVelocityXLeft)) &&
1909 mCurrentPage < getChildCount() - 1) {
1910 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1911 snapToPageWithVelocity(finalPage, velocityX);
1912 } else {
1913 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001914 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001915 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001916 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001917 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001918 }
1919
1920 float scaleX = getScaleX();
1921 int vX = (int) (-velocityX * scaleX);
1922 int initialScrollX = (int) (getScrollX() * scaleX);
1923
Adam Cohenf9618852013-11-08 06:45:03 -08001924 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001925 mScroller.fling(initialScrollX,
1926 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1927 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 }
Adam Cohencae7f572013-11-04 14:42:52 -08001929 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1930 // at this point we have not moved beyond the touch slop
1931 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1932 // we can just page
1933 int nextPage = Math.max(0, mCurrentPage - 1);
1934 if (nextPage != mCurrentPage) {
1935 snapToPage(nextPage);
1936 } else {
1937 snapToDestination();
1938 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001939 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001940 // at this point we have not moved beyond the touch slop
1941 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1942 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001943 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1944 if (nextPage != mCurrentPage) {
1945 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001946 } else {
1947 snapToDestination();
1948 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001949 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1950 // Update the last motion position
1951 mLastMotionX = ev.getX();
1952 mLastMotionY = ev.getY();
1953
1954 // Update the parent down so that our zoom animations take this new movement into
1955 // account
1956 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1957 mParentDownMotionX = pt[0];
1958 mParentDownMotionY = pt[1];
1959 updateDragViewTranslationDuringDrag();
1960 boolean handledFling = false;
1961 if (!DISABLE_FLING_TO_DELETE) {
1962 // Check the velocity and see if we are flinging-to-delete
1963 PointF flingToDeleteVector = isFlingingToDelete();
1964 if (flingToDeleteVector != null) {
1965 onFlingToDelete(flingToDeleteVector);
1966 handledFling = true;
1967 }
1968 }
1969 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1970 (int) mParentDownMotionY)) {
1971 onDropToDelete();
1972 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001973 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001974 if (!mCancelTap) {
1975 onUnhandledTap(ev);
1976 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001977 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001978
1979 // Remove the callback to wait for the side page hover timeout
1980 removeCallbacks(mSidePageHoverRunnable);
1981 // End any intermediate reordering states
1982 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001983 break;
1984
1985 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001986 if (mTouchState == TOUCH_STATE_SCROLLING) {
1987 snapToDestination();
1988 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001989 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001990 break;
1991
1992 case MotionEvent.ACTION_POINTER_UP:
1993 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001994 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001995 break;
1996 }
1997
1998 return true;
1999 }
2000
Adam Cohen7d30a372013-07-01 17:03:59 -07002001 public void onFlingToDelete(View v) {}
2002 public void onRemoveView(View v, boolean deletePermanently) {}
2003 public void onRemoveViewAnimationCompleted() {}
2004 public void onAddView(View v, int index) {}
2005
2006 private void resetTouchState() {
2007 releaseVelocityTracker();
2008 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002009 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002010 mTouchState = TOUCH_STATE_REST;
2011 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002012 }
2013
Adam Cohen1697b792013-09-17 19:08:21 -07002014 protected void onUnhandledTap(MotionEvent ev) {
2015 ((Launcher) getContext()).onClick(this);
2016 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002017
Winson Chung185d7162011-02-28 13:47:29 -08002018 @Override
2019 public boolean onGenericMotionEvent(MotionEvent event) {
2020 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2021 switch (event.getAction()) {
2022 case MotionEvent.ACTION_SCROLL: {
2023 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2024 final float vscroll;
2025 final float hscroll;
2026 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2027 vscroll = 0;
2028 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2029 } else {
2030 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2031 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2032 }
2033 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002034 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2035 : (hscroll > 0 || vscroll > 0);
2036 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002037 scrollRight();
2038 } else {
2039 scrollLeft();
2040 }
2041 return true;
2042 }
2043 }
2044 }
2045 }
2046 return super.onGenericMotionEvent(event);
2047 }
2048
Michael Jurkab8f06722010-10-10 15:58:46 -07002049 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2050 if (mVelocityTracker == null) {
2051 mVelocityTracker = VelocityTracker.obtain();
2052 }
2053 mVelocityTracker.addMovement(ev);
2054 }
2055
2056 private void releaseVelocityTracker() {
2057 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002058 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002059 mVelocityTracker.recycle();
2060 mVelocityTracker = null;
2061 }
2062 }
2063
Winson Chung321e9ee2010-08-09 13:37:56 -07002064 private void onSecondaryPointerUp(MotionEvent ev) {
2065 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2066 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2067 final int pointerId = ev.getPointerId(pointerIndex);
2068 if (pointerId == mActivePointerId) {
2069 // This was our active pointer going up. Choose a new
2070 // active pointer and adjust accordingly.
2071 // TODO: Make this decision more intelligent.
2072 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2073 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2074 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002075 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002076 mActivePointerId = ev.getPointerId(newPointerIndex);
2077 if (mVelocityTracker != null) {
2078 mVelocityTracker.clear();
2079 }
2080 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002081 }
2082
Winson Chung321e9ee2010-08-09 13:37:56 -07002083 @Override
2084 public void requestChildFocus(View child, View focused) {
2085 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002086 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002087 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002088 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002089 }
2090 }
2091
Winson Chung1908d072011-02-24 18:09:44 -08002092 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002093 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002094 }
2095
Adam Cohen7d30a372013-07-01 17:03:59 -07002096 int getPageNearestToPoint(float x) {
2097 int index = 0;
2098 for (int i = 0; i < getChildCount(); ++i) {
2099 if (x < getChildAt(i).getRight() - getScrollX()) {
2100 return index;
2101 } else {
2102 index++;
2103 }
2104 }
2105 return Math.min(index, getChildCount() - 1);
2106 }
2107
Adam Cohend19d3ca2010-09-15 14:43:42 -07002108 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002109 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002110 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002111 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002112 final int childCount = getChildCount();
2113 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002114 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002115 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002116 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002117 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2119 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2120 minDistanceFromScreenCenter = distanceFromScreenCenter;
2121 minDistanceFromScreenCenterIndex = i;
2122 }
2123 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002124 return minDistanceFromScreenCenterIndex;
2125 }
2126
2127 protected void snapToDestination() {
2128 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002129 }
2130
Adam Cohene0f66b52010-11-23 15:06:07 -08002131 private static class ScrollInterpolator implements Interpolator {
2132 public ScrollInterpolator() {
2133 }
2134
2135 public float getInterpolation(float t) {
2136 t -= 1.0f;
2137 return t*t*t*t*t + 1;
2138 }
2139 }
2140
2141 // We want the duration of the page snap animation to be influenced by the distance that
2142 // the screen has to travel, however, we don't want this duration to be effected in a
2143 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2144 // of travel has on the overall snap duration.
2145 float distanceInfluenceForSnapDuration(float f) {
2146 f -= 0.5f; // center the values about 0.
2147 f *= 0.3f * Math.PI / 2.0f;
2148 return (float) Math.sin(f);
2149 }
2150
Michael Jurka0142d492010-08-25 17:46:15 -07002151 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002152 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
Adam Cohen7d30a372013-07-01 17:03:59 -07002153 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002154
Adam Cohenedb40762013-07-18 16:45:45 -07002155 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002156 int delta = newX - mUnboundedScrollX;
2157 int duration = 0;
2158
Adam Cohen265b9a62011-12-07 14:37:18 -08002159 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002160 // If the velocity is low enough, then treat this more as an automatic page advance
2161 // as opposed to an apparent physical response to flinging
2162 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2163 return;
2164 }
2165
2166 // Here we compute a "distance" that will be used in the computation of the overall
2167 // snap duration. This is a function of the actual distance that needs to be traveled;
2168 // we keep this value close to half screen size in order to reduce the variance in snap
2169 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002170 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002171 float distance = halfScreenSize + halfScreenSize *
2172 distanceInfluenceForSnapDuration(distanceRatio);
2173
2174 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002175 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002176
2177 // we want the page's snap velocity to approximately match the velocity at which the
2178 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002179 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2180 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002181
2182 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002183 }
2184
2185 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002186 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002187 }
2188
Adam Cohen7d30a372013-07-01 17:03:59 -07002189 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002190 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002191 }
2192
Michael Jurka0142d492010-08-25 17:46:15 -07002193 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002194 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002195 }
2196
Adam Cohenf9618852013-11-08 06:45:03 -08002197 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2198 snapToPage(whichPage, duration, false, interpolator);
2199 }
2200
2201 protected void snapToPage(int whichPage, int duration, boolean immediate,
2202 TimeInterpolator interpolator) {
Winson Chung86f77532010-08-24 11:08:22 -07002203 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07002204
Adam Cohenedb40762013-07-18 16:45:45 -07002205 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002206 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002207 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002208 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002209 }
2210
2211 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002212 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002213 }
Michael Jurka0142d492010-08-25 17:46:15 -07002214
Adam Cohenf9618852013-11-08 06:45:03 -08002215 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2216 TimeInterpolator interpolator) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002217 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002218 View focusedChild = getFocusedChild();
2219 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002220 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002221 focusedChild.clearFocus();
2222 }
2223
Adam Cohen53805212013-10-01 10:39:23 -07002224 sendScrollAccessibilityEvent();
2225
Michael Jurka0142d492010-08-25 17:46:15 -07002226 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002227 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002228 if (immediate) {
2229 duration = 0;
2230 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002231 duration = Math.abs(delta);
2232 }
2233
Adam Cohenf358a4b2013-07-23 16:47:31 -07002234 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002235 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002236 }
Adam Cohenf9618852013-11-08 06:45:03 -08002237
2238 if (interpolator != null) {
2239 mScroller.setInterpolator(interpolator);
2240 } else {
2241 mScroller.setInterpolator(mDefaultInterpolator);
2242 }
2243
Adam Cohen68d73932010-11-15 10:50:58 -08002244 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002245
Adam Cohen674531f2013-12-13 15:07:14 -08002246 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002247
2248 // Trigger a compute() to finish switching pages if necessary
2249 if (immediate) {
2250 computeScroll();
2251 }
2252
Winson Chung9c0565f2013-07-19 13:49:06 -07002253 // Defer loading associated pages until the scroll settles
2254 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2255
Adam Cohen7d30a372013-07-01 17:03:59 -07002256 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002257 invalidate();
2258 }
2259
Winson Chung321e9ee2010-08-09 13:37:56 -07002260 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002261 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002262 }
2263
2264 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002265 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002266 }
2267
Winson Chung86f77532010-08-24 11:08:22 -07002268 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002269 int result = -1;
2270 if (v != null) {
2271 ViewParent vp = v.getParent();
2272 int count = getChildCount();
2273 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002274 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002275 return i;
2276 }
2277 }
2278 }
2279 return result;
2280 }
2281
2282 /**
2283 * @return True is long presses are still allowed for the current touch
2284 */
2285 public boolean allowLongPress() {
2286 return mAllowLongPress;
2287 }
2288
Adam Cohendbdff6b2013-09-18 19:09:15 -07002289 @Override
2290 public boolean performLongClick() {
2291 mCancelTap = true;
2292 return super.performLongClick();
2293 }
2294
Michael Jurka0142d492010-08-25 17:46:15 -07002295 /**
2296 * Set true to allow long-press events to be triggered, usually checked by
2297 * {@link Launcher} to accept or block dpad-initiated long-presses.
2298 */
2299 public void setAllowLongPress(boolean allowLongPress) {
2300 mAllowLongPress = allowLongPress;
2301 }
2302
Winson Chung321e9ee2010-08-09 13:37:56 -07002303 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002304 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002305
2306 SavedState(Parcelable superState) {
2307 super(superState);
2308 }
2309
2310 private SavedState(Parcel in) {
2311 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002312 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002313 }
2314
2315 @Override
2316 public void writeToParcel(Parcel out, int flags) {
2317 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002318 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002319 }
2320
2321 public static final Parcelable.Creator<SavedState> CREATOR =
2322 new Parcelable.Creator<SavedState>() {
2323 public SavedState createFromParcel(Parcel in) {
2324 return new SavedState(in);
2325 }
2326
2327 public SavedState[] newArray(int size) {
2328 return new SavedState[size];
2329 }
2330 };
2331 }
2332
Winson Chungf314b0e2011-08-16 11:54:27 -07002333 protected void loadAssociatedPages(int page) {
2334 loadAssociatedPages(page, false);
2335 }
2336 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002337 if (mContentIsRefreshable) {
2338 final int count = getChildCount();
2339 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002340 int lowerPageBound = getAssociatedLowerPageBound(page);
2341 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002342 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2343 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002344 // First, clear any pages that should no longer be loaded
2345 for (int i = 0; i < count; ++i) {
2346 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002347 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002348 if (layout.getPageChildCount() > 0) {
2349 layout.removeAllViewsOnPage();
2350 }
2351 mDirtyPageContent.set(i, true);
2352 }
2353 }
2354 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002355 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002356 if ((i != page) && immediateAndOnly) {
2357 continue;
2358 }
Michael Jurka0142d492010-08-25 17:46:15 -07002359 if (lowerPageBound <= i && i <= upperPageBound) {
2360 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002361 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002362 mDirtyPageContent.set(i, false);
2363 }
Winson Chung86f77532010-08-24 11:08:22 -07002364 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002365 }
2366 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002367 }
2368 }
2369
Winson Chunge3193b92010-09-10 11:44:42 -07002370 protected int getAssociatedLowerPageBound(int page) {
2371 return Math.max(0, page - 1);
2372 }
2373 protected int getAssociatedUpperPageBound(int page) {
2374 final int count = getChildCount();
2375 return Math.min(page + 1, count - 1);
2376 }
2377
Winson Chung86f77532010-08-24 11:08:22 -07002378 /**
2379 * This method is called ONLY to synchronize the number of pages that the paged view has.
2380 * To actually fill the pages with information, implement syncPageItems() below. It is
2381 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2382 * and therefore, individual page items do not need to be updated in this method.
2383 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002384 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002385
2386 /**
2387 * This method is called to synchronize the items that are on a particular page. If views on
2388 * the page can be reused, then they should be updated within this method.
2389 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002390 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002391
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002392 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002393 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002394 }
2395 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002396 invalidatePageData(currentPage, false);
2397 }
2398 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 if (!mIsDataReady) {
2400 return;
2401 }
2402
Michael Jurka0142d492010-08-25 17:46:15 -07002403 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002404 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002405 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002406
Michael Jurka0142d492010-08-25 17:46:15 -07002407 // Update all the pages
2408 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002409
Winson Chung5a808352011-06-27 19:08:49 -07002410 // We must force a measure after we've loaded the pages to update the content width and
2411 // to determine the full scroll width
2412 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2413 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2414
2415 // Set a new page as the current page if necessary
2416 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002417 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002418 }
2419
Michael Jurka0142d492010-08-25 17:46:15 -07002420 // Mark each of the pages as dirty
2421 final int count = getChildCount();
2422 mDirtyPageContent.clear();
2423 for (int i = 0; i < count; ++i) {
2424 mDirtyPageContent.add(true);
2425 }
2426
2427 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002428 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002429 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002430 }
Adam Cohen06559042013-09-29 18:30:56 -07002431 if (isPageMoving()) {
2432 // If the page is moving, then snap it to the final position to ensure we don't get
2433 // stuck between pages
2434 snapToDestination();
2435 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002436 }
Winson Chung007c6982011-06-14 13:27:53 -07002437
Adam Cohen7d30a372013-07-01 17:03:59 -07002438 // Animate the drag view back to the original position
2439 void animateDragViewToOriginalPosition() {
2440 if (mDragView != null) {
2441 AnimatorSet anim = new AnimatorSet();
2442 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2443 anim.playTogether(
2444 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002445 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2446 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2447 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002448 anim.addListener(new AnimatorListenerAdapter() {
2449 @Override
2450 public void onAnimationEnd(Animator animation) {
2451 onPostReorderingAnimationCompleted();
2452 }
2453 });
2454 anim.start();
2455 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002456 }
2457
Adam Cohen7d30a372013-07-01 17:03:59 -07002458 protected void onStartReordering() {
2459 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2460 mTouchState = TOUCH_STATE_REORDERING;
2461 mIsReordering = true;
2462
Adam Cohen7d30a372013-07-01 17:03:59 -07002463 // We must invalidate to trigger a redraw to update the layers such that the drag view
2464 // is always drawn on top
2465 invalidate();
2466 }
2467
2468 private void onPostReorderingAnimationCompleted() {
2469 // Trigger the callback when reordering has settled
2470 --mPostReorderingPreZoomInRemainingAnimationCount;
2471 if (mPostReorderingPreZoomInRunnable != null &&
2472 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2473 mPostReorderingPreZoomInRunnable.run();
2474 mPostReorderingPreZoomInRunnable = null;
2475 }
2476 }
2477
2478 protected void onEndReordering() {
2479 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002480 }
2481
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002483 int dragViewIndex = indexOfChild(v);
2484
2485 if (mTouchState != TOUCH_STATE_REST) return false;
2486
Adam Cohen7d30a372013-07-01 17:03:59 -07002487 mTempVisiblePagesRange[0] = 0;
2488 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002489 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002490 mReorderingStarted = true;
2491
2492 // Check if we are within the reordering range
2493 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 dragViewIndex <= mTempVisiblePagesRange[1]) {
2495 // Find the drag view under the pointer
2496 mDragView = getChildAt(dragViewIndex);
2497 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2498 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002499 snapToPage(getPageNearestToCenterOfScreen());
2500 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002501 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002502 return true;
2503 }
2504 return false;
2505 }
2506
2507 boolean isReordering(boolean testTouchState) {
2508 boolean state = mIsReordering;
2509 if (testTouchState) {
2510 state &= (mTouchState == TOUCH_STATE_REORDERING);
2511 }
2512 return state;
2513 }
2514 void endReordering() {
2515 // For simplicity, we call endReordering sometimes even if reordering was never started.
2516 // In that case, we don't want to do anything.
2517 if (!mReorderingStarted) return;
2518 mReorderingStarted = false;
2519
2520 // If we haven't flung-to-delete the current child, then we just animate the drag view
2521 // back into position
2522 final Runnable onCompleteRunnable = new Runnable() {
2523 @Override
2524 public void run() {
2525 onEndReordering();
2526 }
2527 };
2528 if (!mDeferringForDelete) {
2529 mPostReorderingPreZoomInRunnable = new Runnable() {
2530 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002531 onCompleteRunnable.run();
2532 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002533 };
2534 };
2535
2536 mPostReorderingPreZoomInRemainingAnimationCount =
2537 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2538 // Snap to the current page
2539 snapToPage(indexOfChild(mDragView), 0);
2540 // Animate the drag view back to the front position
2541 animateDragViewToOriginalPosition();
2542 } else {
2543 // Handled in post-delete-animation-callbacks
2544 }
2545 }
2546
Adam Cohen7d30a372013-07-01 17:03:59 -07002547 /*
2548 * Flinging to delete - IN PROGRESS
2549 */
2550 private PointF isFlingingToDelete() {
2551 ViewConfiguration config = ViewConfiguration.get(getContext());
2552 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2553
2554 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2555 // Do a quick dot product test to ensure that we are flinging upwards
2556 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2557 mVelocityTracker.getYVelocity());
2558 PointF upVec = new PointF(0f, -1f);
2559 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2560 (vel.length() * upVec.length()));
2561 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2562 return vel;
2563 }
2564 }
2565 return null;
2566 }
2567
2568 /**
2569 * Creates an animation from the current drag view along its current velocity vector.
2570 * For this animation, the alpha runs for a fixed duration and we update the position
2571 * progressively.
2572 */
2573 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2574 private View mDragView;
2575 private PointF mVelocity;
2576 private Rect mFrom;
2577 private long mPrevTime;
2578 private float mFriction;
2579
2580 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2581
2582 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2583 long startTime, float friction) {
2584 mDragView = dragView;
2585 mVelocity = vel;
2586 mFrom = from;
2587 mPrevTime = startTime;
2588 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2589 }
2590
2591 @Override
2592 public void onAnimationUpdate(ValueAnimator animation) {
2593 float t = ((Float) animation.getAnimatedValue()).floatValue();
2594 long curTime = AnimationUtils.currentAnimationTimeMillis();
2595
2596 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2597 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2598
2599 mDragView.setTranslationX(mFrom.left);
2600 mDragView.setTranslationY(mFrom.top);
2601 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2602
2603 mVelocity.x *= mFriction;
2604 mVelocity.y *= mFriction;
2605 mPrevTime = curTime;
2606 }
2607 };
2608
2609 private static final int ANIM_TAG_KEY = 100;
2610
2611 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2612 return new Runnable() {
2613 @Override
2614 public void run() {
2615 int dragViewIndex = indexOfChild(dragView);
2616
2617 // For each of the pages around the drag view, animate them from the previous
2618 // position to the new position in the layout (as a result of the drag view moving
2619 // in the layout)
2620 // NOTE: We can make an assumption here because we have side-bound pages that we
2621 // will always have pages to animate in from the left
Adam Cohenf358a4b2013-07-23 16:47:31 -07002622 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002623 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2624 boolean slideFromLeft = (isLastWidgetPage ||
2625 dragViewIndex > mTempVisiblePagesRange[0]);
2626
2627 // Setup the scroll to the correct page before we swap the views
2628 if (slideFromLeft) {
2629 snapToPageImmediately(dragViewIndex - 1);
2630 }
2631
2632 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2633 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2634 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2635 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2636 ArrayList<Animator> animations = new ArrayList<Animator>();
2637 for (int i = lowerIndex; i <= upperIndex; ++i) {
2638 View v = getChildAt(i);
2639 // dragViewIndex < pageUnderPointIndex, so after we remove the
2640 // drag view all subsequent views to pageUnderPointIndex will
2641 // shift down.
2642 int oldX = 0;
2643 int newX = 0;
2644 if (slideFromLeft) {
2645 if (i == 0) {
2646 // Simulate the page being offscreen with the page spacing
2647 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002648 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002649 } else {
2650 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2651 }
2652 newX = getViewportOffsetX() + getChildOffset(i);
2653 } else {
2654 oldX = getChildOffset(i) - getChildOffset(i - 1);
2655 newX = 0;
2656 }
2657
2658 // Animate the view translation from its old position to its new
2659 // position
2660 AnimatorSet anim = (AnimatorSet) v.getTag();
2661 if (anim != null) {
2662 anim.cancel();
2663 }
2664
2665 // Note: Hacky, but we want to skip any optimizations to not draw completely
2666 // hidden views
2667 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2668 v.setTranslationX(oldX - newX);
2669 anim = new AnimatorSet();
2670 anim.playTogether(
2671 ObjectAnimator.ofFloat(v, "translationX", 0f),
2672 ObjectAnimator.ofFloat(v, "alpha", 1f));
2673 animations.add(anim);
2674 v.setTag(ANIM_TAG_KEY, anim);
2675 }
2676
2677 AnimatorSet slideAnimations = new AnimatorSet();
2678 slideAnimations.playTogether(animations);
2679 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2680 slideAnimations.addListener(new AnimatorListenerAdapter() {
2681 @Override
2682 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002683 mDeferringForDelete = false;
2684 onEndReordering();
2685 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002686 }
2687 });
2688 slideAnimations.start();
2689
2690 removeView(dragView);
2691 onRemoveView(dragView, true);
2692 }
2693 };
2694 }
2695
2696 public void onFlingToDelete(PointF vel) {
2697 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2698
2699 // NOTE: Because it takes time for the first frame of animation to actually be
2700 // called and we expect the animation to be a continuation of the fling, we have
2701 // to account for the time that has elapsed since the fling finished. And since
2702 // we don't have a startDelay, we will always get call to update when we call
2703 // start() (which we want to ignore).
2704 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2705 private int mCount = -1;
2706 private long mStartTime;
2707 private float mOffset;
2708 /* Anonymous inner class ctor */ {
2709 mStartTime = startTime;
2710 }
2711
2712 @Override
2713 public float getInterpolation(float t) {
2714 if (mCount < 0) {
2715 mCount++;
2716 } else if (mCount == 0) {
2717 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2718 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2719 mCount++;
2720 }
2721 return Math.min(1f, mOffset + t);
2722 }
2723 };
2724
2725 final Rect from = new Rect();
2726 final View dragView = mDragView;
2727 from.left = (int) dragView.getTranslationX();
2728 from.top = (int) dragView.getTranslationY();
2729 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2730 from, startTime, FLING_TO_DELETE_FRICTION);
2731
2732 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2733
2734 // Create and start the animation
2735 ValueAnimator mDropAnim = new ValueAnimator();
2736 mDropAnim.setInterpolator(tInterpolator);
2737 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2738 mDropAnim.setFloatValues(0f, 1f);
2739 mDropAnim.addUpdateListener(updateCb);
2740 mDropAnim.addListener(new AnimatorListenerAdapter() {
2741 public void onAnimationEnd(Animator animation) {
2742 onAnimationEndRunnable.run();
2743 }
2744 });
2745 mDropAnim.start();
2746 mDeferringForDelete = true;
2747 }
2748
2749 /* Drag to delete */
2750 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2751 if (mDeleteDropTarget != null) {
2752 mAltTmpRect.set(0, 0, 0, 0);
2753 View parent = (View) mDeleteDropTarget.getParent();
2754 if (parent != null) {
2755 parent.getGlobalVisibleRect(mAltTmpRect);
2756 }
2757 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2758 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2759 return mTmpRect.contains(x, y);
2760 }
2761 return false;
2762 }
2763
2764 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2765
2766 private void onDropToDelete() {
2767 final View dragView = mDragView;
2768
2769 final float toScale = 0f;
2770 final float toAlpha = 0f;
2771
2772 // Create and start the complex animation
2773 ArrayList<Animator> animations = new ArrayList<Animator>();
2774 AnimatorSet motionAnim = new AnimatorSet();
2775 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2776 motionAnim.playTogether(
2777 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2778 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2779 animations.add(motionAnim);
2780
2781 AnimatorSet alphaAnim = new AnimatorSet();
2782 alphaAnim.setInterpolator(new LinearInterpolator());
2783 alphaAnim.playTogether(
2784 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2785 animations.add(alphaAnim);
2786
2787 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2788
2789 AnimatorSet anim = new AnimatorSet();
2790 anim.playTogether(animations);
2791 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2792 anim.addListener(new AnimatorListenerAdapter() {
2793 public void onAnimationEnd(Animator animation) {
2794 onAnimationEndRunnable.run();
2795 }
2796 });
2797 anim.start();
2798
2799 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002800 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002801
2802 /* Accessibility */
2803 @Override
2804 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2805 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002806 info.setScrollable(getPageCount() > 1);
2807 if (getCurrentPage() < getPageCount() - 1) {
2808 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2809 }
2810 if (getCurrentPage() > 0) {
2811 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2812 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002813 }
2814
2815 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002816 public void sendAccessibilityEvent(int eventType) {
2817 // Don't let the view send real scroll events.
2818 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2819 super.sendAccessibilityEvent(eventType);
2820 }
2821 }
2822
2823 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002824 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2825 super.onInitializeAccessibilityEvent(event);
2826 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002827 }
2828
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002829 @Override
2830 public boolean performAccessibilityAction(int action, Bundle arguments) {
2831 if (super.performAccessibilityAction(action, arguments)) {
2832 return true;
2833 }
2834 switch (action) {
2835 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2836 if (getCurrentPage() < getPageCount() - 1) {
2837 scrollRight();
2838 return true;
2839 }
2840 } break;
2841 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2842 if (getCurrentPage() > 0) {
2843 scrollLeft();
2844 return true;
2845 }
2846 } break;
2847 }
2848 return false;
2849 }
2850
Adam Cohen0ffac432013-07-10 11:19:26 -07002851 protected String getCurrentPageDescription() {
2852 return String.format(getContext().getString(R.string.default_scroll_format),
2853 getNextPage() + 1, getChildCount());
2854 }
2855
Winson Chungd11265e2011-08-30 13:37:23 -07002856 @Override
2857 public boolean onHoverEvent(android.view.MotionEvent event) {
2858 return true;
2859 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002860}